public void render(FacesContext context) throws FacesException { if (context.getResponseComplete()) return; Application app = context.getApplication(); ViewHandler view = app.getViewHandler(); beforePhase(context, PhaseId.RENDER_RESPONSE); try { if (log.isLoggable(Level.FINER)) log.finer(context.getViewRoot() + " before render view"); view.renderView(context, context.getViewRoot()); } catch (java.io.IOException e) { if (sendError(context, "renderView", e)) return; throw new FacesException(e); } catch (RuntimeException e) { if (sendError(context, "renderView", e)) return; throw e; } finally { afterPhase(context, PhaseId.RENDER_RESPONSE); logMessages(context); } }
private void restoreView(FacesContext context) throws FacesException { Application app = context.getApplication(); if (app instanceof ApplicationImpl) ((ApplicationImpl) app).initRequest(); ViewHandler view = app.getViewHandler(); view.initView(context); UIViewRoot viewRoot = context.getViewRoot(); if (viewRoot != null) { ExternalContext extContext = context.getExternalContext(); viewRoot.setLocale(extContext.getRequestLocale()); doSetBindings(context.getELContext(), viewRoot); return; } String viewId = calculateViewId(context); String renderKitId = view.calculateRenderKitId(context); RenderKitFactory renderKitFactory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY); RenderKit renderKit = renderKitFactory.getRenderKit(context, renderKitId); ResponseStateManager stateManager = renderKit.getResponseStateManager(); if (stateManager.isPostback(context)) { viewRoot = view.restoreView(context, viewId); if (viewRoot != null) { doSetBindings(context.getELContext(), viewRoot); } else { // XXX: backward compat issues with ViewHandler and StateManager // throw new ViewExpiredException(L.l("{0} is an expired view", viewId)); context.renderResponse(); viewRoot = view.createView(context, viewId); context.setViewRoot(viewRoot); } context.setViewRoot(viewRoot); } else { context.renderResponse(); viewRoot = view.createView(context, viewId); context.setViewRoot(viewRoot); } }
private String calculateViewId(FacesContext context) { Map map = context.getExternalContext().getRequestMap(); String viewId = (String) map.get(RequestDispatcher.INCLUDE_PATH_INFO); if (viewId == null) viewId = context.getExternalContext().getRequestPathInfo(); if (viewId == null) viewId = (String) map.get(RequestDispatcher.INCLUDE_SERVLET_PATH); if (viewId == null) viewId = context.getExternalContext().getRequestServletPath(); return viewId; }
private void logMessages(FacesContext context) { UIViewRoot root = context.getViewRoot(); String viewId = ""; if (root != null) viewId = root.getViewId(); Iterator<FacesMessage> iter = context.getMessages(); while (iter != null && iter.hasNext()) { FacesMessage msg = iter.next(); if (log.isLoggable(Level.FINE)) { if (msg.getDetail() != null) log.fine( viewId + " [ " + msg.getSeverity() + "] " + msg.getSummary() + " " + msg.getDetail()); else log.fine(viewId + " [ " + msg.getSeverity() + "] " + msg.getSummary()); } } }
public void execute(FacesContext context) throws FacesException { boolean isFiner = log.isLoggable(Level.FINER); if (context.getResponseComplete() || context.getRenderResponse()) return; beforePhase(context, PhaseId.RESTORE_VIEW); try { if (isFiner) log.finer("JSF[] before restore view"); restoreView(context); } finally { afterPhase(context, PhaseId.RESTORE_VIEW); } if (context.getResponseComplete() || context.getRenderResponse()) return; UIViewRoot viewRoot = context.getViewRoot(); beforePhase(context, PhaseId.APPLY_REQUEST_VALUES); try { if (isFiner) log.finer(context.getViewRoot() + " before process decodes"); viewRoot.processDecodes(context); } catch (RuntimeException e) { log.log(Level.WARNING, e.toString(), e); } finally { afterPhase(context, PhaseId.APPLY_REQUEST_VALUES); } // // Process Validations (processValidators) // if (context.getResponseComplete() || context.getRenderResponse()) return; beforePhase(context, PhaseId.PROCESS_VALIDATIONS); try { if (isFiner) log.finer(context.getViewRoot() + " before process validators"); viewRoot.processValidators(context); } finally { afterPhase(context, PhaseId.PROCESS_VALIDATIONS); } // // Update Model Values (processUpdates) // if (context.getResponseComplete() || context.getRenderResponse()) return; beforePhase(context, PhaseId.UPDATE_MODEL_VALUES); try { if (isFiner) log.finer(context.getViewRoot() + " before process updates"); viewRoot.processUpdates(context); } catch (RuntimeException e) { if (sendError(context, "processUpdates", e)) return; } finally { afterPhase(context, PhaseId.UPDATE_MODEL_VALUES); } // // Invoke Application (processApplication) // if (context.getResponseComplete() || context.getRenderResponse()) return; beforePhase(context, PhaseId.INVOKE_APPLICATION); try { if (isFiner) log.finer(context.getViewRoot() + " before process application"); viewRoot.processApplication(context); } finally { afterPhase(context, PhaseId.INVOKE_APPLICATION); } }
private boolean sendError(FacesContext context, String lifecycle, Exception e) { for (Throwable cause = e; cause != null; cause = cause.getCause()) { if (cause instanceof DisplayableException) { if (e instanceof RuntimeException) throw (RuntimeException) e; else throw new FacesException(e); } else if (cause instanceof ServletException) throw new FacesException(e); else if (cause instanceof JspException) throw new FacesException(e); } ExternalContext extContext = context.getExternalContext(); Object response = extContext.getResponse(); if (!(response instanceof HttpServletResponse)) { context.renderResponse(); if (e instanceof RuntimeException) throw (RuntimeException) e; else throw new RuntimeException(e); } log.log(Level.WARNING, e.toString(), e); HttpServletResponse res = (HttpServletResponse) response; try { context.renderResponse(); context.responseComplete(); res.setStatus(500, "JSF Exception"); res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println("<body>"); out.println("<h3>JSF exception detected in " + lifecycle + " phase</h3>"); String msg = e.getMessage(); out.println("<span style='color:red;font:bold'>" + Html.escapeHtml(msg) + "</span><br/>"); out.println("<h3>Context: " + context.getViewRoot() + "</h3>"); out.println("<code><pre>"); String errorId = null; if (e instanceof FacesException && msg.startsWith("id=")) { int p = msg.indexOf(' '); errorId = msg.substring(3, p); } printComponentTree(out, errorId, context, context.getViewRoot(), 0); out.println("</pre></code>"); if (!Alarm.isTest()) { out.println("<h3>Stack Trace</h3>"); out.println("<pre>"); if (e.getCause() != null) e.getCause().printStackTrace(out); else e.printStackTrace(out); out.println("</pre>"); } out.println("</body>"); // clear, so we don't just loop Application app = context.getApplication(); ViewHandler view = app.getViewHandler(); UIViewRoot viewRoot = context.getViewRoot(); viewRoot = view.createView(context, viewRoot.getViewId()); context.setViewRoot(viewRoot); // view.writeState(context); // XXX: no need to output state, but review. return true; } catch (IOException e1) { throw new RuntimeException(e); } }