1 20 package org.apache.struts.chain.servlet; 21 22 import java.io.IOException ; 23 24 import javax.servlet.RequestDispatcher ; 25 import javax.servlet.ServletException ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 29 import org.apache.commons.chain.Command; 30 import org.apache.commons.chain.Context; 31 import org.apache.commons.chain.web.servlet.ServletWebContext; 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 35 import org.apache.struts.chain.Constants; 36 import org.apache.struts.config.ForwardConfig; 37 import org.apache.struts.tiles.ComponentContext; 38 import org.apache.struts.tiles.ComponentDefinition; 39 import org.apache.struts.tiles.Controller; 40 import org.apache.struts.tiles.FactoryNotFoundException; 41 import org.apache.struts.tiles.DefinitionsUtil; 42 import org.apache.struts.tiles.TilesUtil; 43 44 import org.apache.struts.upload.MultipartRequestWrapper; 45 46 47 68 public class TilesPreProcessor implements Command 69 { 70 71 72 74 75 private static final Log log = LogFactory.getLog(TilesPreProcessor.class); 76 77 private String forwardConfigKey = Constants.FORWARD_CONFIG_KEY; 78 79 private String includeKey = Constants.INCLUDE_KEY; 80 81 private String moduleConfigKey = Constants.MODULE_CONFIG_KEY; 82 83 84 86 87 92 public String getForwardConfigKey() { 93 94 return (this.forwardConfigKey); 95 96 } 97 98 99 106 public void setForwardConfigKey(String forwardConfigKey) { 107 108 this.forwardConfigKey = forwardConfigKey; 109 110 } 111 112 113 118 public String getIncludeKey() { 119 120 return (this.includeKey); 121 122 } 123 124 125 132 public void setIncludeKey(String includeKey) { 133 134 this.includeKey = includeKey; 135 136 } 137 138 139 141 142 161 public boolean execute(Context context) throws Exception { 162 163 ForwardConfig forwardConfig = (ForwardConfig) 165 context.get(getForwardConfigKey()); 166 if (forwardConfig == null || forwardConfig.getPath() == null) 167 { 168 log.debug("No forwardConfig or no path, so pass to next command."); 169 return (false); 170 } 171 172 ServletWebContext swcontext = (ServletWebContext) context; 173 174 ComponentDefinition definition = null; 175 try 176 { 177 definition = TilesUtil.getDefinition(forwardConfig.getPath(), 178 swcontext.getRequest(), 179 swcontext.getContext()); 180 } 181 catch (FactoryNotFoundException ex) 182 { 183 log.debug("Tiles DefinitionFactory not found, so pass to next command."); 185 return false; 186 } 187 188 boolean doInclude = false; 190 ComponentContext tileContext = null; 191 192 tileContext = ComponentContext.getContext(swcontext.getRequest()); 195 doInclude = (tileContext != null); 196 197 Controller controller = null; 199 200 String uri = null; 202 203 if (definition != null) 204 { 205 uri = definition.getPath(); 209 controller = definition.getOrCreateController(); 210 211 if (tileContext == null) { 212 tileContext = 213 new ComponentContext(definition.getAttributes()); 214 ComponentContext.setContext(tileContext, swcontext.getRequest()); 215 216 } else { 217 tileContext.addMissing(definition.getAttributes()); 218 } 219 } 220 221 definition = DefinitionsUtil.getActionDefinition(swcontext.getRequest()); 227 if (definition != null) { if (definition.getPath() != null) { 231 log.debug("Override forward uri " 232 + uri 233 + " with action uri " 234 + definition.getPath()); 235 uri = definition.getPath(); 236 } 237 238 if (definition.getOrCreateController() != null) { 239 log.debug("Override forward controller with action controller"); 240 controller = definition.getOrCreateController(); 241 } 242 243 if (tileContext == null) { 244 tileContext = 245 new ComponentContext(definition.getAttributes()); 246 ComponentContext.setContext(tileContext, swcontext.getRequest()); 247 } else { 248 tileContext.addMissing(definition.getAttributes()); 249 } 250 } 251 252 253 if (uri == null) { 254 log.debug("no uri computed, so pass to next command"); 255 return false; 256 } 257 258 if (controller != null) { 260 log.trace("Execute controller: " + controller); 261 controller.execute( 262 tileContext, 263 swcontext.getRequest(), 264 swcontext.getResponse(), 265 swcontext.getContext()); 266 } 267 268 271 if (doInclude) { 272 log.info("Tiles process complete; doInclude with " + uri); 273 doInclude(swcontext, uri); 274 return (true); 275 } else { 276 log.info("Tiles process complete; forward to " + uri); 280 context.put(getForwardConfigKey(), new ForwardConfig("tiles-chain", uri, false, true)); 283 return (false); 284 } 285 } 286 287 288 290 296 protected void doInclude( 297 ServletWebContext swcontext, 298 String uri) 299 throws IOException , ServletException { 300 301 HttpServletRequest request = swcontext.getRequest(); 302 303 if (request instanceof MultipartRequestWrapper) { 305 request = ((MultipartRequestWrapper) request).getRequest(); 306 } 307 308 HttpServletResponse response = swcontext.getResponse(); 309 RequestDispatcher rd = swcontext.getContext().getRequestDispatcher(uri); 310 if (rd == null) { 311 response.sendError( 312 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 313 "Error getting RequestDispatcher for " + uri); 314 return; 315 } 316 rd.include(request, response); 317 } 318 319 320 } | Popular Tags |