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.PrepareOperations;
26 import org.apache.struts2.dispatcher.ng.ExecuteOperations;
27 import org.apache.struts2.dispatcher.ng.InitOperations;
28 import org.apache.struts2.dispatcher.mapper.ActionMapping;
29
30 import javax.servlet.*;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33 import java.io.IOException;
34
35 /***
36 * Executes the discovered request information. This filter requires the {@link StrutsPrepareFilter} to have already
37 * been executed in the current chain.
38 */
39 public class StrutsExecuteFilter implements StrutsStatics, Filter {
40 protected PrepareOperations prepare;
41 protected ExecuteOperations execute;
42
43 protected FilterConfig filterConfig;
44
45 public void init(FilterConfig filterConfig) throws ServletException {
46 this.filterConfig = filterConfig;
47 }
48
49 protected synchronized void lazyInit() {
50 if (execute == null) {
51 InitOperations init = new InitOperations();
52 Dispatcher dispatcher = init.findDispatcherOnThread();
53 init.initStaticContentLoader(new FilterHostConfig(filterConfig), dispatcher);
54
55 prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
56 execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);
57 }
58
59 }
60
61 public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
62
63 HttpServletRequest request = (HttpServletRequest) req;
64 HttpServletResponse response = (HttpServletResponse) res;
65
66 if (excludeUrl(request)) {
67 chain.doFilter(request, response);
68 return;
69 }
70
71
72 if (execute == null) {
73 lazyInit();
74 }
75
76 ActionMapping mapping = prepare.findActionMapping(request, response);
77
78
79
80 Integer recursionCounter = (Integer) request.getAttribute(PrepareOperations.CLEANUP_RECURSION_COUNTER);
81
82 if (mapping == null || recursionCounter > 1) {
83 boolean handled = execute.executeStaticResourceRequest(request, response);
84 if (!handled) {
85 chain.doFilter(request, response);
86 }
87 } else {
88 execute.executeAction(request, response, mapping);
89 }
90 }
91
92 private boolean excludeUrl(HttpServletRequest request) {
93 return request.getAttribute(StrutsPrepareFilter.REQUEST_EXCLUDED_FROM_ACTION_MAPPING) != null;
94 }
95
96 public void destroy() {
97 prepare = null;
98 execute = null;
99 filterConfig = null;
100 }
101 }