1 18 19 package org.apache.struts.tiles; 20 21 import java.io.IOException ; 22 23 import javax.servlet.ServletException ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.apache.struts.action.ActionServlet; 30 import org.apache.struts.action.RequestProcessor; 31 import org.apache.struts.config.ForwardConfig; 32 import org.apache.struts.config.ModuleConfig; 33 34 52 public class TilesRequestProcessor extends RequestProcessor { 53 54 57 protected DefinitionsFactory definitionsFactory = null; 58 59 62 protected static Log log = LogFactory.getLog(TilesRequestProcessor.class); 63 64 71 public void init(ActionServlet servlet, ModuleConfig moduleConfig) 72 throws ServletException { 73 74 super.init(servlet, moduleConfig); 75 this.initDefinitionsMapping(); 76 } 77 78 82 protected void initDefinitionsMapping() throws ServletException { 83 definitionsFactory = 85 ( 86 (TilesUtilStrutsImpl) TilesUtil 87 .getTilesUtil()) 88 .getDefinitionsFactory( 89 getServletContext(), 90 moduleConfig); 91 92 if (definitionsFactory == null) { 94 log.info( 95 "Definition Factory not found for module '" 96 + moduleConfig.getPrefix() 97 + "'. " 98 + "Have you declared the appropriate plugin in struts-config.xml ?"); 99 100 return; 101 } 102 103 log.info( 104 "Tiles definition factory found for request processor '" 105 + moduleConfig.getPrefix() 106 + "'."); 107 108 } 109 110 122 protected boolean processTilesDefinition( 123 String definitionName, 124 boolean contextRelative, 125 HttpServletRequest request, 126 HttpServletResponse response) 127 throws IOException , ServletException { 128 129 boolean doInclude = false; 131 132 Controller controller = null; 134 135 String uri = null; 137 138 ComponentContext tileContext = null; 139 140 try { 141 tileContext = ComponentContext.getContext(request); 144 doInclude = (tileContext != null); 145 ComponentDefinition definition = null; 146 147 if (definitionsFactory != null) { 150 try { 152 definition = 153 definitionsFactory.getDefinition( 154 definitionName, 155 request, 156 getServletContext()); 157 } catch (NoSuchDefinitionException ex) { 158 log.debug("NoSuchDefinitionException " + ex.getMessage()); 160 } 161 if (definition != null) { uri = definition.getPath(); 165 controller = definition.getOrCreateController(); 166 167 if (tileContext == null) { 168 tileContext = 169 new ComponentContext(definition.getAttributes()); 170 ComponentContext.setContext(tileContext, request); 171 172 } else { 173 tileContext.addMissing(definition.getAttributes()); 174 } 175 } 176 } 177 178 definition = DefinitionsUtil.getActionDefinition(request); 180 if (definition != null) { if (definition.getPath() != null) { 184 uri = definition.getPath(); 185 } 186 187 if (definition.getOrCreateController() != null) { 188 controller = definition.getOrCreateController(); 189 } 190 191 if (tileContext == null) { 192 tileContext = 193 new ComponentContext(definition.getAttributes()); 194 ComponentContext.setContext(tileContext, request); 195 } else { 196 tileContext.addMissing(definition.getAttributes()); 197 } 198 } 199 200 } catch (java.lang.InstantiationException ex) { 201 202 log.error("Can't create associated controller", ex); 203 204 throw new ServletException ( 205 "Can't create associated controller", 206 ex); 207 } catch (DefinitionsFactoryException ex) { 208 throw new ServletException (ex); 209 } 210 211 if (uri == null) { 213 return false; 214 } 215 216 if (controller != null) { 218 try { 219 controller.execute( 220 tileContext, 221 request, 222 response, 223 getServletContext()); 224 225 } catch (Exception e) { 226 throw new ServletException (e); 227 } 228 } 229 230 if (log.isDebugEnabled()) { 233 log.debug("uri=" + uri + " doInclude=" + doInclude); 234 } 235 236 if (doInclude) { 237 doInclude(uri, request, response); 238 } else { 239 doForward(uri, request, response); } 241 242 return true; 243 } 244 245 253 protected void doForward( 254 String uri, 255 HttpServletRequest request, 256 HttpServletResponse response) 257 throws IOException , ServletException { 258 259 if (response.isCommitted()) { 260 this.doInclude(uri, request, response); 261 262 } else { 263 super.doForward(uri, request, response); 264 } 265 } 266 267 281 protected void processForwardConfig( 282 HttpServletRequest request, 283 HttpServletResponse response, 284 ForwardConfig forward) 285 throws IOException , ServletException { 286 287 if (forward == null) { 289 return; 290 } 291 292 if (log.isDebugEnabled()) { 293 log.debug( 294 "processForwardConfig(" 295 + forward.getPath() 296 + ", " 297 + forward.getContextRelative() 298 + ")"); 299 } 300 301 if (processTilesDefinition(forward.getPath(), 303 forward.getContextRelative(), 304 request, 305 response)) { 306 if (log.isDebugEnabled()) { 307 log.debug( 308 " '" + forward.getPath() + "' - processed as definition"); 309 } 310 return; 311 } 312 313 if (log.isDebugEnabled()) { 314 log.debug(" '" + forward.getPath() + "' - processed as uri"); 315 } 316 317 super.processForwardConfig(request, response, forward); 319 } 320 321 335 protected void internalModuleRelativeForward( 336 String uri, 337 HttpServletRequest request, 338 HttpServletResponse response) 339 throws IOException , ServletException { 340 341 if (processTilesDefinition(uri, false, request, response)) { 342 return; 343 } 344 345 super.internalModuleRelativeForward(uri, request, response); 346 } 347 348 359 protected void internalModuleRelativeInclude( 360 String uri, 361 HttpServletRequest request, 362 HttpServletResponse response) 363 throws IOException , ServletException { 364 365 if (processTilesDefinition(uri, false, request, response)) { 366 return; 367 } 368 369 super.internalModuleRelativeInclude(uri, request, response); 370 } 371 372 375 public DefinitionsFactory getDefinitionsFactory() { 376 return definitionsFactory; 377 } 378 379 } 380 | Popular Tags |