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   * 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      private PrepareOperations prepare;
41      private ExecuteOperations execute;
42  
43      private 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          // This is necessary since we need the dispatcher instance, which was created by the prepare filter
67          lazyInit();
68  
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      }
79  
80      public void destroy() {
81          prepare = null;
82          execute = null;
83          filterConfig = null;
84      }
85  }