1   package com.atlassian.plugins.rest.common.expand;
2   
3   import com.atlassian.plugins.rest.common.expand.resolver.EntityExpanderResolver;
4   import com.atlassian.plugins.rest.common.expand.jersey.ExpandResponseFilter;
5   
6   /**
7    * This is a general-purpose expander for atlassian-rest that delegates the
8    * expand process to the entity that is to be expanded (instead of having that
9    * knowledge in a separate {@link com.atlassian.plugins.rest.common.expand.EntityExpander}.
10   * As a result, this expander can be used for every entity that implements
11   * {@link SelfExpanding}.
12   *
13   * @since   v1.0.7
14   * @author  Erik van Zijst
15   */
16  public class SelfExpandingExpander extends AbstractRecursiveEntityExpander<SelfExpanding>
17  {
18      protected SelfExpanding expandInternal(SelfExpanding selfExpandingObject)
19      {
20          selfExpandingObject.expand();
21          return selfExpandingObject;
22      }
23  
24      /**
25       * To use the self expanding mechanism, make sure you register an instance
26       * of this {@link EntityExpanderResolver} in your application's
27       * {@link ExpandResponseFilter}.
28       */
29      public static class Resolver implements EntityExpanderResolver
30      {
31          private static final SelfExpandingExpander expander = new SelfExpandingExpander();
32  
33          public <T> boolean hasExpander(T instance)
34          {
35              return hasExpander(instance.getClass());
36          }
37  
38          public boolean hasExpander(Class<?> aClass)
39          {
40              return SelfExpanding.class.isAssignableFrom(aClass);
41          }
42  
43          @SuppressWarnings("unchecked")
44          public <T> EntityExpander<T> getExpander(T instance)
45          {
46              return (EntityExpander<T>) getExpander(instance.getClass());
47          }
48  
49          public <T> EntityExpander<T> getExpander(Class<? extends T> aClass)
50          {
51              return hasExpander(aClass) ? (EntityExpander<T>) expander : null;
52          }
53      }
54  }