protected void addViewParameters( FacesContext ctx, String viewId, Map<String, List<String>> existingParameters) { UIViewRoot currentRoot = ctx.getViewRoot(); String currentViewId = currentRoot.getViewId(); Collection<UIViewParameter> toViewParams; Collection<UIViewParameter> currentViewParams; boolean currentIsSameAsNew = false; currentViewParams = ViewMetadata.getViewParameters(currentRoot); if (currentViewId.equals(viewId)) { currentIsSameAsNew = true; toViewParams = currentViewParams; } else { ViewDeclarationLanguage pdl = getViewDeclarationLanguage(ctx, viewId); ViewMetadata viewMetadata = pdl.getViewMetadata(ctx, viewId); UIViewRoot root = viewMetadata.createMetadataView(ctx); toViewParams = ViewMetadata.getViewParameters(root); } if (toViewParams.isEmpty()) { return; } for (UIViewParameter viewParam : toViewParams) { String value; // don't bother looking at view parameter if it's been overridden if (existingParameters.containsKey(viewParam.getName())) { continue; } else if (paramHasValueExpression(viewParam)) { value = viewParam.getStringValueFromModel(ctx); } else { // Anonymous view parameter: // Get string value from UIViewParameter instance stored in current view if (currentIsSameAsNew) { value = viewParam.getStringValue(ctx); } // ...or transfer string value from matching UIViewParameter instance stored in current view else { value = getStringValueToTransfer(ctx, viewParam, currentViewParams); } } if (value != null) { List<String> existing = existingParameters.get(viewParam.getName()); if (existing == null) { existing = new ArrayList<String>(4); existingParameters.put(viewParam.getName(), existing); } existing.add(value); } } }
/** * 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"); } }
/** * 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 deliverPostRestoreStateEvent(facesContext); if (!facesContext.isPostback()) { facesContext.renderResponse(); } return; } FacesException thrownException = null; try { // 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) { 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) { if (isCSRFOptionEnabled(facesContext)) { String convertedViewId = viewHandler.deriveViewId(facesContext, viewId); if (!TokenHelper.verifyToken(facesContext, convertedViewId)) { throw new FacesException("Token verification failed."); } } facesContext.setProcessingEvents(false); // 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); facesContext.setProcessingEvents(true); 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); } } catch (FacesException fe) { thrownException = fe; } finally { if (null == thrownException) { deliverPostRestoreStateEvent(facesContext); } else { throw thrownException; } } if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine("Exiting RestoreViewPhase"); } }