001: /*
002: * $Id: SwitchAction.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.actions;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.apache.struts.Globals;
026: import org.apache.struts.action.ActionForm;
027: import org.apache.struts.action.ActionForward;
028: import org.apache.struts.action.ActionMapping;
029: import org.apache.struts.util.ModuleUtils;
030:
031: import javax.servlet.ServletException;
032: import javax.servlet.http.HttpServletRequest;
033: import javax.servlet.http.HttpServletResponse;
034:
035: /**
036: * <p>A standard <strong>Action</strong> that switches to a new module and
037: * then forwards control to a URI (specified in a number of possible ways)
038: * within the new module.</p>
039: *
040: * <p>Valid request parameters for this Action are:</p>
041: *
042: * <ul>
043: *
044: * <li><strong>page</strong> - Module-relative URI (beginning with "/") to
045: * which control should be forwarded after switching.</li>
046: *
047: * <li><strong>prefix</strong> - The module prefix (beginning with "/") of the
048: * module to which control should be switched. Use a zero-length string for
049: * the default module. The appropriate <code>ModuleConfig</code> object will
050: * be stored as a request attribute, so any subsequent logic will assume the
051: * new module.</li>
052: *
053: * </ul>
054: *
055: * @version $Rev: 471754 $ $Date: 2005-05-14 21:27:02 -0400 (Sat, 14 May 2005)
056: * $
057: * @since Struts 1.1
058: */
059: public class SwitchAction extends BaseAction {
060: // ----------------------------------------------------- Instance Variables
061:
062: /**
063: * Commons Logging instance.
064: */
065: protected static Log log = LogFactory.getLog(SwitchAction.class);
066:
067: /**
068: * Process the specified HTTP request, and create the corresponding HTTP
069: * response (or forward to another web component that will create it).
070: * Return an <code>ActionForward</code> instance describing where and how
071: * control should be forwarded, or <code>null</code> if the response has
072: * already been completed.
073: *
074: * @param mapping The ActionMapping used to select this instance
075: * @param form The optional ActionForm bean for this request (if any)
076: * @param request The HTTP request we are processing
077: * @param response The HTTP response we are creating
078: * @return Return an <code>ActionForward</code> instance describing where
079: * and how control should be forwarded, or <code>null</code> if
080: * the response has already been completed.
081: * @throws Exception if an exception occurs
082: */
083: public ActionForward execute(ActionMapping mapping,
084: ActionForm form, HttpServletRequest request,
085: HttpServletResponse response) throws Exception {
086: // Identify the request parameters controlling our actions
087: String page = request.getParameter("page");
088: String prefix = request.getParameter("prefix");
089:
090: if ((page == null) || (prefix == null)) {
091: String message = messages.getMessage("switch.required");
092:
093: log.error(message);
094: throw new ServletException(message);
095: }
096:
097: // Switch to the requested module
098: ModuleUtils.getInstance().selectModule(prefix, request,
099: getServlet().getServletContext());
100:
101: if (request.getAttribute(Globals.MODULE_KEY) == null) {
102: String message = messages.getMessage("switch.prefix",
103: prefix);
104:
105: log.error(message);
106: throw new ServletException(message);
107: }
108:
109: // Forward control to the specified module-relative URI
110: return (new ActionForward(page));
111: }
112: }
|