1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts2.dispatcher.ng.filter;
22
23 import org.apache.struts2.StrutsStatics;
24 import org.apache.struts2.dispatcher.Dispatcher;
25 import org.apache.struts2.dispatcher.ng.InitOperations;
26 import org.apache.struts2.dispatcher.ng.PrepareOperations;
27
28 import javax.servlet.*;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31 import java.io.IOException;
32 import java.util.List;
33 import java.util.regex.Pattern;
34
35 /***
36 * Prepares the request for execution by a later {@link org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter} filter instance.
37 */
38 public class StrutsPrepareFilter implements StrutsStatics, Filter {
39
40 protected static final String REQUEST_EXCLUDED_FROM_ACTION_MAPPING = StrutsPrepareFilter.class.getName() + ".REQUEST_EXCLUDED_FROM_ACTION_MAPPING";
41
42 protected PrepareOperations prepare;
43 protected List<Pattern> excludedPatterns = null;
44
45 public void init(FilterConfig filterConfig) throws ServletException {
46 InitOperations init = new InitOperations();
47 try {
48 FilterHostConfig config = new FilterHostConfig(filterConfig);
49 init.initLogging(config);
50 Dispatcher dispatcher = init.initDispatcher(config);
51
52 prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
53 this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);
54
55 postInit(dispatcher, filterConfig);
56 } finally {
57 init.cleanup();
58 }
59 }
60
61 /***
62 * Callback for post initialization
63 */
64 protected void postInit(Dispatcher dispatcher, FilterConfig filterConfig) {
65 }
66
67 public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
68
69 HttpServletRequest request = (HttpServletRequest) req;
70 HttpServletResponse response = (HttpServletResponse) res;
71
72 try {
73 prepare.setEncodingAndLocale(request, response);
74 prepare.createActionContext(request, response);
75 prepare.assignDispatcherToThread();
76 if ( excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {
77 request.setAttribute(REQUEST_EXCLUDED_FROM_ACTION_MAPPING, new Object());
78 } else {
79 request = prepare.wrapRequest(request);
80 prepare.findActionMapping(request, response);
81 }
82 chain.doFilter(request, response);
83 } finally {
84 prepare.cleanupRequest(request);
85 }
86 }
87
88 public void destroy() {
89 prepare.cleanupDispatcher();
90 }
91 }