1 package com.atlassian.plugin.servlet.filter;
2
3 import com.atlassian.plugin.servlet.ServletModuleManager;
4 import com.atlassian.plugin.servlet.util.ServletContextServletModuleManagerAccessor;
5 import org.slf4j.Logger;
6 import org.slf4j.LoggerFactory;
7
8 import javax.servlet.Filter;
9 import javax.servlet.FilterChain;
10 import javax.servlet.FilterConfig;
11 import javax.servlet.ServletException;
12 import javax.servlet.ServletRequest;
13 import javax.servlet.ServletResponse;
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16 import java.io.IOException;
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 public class ServletFilterModuleContainerFilter implements Filter {
32 private static final Logger log = LoggerFactory.getLogger(ServletFilterModuleContainerFilter.class);
33
34 private FilterConfig filterConfig;
35 private FilterLocation location;
36
37 public void init(FilterConfig filterConfig) throws ServletException {
38 this.filterConfig = filterConfig;
39 location = FilterLocation.parse(filterConfig.getInitParameter("location"));
40 }
41
42 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
43 doFilter((HttpServletRequest) request, (HttpServletResponse) response, chain);
44 }
45
46 void doFilter(HttpServletRequest request, HttpServletResponse response, final FilterChain chain) throws IOException, ServletException {
47 if (getServletModuleManager() == null) {
48 log.info("Could not get ServletModuleManager. Skipping filter plugins.");
49 chain.doFilter(request, response);
50 return;
51 }
52
53 final Iterable<Filter> filters = getServletModuleManager().getFilters(location, getUri(request), filterConfig, request.getDispatcherType());
54 FilterChain pluginFilterChain = new IteratingFilterChain(filters.iterator(), chain);
55 pluginFilterChain.doFilter(request, response);
56 }
57
58 public void destroy() {
59 }
60
61
62
63
64
65 protected ServletModuleManager getServletModuleManager() {
66 return ServletContextServletModuleManagerAccessor.getServletModuleManager(filterConfig.getServletContext());
67 }
68
69 protected final FilterConfig getFilterConfig() {
70 return filterConfig;
71 }
72
73 protected final FilterLocation getFilterLocation() {
74 return location;
75 }
76
77
78
79
80
81
82
83 private static String getUri(HttpServletRequest request) {
84
85 String uri = (String) request
86 .getAttribute("javax.servlet.include.servlet_path");
87 if (uri != null) {
88 return uri;
89 }
90
91 uri = getServletPath(request);
92 if (uri != null && !"".equals(uri)) {
93 return uri;
94 }
95
96 uri = request.getRequestURI();
97 return uri.substring(request.getContextPath().length());
98 }
99
100
101
102
103
104
105
106
107
108 private static String getServletPath(HttpServletRequest request) {
109 String servletPath = request.getServletPath();
110
111 String requestUri = request.getRequestURI();
112
113 if (requestUri != null && servletPath != null && !requestUri.endsWith(servletPath)) {
114 int pos = requestUri.indexOf(servletPath);
115 if (pos > -1) {
116 servletPath = requestUri.substring(requestUri.indexOf(servletPath));
117 }
118 }
119
120 if (null != servletPath && !"".equals(servletPath)) {
121 return servletPath;
122 }
123
124 int startIndex = request.getContextPath().equals("") ? 0 : request.getContextPath().length();
125 int endIndex = request.getPathInfo() == null ? requestUri.length() : requestUri.lastIndexOf(request.getPathInfo());
126
127 if (startIndex > endIndex) {
128 endIndex = startIndex;
129 }
130
131 return requestUri.substring(startIndex, endIndex);
132 }
133 }