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.PrepareOperations;
26  import org.apache.struts2.dispatcher.ng.InitOperations;
27  
28  import javax.servlet.*;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  import java.io.IOException;
32  
33  /***
34   * Prepares the request for execution by a later {@link org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter} filter instance.
35   */
36  public class StrutsPrepareFilter implements StrutsStatics, Filter {
37      private PrepareOperations prepare;
38  
39      public void init(FilterConfig filterConfig) throws ServletException {
40          InitOperations init = new InitOperations();
41          try {
42              FilterHostConfig config = new FilterHostConfig(filterConfig);
43              init.initLogging(config);
44              Dispatcher dispatcher = init.initDispatcher(config);
45  
46              prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
47          } finally {
48              init.cleanup();
49          }
50  
51      }
52  
53      public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
54  
55          HttpServletRequest request = (HttpServletRequest) req;
56          HttpServletResponse response = (HttpServletResponse) res;
57  
58          try {
59              prepare.createActionContext(request, response);
60              prepare.assignDispatcherToThread();
61              prepare.setEncodingAndLocale(request, response);
62              request = prepare.wrapRequest(request);
63              prepare.findActionMapping(request, response);
64  
65              chain.doFilter(request, response);
66          } finally {
67              prepare.cleanupRequest(request);
68          }
69      }
70  
71      public void destroy() {
72          prepare.cleanupDispatcher();
73      }
74  }