1 /* 2 * $Id$ 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.struts.extras.actions; 22 23 import jakarta.servlet.ServletException; 24 import jakarta.servlet.http.HttpServletRequest; 25 import jakarta.servlet.http.HttpServletResponse; 26 27 import org.apache.struts.Globals; 28 import org.apache.struts.action.ActionForm; 29 import org.apache.struts.action.ActionForward; 30 import org.apache.struts.action.ActionMapping; 31 import org.apache.struts.util.ModuleUtils; 32 import org.slf4j.Logger; 33 import org.slf4j.LoggerFactory; 34 35 /** 36 * <p>A standard <strong>Action</strong> that switches to a new module and 37 * then forwards control to a URI (specified in a number of possible ways) 38 * within the new module.</p> 39 * 40 * <p>Valid request parameters for this Action are:</p> 41 * 42 * <ul> 43 * 44 * <li><strong>page</strong> - Module-relative URI (beginning with "/") to 45 * which control should be forwarded after switching.</li> 46 * 47 * <li><strong>prefix</strong> - The module prefix (beginning with "/") of the 48 * module to which control should be switched. Use a zero-length string for 49 * the default module. The appropriate <code>ModuleConfig</code> object will 50 * be stored as a request attribute, so any subsequent logic will assume the 51 * new module.</li> 52 * 53 * </ul> 54 * 55 * @version $Rev$ $Date: 2005-05-14 21:27:02 -0400 (Sat, 14 May 2005) 56 * $ 57 * @since Struts 1.1 58 */ 59 public class SwitchAction extends BaseAction { 60 private static final long serialVersionUID = -8632296010034881915L; 61 62 // ----------------------------------------------------- Instance Variables 63 64 /** 65 * The {@code Log} instance for this class. 66 */ 67 private transient final Logger log = 68 LoggerFactory.getLogger(SwitchAction.class); 69 70 /** 71 * Process the specified HTTP request, and create the corresponding HTTP 72 * response (or forward to another web component that will create it). 73 * Return an <code>ActionForward</code> instance describing where and how 74 * control should be forwarded, or <code>null</code> if the response has 75 * already been completed. 76 * 77 * @param mapping The ActionMapping used to select this instance 78 * @param form The optional ActionForm bean for this request (if any) 79 * @param request The HTTP request we are processing 80 * @param response The HTTP response we are creating 81 * @return Return an <code>ActionForward</code> instance describing where 82 * and how control should be forwarded, or <code>null</code> if 83 * the response has already been completed. 84 * @throws Exception if an exception occurs 85 */ 86 public ActionForward execute(ActionMapping mapping, ActionForm form, 87 HttpServletRequest request, HttpServletResponse response) 88 throws Exception { 89 // Identify the request parameters controlling our actions 90 String page = request.getParameter("page"); 91 String prefix = request.getParameter("prefix"); 92 93 if ((page == null) || (prefix == null)) { 94 String message = messages.getMessage("switch.required"); 95 96 log.error(message); 97 throw new ServletException(message); 98 } 99 100 // Switch to the requested module 101 ModuleUtils.getInstance().selectModule(prefix, request, 102 getServlet().getServletContext()); 103 104 if (request.getAttribute(Globals.MODULE_KEY) == null) { 105 String message = messages.getMessage("switch.prefix", prefix); 106 107 log.error(message); 108 throw new ServletException(message); 109 } 110 111 // Forward control to the specified module-relative URI 112 return (new ActionForward(page)); 113 } 114 }