1 package com.atlassian.plugin.servlet;
2
3 import com.atlassian.plugin.Plugin;
4 import com.atlassian.plugin.servlet.descriptors.ServletFilterModuleDescriptor;
5 import com.atlassian.plugin.servlet.descriptors.ServletModuleDescriptor;
6 import com.atlassian.plugin.servlet.filter.FilterDispatcherCondition;
7 import com.atlassian.plugin.servlet.filter.FilterLocation;
8 import com.atlassian.plugin.servlet.filter.ServletFilterModuleContainerFilter;
9
10 import javax.servlet.Filter;
11 import javax.servlet.FilterConfig;
12 import javax.servlet.Servlet;
13 import javax.servlet.ServletConfig;
14 import javax.servlet.ServletContext;
15 import javax.servlet.ServletContextListener;
16 import javax.servlet.ServletException;
17 import javax.servlet.http.HttpServlet;
18
19 /**
20 * The ServletModuleManager is responsible for servlets and filters - and their servlet contexts - defined in plugins.
21 * It is used by instances of the {@link ServletModuleContainerServlet} and {@link ServletFilterModuleContainerFilter}
22 * to lookup, create and wrap the filters and servlets defined in plugins.
23 * <p/>
24 * When the first {@link Filter} or {@link Servlet} is first accessed in a plugin, a new {@link ServletContext} is
25 * created for all the modules in the plugin to share. This is done by wrapping the applications
26 * {@link ServletContext}, creating a map of attributes that are local to the plugin that are shadowed by the
27 * applications {@link ServletContext} attributes, merging any servlet context init parameters from the plugin and the
28 * application, and then running through any {@link ServletContextListener}s defined by the plugin has calling their
29 * contextInitialized() methods.
30 * <p/>
31 * The shadowing of the the plugins {@link ServletContext}s attributes are shadowed by the applications attributes
32 * means that if an attribute does not exist in the plugin local attribute map, the applications attributes will be
33 * returned. The plugin is thereby prevented from modifying the base applications context attributes on an application
34 * wide scope and can instead only change them, but not remove them, on a local scope.
35 * <p/>
36 * The init parameters in the plugin will override parameters from the base applications servlet
37 * init parameters that have the same name.
38 * <p/>
39 * During the creation of Filters and Servlets, the {@link FilterConfig} and {@link ServletConfig} provided to
40 * Filters and Servlets contain the plugin local {@link ServletContext}, as described above,
41 * and provides access to the init parameters defined in the plugin xml for the Filter or Servlet.
42 * <p/>
43 * After being created, the filters and servlets are wrapped to ensure the the init(), service(), doFilter(),
44 * and destroy() methods and other methods defined in the Filter and Servlet interfaces are executed in the plugins
45 * {@link ClassLoader}.
46 * <p/>
47 * The plugins {@link ServletContext} is not destroyed until the plugin is disabled. It is also at this time that any
48 * {@link ServletContextListener}s will have their contextDestroyed() methods called.
49 */
50 public interface ServletModuleManager {
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
120 /**
121 * To be invoked by {@link com.atlassian.plugin.servlet.PluginServletContextWrapper#addServlet(String, String)} and
122 * {@link com.atlassian.plugin.servlet.PluginServletContextWrapper#addServlet(String, Class)}.
123 * <p/>
124 * Module containing the servlet will use <code>servletName</code> for:
125 * <ul>
126 * <li>key, with "-servlet" appended</li>
127 * <li>name, with "Servlet" appended</li>
128 * <li>url-pattern, with "/" prepended</li>
129 * </ul>
130 * <p/>
131 * An instance of <code>className</code> will be instantiated and initialised on first use.
132 *
133 * @param plugin mandatory
134 * @param servletName mandatory
135 * @param className mandatory
136 */
137 void addServlet(Plugin plugin, String servletName, String className);
138
139 /**
140 * To be invoked by {@link com.atlassian.plugin.servlet.PluginServletContextWrapper#addServlet(String, javax.servlet.Servlet)}.
141 * <p/>
142 * Module containing the servlet will use <code>servletName</code> for:
143 * <ul>
144 * <li>key, with "-servlet" appended</li>
145 * <li>name, with "Servlet" appended</li>
146 * <li>url-pattern, with "/" prepended</li>
147 * </ul>
148 * <p/>
149 * <code>servlet</code> will be initialised on first use.
150 *
151 * @param plugin mandatory
152 * @param servletName mandatory
153 * @param servlet mandatory
154 * @param servletContext mandatory
155 */
156 void addServlet(Plugin plugin, String servletName, HttpServlet servlet, ServletContext servletContext);
157 }