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.action.RequestProcessor; 41 import org.apache.struts.config.FormBeanConfig; 42 import org.apache.struts.config.ForwardConfig; 43 import org.apache.struts.faces.Constants; 44 import org.apache.struts.faces.component.FormComponent; 45 46 47 48 58 59 public class FacesRequestProcessor extends RequestProcessor { 60 61 62 64 65 68 protected static Log log = LogFactory.getLog(FacesRequestProcessor.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 Action processActionCreate(HttpServletRequest request, 166 HttpServletResponse response, 167 ActionMapping mapping) 168 throws IOException { 169 170 if (log.isTraceEnabled()) { 171 log.trace("Performing standard action create"); 172 } 173 Action result = super.processActionCreate(request, response, mapping); 174 if (log.isDebugEnabled()) { 175 log.debug("Standard action create returned " + 176 result.getClass().getName() + " instance"); 177 } 178 return (result); 179 180 } 181 182 183 protected ActionForm processActionForm(HttpServletRequest request, 185 HttpServletResponse response, 186 ActionMapping mapping) { 187 if (log.isTraceEnabled()) { 188 log.trace("Performing standard action form processing"); 189 String attribute = mapping.getAttribute(); 190 if (attribute != null) { 191 String name = mapping.getName(); 192 FormBeanConfig fbc = moduleConfig.findFormBeanConfig(name); 193 if (fbc != null) { 194 if ("request".equals(mapping.getScope())) { 195 log.trace(" Bean in request scope = " + 196 request.getAttribute(attribute)); 197 } else { 198 log.trace(" Bean in session scope = " + 199 request.getSession().getAttribute(attribute)); 200 } 201 } else { 202 log.trace(" No FormBeanConfig for '" + name + "'"); 203 } 204 } else { 205 log.trace(" No form bean for this action"); 206 } 207 } 208 ActionForm result = 209 super.processActionForm(request, response, mapping); 210 if (log.isDebugEnabled()) { 211 log.debug("Standard action form returned " + 212 result); 213 } 214 return (result); 215 216 217 } 218 219 220 protected ActionForward processActionPerform(HttpServletRequest request, 222 HttpServletResponse response, 223 Action action, 224 ActionForm form, 225 ActionMapping mapping) 226 throws IOException , ServletException { 227 228 if (log.isTraceEnabled()) { 229 log.trace("Performing standard action perform"); 230 } 231 ActionForward result = 232 super.processActionPerform(request, response, action, 233 form, mapping); 234 if (log.isDebugEnabled()) { 235 log.debug("Standard action perform returned " + 236 (result == null ? "NULL" : 237 result.getPath()) + " forward path"); 238 } 239 return (result); 240 241 } 242 243 244 protected boolean processForward(HttpServletRequest request, 246 HttpServletResponse response, 247 ActionMapping mapping) 248 throws IOException , ServletException { 249 250 if (log.isTraceEnabled()) { 251 log.trace("Performing standard forward handling"); 252 } 253 boolean result = super.processForward 254 (request, response, mapping); 255 if (log.isDebugEnabled()) { 256 log.debug("Standard forward handling returned " + result); 257 } 258 return (result); 259 260 } 261 262 263 protected void processForwardConfig(HttpServletRequest request, 265 HttpServletResponse response, 266 ForwardConfig forward) 267 throws IOException , ServletException { 268 269 if (log.isTraceEnabled()) { 270 log.trace("Performing standard forward config handling"); 271 } 272 super.processForwardConfig(request, response, forward); 273 if (log.isDebugEnabled()) { 274 log.debug("Standard forward config handling completed"); 275 } 276 277 } 278 279 280 protected boolean processInclude(HttpServletRequest request, 282 HttpServletResponse response, 283 ActionMapping mapping) 284 throws IOException , ServletException { 285 286 if (log.isTraceEnabled()) { 287 log.trace("Performing standard include handling"); 288 } 289 boolean result = super.processInclude 290 (request, response, mapping); 291 if (log.isDebugEnabled()) { 292 log.debug("Standard include handling returned " + result); 293 } 294 return (result); 295 296 } 297 298 299 311 protected String processPath(HttpServletRequest request, 312 HttpServletResponse response) 313 throws IOException { 314 315 ActionEvent event = (ActionEvent) 317 request.getAttribute(Constants.ACTION_EVENT_KEY); 318 319 if (event == null) { 321 if (log.isTraceEnabled()) { 322 log.trace("Performing standard processPath() processing"); 323 } 324 return (super.processPath(request, response)); 325 } 326 327 UIComponent component = event.getComponent(); 329 if (log.isTraceEnabled()) { 330 log.trace("Locating form parent for command component " + 331 event.getComponent()); 332 } 333 while (!(component instanceof FormComponent)) { 334 component = component.getParent(); 335 if (component == null) { 336 log.warn("Command component was not nested in a Struts form!"); 337 return (null); 338 } 339 } 340 if (log.isDebugEnabled()) { 341 log.debug("Returning selected path of '" + 342 ((FormComponent) component).getAction() + "'"); 343 } 344 return (((FormComponent) component).getAction()); 345 346 } 347 348 349 364 protected void processPopulate(HttpServletRequest request, 365 HttpServletResponse response, 366 ActionForm form, 367 ActionMapping mapping) 368 throws ServletException { 369 370 ActionEvent event = (ActionEvent) 372 request.getAttribute(Constants.ACTION_EVENT_KEY); 373 374 if (event == null) { 376 if (log.isTraceEnabled()) { 377 log.trace("Performing standard processPopulate() processing"); 378 } 379 super.processPopulate(request, response, form, mapping); 380 return; 381 } 382 383 if (log.isTraceEnabled()) { 386 log.trace("Faces request, so no processPopulate() processing"); 387 } 388 UIComponent source = event.getComponent(); 389 if (source instanceof UICommand) { 390 if ("cancel".equals(((UICommand) source).getId())) { 391 if (log.isTraceEnabled()) { 392 log.trace("Faces request with cancel button pressed"); 393 } 394 request.setAttribute(Globals.CANCEL_KEY, Boolean.TRUE); 395 } 396 } 397 398 } 399 400 401 protected boolean processValidate(HttpServletRequest request, 403 HttpServletResponse response, 404 ActionForm form, 405 ActionMapping mapping) 406 throws IOException , ServletException { 407 408 if (log.isTraceEnabled()) { 409 log.trace("Performing standard validation"); 410 } 411 boolean result = super.processValidate 412 (request, response, form, mapping); 413 if (log.isDebugEnabled()) { 414 log.debug("Standard validation processing returned " + result); 415 } 416 return (result); 417 418 } 419 420 421 423 424 430 private boolean isStrutsRequest(String uri) { 431 432 int question = uri.indexOf("?"); 433 if (question >= 0) { 434 uri = uri.substring(0, question); 435 } 436 String mapping = (String ) 437 servlet.getServletContext().getAttribute(Globals.SERVLET_KEY); 438 if (mapping == null) { 439 return (false); 440 } else if (mapping.startsWith("*.")) { 441 return (uri.endsWith(mapping.substring(1))); 442 } else if (mapping.endsWith("/*")) { 443 return (uri.startsWith(mapping.substring(0, mapping.length() - 2))); 444 } else { 445 return (false); 446 } 447 448 } 449 450 451 } 452 | Popular Tags |