View Javadoc

1   package com.atlassian.plugin.servlet.filter;
2   
3   import java.io.IOException;
4   
5   import javax.servlet.Filter;
6   import javax.servlet.FilterChain;
7   import javax.servlet.FilterConfig;
8   import javax.servlet.ServletException;
9   import javax.servlet.ServletRequest;
10  import javax.servlet.ServletResponse;
11  import javax.servlet.http.HttpServletRequest;
12  import javax.servlet.http.HttpServletResponse;
13  
14  import org.apache.commons.logging.Log;
15  import org.apache.commons.logging.LogFactory;
16  
17  import com.atlassian.plugin.servlet.ServletModuleManager;
18  
19  /**
20   * Applications need to create a concrete subclass of this for use in their filter stack.  This filters responsiblity
21   * is to retrieve the filters to be applied to the request from the {@link ServletModuleManager} and build a
22   * {@link FilterChain} from them.  Once all the filters in the chain have been applied to the request, this filter 
23   * returns control to the main chain.
24   * <p/>
25   * There is one init parameters that must be configured for this filter, the "location" parameter.  It can be one of
26   * "top", "middle" or "bottom".  A filter with a "top" location must appear before the filter with a "middle" location
27   * which must appear before a filter with a "bottom" location.  Where any other application filters lie in the filter
28   * stack is completely up to the application.        
29   * 
30   * @since 2.1.0
31   */
32  public abstract class ServletFilterModuleContainerFilter implements Filter
33  {
34      private static final Log log = LogFactory.getLog(ServletFilterModuleContainerFilter.class);
35      
36      private FilterConfig filterConfig;
37      private FilterLocation location;
38  
39      public void init(FilterConfig filterConfig) throws ServletException
40      {
41          this.filterConfig = filterConfig;
42          location = FilterLocation.valueOf(filterConfig.getInitParameter("location"));
43      }
44  
45      public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
46      {
47          doFilter((HttpServletRequest) request, (HttpServletResponse) response, chain);
48      }
49      
50      void doFilter(HttpServletRequest request, HttpServletResponse response, final FilterChain chain) throws IOException, ServletException
51      {
52          if (getServletModuleManager() == null)
53          {
54              log.error("Could not get DefaultServletModuleManager?");
55              response.sendError(500, "Could not get DefaultServletModuleManager.");
56              return;
57          }
58          
59          final Iterable<Filter> filters = getServletModuleManager().getFilters(location, request.getPathInfo(), filterConfig);
60          FilterChain pluginFilterChain = new IteratingFilterChain(filters.iterator(), chain);
61          pluginFilterChain.doFilter(request, response);
62      }
63      
64      public void destroy()
65      {
66      }
67  
68      /**
69       * Retrieve the DefaultServletModuleManager from your container framework.
70       */
71      protected abstract ServletModuleManager getServletModuleManager();
72  }