View Javadoc

1   package com.atlassian.plugin.servlet.descriptors;
2   
3   import java.util.Comparator;
4   
5   import javax.servlet.Filter;
6   
7   import org.dom4j.Element;
8   
9   import com.atlassian.plugin.*;
10  import com.atlassian.plugin.hostcontainer.HostContainer;
11  import com.atlassian.plugin.servlet.ServletModuleManager;
12  import com.atlassian.plugin.servlet.filter.FilterLocation;
13  
14  /**
15   * A module descriptor that allows plugin developers to define servlet filters.  Developers can define what urls the 
16   * filter should be applied to by defining one or more <url-pattern> elements and they can decide where in the
17   * filter stack a plugin filter should go by defining the "location" and "weight" attributes. 
18   * <p/>
19   * The location attribute can have one of four values:
20   * </p>
21   * <ul>
22   * <li>after-encoding - after the character encoding filter</li>
23   * <li>before-login - before the login filter</li>
24   * <li>before-decoration - before any global decoration like sitemesh</li>
25   * <li>before-dispatch - before any dispatching filters or servlets</li>
26   * </ul>
27   * <p>
28   * The default for the location attribute is "before-dispatch".
29   * <p/>
30   * The weight attribute can have any integer value.  Filters with lower values of the weight attribute will come before
31   * those with higher values within the same location.
32   *
33   * @since 2.1.0
34   */
35  public class ServletFilterModuleDescriptor extends BaseServletModuleDescriptor<Filter> implements StateAware
36  {
37      static final String DEFAULT_LOCATION = FilterLocation.BEFORE_DISPATCH.name();
38      static final String DEFAULT_WEIGHT = "100";
39      
40      private FilterLocation location;
41  
42      private int weight;
43      private final ServletModuleManager servletModuleManager;
44      private final HostContainer hostContainer;
45  
46      /**
47       * @deprecated Since 2.2.0, don't extend and use {@link #ServletFilterModuleDescriptor(com.atlassian.plugin.hostcontainer.HostContainer ,ServletModuleManager)} instead
48       */
49      @Deprecated
50      public ServletFilterModuleDescriptor()
51      {
52          this(null, null);
53      }
54  
55      /**
56       * Creates a descriptor that uses a module factory to create instances
57       *
58       * @param hostContainer The module factory
59       * @since 2.2.0
60       */
61      public ServletFilterModuleDescriptor(HostContainer hostContainer, ServletModuleManager servletModuleManager)
62      {
63          this.hostContainer = hostContainer;
64          this.servletModuleManager = servletModuleManager;
65      }
66  
67      public static final Comparator<ServletFilterModuleDescriptor> byWeight = new Comparator<ServletFilterModuleDescriptor>()
68      {
69          public int compare(ServletFilterModuleDescriptor lhs, ServletFilterModuleDescriptor rhs)
70          {
71              return Integer.valueOf(lhs.getWeight()).compareTo(rhs.getWeight());
72          }
73      };
74  
75      public void init(Plugin plugin, Element element) throws PluginParseException
76      {
77          super.init(plugin, element);
78          try
79          {
80              location = FilterLocation.parse(element.attributeValue("location", DEFAULT_LOCATION));
81              weight = Integer.valueOf(element.attributeValue("weight", DEFAULT_WEIGHT));
82          }
83          catch (IllegalArgumentException ex)
84          {
85              throw new PluginParseException(ex);
86          }
87      }
88      
89      public void enabled()
90      {
91          super.enabled();
92          getServletModuleManager().addFilterModule(this);
93      }
94  
95      public void disabled()
96      {
97          getServletModuleManager().removeFilterModule(this);
98          super.disabled();
99      }
100 
101     @Override
102     public Filter getModule()
103     {
104         Filter filter = null;
105         try
106         {
107             // Give the plugin a go first
108             if (plugin instanceof AutowireCapablePlugin)
109                 filter = ((AutowireCapablePlugin)plugin).autowire(getModuleClass());
110             else
111             {
112                 if (hostContainer != null)
113                 {
114                     filter = hostContainer.create(getModuleClass());
115                 }
116                 else
117                 {
118                     filter = getModuleClass().newInstance();
119                     autowireObject(filter);
120                 }
121             }
122         }
123         catch (InstantiationException e)
124         {
125             log.error("Error instantiating: " + getModuleClass(), e);
126         }
127         catch (IllegalAccessException e)
128         {
129             log.error("Error accessing: " + getModuleClass(), e);
130         }
131         return filter;
132     }
133 
134     public FilterLocation getLocation()
135     {
136         return location;
137     }
138 
139     public int getWeight()
140     {
141         return weight;
142     }
143 
144     /**
145      * @deprecated Since 2.2.0, don't extend and use a {@link com.atlassian.plugin.hostcontainer.HostContainer} instead
146      */
147     @Deprecated
148     protected void autowireObject(Object obj)
149     {
150         throw new UnsupportedOperationException("This method must be overridden if a HostContainer is not used");
151     }
152 
153     /**
154      * @deprecated Since 2.2.0, don't extend and use a {@link com.atlassian.plugin.hostcontainer.HostContainer} instead
155      */
156     @Deprecated
157     protected ServletModuleManager getServletModuleManager()
158     {
159         if (servletModuleManager == null)
160         {
161             throw new IllegalStateException("This method must be implemented if a HostContainer is not used");
162         }
163 
164         return servletModuleManager;
165     }
166 
167 }