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