1 16 17 package org.apache.struts.faces.application; 18 19 20 import java.io.IOException ; 21 import javax.faces.FactoryFinder; 22 import javax.faces.application.ViewHandler; 23 import javax.faces.component.UICommand; 24 import javax.faces.component.UIComponent; 25 import javax.faces.context.FacesContext; 26 import javax.faces.context.FacesContextFactory; 27 import javax.faces.event.ActionEvent; 28 import javax.faces.lifecycle.Lifecycle; 29 import javax.faces.lifecycle.LifecycleFactory; 30 import javax.servlet.ServletException ; 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 import org.apache.struts.Globals; 36 import org.apache.struts.action.Action; 37 import org.apache.struts.action.ActionForm; 38 import org.apache.struts.action.ActionForward; 39 import org.apache.struts.action.ActionMapping; 40 import org.apache.struts.config.FormBeanConfig; 41 import org.apache.struts.config.ForwardConfig; 42 import org.apache.struts.faces.Constants; 43 import org.apache.struts.faces.component.FormComponent; 44 import org.apache.struts.tiles.TilesRequestProcessor; 45 46 47 57 58 public class FacesTilesRequestProcessor extends TilesRequestProcessor { 59 60 61 63 64 67 protected static Log log = 68 LogFactory.getLog(FacesTilesRequestProcessor.class); 69 70 71 72 74 75 89 protected void doForward(String uri, 90 HttpServletRequest request, 91 HttpServletResponse response) 92 throws IOException , ServletException { 93 94 if (log.isDebugEnabled()) { 95 log.debug("doForward(" + uri + ")"); 96 } 97 98 request.removeAttribute(Constants.ACTION_EVENT_KEY); 100 101 if (isStrutsRequest(uri)) { 103 if (response.isCommitted()) { 104 if (log.isTraceEnabled()) { 105 log.trace(" super.doInclude(" + uri + ")"); 106 } 107 super.doInclude(uri, request, response); 108 } else { 109 if (log.isTraceEnabled()) { 110 log.trace(" super.doForward(" + uri + ")"); 111 } 112 super.doForward(uri, request, response); 113 } 114 return; 115 } 116 117 LifecycleFactory lf = (LifecycleFactory) 119 FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY); 120 Lifecycle lifecycle = lf.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE); 122 boolean created = false; 123 FacesContext context = FacesContext.getCurrentInstance(); 124 if (context == null) { 125 if (log.isTraceEnabled()) { 126 log.trace(" Creating new FacesContext for '" + uri + "'"); 127 } 128 created = true; 129 FacesContextFactory fcf = (FacesContextFactory) 130 FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY); 131 context = fcf.getFacesContext(servlet.getServletContext(), 132 request, response, lifecycle); 133 } 134 135 ViewHandler vh = context.getApplication().getViewHandler(); 137 if (log.isTraceEnabled()) { 138 log.trace(" Creating new view for '" + uri + "'"); 139 } 140 context.setViewRoot(vh.createView(context, uri)); 141 142 if (log.isTraceEnabled()) { 144 log.trace(" Rendering view for '" + uri + "'"); 145 } 146 try { 147 lifecycle.render(context); 148 } finally { 149 if (created) { 150 if (log.isTraceEnabled()) { 151 log.trace(" Releasing context for '" + uri + "'"); 152 } 153 context.release(); 154 } else { 155 if (log.isTraceEnabled()) { 156 log.trace(" Rendering completed"); 157 } 158 } 159 } 160 161 } 162 163 164 protected void internalModuleRelativeForward 166 (String uri, HttpServletRequest request, HttpServletResponse response) 167 throws IOException , ServletException { 168 169 if (log.isTraceEnabled()) { 170 log.trace("Performing internal module relative forward to '" + 171 uri + "'"); 172 } 173 super.internalModuleRelativeForward(uri, request, response); 174 175 } 176 177 178 protected Action processActionCreate(HttpServletRequest request, 180 HttpServletResponse response, 181 ActionMapping mapping) 182 throws IOException { 183 184 if (log.isTraceEnabled()) { 185 log.trace("Performing standard action create"); 186 } 187 Action result = super.processActionCreate(request, response, mapping); 188 if (log.isDebugEnabled()) { 189 log.debug("Standard action create returned " + 190 result.getClass().getName() + " instance"); 191 } 192 return (result); 193 194 } 195 196 197 protected ActionForm processActionForm(HttpServletRequest request, 199 HttpServletResponse response, 200 ActionMapping mapping) { 201 if (log.isTraceEnabled()) { 202 log.trace("Performing standard action form processing"); 203 String attribute = mapping.getAttribute(); 204 if (attribute != null) { 205 String name = mapping.getName(); 206 FormBeanConfig fbc = moduleConfig.findFormBeanConfig(name); 207 if (fbc != null) { 208 if ("request".equals(mapping.getScope())) { 209 log.trace(" Bean in request scope = " + 210 request.getAttribute(attribute)); 211 } else { 212 log.trace(" Bean in session scope = " + 213 request.getSession().getAttribute(attribute)); 214 } 215 } else { 216 log.trace(" No FormBeanConfig for '" + name + "'"); 217 } 218 } else { 219 log.trace(" No form bean for this action"); 220 } 221 } 222 ActionForm result = 223 super.processActionForm(request, response, mapping); 224 if (log.isDebugEnabled()) { 225 log.debug("Standard action form returned " + 226 result); 227 } 228 return (result); 229 230 231 } 232 233 234 protected ActionForward processActionPerform(HttpServletRequest request, 236 HttpServletResponse response, 237 Action action, 238 ActionForm form, 239 ActionMapping mapping) 240 throws IOException , ServletException { 241 242 if (log.isTraceEnabled()) { 243 log.trace("Performing standard action perform"); 244 } 245 ActionForward result = 246 super.processActionPerform(request, response, action, 247 form, mapping); 248 if (log.isDebugEnabled()) { 249 log.debug("Standard action perform returned " + 250 (result == null ? "NULL" : 251 result.getPath()) + " forward path"); 252 } 253 return (result); 254 255 } 256 257 258 protected boolean processForward(HttpServletRequest request, 260 HttpServletResponse response, 261 ActionMapping mapping) 262 throws IOException , ServletException { 263 264 if (log.isTraceEnabled()) { 265 log.trace("Performing standard forward handling"); 266 } 267 boolean result = super.processForward 268 (request, response, mapping); 269 if (log.isDebugEnabled()) { 270 log.debug("Standard forward handling returned " + result); 271 } 272 return (result); 273 274 } 275 276 277 protected void processForwardConfig(HttpServletRequest request, 279 HttpServletResponse response, 280 ForwardConfig forward) 281 throws IOException , ServletException { 282 283 if (log.isTraceEnabled()) { 284 log.trace("Performing standard forward config handling"); 285 } 286 super.processForwardConfig(request, response, forward); 287 if (log.isDebugEnabled()) { 288 log.debug("Standard forward config handling completed"); 289 } 290 291 } 292 293 294 protected boolean processInclude(HttpServletRequest request, 296 HttpServletResponse response, 297 ActionMapping mapping) 298 throws IOException , ServletException { 299 300 if (log.isTraceEnabled()) { 301 log.trace("Performing standard include handling"); 302 } 303 boolean result = super.processInclude 304 (request, response, mapping); 305 if (log.isDebugEnabled()) { 306 log.debug("Standard include handling returned " + result); 307 } 308 return (result); 309 310 } 311 312 313 325 protected String processPath(HttpServletRequest request, 326 HttpServletResponse response) 327 throws IOException { 328 329 ActionEvent event = (ActionEvent) 331 request.getAttribute(Constants.ACTION_EVENT_KEY); 332 333 if (event == null) { 335 if (log.isTraceEnabled()) { 336 log.trace("Performing standard processPath() processing"); 337 } 338 return (super.processPath(request, response)); 339 } 340 341 UIComponent component = event.getComponent(); 343 if (log.isTraceEnabled()) { 344 log.trace("Locating form parent for command component " + 345 event.getComponent()); 346 } 347 while (!(component instanceof FormComponent)) { 348 component = component.getParent(); 349 if (component == null) { 350 log.warn("Command component was not nested in a Struts form!"); 351 return (null); 352 } 353 } 354 if (log.isTraceEnabled()) { 355 log.trace("Returning selected path of " + 356 ((FormComponent) component).getAction()); 357 } 358 return (((FormComponent) component).getAction()); 359 360 } 361 362 363 378 protected void processPopulate(HttpServletRequest request, 379 HttpServletResponse response, 380 ActionForm form, 381 ActionMapping mapping) 382 throws ServletException { 383 384 ActionEvent event = (ActionEvent) 386 request.getAttribute(Constants.ACTION_EVENT_KEY); 387 388 if (event == null) { 390 if (log.isTraceEnabled()) { 391 log.trace("Performing standard processPopulate() processing"); 392 } 393 super.processPopulate(request, response, form, mapping); 394 return; 395 } 396 397 if (log.isTraceEnabled()) { 400 log.trace("Faces request, so no processPopulate() processing"); 401 } 402 UIComponent source = event.getComponent(); 403 if (source instanceof UICommand) { 404 if ("cancel".equals(((UICommand) source).getId())) { 405 if (log.isTraceEnabled()) { 406 log.trace("Faces request with cancel button pressed"); 407 } 408 request.setAttribute(Globals.CANCEL_KEY, Boolean.TRUE); 409 } 410 } 411 412 } 413 414 415 protected boolean processValidate(HttpServletRequest request, 417 HttpServletResponse response, 418 ActionForm form, 419 ActionMapping mapping) 420 throws IOException , ServletException { 421 422 if (log.isTraceEnabled()) { 423 log.trace("Performing standard validation"); 424 } 425 boolean result = super.processValidate 426 (request, response, form, mapping); 427 if (log.isDebugEnabled()) { 428 log.debug("Standard validation processing returned " + result); 429 } 430 return (result); 431 432 } 433 434 435 437 438 444 private boolean isStrutsRequest(String uri) { 445 446 int question = uri.indexOf("?"); 447 if (question >= 0) { 448 uri = uri.substring(0, question); 449 } 450 String mapping = (String ) 451 servlet.getServletContext().getAttribute(Globals.SERVLET_KEY); 452 if (mapping == null) { 453 return (false); 454 } else if (mapping.startsWith("*.")) { 455 return (uri.endsWith(mapping.substring(1))); 456 } else if (mapping.endsWith("/*")) { 457 return (uri.startsWith(mapping.substring(0, mapping.length() - 2))); 458 } else { 459 return (false); 460 } 461 462 } 463 464 465 } 466 | Popular Tags |