1 package com.atlassian.plugins.rest.common.interceptor.impl;
2
3 import com.atlassian.plugin.AutowireCapablePlugin;
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 class InterceptorChainBuilder
19 {
20 private final LinkedHashMap<Class<? extends ResourceInterceptor>, ResourceInterceptor> defaultResourceInterceptors;
21 private final AutowireCapablePlugin plugin;
22
23 public InterceptorChainBuilder(AutowireCapablePlugin plugin, ResourceInterceptor... resourceInterceptors)
24 {
25 defaultResourceInterceptors = new LinkedHashMap<Class<? extends ResourceInterceptor>, ResourceInterceptor>();
26 for (ResourceInterceptor resourceInterceptor : resourceInterceptors)
27 {
28 defaultResourceInterceptors.put(resourceInterceptor.getClass(), resourceInterceptor);
29 }
30 this.plugin = plugin;
31 }
32
33 public List<ResourceInterceptor> getResourceInterceptorsForMethod(Method m)
34 {
35
36 InterceptorChain chain = m.getAnnotation(InterceptorChain.class);
37 if (chain == null)
38 {
39
40 chain = m.getDeclaringClass().getAnnotation(InterceptorChain.class);
41 if (chain == null)
42 {
43
44 chain = m.getDeclaringClass().getPackage().getAnnotation(InterceptorChain.class);
45 }
46 }
47
48 if (chain != null)
49 {
50 return buildFromClass(chain.value());
51 }
52 else
53 {
54
55 return new ArrayList<ResourceInterceptor>(defaultResourceInterceptors.values());
56 }
57 }
58
59 private List<ResourceInterceptor> buildFromClass(Class<? extends ResourceInterceptor>[] resourceInterceptorClasses)
60 {
61 List<ResourceInterceptor> resourceInterceptors = new ArrayList<ResourceInterceptor>();
62 for (Class<? extends ResourceInterceptor> resourceInterceptorClass : resourceInterceptorClasses)
63 {
64 if (defaultResourceInterceptors.containsKey(resourceInterceptorClass))
65 {
66 resourceInterceptors.add(defaultResourceInterceptors.get(resourceInterceptorClass));
67 }
68 else
69 {
70 resourceInterceptors.add(plugin.autowire(resourceInterceptorClass));
71
72
73 }
74 }
75 return resourceInterceptors;
76 }
77 }