1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.seasar.cubby.filter; |
18 | |
|
19 | |
import static org.seasar.cubby.CubbyConstants.ATTR_FILTER_CHAIN; |
20 | |
import static org.seasar.cubby.CubbyConstants.ATTR_PARAMS; |
21 | |
import static org.seasar.cubby.CubbyConstants.ATTR_ROUTING; |
22 | |
import static org.seasar.cubby.internal.util.LogMessages.format; |
23 | |
|
24 | |
import java.io.IOException; |
25 | |
import java.util.ArrayList; |
26 | |
import java.util.Collections; |
27 | |
import java.util.Iterator; |
28 | |
import java.util.List; |
29 | |
import java.util.Map; |
30 | |
import java.util.StringTokenizer; |
31 | |
import java.util.regex.Matcher; |
32 | |
import java.util.regex.Pattern; |
33 | |
|
34 | |
import javax.servlet.Filter; |
35 | |
import javax.servlet.FilterChain; |
36 | |
import javax.servlet.FilterConfig; |
37 | |
import javax.servlet.ServletException; |
38 | |
import javax.servlet.ServletRequest; |
39 | |
import javax.servlet.ServletResponse; |
40 | |
import javax.servlet.http.HttpServletRequest; |
41 | |
import javax.servlet.http.HttpServletResponse; |
42 | |
|
43 | |
import org.seasar.cubby.CubbyConstants; |
44 | |
import org.seasar.cubby.controller.MessagesBehaviour; |
45 | |
import org.seasar.cubby.internal.controller.ActionProcessor; |
46 | |
import org.seasar.cubby.internal.controller.ActionResultWrapper; |
47 | |
import org.seasar.cubby.internal.controller.ThreadContext; |
48 | |
import org.seasar.cubby.internal.controller.impl.ActionProcessorImpl; |
49 | |
import org.seasar.cubby.internal.util.RequestUtils; |
50 | |
import org.seasar.cubby.internal.util.StringUtils; |
51 | |
import org.seasar.cubby.plugin.Plugin; |
52 | |
import org.seasar.cubby.plugin.PluginRegistry; |
53 | |
import org.seasar.cubby.plugin.RequestProcessingInvocation; |
54 | |
import org.seasar.cubby.plugin.RoutingInvocation; |
55 | |
import org.seasar.cubby.routing.PathInfo; |
56 | |
import org.seasar.cubby.routing.PathResolver; |
57 | |
import org.seasar.cubby.routing.Routing; |
58 | |
import org.seasar.cubby.spi.ContainerProvider; |
59 | |
import org.seasar.cubby.spi.PathResolverProvider; |
60 | |
import org.seasar.cubby.spi.ProviderFactory; |
61 | |
import org.seasar.cubby.spi.RequestParserProvider; |
62 | |
import org.seasar.cubby.spi.container.Container; |
63 | |
import org.slf4j.Logger; |
64 | |
import org.slf4j.LoggerFactory; |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | 13 | public class CubbyFilter implements Filter { |
76 | |
|
77 | |
|
78 | 1 | private static final Logger logger = LoggerFactory |
79 | |
.getLogger(CubbyFilter.class); |
80 | |
|
81 | |
|
82 | |
public static final String IGNORE_PATH_PATTERN = "ignorePathPattern"; |
83 | |
|
84 | |
|
85 | 13 | private final List<Pattern> ignorePathPatterns = new ArrayList<Pattern>(); |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
public void init(final FilterConfig config) throws ServletException { |
120 | 2 | final String ignorePathPatternString = config |
121 | |
.getInitParameter(IGNORE_PATH_PATTERN); |
122 | 2 | if (!StringUtils.isEmpty(ignorePathPatternString)) { |
123 | |
|
124 | 2 | for (final StringTokenizer tokenizer = new StringTokenizer( |
125 | 6 | ignorePathPatternString, ","); tokenizer.hasMoreTokens();) { |
126 | 4 | final String token = tokenizer.nextToken(); |
127 | 4 | final Pattern pattern = Pattern.compile(token); |
128 | 4 | ignorePathPatterns.add(pattern); |
129 | 4 | } |
130 | |
} |
131 | 2 | } |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
public void destroy() { |
137 | 2 | } |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
public void doFilter(final ServletRequest req, final ServletResponse res, |
161 | |
final FilterChain chain) throws IOException, ServletException { |
162 | 2 | final HttpServletRequest request = (HttpServletRequest) req; |
163 | 2 | final HttpServletResponse response = (HttpServletResponse) res; |
164 | |
|
165 | 2 | final String path = RequestUtils.getPath(request); |
166 | 2 | if (logger.isDebugEnabled()) { |
167 | 2 | logger.debug(format("DCUB0006", path)); |
168 | |
} |
169 | |
|
170 | 2 | final PathInfo pathInfo = findPathInfo(request, response, path, |
171 | |
ignorePathPatterns); |
172 | |
|
173 | 2 | if (pathInfo != null) { |
174 | 1 | request.setAttribute(ATTR_FILTER_CHAIN, chain); |
175 | |
try { |
176 | 1 | processRequest(request, response, pathInfo); |
177 | 0 | } catch (final Exception e) { |
178 | 0 | if (e instanceof IOException) { |
179 | 0 | throw (IOException) e; |
180 | 0 | } else if (e instanceof ServletException) { |
181 | 0 | throw (ServletException) e; |
182 | |
} else { |
183 | 0 | throw new ServletException(e); |
184 | |
} |
185 | 1 | } |
186 | |
} else { |
187 | 1 | chain.doFilter(request, response); |
188 | |
} |
189 | 2 | } |
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
protected PathInfo findPathInfo(final HttpServletRequest request, |
216 | |
final HttpServletResponse response, final String path, |
217 | |
List<Pattern> ignorePathPatterns) { |
218 | |
final PathInfo pathInfo; |
219 | 5 | final Routing routing = RequestUtils |
220 | |
.getAttribute(request, ATTR_ROUTING); |
221 | 5 | if (routing != null) { |
222 | 1 | request.removeAttribute(ATTR_ROUTING); |
223 | 1 | pathInfo = new ForwardFromActionPathInfo(routing); |
224 | 4 | } else if (isIgnorePath(path, ignorePathPatterns)) { |
225 | 1 | pathInfo = null; |
226 | |
} else { |
227 | 3 | final PathResolver pathResolver = createPathResolver(); |
228 | 3 | pathInfo = invokeRouting(request, response, path, pathResolver); |
229 | |
} |
230 | 5 | return pathInfo; |
231 | |
} |
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
private static boolean isIgnorePath(final String path, |
244 | |
final List<Pattern> ignorePathPatterns) { |
245 | 4 | for (final Pattern pattern : ignorePathPatterns) { |
246 | 5 | final Matcher matcher = pattern.matcher(path); |
247 | 5 | if (matcher.matches()) { |
248 | 1 | return true; |
249 | |
} |
250 | 4 | } |
251 | 3 | return false; |
252 | |
} |
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
protected PathInfo invokeRouting(final HttpServletRequest request, |
269 | |
final HttpServletResponse response, final String path, |
270 | |
final PathResolver pathResolver) { |
271 | 3 | final RoutingInvocation routingInvocation = new RoutingInvocationImpl( |
272 | |
path, pathResolver, request, response); |
273 | |
try { |
274 | 3 | return routingInvocation.proceed(); |
275 | 0 | } catch (final Exception e) { |
276 | 0 | logger.warn("routing failed.", e); |
277 | 0 | return null; |
278 | |
} |
279 | |
} |
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | 4 | static class RoutingInvocationImpl implements RoutingInvocation { |
287 | |
|
288 | |
|
289 | |
private final String path; |
290 | |
|
291 | |
|
292 | |
private final PathResolver pathResolver; |
293 | |
|
294 | |
|
295 | |
private final HttpServletRequest request; |
296 | |
|
297 | |
|
298 | |
private final HttpServletResponse response; |
299 | |
|
300 | |
|
301 | |
private final Iterator<Plugin> pluginsIterator; |
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
public RoutingInvocationImpl(final String path, |
316 | |
final PathResolver pathResolver, |
317 | |
final HttpServletRequest request, |
318 | 3 | final HttpServletResponse response) { |
319 | 3 | this.path = path; |
320 | 3 | this.pathResolver = pathResolver; |
321 | 3 | this.request = request; |
322 | 3 | this.response = response; |
323 | |
|
324 | 3 | final PluginRegistry pluginRegistry = PluginRegistry.getInstance(); |
325 | 3 | this.pluginsIterator = pluginRegistry.getPlugins().iterator(); |
326 | 3 | } |
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
public PathInfo proceed() throws Exception { |
332 | 4 | if (pluginsIterator.hasNext()) { |
333 | 1 | final Plugin plugin = pluginsIterator.next(); |
334 | 1 | return plugin.invokeRouting(this); |
335 | |
} else { |
336 | 3 | final PathInfo pathInfo = pathResolver.getPathInfo(path, |
337 | |
request.getMethod(), request.getCharacterEncoding()); |
338 | 3 | return pathInfo; |
339 | |
} |
340 | |
} |
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
public String getPath() { |
346 | 0 | return path; |
347 | |
} |
348 | |
|
349 | |
|
350 | |
|
351 | |
|
352 | |
public PathResolver getPathResolver() { |
353 | 0 | return pathResolver; |
354 | |
} |
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
public HttpServletRequest getRequest() { |
360 | 0 | return request; |
361 | |
} |
362 | |
|
363 | |
|
364 | |
|
365 | |
|
366 | |
public HttpServletResponse getResponse() { |
367 | 0 | return response; |
368 | |
} |
369 | |
|
370 | |
} |
371 | |
|
372 | |
|
373 | |
|
374 | |
|
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | |
|
384 | |
protected void processRequest(final HttpServletRequest request, |
385 | |
final HttpServletResponse response, final PathInfo pathInfo) |
386 | |
throws Exception { |
387 | 2 | final HttpServletRequest wrappedRequest = new CubbyHttpServletRequestWrapper( |
388 | |
this, request, pathInfo.getURIParameters()); |
389 | 2 | final RequestProcessingInvocation invocation = new RequestProcessingInvocationImpl( |
390 | |
this, wrappedRequest, response, pathInfo); |
391 | 2 | invocation.proceed(); |
392 | 2 | } |
393 | |
|
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
|
399 | |
|
400 | |
|
401 | |
protected Map<String, Object[]> parseRequest( |
402 | |
final HttpServletRequest request) { |
403 | 1 | final RequestParserProvider requestParserProvider = ProviderFactory |
404 | |
.get(RequestParserProvider.class); |
405 | 1 | final Map<String, Object[]> parameterMap = requestParserProvider |
406 | |
.getParameterMap(request); |
407 | 1 | return parameterMap; |
408 | |
} |
409 | |
|
410 | |
|
411 | |
|
412 | |
|
413 | |
|
414 | |
|
415 | 3 | static class RequestProcessingInvocationImpl implements |
416 | |
RequestProcessingInvocation { |
417 | |
|
418 | |
|
419 | |
private CubbyFilter cubbyFilter; |
420 | |
|
421 | |
|
422 | |
private final HttpServletRequest request; |
423 | |
|
424 | |
|
425 | |
private final HttpServletResponse response; |
426 | |
|
427 | |
|
428 | |
private final PathInfo pathInfo; |
429 | |
|
430 | |
|
431 | |
private final Iterator<Plugin> pluginsIterator; |
432 | |
|
433 | |
|
434 | |
|
435 | |
|
436 | |
|
437 | |
|
438 | |
|
439 | |
|
440 | |
|
441 | |
|
442 | |
|
443 | |
|
444 | |
|
445 | |
public RequestProcessingInvocationImpl(final CubbyFilter cubbyFilter, |
446 | |
final HttpServletRequest request, |
447 | 2 | final HttpServletResponse response, final PathInfo pathInfo) { |
448 | 2 | this.cubbyFilter = cubbyFilter; |
449 | 2 | this.request = request; |
450 | 2 | this.response = response; |
451 | 2 | this.pathInfo = pathInfo; |
452 | |
|
453 | 2 | final PluginRegistry pluginRegistry = PluginRegistry.getInstance(); |
454 | 2 | this.pluginsIterator = pluginRegistry.getPlugins().iterator(); |
455 | 2 | } |
456 | |
|
457 | |
|
458 | |
|
459 | |
|
460 | |
public Void proceed() throws Exception { |
461 | 3 | if (pluginsIterator.hasNext()) { |
462 | 1 | final Plugin plugin = pluginsIterator.next(); |
463 | 1 | plugin.invokeRequestProcessing(this); |
464 | 1 | } else { |
465 | 2 | final HttpServletRequest request = getRequest(); |
466 | 2 | final Map<String, Object[]> parameterMap = cubbyFilter |
467 | |
.parseRequest(request); |
468 | 2 | request.setAttribute(ATTR_PARAMS, parameterMap); |
469 | 2 | final Routing routing = pathInfo.dispatch(parameterMap); |
470 | 2 | ThreadContext.enter(request, response); |
471 | |
try { |
472 | 2 | final ActionProcessor actionProcessor = cubbyFilter |
473 | |
.createActionProcessor(); |
474 | 2 | final ActionResultWrapper actionResultWrapper = actionProcessor |
475 | |
.process(request, response, routing); |
476 | 2 | actionResultWrapper.execute(request, response); |
477 | |
} finally { |
478 | 2 | ThreadContext.exit(); |
479 | 2 | } |
480 | |
} |
481 | 3 | return null; |
482 | |
} |
483 | |
|
484 | |
|
485 | |
|
486 | |
|
487 | |
public HttpServletRequest getRequest() { |
488 | 2 | return request; |
489 | |
} |
490 | |
|
491 | |
|
492 | |
|
493 | |
|
494 | |
public HttpServletResponse getResponse() { |
495 | 0 | return response; |
496 | |
} |
497 | |
|
498 | |
|
499 | |
|
500 | |
|
501 | |
public PathInfo getPathInfo() { |
502 | 0 | return pathInfo; |
503 | |
} |
504 | |
|
505 | |
} |
506 | |
|
507 | |
|
508 | |
|
509 | |
|
510 | |
|
511 | |
|
512 | |
protected PathResolver createPathResolver() { |
513 | 1 | final PathResolverProvider pathResolverProvider = ProviderFactory |
514 | |
.get(PathResolverProvider.class); |
515 | 1 | final PathResolver pathResolver = pathResolverProvider |
516 | |
.getPathResolver(); |
517 | 1 | return pathResolver; |
518 | |
} |
519 | |
|
520 | |
|
521 | |
|
522 | |
|
523 | |
|
524 | |
|
525 | |
protected ActionProcessor createActionProcessor() { |
526 | 1 | final ActionProcessor actionProcessor = new ActionProcessorImpl(); |
527 | 1 | return actionProcessor; |
528 | |
} |
529 | |
|
530 | |
|
531 | |
|
532 | |
|
533 | |
|
534 | |
|
535 | |
protected MessagesBehaviour createMessagesBehaviour() { |
536 | 5 | final Container container = ProviderFactory |
537 | |
.get(ContainerProvider.class).getContainer(); |
538 | 5 | final MessagesBehaviour messagesBehaviour = container |
539 | |
.lookup(MessagesBehaviour.class); |
540 | 5 | return messagesBehaviour; |
541 | |
} |
542 | |
|
543 | |
|
544 | |
|
545 | |
|
546 | |
|
547 | |
|
548 | 13 | static class ForwardFromActionPathInfo implements PathInfo { |
549 | |
|
550 | |
private final Routing routing; |
551 | |
|
552 | 1 | private final Map<String, String[]> uriParameters = Collections |
553 | |
.emptyMap(); |
554 | |
|
555 | 1 | public ForwardFromActionPathInfo(final Routing routing) { |
556 | 1 | this.routing = routing; |
557 | 1 | } |
558 | |
|
559 | |
|
560 | |
|
561 | |
|
562 | |
public Map<String, String[]> getURIParameters() { |
563 | 0 | return uriParameters; |
564 | |
} |
565 | |
|
566 | |
|
567 | |
|
568 | |
|
569 | |
public Routing dispatch(final Map<String, Object[]> parameterMap) { |
570 | 1 | return routing; |
571 | |
} |
572 | |
|
573 | |
} |
574 | |
|
575 | |
} |