View Javadoc

1   /*
2    * Copyright 2003-2006 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.struts.chain.commands.servlet;
17  
18  import java.io.IOException;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.struts.action.ActionServlet;
23  import org.apache.struts.chain.commands.AbstractPerformForward;
24  import org.apache.struts.chain.contexts.ActionContext;
25  import org.apache.struts.chain.contexts.ServletActionContext;
26  import org.apache.struts.config.ForwardConfig;
27  import org.apache.struts.config.ModuleConfig;
28  import org.apache.struts.util.MessageResources;
29  import org.apache.struts.util.RequestUtils;
30  import org.apache.struts.util.ModuleUtils;
31  
32  import javax.servlet.RequestDispatcher;
33  import javax.servlet.ServletContext;
34  import javax.servlet.ServletException;
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletResponse;
37  
38  /***
39   * <p>Perform forwarding or redirection based on the specified
40   * <code>ForwardConfig</code> (if any).</p>
41   *
42   * @version $Rev: 421119 $ $Date: 2006-07-11 21:49:11 -0700 (Tue, 11 Jul 2006) $
43   */
44  public class PerformForward extends AbstractPerformForward {
45      private static final Log LOG = LogFactory.getLog(PerformForward.class);
46  
47      // ------------------------------------------------------- Protected Methods
48  
49      /***
50       * <p>Perform the appropriate processing on the specified
51       * <code>ForwardConfig</code>.</p>
52       *
53       * @param context       The context for this request
54       * @param forwardConfig The forward to be performed
55       */
56      protected void perform(ActionContext context, ForwardConfig forwardConfig)
57          throws Exception {
58          ServletActionContext sacontext = (ServletActionContext) context;
59          String uri = forwardConfig.getPath();
60  
61          if (uri == null) {
62              ActionServlet servlet = sacontext.getActionServlet();
63              MessageResources resources = servlet.getInternal();
64  
65              throw new IllegalArgumentException(resources.getMessage("forwardPathNull"));
66          }
67  
68          HttpServletRequest request = sacontext.getRequest();
69          ServletContext servletContext = sacontext.getContext();
70          HttpServletResponse response = sacontext.getResponse();
71  
72          if (uri.startsWith("/")) {
73              uri = resolveModuleRelativePath(forwardConfig, servletContext, request);
74          }
75  
76  
77          if (response.isCommitted() && !forwardConfig.getRedirect()) {
78              handleAsInclude(uri, servletContext, request, response);
79          } else if (forwardConfig.getRedirect()) {
80              handleAsRedirect(uri, request, response);
81          } else {
82              handleAsForward(uri, servletContext, request, response);
83          }
84      }
85  
86      private String resolveModuleRelativePath(ForwardConfig forwardConfig, ServletContext servletContext, HttpServletRequest request) {
87          String prefix = forwardConfig.getModule();
88          ModuleConfig moduleConfig = ModuleUtils.getInstance().getModuleConfig(prefix, request, servletContext);
89          return RequestUtils.forwardURL(request,forwardConfig, moduleConfig);
90      }
91  
92      private void handleAsForward(String uri, ServletContext servletContext, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
93          RequestDispatcher rd = servletContext.getRequestDispatcher(uri);
94  
95          if (LOG.isDebugEnabled()) {
96              LOG.debug("Forwarding to " + uri);
97          }
98  
99          rd.forward(request, response);
100     }
101 
102     private void handleAsRedirect(String uri, HttpServletRequest request, HttpServletResponse response) throws IOException {
103         if (uri.startsWith("/")) {
104             uri = request.getContextPath() + uri;
105         }
106 
107         if (LOG.isDebugEnabled()) {
108             LOG.debug("Redirecting to " + uri);
109         }
110 
111         response.sendRedirect(response.encodeRedirectURL(uri));
112     }
113 
114     private void handleAsInclude(String uri, ServletContext servletContext, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
115         RequestDispatcher rd = servletContext.getRequestDispatcher(uri);
116 
117         if (rd == null) {
118             response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
119                 "Error getting RequestDispatcher for " + uri);
120             return;
121         }
122 
123         if (LOG.isDebugEnabled()) {
124             LOG.debug("Including " + uri);
125         }
126 
127         rd.include(request, response);
128     }
129 }