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