PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/atlassian-plugins-servlet/src/main/java/com/atlassian/plugin/servlet/filter/ServletFilterModuleContainerFilter.java

https://bitbucket.org/purewind/atlassian-plugins
Java | 148 lines | 94 code | 21 blank | 33 comment | 22 complexity | 958c7b1d09fce2b92dc7fa5ca79de565 MD5 | raw file
  1. package com.atlassian.plugin.servlet.filter;
  2. import com.atlassian.plugin.servlet.ServletModuleManager;
  3. import com.atlassian.plugin.servlet.util.ServletContextServletModuleManagerAccessor;
  4. import org.apache.commons.lang.StringUtils;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import javax.servlet.Filter;
  8. import javax.servlet.FilterChain;
  9. import javax.servlet.FilterConfig;
  10. import javax.servlet.ServletException;
  11. import javax.servlet.ServletRequest;
  12. import javax.servlet.ServletResponse;
  13. import javax.servlet.http.HttpServletRequest;
  14. import javax.servlet.http.HttpServletResponse;
  15. import java.io.IOException;
  16. import java.util.Arrays;
  17. /**
  18. * Applications need to create a concrete subclass of this for use in their filter stack. This filters responsiblity
  19. * is to retrieve the filters to be applied to the request from the {@link ServletModuleManager} and build a
  20. * {@link FilterChain} from them. Once all the filters in the chain have been applied to the request, this filter
  21. * returns control to the main chain.
  22. * <p/>
  23. * There is one init parameters that must be configured for this filter, the "location" parameter. It can be one of
  24. * "top", "middle" or "bottom". A filter with a "top" location must appear before the filter with a "middle" location
  25. * which must appear before a filter with a "bottom" location. Where any other application filters lie in the filter
  26. * stack is completely up to the application.
  27. *
  28. * @since 2.1.0
  29. */
  30. public class ServletFilterModuleContainerFilter implements Filter {
  31. private static final Logger log = LoggerFactory.getLogger(ServletFilterModuleContainerFilter.class);
  32. private FilterConfig filterConfig;
  33. private FilterLocation location;
  34. private FilterDispatcherCondition condition;
  35. public void init(FilterConfig filterConfig) throws ServletException {
  36. this.filterConfig = filterConfig;
  37. location = FilterLocation.parse(filterConfig.getInitParameter("location"));
  38. String dispatcherCondition = filterConfig.getInitParameter("dispatcher");
  39. if (StringUtils.isNotBlank(dispatcherCondition)) {
  40. if (!FilterDispatcherCondition.contains(dispatcherCondition)) {
  41. throw new ServletException("The dispatcher value must be one of the following only " +
  42. Arrays.asList(FilterDispatcherCondition.values()) + " - '" + condition + "' is not a valid value.");
  43. } else {
  44. condition = FilterDispatcherCondition.valueOf(dispatcherCondition);
  45. }
  46. } else {
  47. throw new ServletException("The dispatcher init param must be specified and be one of " +
  48. Arrays.asList(FilterDispatcherCondition.values()));
  49. }
  50. }
  51. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  52. doFilter((HttpServletRequest) request, (HttpServletResponse) response, chain);
  53. }
  54. void doFilter(HttpServletRequest request, HttpServletResponse response, final FilterChain chain) throws IOException, ServletException {
  55. if (getServletModuleManager() == null) {
  56. log.info("Could not get ServletModuleManager. Skipping filter plugins.");
  57. chain.doFilter(request, response);
  58. return;
  59. }
  60. final Iterable<Filter> filters = getServletModuleManager().getFilters(location, getUri(request), filterConfig, condition);
  61. FilterChain pluginFilterChain = new IteratingFilterChain(filters.iterator(), chain);
  62. pluginFilterChain.doFilter(request, response);
  63. }
  64. public void destroy() {
  65. }
  66. /**
  67. * Retrieve the DefaultServletModuleManager from your container framework. Uses the {@link com.atlassian.plugin.servlet.util.ServletContextServletModuleManagerAccessor}
  68. * by default.
  69. */
  70. protected ServletModuleManager getServletModuleManager() {
  71. return ServletContextServletModuleManagerAccessor.getServletModuleManager(filterConfig.getServletContext());
  72. }
  73. protected final FilterConfig getFilterConfig() {
  74. return filterConfig;
  75. }
  76. protected final FilterLocation getFilterLocation() {
  77. return location;
  78. }
  79. /**
  80. * Gets the uri from the request. Copied from Struts 2.1.0.
  81. *
  82. * @param request The request
  83. * @return The uri
  84. */
  85. private static String getUri(HttpServletRequest request) {
  86. // handle http dispatcher includes.
  87. String uri = (String) request
  88. .getAttribute("javax.servlet.include.servlet_path");
  89. if (uri != null) {
  90. return uri;
  91. }
  92. uri = getServletPath(request);
  93. if (uri != null && !"".equals(uri)) {
  94. return uri;
  95. }
  96. uri = request.getRequestURI();
  97. return uri.substring(request.getContextPath().length());
  98. }
  99. /**
  100. * Retrieves the current request servlet path.
  101. * Deals with differences between servlet specs (2.2 vs 2.3+).
  102. * Copied from Struts 2.1.0.
  103. *
  104. * @param request the request
  105. * @return the servlet path
  106. */
  107. private static String getServletPath(HttpServletRequest request) {
  108. String servletPath = request.getServletPath();
  109. String requestUri = request.getRequestURI();
  110. // Detecting other characters that the servlet container cut off (like anything after ';')
  111. if (requestUri != null && servletPath != null && !requestUri.endsWith(servletPath)) {
  112. int pos = requestUri.indexOf(servletPath);
  113. if (pos > -1) {
  114. servletPath = requestUri.substring(requestUri.indexOf(servletPath));
  115. }
  116. }
  117. if (null != servletPath && !"".equals(servletPath)) {
  118. return servletPath;
  119. }
  120. int startIndex = request.getContextPath().equals("") ? 0 : request.getContextPath().length();
  121. int endIndex = request.getPathInfo() == null ? requestUri.length() : requestUri.lastIndexOf(request.getPathInfo());
  122. if (startIndex > endIndex) { // this should not happen
  123. endIndex = startIndex;
  124. }
  125. return requestUri.substring(startIndex, endIndex);
  126. }
  127. }