/atlassian-plugins-servlet/src/main/java/com/atlassian/plugin/servlet/ServletModuleManager.java

https://bitbucket.org/purewind/atlassian-plugins · Java · 157 lines · 27 code · 11 blank · 119 comment · 0 complexity · 95d11195429be43cdafe2f691448b4a4 MD5 · raw file

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