View Javadoc

1   /*
2    * $Id: SwitchAction.java 376812 2006-02-10 19:42:38Z husted $
3    *
4    * Copyright 2002-2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts.actions;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.struts.Globals;
23  import org.apache.struts.action.ActionForm;
24  import org.apache.struts.action.ActionForward;
25  import org.apache.struts.action.ActionMapping;
26  import org.apache.struts.util.ModuleUtils;
27  
28  import javax.servlet.ServletException;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  
32  /***
33   * <p>A standard <strong>Action</strong> that switches to a new module and
34   * then forwards control to a URI (specified in a number of possible ways)
35   * within the new module.</p>
36   *
37   * <p>Valid request parameters for this Action are:</p>
38   *
39   * <ul>
40   *
41   * <li><strong>page</strong> - Module-relative URI (beginning with "/") to
42   * which control should be forwarded after switching.</li>
43   *
44   * <li><strong>prefix</strong> - The module prefix (beginning with "/") of the
45   * module to which control should be switched.  Use a zero-length string for
46   * the default module.  The appropriate <code>ModuleConfig</code> object will
47   * be stored as a request attribute, so any subsequent logic will assume the
48   * new module.</li>
49   *
50   * </ul>
51   *
52   * @version $Rev: 376812 $ $Date: 2005-05-14 21:27:02 -0400 (Sat, 14 May 2005)
53   *          $
54   * @since Struts 1.1
55   */
56  public class SwitchAction extends BaseAction {
57      // ----------------------------------------------------- Instance Variables
58  
59      /***
60       * Commons Logging instance.
61       */
62      protected static Log log = LogFactory.getLog(SwitchAction.class);
63  
64      /***
65       * Process the specified HTTP request, and create the corresponding HTTP
66       * response (or forward to another web component that will create it).
67       * Return an <code>ActionForward</code> instance describing where and how
68       * control should be forwarded, or <code>null</code> if the response has
69       * already been completed.
70       *
71       * @param mapping  The ActionMapping used to select this instance
72       * @param form     The optional ActionForm bean for this request (if any)
73       * @param request  The HTTP request we are processing
74       * @param response The HTTP response we are creating
75       * @return Return an <code>ActionForward</code> instance describing where
76       *         and how control should be forwarded, or <code>null</code> if
77       *         the response has already been completed.
78       * @throws Exception if an exception occurs
79       */
80      public ActionForward execute(ActionMapping mapping, ActionForm form,
81          HttpServletRequest request, HttpServletResponse response)
82          throws Exception {
83          // Identify the request parameters controlling our actions
84          String page = request.getParameter("page");
85          String prefix = request.getParameter("prefix");
86  
87          if ((page == null) || (prefix == null)) {
88              String message = messages.getMessage("switch.required");
89  
90              log.error(message);
91              throw new ServletException(message);
92          }
93  
94          // Switch to the requested module
95          ModuleUtils.getInstance().selectModule(prefix, request,
96              getServlet().getServletContext());
97  
98          if (request.getAttribute(Globals.MODULE_KEY) == null) {
99              String message = messages.getMessage("switch.prefix", prefix);
100 
101             log.error(message);
102             throw new ServletException(message);
103         }
104 
105         // Forward control to the specified module-relative URI
106         return (new ActionForward(page));
107     }
108 }