1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.kew.web; |
18 | |
|
19 | |
import org.apache.log4j.Logger; |
20 | |
import org.kuali.rice.core.api.config.property.Config; |
21 | |
import org.kuali.rice.core.api.config.property.ConfigContext; |
22 | |
import org.kuali.rice.core.api.reflect.ObjectDefinition; |
23 | |
import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; |
24 | |
import org.kuali.rice.core.api.util.ClassLoaderUtils; |
25 | |
|
26 | |
import javax.servlet.Filter; |
27 | |
import javax.servlet.FilterChain; |
28 | |
import javax.servlet.FilterConfig; |
29 | |
import javax.servlet.ServletContext; |
30 | |
import javax.servlet.ServletException; |
31 | |
import javax.servlet.ServletRequest; |
32 | |
import javax.servlet.ServletResponse; |
33 | |
import javax.servlet.http.HttpServletRequest; |
34 | |
import java.io.IOException; |
35 | |
import java.util.Collections; |
36 | |
import java.util.Enumeration; |
37 | |
import java.util.HashMap; |
38 | |
import java.util.Iterator; |
39 | |
import java.util.LinkedList; |
40 | |
import java.util.List; |
41 | |
import java.util.Map; |
42 | |
import java.util.SortedSet; |
43 | |
import java.util.TreeSet; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | 0 | public class BootstrapFilter implements Filter { |
53 | 0 | private static final Logger LOG = Logger.getLogger(BootstrapFilter.class); |
54 | |
|
55 | |
private static final String FILTER_PREFIX = "filter."; |
56 | |
|
57 | |
private static final String CLASS_SUFFIX = ".class"; |
58 | |
|
59 | |
private static final String FILTER_MAPPING_PREFIX = "filtermapping."; |
60 | |
|
61 | |
private FilterConfig config; |
62 | |
|
63 | 0 | private final Map<String, Filter> filters = new HashMap<String, Filter>(); |
64 | |
|
65 | 0 | private final SortedSet<FilterMapping> filterMappings = new TreeSet<FilterMapping>(); |
66 | |
|
67 | 0 | private boolean initted = false; |
68 | |
|
69 | |
public void init(FilterConfig cfg) throws ServletException { |
70 | 0 | this.config = cfg; |
71 | 0 | } |
72 | |
|
73 | |
private void addFilter(String name, String classname, Map<String, String> props) throws ServletException { |
74 | 0 | LOG.debug("Adding filter: " + name + "=" + classname); |
75 | 0 | Object filterObject = GlobalResourceLoader.getResourceLoader().getObject(new ObjectDefinition(classname)); |
76 | 0 | if (filterObject == null) { |
77 | 0 | throw new ServletException("Filter '" + name + "' class not found: " + classname); |
78 | |
|
79 | |
} |
80 | 0 | if (!(filterObject instanceof Filter)) { |
81 | 0 | LOG.error("Class '" + filterObject.getClass() + "' does not implement servlet javax.servlet.Filter"); |
82 | 0 | return; |
83 | |
} |
84 | 0 | Filter filter = (Filter) filterObject; |
85 | 0 | BootstrapFilterConfig fc = new BootstrapFilterConfig(config.getServletContext(), name); |
86 | 0 | for (Map.Entry<String, String> entry : props.entrySet()) { |
87 | 0 | String key = entry.getKey().toString(); |
88 | 0 | final String prefix = FILTER_PREFIX + name + "."; |
89 | 0 | if (!key.startsWith(prefix) || key.equals(FILTER_PREFIX + name + CLASS_SUFFIX)) { |
90 | 0 | continue; |
91 | |
} |
92 | 0 | String paramName = key.substring(prefix.length()); |
93 | 0 | fc.addInitParameter(paramName, entry.getValue()); |
94 | 0 | } |
95 | |
try { |
96 | 0 | filter.init(fc); |
97 | 0 | filters.put(name, filter); |
98 | 0 | } catch (ServletException se) { |
99 | 0 | LOG.error("Error initializing filter: " + name + " [" + classname + "]", se); |
100 | 0 | } |
101 | 0 | } |
102 | |
|
103 | |
private void addFilterMapping(String filterName, String orderNumber, String value) { |
104 | 0 | filterMappings.add(new FilterMapping(filterName, orderNumber, value)); |
105 | 0 | } |
106 | |
|
107 | |
private synchronized void init() throws ServletException { |
108 | 0 | if (initted) { |
109 | 0 | return; |
110 | |
} |
111 | 0 | LOG.debug("initializing..."); |
112 | 0 | Config cfg = ConfigContext.getCurrentContextConfig(); |
113 | |
|
114 | |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
115 | 0 | final Map<String, String> p = new HashMap<String, String>((Map) cfg.getProperties()); |
116 | |
|
117 | 0 | for (Map.Entry<String, String> entry : p.entrySet()) { |
118 | 0 | String key = entry.getKey().toString(); |
119 | 0 | if (key.startsWith(FILTER_MAPPING_PREFIX)) { |
120 | 0 | String[] values = key.split("\\."); |
121 | 0 | if (values.length != 2 && values.length != 3) { |
122 | 0 | throw new ServletException("Invalid filter mapping defined. Should contain 2 or 3 pieces in the form of filtermapping.<<filter name>>.<<order number>> with the last piece optional."); |
123 | |
} |
124 | 0 | String filterName = values[1]; |
125 | 0 | String orderNumber = (values.length == 2 ? "0" : values[2]); |
126 | 0 | String value = entry.getValue(); |
127 | 0 | addFilterMapping(filterName, orderNumber, value); |
128 | 0 | } else if (key.startsWith(FILTER_PREFIX) && key.endsWith(CLASS_SUFFIX)) { |
129 | 0 | String name = key.substring(FILTER_PREFIX.length(), key.length() - CLASS_SUFFIX.length()); |
130 | 0 | String value = entry.getValue(); |
131 | |
|
132 | |
|
133 | |
|
134 | 0 | addFilter(name, value, p); |
135 | |
} |
136 | 0 | } |
137 | |
|
138 | 0 | for (String filterName : filters.keySet()) { |
139 | 0 | if (!hasFilterMapping(filterName)) { |
140 | 0 | LOG.warn("NO FILTER MAPPING DETECTED. Filter " + filterName + " has no mapping and will not be called."); |
141 | |
} |
142 | |
} |
143 | 0 | initted = true; |
144 | 0 | } |
145 | |
|
146 | |
private boolean hasFilterMapping(String filterName) { |
147 | 0 | for (FilterMapping filterMapping : filterMappings) { |
148 | 0 | if (filterMapping.getFilterName().equals(filterName)) { |
149 | 0 | return true; |
150 | |
} |
151 | |
} |
152 | 0 | return false; |
153 | |
} |
154 | |
|
155 | |
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { |
156 | 0 | LOG.debug("Begin BootstrapFilter..."); |
157 | 0 | init(); |
158 | |
|
159 | 0 | if (!filterMappings.isEmpty() && request instanceof HttpServletRequest) { |
160 | 0 | chain = buildChain((HttpServletRequest) request, chain); |
161 | |
} |
162 | 0 | LOG.debug("...ending BootstrapFilter preperation, executing BootstrapFilter Chain."); |
163 | 0 | chain.doFilter(request, response); |
164 | |
|
165 | 0 | } |
166 | |
|
167 | |
private FilterChain buildChain(HttpServletRequest request, FilterChain targetChain) { |
168 | 0 | BootstrapFilterChain chain = new BootstrapFilterChain(targetChain, ClassLoaderUtils.getDefaultClassLoader()); |
169 | 0 | String requestPath = request.getServletPath(); |
170 | 0 | for (FilterMapping mapping : filterMappings) { |
171 | 0 | Filter filter = filters.get(mapping.getFilterName()); |
172 | 0 | if (!chain.containsFilter(filter) && matchFiltersURL(mapping.getUrlPattern(), requestPath)) { |
173 | 0 | chain.addFilter(filter); |
174 | |
} |
175 | 0 | } |
176 | 0 | return chain; |
177 | |
} |
178 | |
|
179 | |
public void destroy() { |
180 | 0 | for (Filter filter : filters.values()) { |
181 | |
try { |
182 | 0 | filter.destroy(); |
183 | 0 | } catch (Exception e) { |
184 | 0 | LOG.error("Error destroying filter: " + filter, e); |
185 | 0 | } |
186 | |
} |
187 | 0 | } |
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
private boolean matchFiltersURL(String urlPattern, String requestPath) { |
193 | |
|
194 | 0 | if (requestPath == null) { |
195 | 0 | return (false); |
196 | |
} |
197 | |
|
198 | |
|
199 | 0 | if (urlPattern == null) { |
200 | 0 | return (false); |
201 | |
} |
202 | |
|
203 | |
|
204 | 0 | if (urlPattern.equals(requestPath)) { |
205 | 0 | return (true); |
206 | |
} |
207 | |
|
208 | |
|
209 | 0 | if (urlPattern.equals("/*") || urlPattern.equals("*")) { |
210 | 0 | return (true); |
211 | |
} |
212 | 0 | if (urlPattern.endsWith("/*")) { |
213 | 0 | if (urlPattern.regionMatches(0, requestPath, 0, urlPattern.length() - 2)) { |
214 | 0 | if (requestPath.length() == (urlPattern.length() - 2)) { |
215 | 0 | return (true); |
216 | 0 | } else if ('/' == requestPath.charAt(urlPattern.length() - 2)) { |
217 | 0 | return (true); |
218 | |
} |
219 | |
} |
220 | 0 | return (false); |
221 | |
} |
222 | |
|
223 | |
|
224 | 0 | if (urlPattern.startsWith("*.")) { |
225 | 0 | int slash = requestPath.lastIndexOf('/'); |
226 | 0 | int period = requestPath.lastIndexOf('.'); |
227 | 0 | if ((slash >= 0) && (period > slash) && (period != requestPath.length() - 1) && ((requestPath.length() - period) == (urlPattern.length() - 1))) { |
228 | 0 | return (urlPattern.regionMatches(2, requestPath, period + 1, urlPattern.length() - 2)); |
229 | |
} |
230 | |
} |
231 | |
|
232 | |
|
233 | 0 | return (false); |
234 | |
|
235 | |
} |
236 | |
|
237 | |
} |
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
class BootstrapFilterChain implements FilterChain { |
246 | |
|
247 | 0 | private final List<Filter> filters = new LinkedList<Filter>(); |
248 | |
|
249 | |
private final FilterChain target; |
250 | |
|
251 | |
private Iterator<Filter> filterIterator; |
252 | |
|
253 | |
private ClassLoader originalClassLoader; |
254 | |
|
255 | 0 | public BootstrapFilterChain(FilterChain target, ClassLoader originalClassLoader) { |
256 | 0 | this.target = target; |
257 | 0 | this.originalClassLoader = originalClassLoader; |
258 | 0 | } |
259 | |
|
260 | |
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { |
261 | 0 | if (filterIterator == null) { |
262 | 0 | filterIterator = filters.iterator(); |
263 | |
} |
264 | 0 | if (filterIterator.hasNext()) { |
265 | 0 | (filterIterator.next()).doFilter(request, response, this); |
266 | |
} else { |
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | 0 | Thread.currentThread().setContextClassLoader(originalClassLoader); |
272 | 0 | target.doFilter(request, response); |
273 | |
} |
274 | 0 | } |
275 | |
|
276 | |
public void addFilter(Filter filter) { |
277 | 0 | filters.add(filter); |
278 | 0 | } |
279 | |
|
280 | |
public boolean containsFilter(Filter filter) { |
281 | 0 | return filters.contains(filter); |
282 | |
} |
283 | |
|
284 | |
public boolean isEmpty() { |
285 | 0 | return filters.isEmpty(); |
286 | |
} |
287 | |
|
288 | |
} |
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
class BootstrapFilterConfig implements FilterConfig { |
296 | |
|
297 | |
private final ServletContext servletContext; |
298 | |
|
299 | |
private final String filterName; |
300 | |
|
301 | 0 | private final Map<String, String> initParameters = new HashMap<String, String>(); |
302 | |
|
303 | |
public BootstrapFilterConfig() { |
304 | 0 | this(null, ""); |
305 | 0 | } |
306 | |
|
307 | |
public BootstrapFilterConfig(String filterName) { |
308 | 0 | this(null, filterName); |
309 | 0 | } |
310 | |
|
311 | |
public BootstrapFilterConfig(ServletContext servletContext) { |
312 | 0 | this(servletContext, ""); |
313 | 0 | } |
314 | |
|
315 | 0 | public BootstrapFilterConfig(ServletContext servletContext, String filterName) { |
316 | 0 | this.servletContext = servletContext; |
317 | 0 | this.filterName = filterName; |
318 | 0 | } |
319 | |
|
320 | |
public String getFilterName() { |
321 | 0 | return filterName; |
322 | |
} |
323 | |
|
324 | |
public ServletContext getServletContext() { |
325 | 0 | return servletContext; |
326 | |
} |
327 | |
|
328 | |
public void addInitParameter(String name, String value) { |
329 | 0 | this.initParameters.put(name, value); |
330 | 0 | } |
331 | |
|
332 | |
public String getInitParameter(String name) { |
333 | 0 | return this.initParameters.get(name); |
334 | |
} |
335 | |
|
336 | |
public Enumeration<String> getInitParameterNames() { |
337 | 0 | return Collections.enumeration(this.initParameters.keySet()); |
338 | |
} |
339 | |
|
340 | |
} |
341 | |
|
342 | 0 | class FilterMapping implements Comparable<FilterMapping> { |
343 | |
|
344 | |
private String filterName; |
345 | |
|
346 | |
private String orderValue; |
347 | |
|
348 | |
private String urlPattern; |
349 | |
|
350 | 0 | public FilterMapping(String filterName, String orderValue, String urlPattern) { |
351 | 0 | this.filterName = filterName; |
352 | 0 | this.orderValue = orderValue; |
353 | 0 | this.urlPattern = urlPattern; |
354 | 0 | } |
355 | |
|
356 | |
public int compareTo(FilterMapping object) { |
357 | 0 | return orderValue.compareTo(object.orderValue); |
358 | |
} |
359 | |
|
360 | |
public String getFilterName() { |
361 | 0 | return filterName; |
362 | |
} |
363 | |
|
364 | |
public String getOrderValue() { |
365 | 0 | return orderValue; |
366 | |
} |
367 | |
|
368 | |
public String getUrlPattern() { |
369 | 0 | return urlPattern; |
370 | |
} |
371 | |
|
372 | |
} |