1 /*
2 * $Id: DefaultActionSupport.java 651946 2008-04-27 13:41:38Z apetrelli $
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
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 // This is necessary since we need the dispatcher instance, which was created by the prepare filter
72 if (execute == null) {
73 lazyInit();
74 }
75
76 ActionMapping mapping = prepare.findActionMapping(request, response);
77
78 //if recusrion counter is > 1, it means we are in a "forward", in that case a mapping will still be
79 //in the request, if we handle it, it will lead to an infinte loop, see WW-3077
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 }