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  {
19      private final List<ResourceInterceptor> interceptors;
20      private final Object resource;
21      private final HttpContext httpContext;
22      private final AbstractResourceMethod method;
23      private final Object[] parameters;
24  
25      public DefaultMethodInvocation(Object resource, AbstractResourceMethod method, HttpContext httpContext, List<ResourceInterceptor> interceptors,
26                                     Object[] params)
27      {
28          this.resource = resource;
29          this.method = method;
30          this.httpContext = httpContext;
31          this.interceptors = new ArrayList<ResourceInterceptor>(interceptors);
32          this.parameters = params;
33      }
34  
35      public Object getResource()
36      {
37          return resource;
38      }
39  
40      public HttpContext getHttpContext()
41      {
42          return httpContext;
43      }
44  
45      public AbstractResourceMethod getMethod()
46      {
47          return method;
48      }
49  
50      public Object[] getParameters()
51      {
52          return parameters;
53      }
54  
55  
56      public void invoke() throws IllegalAccessException, InvocationTargetException
57      {
58          if (!interceptors.isEmpty())
59          {
60              ResourceInterceptor interceptor = interceptors.remove(0);
61              interceptor.intercept(this);
62          }
63          else
64          {
65              throw new IllegalStateException("End of interceptor chain");
66          }
67      }
68  }