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