View Javadoc

1   package com.atlassian.plugin.servlet;
2   
3   import javax.servlet.Filter;
4   import javax.servlet.FilterConfig;
5   import javax.servlet.ServletConfig;
6   import javax.servlet.ServletContextListener;
7   import javax.servlet.ServletException;
8   import javax.servlet.http.HttpServlet;
9   
10  import com.atlassian.plugin.servlet.descriptors.ServletFilterModuleDescriptor;
11  import com.atlassian.plugin.servlet.descriptors.ServletModuleDescriptor;
12  import com.atlassian.plugin.servlet.filter.FilterLocation;
13  import com.atlassian.plugin.servlet.filter.ServletFilterModuleContainerFilter;
14  
15  /**
16   * The ServletModuleManager is responsible for servlets and filters - and their servlet contexts - defined in plugins.
17   * It is used by instances of the {@link ServletModuleContainerServlet} and {@link ServletFilterModuleContainerFilter}
18   * to lookup, create and wrap the filters and servlets defined in plugins.  
19   * <p/>
20   * When the first {@link Filter} or {@link Servlet} is first accessed in a plugin, a new {@link ServletContext} is
21   * created for all the modules in the plugin to share.  This is done by wrapping the applications 
22   * {@link ServletContext}, creating a map of attributes that are local to the plugin that are shadowed by the 
23   * applications {@link ServletContext} attributes, merging any servlet context init parameters from the plugin and the
24   * application, and then running through any {@link ServletContextListener}s defined by the plugin has calling their
25   * contextInitialized() methods. 
26   * <p/>
27   * The shadowing of the the plugins {@link ServletContext}s attributes are shadowed by the applications attributes 
28   * means that if an attribute does not exist in the plugin local attribute map, the applications attributes will be 
29   * returned.  The plugin is thereby prevented from modifying the base applications context attributes on an application
30   * wide scope and can instead only change them, but not remove them, on a local scope.  
31   * <p/>
32   * The init parameters in the plugin will override parameters from the base applications servlet
33   * init parameters that have the same name.
34   * <p/>
35   * During the creation of Filters and Servlets, the {@link FilterConfig} and {@link ServletConfig} provided to 
36   * Filters and Servlets contain the plugin local {@link ServletContext}, as described above, 
37   * and provides access to the init parameters defined in the plugin xml for the Filter or Servlet.   
38   * <p/>
39   * After being created, the filters and servlets are wrapped to ensure the the init(), service(), doFilter(), 
40   * and destroy() methods and other methods defined in the Filter and Servlet interfaces are executed in the plugins
41   * {@link ClassLoader}.
42   * <p/>
43   * The plugins {@link ServletContext} is not destroyed until the plugin is disabled.  It is also at this time that any
44   * {@link ServletContextListener}s will have their contextDestroyed() methods called.
45   */
46  public interface ServletModuleManager
47  {
48      /**
49       * Register a new servlet plugin module.
50       * 
51       * @param descriptor Details of what the servlet class is and the path it should serve.
52       */
53      void addServletModule(ServletModuleDescriptor descriptor);
54  
55      /**
56       * Return an instance of the HttpServlet that should be used to serve content matching the provided url path.
57       * 
58       * @param path Path of the incoming request to serve. 
59       * @param servletConfig ServletConfig given to the delegating servlet. 
60       * @return HttpServlet that has been registered to serve up content matching the passed in path.
61       * @throws ServletException Thrown if there is a problem initializing the servlet to be returned.
62       */
63      HttpServlet getServlet(String path, final ServletConfig servletConfig) throws ServletException;
64  
65      /**
66       * Remove a previously registered servlet plugin module.  Requests that come in on the path described in the 
67       * descriptor will no longer be served.
68       *  
69       * @param descriptor Details of what servlet module to remove.
70       */
71      void removeServletModule(ServletModuleDescriptor descriptor);
72  
73      /**
74       * Register a new filter plugin module.
75       * 
76       * @param descriptor Details of what the filter class is and the path it should serve.
77       */
78      void addFilterModule(ServletFilterModuleDescriptor descriptor);
79  
80      /**
81       * Returns the filters that have been registered to filter requests at the specified path matching the location 
82       * in the filter stack. 
83       * 
84       * @param location Place in the applications filter stack the filters should be applied.
85       * @param pathInfo Path of the incoming request to filter.
86       * @param filterConfig FilterConfig given to the delegating filter.
87       * @return List of filters to be applied, already sorted by weight
88       * @throws ServletException Thrown if there is a problem initializing one of the filters to apply.
89       */
90      Iterable<Filter> getFilters(FilterLocation location, String pathInfo, FilterConfig filterConfig) throws ServletException;
91  
92      /**
93       * Remove a previously registered filter plugin module.  Requests that come in on the path described in the 
94       * descriptor will no longer be served.
95       *  
96       * @param descriptor Details of what filter module to remove.
97       */
98      void removeFilterModule(ServletFilterModuleDescriptor descriptor);
99  }