View Javadoc

1   package com.atlassian.plugins.rest.common.interceptor.impl;
2   
3   import com.atlassian.plugins.rest.common.interceptor.MethodInvocation;
4   import com.atlassian.plugins.rest.common.interceptor.ResourceInterceptor;
5   import com.sun.jersey.api.core.HttpContext;
6   import com.sun.jersey.api.model.AbstractResourceMethod;
7   
8   import java.lang.reflect.InvocationTargetException;
9   import java.util.List;
10  import java.util.ArrayList;
11  
12  /**
13   * Default implementation of the {@link MethodInvocation}.  Continually calls interceptors until empty.
14   *
15   * @since 2.0
16   */
17  class DefaultMethodInvocation implements MethodInvocation {
18      private final List<ResourceInterceptor> interceptors;
19      private final Object resource;
20      private final HttpContext httpContext;
21      private final AbstractResourceMethod method;
22      private final Object[] parameters;
23  
24      public DefaultMethodInvocation(Object resource, AbstractResourceMethod method, HttpContext httpContext, List<ResourceInterceptor> interceptors,
25                                     Object[] params) {
26          this.resource = resource;
27          this.method = method;
28          this.httpContext = httpContext;
29          this.interceptors = new ArrayList<ResourceInterceptor>(interceptors);
30          this.parameters = params;
31      }
32  
33      public Object getResource() {
34          return resource;
35      }
36  
37      public HttpContext getHttpContext() {
38          return httpContext;
39      }
40  
41      public AbstractResourceMethod getMethod() {
42          return method;
43      }
44  
45      public Object[] getParameters() {
46          return parameters;
47      }
48  
49  
50      public void invoke() throws IllegalAccessException, InvocationTargetException {
51          if (!interceptors.isEmpty()) {
52              ResourceInterceptor interceptor = interceptors.remove(0);
53              interceptor.intercept(this);
54          } else {
55              throw new IllegalStateException("End of interceptor chain");
56          }
57      }
58  }