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.servlet;
22  
23  import org.apache.struts2.dispatcher.Dispatcher;
24  import org.apache.struts2.dispatcher.ng.InitOperations;
25  import org.apache.struts2.dispatcher.ng.PrepareOperations;
26  import org.apache.struts2.dispatcher.ng.ExecuteOperations;
27  import org.apache.struts2.dispatcher.mapper.ActionMapping;
28  
29  import javax.servlet.http.HttpServlet;
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  import javax.servlet.*;
33  import java.io.IOException;
34  
35  /***
36   * Servlet dispatcher for Struts.  The preferred way to use Struts is as a filter via the
37   * {@link org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter} and its variants.  This servlet dispatcher
38   * is only for those that really know what they are doing as it may not support every feature of Struts, particularly
39   * static resource serving.
40   */
41  public class StrutsServlet extends HttpServlet {
42  
43      private PrepareOperations prepare;
44      private ExecuteOperations execute;
45  
46      @Override
47      public void init(ServletConfig filterConfig) throws ServletException {
48          InitOperations init = new InitOperations();
49          try {
50              ServletHostConfig config = new ServletHostConfig(filterConfig);
51              init.initLogging(config);
52              Dispatcher dispatcher = init.initDispatcher(config);
53              init.initStaticContentLoader(config, dispatcher);
54  
55              prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
56              execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);
57          } finally {
58              init.cleanup();
59          }
60      }
61  
62      @Override
63      public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
64  
65          try {
66              prepare.createActionContext(request, response);
67              prepare.assignDispatcherToThread();
68              prepare.setEncodingAndLocale(request, response);
69              request = prepare.wrapRequest(request);
70              ActionMapping mapping = prepare.findActionMapping(request, response);
71              if (mapping == null) {
72                  boolean handled = execute.executeStaticResourceRequest(request, response);
73                  if (!handled)
74                      throw new ServletException("Resource loading not supported, use the StrutsPrepareAndExecuteFilter instead.");
75              } else {
76                  execute.executeAction(request, response, mapping);
77              }
78          } finally {
79              prepare.cleanupRequest(request);
80          }
81      }
82  
83      @Override
84      public void destroy() {
85          prepare.cleanupDispatcher();
86      }
87  }