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.AutowireCapablePlugin;
10  import com.atlassian.plugin.Plugin;
11  import com.atlassian.plugin.PluginParseException;
12  import com.atlassian.plugin.StateAware;
13  import com.atlassian.plugin.servlet.ServletModuleManager;
14  import com.atlassian.plugin.servlet.filter.FilterLocation;
15  
16  /**
17   * A module descriptor that allows plugin developers to define servlet filters.  Developers can define what urls the 
18   * filter should be applied to by defining one or more <url-pattern> elements and they can decide where in the
19   * filter stack a plugin filter should go by defining the "location" and "weight" attributes. 
20   * <p/>
21   * The location attribute can have one of three values, "top", "middle" and "bottom".  Where each of these filters lies
22   * relative to the applications filters depends on the application.  But filters with "top" will always come before 
23   * those defined with "middle" which always come before "bottom".  The default for the location attribute is "bottom".
24   * <p/>
25   * The weight attribute can have any integer value.  Filters with lower values of the weight attribute will come before
26   * those with higher values within the same location.
27   *
28   * @since 2.1.0
29   */
30  public abstract class ServletFilterModuleDescriptor extends BaseServletModuleDescriptor<Filter> implements StateAware
31  {
32      static final String DEFAULT_LOCATION = FilterLocation.bottom.name();
33      static final String DEFAULT_WEIGHT = "100";
34      
35      private FilterLocation location;
36      private int weight;
37      
38      public static final Comparator<ServletFilterModuleDescriptor> byWeight = new Comparator<ServletFilterModuleDescriptor>()
39      {
40          public int compare(ServletFilterModuleDescriptor lhs, ServletFilterModuleDescriptor rhs)
41          {
42              return Integer.valueOf(lhs.getWeight()).compareTo(rhs.getWeight());
43          }
44      };
45      
46      public void init(Plugin plugin, Element element) throws PluginParseException
47      {
48          super.init(plugin, element);
49          location = FilterLocation.valueOf(element.attributeValue("location", DEFAULT_LOCATION));
50          weight = Integer.valueOf(element.attributeValue("weight", DEFAULT_WEIGHT));
51      }
52      
53      public void enabled()
54      {
55          super.enabled();
56          getServletModuleManager().addFilterModule(this);
57      }
58  
59      public void disabled()
60      {
61          getServletModuleManager().removeFilterModule(this);
62          super.disabled();
63      }
64  
65      @Override
66      public Filter getModule()
67      {
68          Filter filter = null;
69          try
70          {
71              // Give the plugin a go first
72              if (plugin instanceof AutowireCapablePlugin)
73                  filter = ((AutowireCapablePlugin)plugin).autowire(getModuleClass());
74              else
75              {
76                  filter = getModuleClass().newInstance();
77                  autowireObject(filter);
78              }
79          }
80          catch (InstantiationException e)
81          {
82              log.error("Error instantiating: " + getModuleClass(), e);
83          }
84          catch (IllegalAccessException e)
85          {
86              log.error("Error accessing: " + getModuleClass(), e);
87          }
88          return filter;
89      }
90  
91      public FilterLocation getLocation()
92      {
93          return location;
94      }
95  
96      public int getWeight()
97      {
98          return weight;
99      }
100 
101     /**
102      * Autowire an object. Implement this in your IoC framework or simply do nothing.
103      */
104     protected abstract void autowireObject(Object obj);
105 
106     /**
107      * Retrieve the DefaultServletModuleManager class from your container framework.
108      */
109     protected abstract ServletModuleManager getServletModuleManager();
110 }