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 22 package org.apache.struts.tiles; 23 24 import java.io.IOException; 25 26 import jakarta.servlet.ServletContext; 27 import jakarta.servlet.ServletException; 28 import jakarta.servlet.ServletRequest; 29 import jakarta.servlet.http.HttpServletRequest; 30 import jakarta.servlet.http.HttpServletResponse; 31 32 import org.apache.struts.Globals; 33 import org.apache.struts.config.ModuleConfig; 34 import org.apache.struts.util.ModuleUtils; 35 36 /** 37 * Implementation of TilesUtil for Struts multi modules. 38 * Methods in this implementation are aware of the Struts module context. 39 * <br> 40 * <ul> 41 * <li>The method getFactory(...) returns the factory for the current Struts 42 * module.</li> 43 * <li>Methods doForward() and doInclude() use their counterparts in the 44 * current RequestProcessor (todo).</li> 45 * <li>The method createFactory(...) creates a factory for the current module and 46 * stores it under the appropriate property name.</li> 47 * </ul> 48 */ 49 public class TilesUtilStrutsModulesImpl extends TilesUtilStrutsImpl { 50 private static final long serialVersionUID = 2840296621378046205L; 51 52 /** 53 * Do a forward using request dispatcher. 54 * 55 * This method is used by the Tiles package anytime a forward is required. 56 * @param uri Uri or Definition name to forward. 57 * @param request Current page request. 58 * @param response Current page response. 59 * @param servletContext Current servlet context. 60 */ 61 public void doForward( 62 String uri, 63 HttpServletRequest request, 64 HttpServletResponse response, 65 ServletContext servletContext) 66 throws IOException, ServletException { 67 68 request.getRequestDispatcher(uri).forward(request, response); 69 } 70 71 /** 72 * Do an include using request dispatcher. 73 * 74 * This method is used by the Tiles package anytime an include is required. 75 * @param uri Uri or Definition name to forward. 76 * @param request Current page request. 77 * @param response Current page response. 78 * @param servletContext Current servlet context. 79 */ 80 public void doInclude( 81 String uri, 82 HttpServletRequest request, 83 HttpServletResponse response, 84 ServletContext servletContext) 85 throws IOException, ServletException { 86 87 request.getRequestDispatcher(uri).include(request, response); 88 } 89 90 /** 91 * Get the definition factory from appropriate servlet context. 92 * @param request Current request. 93 * @param servletContext Current servlet context. 94 * @return Definitions factory or null if not found. 95 */ 96 public DefinitionsFactory getDefinitionsFactory( 97 ServletRequest request, 98 ServletContext servletContext) { 99 100 return getDefinitionsFactory( 101 servletContext, 102 getModuleConfig((HttpServletRequest) request, servletContext)); 103 } 104 105 /** 106 * Get definition factory for the module attached to specified moduleConfig. 107 * @param servletContext Current servlet context. 108 * @param moduleConfig Module config of the module for which the factory is requested. 109 * @return Definitions factory or null if not found. 110 */ 111 public DefinitionsFactory getDefinitionsFactory( 112 ServletContext servletContext, 113 ModuleConfig moduleConfig) { 114 115 return (DefinitionsFactory) servletContext.getAttribute( 116 DEFINITIONS_FACTORY + moduleConfig.getPrefix()); 117 } 118 119 /** 120 * Make definition factory accessible to tags. 121 * Factory is stored in servlet context. 122 * @param factory Factory to be made accessible. 123 * @param servletContext Current servlet context. 124 */ 125 protected void makeDefinitionsFactoryAccessible( 126 DefinitionsFactory factory, 127 ServletContext servletContext) { 128 129 String prefix = factory.getConfig().getFactoryName(); 130 servletContext.setAttribute(DEFINITIONS_FACTORY + prefix, factory); 131 } 132 133 /** 134 * Get Tiles RequestProcessor associated to the current module. 135 * @param request Current request. 136 * @param servletContext Current servlet context. 137 * @return The {@link TilesRequestProcessor} for the current request. 138 */ 139 protected TilesRequestProcessor getRequestProcessor( 140 HttpServletRequest request, 141 ServletContext servletContext) { 142 143 ModuleConfig moduleConfig = getModuleConfig(request, servletContext); 144 145 return (TilesRequestProcessor) servletContext.getAttribute( 146 Globals.REQUEST_PROCESSOR_KEY + moduleConfig.getPrefix()); 147 } 148 149 /** 150 * Get the current ModuleConfig. 151 * <br> 152 * Lookup in the request and do selectModule if not found. The side effect 153 * is, that the ModuleConfig object is set in the request if it was not present. 154 * @param request Current request. 155 * @param servletContext Current servlet context*. 156 * @return The ModuleConfig for current request. 157 */ 158 protected ModuleConfig getModuleConfig( 159 HttpServletRequest request, 160 ServletContext servletContext) { 161 162 ModuleConfig moduleConfig = 163 ModuleUtils.getInstance().getModuleConfig(request); 164 165 if (moduleConfig == null) { 166 // ModuleConfig not found in current request. Select it. 167 ModuleUtils.getInstance().selectModule(request, servletContext); 168 moduleConfig = ModuleUtils.getInstance().getModuleConfig(request); 169 } 170 171 return moduleConfig; 172 } 173 174 }