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.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  }