private void deliverPostRestoreStateEvent(FacesContext facesContext) throws FacesException { UIViewRoot root = facesContext.getViewRoot(); final PostRestoreStateEvent postRestoreStateEvent = new PostRestoreStateEvent(root); try { Set<VisitHint> hints = EnumSet.of(VisitHint.SKIP_ITERATION); VisitContext visitContext = VisitContext.createVisitContext(facesContext, null, hints); root.visitTree( visitContext, new VisitCallback() { public VisitResult visit(VisitContext context, UIComponent target) { postRestoreStateEvent.setComponent(target); target.processEvent(postRestoreStateEvent); //noinspection ReturnInsideFinallyBlock return VisitResult.ACCEPT; } }); } catch (AbortProcessingException e) { facesContext .getApplication() .publishEvent( facesContext, ExceptionQueuedEvent.class, new ExceptionQueuedEventContext(facesContext, e, null, PhaseId.RESTORE_VIEW)); } }
public void testSpecificIdTraversal() throws Exception { buildTree(); UIViewRoot root = getFacesContext().getViewRoot(); final StringBuilder builder = new StringBuilder(); HashSet ids = new HashSet(); ids.add("form:panel0:data:3:output0"); ids.add("form:panel1:data:0:output0"); root.visitTree( VisitContext.createVisitContext(getFacesContext(), ids, null), new VisitCallback() { public VisitResult visit(VisitContext context, UIComponent target) { builder.append(target.getClientId(context.getFacesContext()) + " "); return VisitResult.ACCEPT; } }); System.out.println(builder); String result = builder.toString().trim(); assertEquals(result, "form:panel0:data:3:output0 form:panel1:data:0:output0"); }
/** * PRECONDITION: the necessary factories have been installed in the ServletContext attr set. * * <p> * * <p>POSTCONDITION: The facesContext has been initialized with a tree. */ public void execute(FacesContext facesContext) throws FacesException { if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine("Entering RestoreViewPhase"); } if (null == facesContext) { throw new FacesException( MessageUtils.getExceptionMessageString(MessageUtils.NULL_CONTEXT_ERROR_MESSAGE_ID)); } // If an app had explicitely set the tree in the context, use that; // UIViewRoot viewRoot = facesContext.getViewRoot(); if (viewRoot != null) { if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine("Found a pre created view in FacesContext"); } facesContext.getViewRoot().setLocale(facesContext.getExternalContext().getRequestLocale()); // do per-component actions UIViewRoot root = facesContext.getViewRoot(); final PostRestoreStateEvent event = new PostRestoreStateEvent(root); try { root.visitTree( VisitContext.createVisitContext(facesContext), new VisitCallback() { public VisitResult visit(VisitContext context, UIComponent target) { event.setComponent(target); target.processEvent(event); return VisitResult.ACCEPT; } }); } catch (AbortProcessingException e) { facesContext .getApplication() .publishEvent( ExceptionQueuedEvent.class, new ExceptionQueuedEventContext(facesContext, e)); } if (!facesContext.isPostback()) { facesContext.renderResponse(); } return; } // Reconstitute or create the request tree Map requestMap = facesContext.getExternalContext().getRequestMap(); String viewId = (String) requestMap.get("javax.servlet.include.path_info"); if (viewId == null) { viewId = facesContext.getExternalContext().getRequestPathInfo(); } // It could be that this request was mapped using // a prefix mapping in which case there would be no // path_info. Query the servlet path. if (viewId == null) { viewId = (String) requestMap.get("javax.servlet.include.servlet_path"); } if (viewId == null) { viewId = facesContext.getExternalContext().getRequestServletPath(); } if (viewId == null) { if (LOGGER.isLoggable(Level.WARNING)) { LOGGER.warning("viewId is null"); } throw new FacesException( MessageUtils.getExceptionMessageString(MessageUtils.NULL_REQUEST_VIEW_ERROR_MESSAGE_ID)); } ViewHandler viewHandler = Util.getViewHandler(facesContext); boolean isPostBack = (facesContext.isPostback() && !isErrorPage(facesContext)); if (isPostBack) { // try to restore the view viewRoot = viewHandler.restoreView(facesContext, viewId); if (viewRoot == null) { if (is11CompatEnabled(facesContext)) { // 1.1 -> create a new view and flag that the response should // be immediately rendered if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine("Postback: recreating a view for " + viewId); } viewRoot = viewHandler.createView(facesContext, viewId); facesContext.renderResponse(); } else { Object[] params = {viewId}; throw new ViewExpiredException( MessageUtils.getExceptionMessageString( MessageUtils.RESTORE_VIEW_ERROR_MESSAGE_ID, params), viewId); } } facesContext.setViewRoot(viewRoot); if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine("Postback: restored view for " + viewId); } } else { if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine("New request: creating a view for " + viewId); } ViewDeclarationLanguage vdl = facesContext .getApplication() .getViewHandler() .getViewDeclarationLanguage(facesContext, viewId); if (vdl != null) { // If we have one, get the ViewMetadata... ViewMetadata metadata = vdl.getViewMetadata(facesContext, viewId); if (metadata != null) { // perhaps it's not supported // and use it to create the ViewRoot. This will have, at most // the UIViewRoot and its metadata facet. viewRoot = metadata.createMetadataView(facesContext); // Only skip to render response if there are no view parameters Collection<UIViewParameter> params = ViewMetadata.getViewParameters(viewRoot); if (params.isEmpty()) { facesContext.renderResponse(); } } } else { facesContext.renderResponse(); } if (null == viewRoot) { viewRoot = (Util.getViewHandler(facesContext)).createView(facesContext, viewId); } facesContext.setViewRoot(viewRoot); assert (null != viewRoot); facesContext.getApplication().publishEvent(PostAddToViewEvent.class, viewRoot); } if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine("Exiting RestoreViewPhase"); } }