View Javadoc

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.mapper.ActionMapping;
26  import org.apache.struts2.dispatcher.ng.ExecuteOperations;
27  import org.apache.struts2.dispatcher.ng.InitOperations;
28  import org.apache.struts2.dispatcher.ng.PrepareOperations;
29  
30  import javax.servlet.*;
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  import java.io.IOException;
34  import java.util.List;
35  import java.util.regex.Pattern;
36  
37  /***
38   * Handles both the preparation and execution phases of the Struts dispatching process.  This filter is better to use
39   * when you don't have another filter that needs access to action context information, such as Sitemesh.
40   */
41  public class StrutsPrepareAndExecuteFilter implements StrutsStatics, Filter {
42      protected PrepareOperations prepare;
43      protected ExecuteOperations execute;
44  	protected List<Pattern> excludedPatterns = null;
45  
46      public void init(FilterConfig filterConfig) throws ServletException {
47          InitOperations init = new InitOperations();
48          try {
49              FilterHostConfig config = new FilterHostConfig(filterConfig);
50              init.initLogging(config);
51              Dispatcher dispatcher = init.initDispatcher(config);
52              init.initStaticContentLoader(config, dispatcher);
53  
54              prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
55              execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);
56  			this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);
57  
58              postInit(dispatcher, filterConfig);
59          } finally {
60              init.cleanup();
61          }
62  
63      }
64  
65      /***
66       * Callback for post initialization
67       */
68      protected void postInit(Dispatcher dispatcher, FilterConfig filterConfig) {
69      }
70  
71      public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
72  
73          HttpServletRequest request = (HttpServletRequest) req;
74          HttpServletResponse response = (HttpServletResponse) res;
75  
76          try {
77              prepare.setEncodingAndLocale(request, response);
78              prepare.createActionContext(request, response);
79              prepare.assignDispatcherToThread();
80  			if ( excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {
81  				chain.doFilter(request, response);
82  			} else {
83  				request = prepare.wrapRequest(request);
84  				ActionMapping mapping = prepare.findActionMapping(request, response, true);
85  				if (mapping == null) {
86  					boolean handled = execute.executeStaticResourceRequest(request, response);
87  					if (!handled) {
88  						chain.doFilter(request, response);
89  					}
90  				} else {
91  					execute.executeAction(request, response, mapping);
92  				}
93  			}
94          } finally {
95              prepare.cleanupRequest(request);
96          }
97      }
98  
99      public void destroy() {
100         prepare.cleanupDispatcher();
101     }
102 }