1 package com.atlassian.plugins.rest.common.interceptor.impl;
2
3 import com.atlassian.annotations.tenancy.TenantAware;
4 import com.atlassian.plugin.module.ContainerManagedPlugin;
5 import com.atlassian.plugins.rest.common.interceptor.InterceptorChain;
6 import com.atlassian.plugins.rest.common.interceptor.ResourceInterceptor;
7
8 import java.lang.reflect.Method;
9 import java.util.ArrayList;
10 import java.util.LinkedHashMap;
11 import java.util.List;
12
13 import static com.atlassian.annotations.tenancy.TenancyScope.TENANTLESS;
14
15
16
17
18
19
20
21
22
23
24
25 public class InterceptorChainBuilder {
26
27 @TenantAware(TENANTLESS)
28 private final LinkedHashMap<Class<? extends ResourceInterceptor>, ResourceInterceptor> defaultResourceInterceptors;
29 private final ContainerManagedPlugin plugin;
30
31 public InterceptorChainBuilder(ContainerManagedPlugin plugin, ResourceInterceptor... resourceInterceptors) {
32 defaultResourceInterceptors = new LinkedHashMap<>();
33 for (ResourceInterceptor resourceInterceptor : resourceInterceptors) {
34 defaultResourceInterceptors.put(resourceInterceptor.getClass(), resourceInterceptor);
35 }
36 this.plugin = plugin;
37 }
38
39 public List<ResourceInterceptor> getResourceInterceptorsForMethod(Method m) {
40
41 InterceptorChain chain = m.getAnnotation(InterceptorChain.class);
42 if (chain == null) {
43
44 chain = m.getDeclaringClass().getAnnotation(InterceptorChain.class);
45 if (chain == null) {
46
47 chain = m.getDeclaringClass().getPackage().getAnnotation(InterceptorChain.class);
48 }
49 }
50
51 if (chain != null) {
52 return buildFromClass(chain.value());
53 } else {
54
55 return new ArrayList<ResourceInterceptor>(defaultResourceInterceptors.values());
56 }
57 }
58
59 private List<ResourceInterceptor> buildFromClass(Class<? extends ResourceInterceptor>[] resourceInterceptorClasses) {
60 List<ResourceInterceptor> resourceInterceptors = new ArrayList<ResourceInterceptor>();
61 for (Class<? extends ResourceInterceptor> resourceInterceptorClass : resourceInterceptorClasses) {
62 if (defaultResourceInterceptors.containsKey(resourceInterceptorClass)) {
63 resourceInterceptors.add(defaultResourceInterceptors.get(resourceInterceptorClass));
64 } else {
65 resourceInterceptors.add(plugin.getContainerAccessor().createBean(resourceInterceptorClass));
66
67
68 }
69 }
70 return resourceInterceptors;
71 }
72 }