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