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.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   * Handles both the preparation and execution phases of the Struts dispatching process.  This filter is better to use
37   * when you don't have another filter that needs access to action context information, such as Sitemesh.
38   */
39  public class StrutsPrepareAndExecuteFilter implements StrutsStatics, Filter {
40      private PrepareOperations prepare;
41      private ExecuteOperations execute;
42  
43      public void init(FilterConfig filterConfig) throws ServletException {
44          InitOperations init = new InitOperations();
45          try {
46              FilterHostConfig config = new FilterHostConfig(filterConfig);
47              init.initLogging(config);
48              Dispatcher dispatcher = init.initDispatcher(config);
49              init.initStaticContentLoader(config, dispatcher);
50  
51              prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
52              execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);
53          } finally {
54              init.cleanup();
55          }
56  
57      }
58  
59      public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
60  
61          HttpServletRequest request = (HttpServletRequest) req;
62          HttpServletResponse response = (HttpServletResponse) res;
63  
64          try {
65              prepare.createActionContext(request, response);
66              prepare.assignDispatcherToThread();
67              prepare.setEncodingAndLocale(request, response);
68              request = prepare.wrapRequest(request);
69              ActionMapping mapping = prepare.findActionMapping(request, response);
70              if (mapping == null) {
71                  boolean handled = execute.executeStaticResourceRequest(request, response);
72                  if (!handled) {
73                      chain.doFilter(request, response);
74                  }
75              } else {
76                  execute.executeAction(request, response, mapping);
77              }
78          } finally {
79              prepare.cleanupRequest(request);
80          }
81      }
82  
83      public void destroy() {
84          prepare.cleanupDispatcher();
85      }
86  }