View Javadoc

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   * 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      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          // First check the method
36          InterceptorChain chain = m.getAnnotation(InterceptorChain.class);
37          if (chain == null) {
38              // Next check the class
39              chain = m.getDeclaringClass().getAnnotation(InterceptorChain.class);
40              if (chain == null) {
41                  // Finally, check the package
42                  chain = m.getDeclaringClass().getPackage().getAnnotation(InterceptorChain.class);
43              }
44          }
45  
46          if (chain != null) {
47              return buildFromClass(chain.value());
48          } else {
49              // Return default interceptor list
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                  // todo: we should find a way to autowire from jersey injectables
63              }
64          }
65          return resourceInterceptors;
66      }
67  }