View Javadoc

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   * Builds the interceptor chain for the resource method.  Uses the {@link InterceptorChain} resource by building the
14   * chain from the method then class then package, then default interceptors passed into the constructor.
15   *
16   * This is a private class and used by the  {@link EntityParamDispatchProviderWrapper}
17   * and {@link com.atlassian.plugins.rest.common.multipart.jersey.MultipartFormDispatchProvider}
18   * which both will use this helper class to wrap calls to rest methods with interceptors.
19   *
20   * @since 2.0
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          // First check the method
40          InterceptorChain chain = m.getAnnotation(InterceptorChain.class);
41          if (chain == null)
42          {
43              // Next check the class
44              chain = m.getDeclaringClass().getAnnotation(InterceptorChain.class);
45              if (chain == null)
46              {
47                  // Finally, check the package
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              // Return default interceptor list
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                  // todo: we should find a way to autowire from jersey injectables
77              }
78          }
79          return resourceInterceptors;
80      }
81  }