View Javadoc

1   package com.atlassian.plugins.rest.common.expand.interceptor;
2   
3   import com.atlassian.plugins.rest.common.interceptor.ResourceInterceptor;
4   import com.atlassian.plugins.rest.common.interceptor.MethodInvocation;
5   import com.atlassian.plugins.rest.common.expand.resolver.EntityExpanderResolver;
6   import com.atlassian.plugins.rest.common.expand.parameter.ExpandParameter;
7   import com.atlassian.plugins.rest.common.expand.parameter.DefaultExpandParameter;
8   import com.atlassian.plugins.rest.common.expand.EntityCrawler;
9   import com.google.common.base.Preconditions;
10  import com.sun.jersey.api.core.HttpRequestContext;
11  import com.sun.jersey.api.core.HttpResponseContext;
12  
13  import java.lang.reflect.InvocationTargetException;
14  
15  /**
16   * Expands the entity returned from the resource method
17   *
18   * @since 2.0
19   */
20  public class ExpandInterceptor implements ResourceInterceptor
21  {
22      private final EntityExpanderResolver expanderResolver;
23      private final String expandParameterName;
24  
25      public ExpandInterceptor(EntityExpanderResolver expanderResolver)
26      {
27          this("expand", expanderResolver);
28      }
29  
30      public ExpandInterceptor(String expandParameterName, EntityExpanderResolver expanderResolver)
31      {
32          this.expanderResolver = expanderResolver;
33          this.expandParameterName = Preconditions.checkNotNull(expandParameterName);
34      }
35      
36      public void intercept(MethodInvocation invocation) throws IllegalAccessException, InvocationTargetException
37      {
38          invocation.invoke();
39          HttpRequestContext request = invocation.getHttpContext().getRequest();
40          HttpResponseContext response = invocation.getHttpContext().getResponse();
41          final ExpandParameter expandParameter = new DefaultExpandParameter(request.getQueryParameters().get(expandParameterName));
42          new EntityCrawler().crawl(response.getEntity(), expandParameter, expanderResolver);
43      }
44  }