@Override public void handle() throws FacesException { final Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator(); while (i.hasNext()) { ExceptionQueuedEvent event = i.next(); ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource(); Throwable t = context.getException(); final FacesContext fc = FacesContext.getCurrentInstance(); final Map<String, Object> requestMap = fc.getExternalContext().getRequestMap(); try { System.out.printf(">>> Exception caught: %s", t.getMessage()); t.printStackTrace(); requestMap.put("exceptionMessage", t.getMessage()); ExternalContext extContext = fc.getExternalContext(); String url = extContext.encodeActionURL(extContext.getRequestContextPath() + "/500.xhtml"); extContext.redirect(url); } catch (Exception e) { String errorPageLocation = "/WEB-INF/500.xhtml"; fc.setViewRoot(fc.getApplication().getViewHandler().createView(fc, errorPageLocation)); fc.getPartialViewContext().setRenderAll(false); fc.renderResponse(); } finally { i.remove(); } } getWrapped().handle(); }
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); } }
/** Decodes the data from the form. */ @Override public void decode(FacesContext context, UIComponent component) { String clientId = component.getClientId(context); ExternalContext ext = context.getExternalContext(); Map<String, String> paramMap = ext.getRequestParameterMap(); String value = paramMap.get(clientId); if (value != null) ((EditableValueHolder) component).setSubmittedValue(value); }
@Override public void afterPhase(PhaseEvent event) { FacesContext facesContext = event.getFacesContext(); ExternalContext externalContext = facesContext.getExternalContext(); HttpServletRequest request = (HttpServletRequest) externalContext.getRequest(); if (!request.getMethod().equals("POST")) { Map<?, ?> flashScope = (Map<?, ?>) facesContext .getApplication() .createValueBinding("#{flashScope}") .getValue(facesContext); flashScope.clear(); } }
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); } }
public static void outputResponseDocument( final PipelineContext pipelineContext, final ExternalContext externalContext, final IndentedLogger indentedLogger, final SAXStore annotatedDocument, final XFormsContainingDocument containingDocument, final XMLReceiver xmlReceiver) throws SAXException, IOException { final List<XFormsContainingDocument.Load> loads = containingDocument.getLoadsToRun(); if (containingDocument.isGotSubmissionReplaceAll()) { // 1. Got a submission with replace="all" // NOP: Response already sent out by a submission // TODO: modify XFormsModelSubmission accordingly indentedLogger.logDebug("", "handling response for submission with replace=\"all\""); } else if (loads != null && loads.size() > 0) { // 2. Got at least one xforms:load // Send redirect out // Get first load only final XFormsContainingDocument.Load load = loads.get(0); // Send redirect final String redirectResource = load.getResource(); indentedLogger.logDebug( "", "handling redirect response for xforms:load", "url", redirectResource); // Set isNoRewrite to true, because the resource is either a relative path or already contains // the servlet context externalContext.getResponse().sendRedirect(redirectResource, null, false, false, true); // Still send out a null document to signal that no further processing must take place XMLUtils.streamNullDocument(xmlReceiver); } else { // 3. Regular case: produce an XHTML document out final ElementHandlerController controller = new ElementHandlerController(); // Register handlers on controller (the other handlers are registered by the body handler) { controller.registerHandler( XHTMLHeadHandler.class.getName(), XMLConstants.XHTML_NAMESPACE_URI, "head"); controller.registerHandler( XHTMLBodyHandler.class.getName(), XMLConstants.XHTML_NAMESPACE_URI, "body"); // Register a handler for AVTs on HTML elements final boolean hostLanguageAVTs = XFormsProperties .isHostLanguageAVTs(); // TODO: this should be obtained per document, but we only // know about this in the extractor if (hostLanguageAVTs) { controller.registerHandler( XXFormsAttributeHandler.class.getName(), XFormsConstants.XXFORMS_NAMESPACE_URI, "attribute"); controller.registerHandler( XHTMLElementHandler.class.getName(), XMLConstants.XHTML_NAMESPACE_URI); } // Swallow XForms elements that are unknown controller.registerHandler( NullHandler.class.getName(), XFormsConstants.XFORMS_NAMESPACE_URI); controller.registerHandler( NullHandler.class.getName(), XFormsConstants.XXFORMS_NAMESPACE_URI); controller.registerHandler(NullHandler.class.getName(), XFormsConstants.XBL_NAMESPACE_URI); } // Set final output controller.setOutput(new DeferredXMLReceiverImpl(xmlReceiver)); // Set handler context controller.setElementHandlerContext( new HandlerContext( controller, pipelineContext, containingDocument, externalContext, null)); // Process the entire input annotatedDocument.replay( new ExceptionWrapperXMLReceiver(controller, "converting XHTML+XForms document to XHTML")); } containingDocument.afterInitialResponse(); }