Search in sources :

Example 1 with FacesMessageWrapper

use of com.liferay.faces.bridge.util.internal.FacesMessageWrapper in project liferay-faces-bridge-impl by liferay.

the class BridgeRequestScopeImpl method saveState.

/**
 * Saves the state of the FacesContext as required by section 5.1.2 of the JSR 329 spec. This method is designed to
 * be called during the ACTION_PHASE of the portlet lifecycle.
 *
 * @param  facesContext  The current faces context.
 */
@Override
public void saveState(FacesContext facesContext) {
    logger.debug("saveState(facesContext)");
    // If the bridge request scope began in the ACTION_PHASE, EVENT_PHASE, or RESOURCE_PHASE of the portlet
    // lifecycle, then
    ExternalContext externalContext = facesContext.getExternalContext();
    PortletResponse portletResponse = (PortletResponse) externalContext.getResponse();
    if ((beganInPhase == Bridge.PortletPhase.ACTION_PHASE) || (beganInPhase == Bridge.PortletPhase.EVENT_PHASE) || (beganInPhase == Bridge.PortletPhase.RESOURCE_PHASE)) {
        // Save the view root.
        setAttribute(BRIDGE_REQ_SCOPE_ATTR_FACES_VIEW_ROOT, facesContext.getViewRoot());
        // If the PortletMode hasn't changed, then preserve the "javax.faces.ViewState" request parameter value.
        if (!portletModeChanged) {
            if (portletResponse instanceof ActionResponse) {
                String viewState = externalContext.getRequestParameterMap().get(ResponseStateManager.VIEW_STATE_PARAM);
                if (viewState != null) {
                    // NOTE: Although it is possible to save this as a render parameter, can't use that approach
                    // because portlet containers like Pluto will add the "javax.faces.ViewState" parameter to any
                    // ResourceURLs that are created during the RENDER_PHASE of the portlet lifecycle.
                    setAttribute(ResponseStateManager.VIEW_STATE_PARAM, viewState);
                }
            }
        }
        // If specified in the WEB-INF/portlet.xml descriptor, then preserve the action parameters.
        if (preserveActionParams) {
            Map<String, String> actionRequestParameterMap = new HashMap<String, String>(externalContext.getRequestParameterMap());
            actionRequestParameterMap.remove(ResponseStateManager.VIEW_STATE_PARAM);
            actionRequestParameterMap.remove(JAVAX_FACES_ENCODED_URL_PARAM);
            setAttribute(BRIDGE_REQ_SCOPE_ATTR_ACTION_PARAMS, actionRequestParameterMap);
        }
        // Save the list of faces messages.
        List<FacesMessageWrapper> facesMessageWrappers = new ArrayList<FacesMessageWrapper>();
        Iterator<String> clientIds = facesContext.getClientIdsWithMessages();
        while (clientIds.hasNext()) {
            String clientId = clientIds.next();
            Iterator<FacesMessage> facesMessages = facesContext.getMessages(clientId);
            while (facesMessages.hasNext()) {
                FacesMessage facesMessage = facesMessages.next();
                FacesMessageWrapper facesMessageWrapper = new FacesMessageWrapper(clientId, facesMessage);
                facesMessageWrappers.add(facesMessageWrapper);
            }
        }
        if (facesMessageWrappers.size() > 0) {
            setAttribute(BRIDGE_REQ_SCOPE_ATTR_FACES_MESSAGES, facesMessageWrappers);
        } else {
            logger.trace("Not saving any faces messages");
        }
        // NOTE: PROPOSED-FOR-BRIDGE3-API: https://issues.apache.org/jira/browse/PORTLETBRIDGE-203 Build up a list
        // of attributes found in the FacesContext attribute map and save them. It has to be copied in this manner
        // because the Faces implementation likely calls the clear() method during the call to its
        // FacesContextImpl.release() method.
        saveJSF2FacesContextAttributes(facesContext);
        boolean saveNonExcludedAttributes = true;
        // If a redirect occurred, then indicate that the non-excluded request attributes are not to be preserved.
        if (redirectOcurred) {
            // TCK: eventScopeNotRestoredRedirectTest
            logger.trace("Due to redirect, not saving any non-excluded request attributes");
            saveNonExcludedAttributes = false;
        } else // not to be preserved.
        if (portletModeChanged) {
            logger.trace("Due to PortletMode change, not saving any non-excluded request attributes");
            saveNonExcludedAttributes = false;
        }
        // If running in the ACTION_PHASE or EVENT_PHASE, then the client window must be saved as well so that it
        // can be restored.
        Bridge.PortletPhase portletRequestPhase = BridgeUtil.getPortletRequestPhase(facesContext);
        if ((portletRequestPhase == Bridge.PortletPhase.ACTION_PHASE) || (portletRequestPhase == Bridge.PortletPhase.EVENT_PHASE)) {
            // PROPOSE-FOR-BRIDGE3-API
            saveClientWindow(externalContext);
        }
        // instances that may have been created during the ACTION_PHASE that need to survive to the RENDER_PHASE.
        if (saveNonExcludedAttributes) {
            Map<String, Object> requestMap = externalContext.getRequestMap();
            Set<String> nonExcludedAttributeNames = getNonExcludedRequestAttributes(requestMap);
            List<RequestAttribute> savedRequestAttributes = new ArrayList<RequestAttribute>();
            for (Map.Entry<String, Object> mapEntry : requestMap.entrySet()) {
                String attributeName = mapEntry.getKey();
                if (nonExcludedAttributeNames.contains(attributeName)) {
                    Object attributeValue = mapEntry.getValue();
                    logger.trace("SAVING non-excluded request attribute name=[{0}] value=[{1}]", attributeName, attributeValue);
                    savedRequestAttributes.add(new RequestAttribute(attributeName, attributeValue));
                }
            }
            if (savedRequestAttributes.size() > 0) {
                setAttribute(BRIDGE_REQ_SCOPE_ATTR_REQUEST_ATTRIBUTES, savedRequestAttributes);
            } else {
                logger.trace("Not saving any non-excluded request attributes");
            }
        }
    }
    // If running in the ACTION_PHASE or EVENT_PHASE, then the Flash scope must be saved as well so that it can be
    // restored.
    Bridge.PortletPhase portletRequestPhase = BridgeUtil.getPortletRequestPhase(facesContext);
    if ((portletRequestPhase == Bridge.PortletPhase.ACTION_PHASE) || (portletRequestPhase == Bridge.PortletPhase.EVENT_PHASE)) {
        // PROPOSED-FOR-JSR344-API: http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-1070
        // PROPOSED-FOR-BRIDGE3-API: https://issues.apache.org/jira/browse/PORTLETBRIDGE-201
        saveFlashState(externalContext);
    }
    // can be restored.
    if ((portletRequestPhase == Bridge.PortletPhase.ACTION_PHASE) || (portletRequestPhase == Bridge.PortletPhase.EVENT_PHASE)) {
        IncongruityContext incongruityContext = (IncongruityContext) externalContext.getRequestMap().get(IncongruityContext.class.getName());
        Map<String, Object> incongruityAttributeMap = incongruityContext.getAttributes();
        int mapSize = incongruityAttributeMap.size();
        List<IncongruityAttribute> savedIncongruityAttributes = new ArrayList<IncongruityAttribute>(mapSize);
        for (Map.Entry<String, Object> mapEntry : incongruityAttributeMap.entrySet()) {
            String name = mapEntry.getKey();
            Object value = mapEntry.getValue();
            logger.trace("Saving IncongruityContext attribute name=[{0}] value=[{1}]", name, value);
            savedIncongruityAttributes.add(new IncongruityAttribute(name, value));
        }
        setAttribute(BRIDGE_REQ_SCOPE_ATTR_INCONGRUITY_CONTEXT_ATTRIBUTES, savedIncongruityAttributes);
    }
    if (!postRedirectGetSupported && ((portletRequestPhase == Bridge.PortletPhase.ACTION_PHASE) || (portletRequestPhase == Bridge.PortletPhase.EVENT_PHASE))) {
        Map<String, Object> requestMap = externalContext.getRequestMap();
        Set<String> nonExcludedAttributeNames = getNonExcludedRequestAttributes(requestMap);
        PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
        simulatePostRedirectGet(portletRequest, nonExcludedAttributeNames);
    }
}
Also used : HashMap(java.util.HashMap) PortletPhase(javax.portlet.faces.Bridge.PortletPhase) IncongruityContext(com.liferay.faces.bridge.context.internal.IncongruityContext) ArrayList(java.util.ArrayList) ExternalContext(javax.faces.context.ExternalContext) FacesMessageWrapper(com.liferay.faces.bridge.util.internal.FacesMessageWrapper) PortletResponse(javax.portlet.PortletResponse) ActionResponse(javax.portlet.ActionResponse) PortletRequest(javax.portlet.PortletRequest) FacesMessage(javax.faces.application.FacesMessage) HashMap(java.util.HashMap) Map(java.util.Map) Bridge(javax.portlet.faces.Bridge)

Example 2 with FacesMessageWrapper

use of com.liferay.faces.bridge.util.internal.FacesMessageWrapper in project liferay-faces-bridge-impl by liferay.

the class BridgeRequestScopeImpl method restoreState.

@Override
@SuppressWarnings("unchecked")
public void restoreState(FacesContext facesContext) {
    logger.debug("restoreState(facesContext)");
    boolean restoreNonExcludedRequestAttributes = ((beganInPhase == Bridge.PortletPhase.ACTION_PHASE) || (beganInPhase == Bridge.PortletPhase.EVENT_PHASE) || (beganInPhase == Bridge.PortletPhase.RESOURCE_PHASE));
    PortletPhase portletRequestPhase = BridgeUtil.getPortletRequestPhase(facesContext);
    if (portletRequestPhase == Bridge.PortletPhase.HEADER_PHASE) {
        ExternalContext externalContext = facesContext.getExternalContext();
        PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
        if (!portletMode.equals(portletRequest.getPortletMode())) {
            if (!portletModeChanged) {
                // TCK (requestNoScopeOnModeChangeTest) - In this test, a navigation-rule fires in the
                // ACTION_PHASE of the portlet lifecycle that contains a portlet mode change from VIEW to EDIT.
                // Since the BridgeRequestScope instance created in the ACTION_PHASE is not maintained, "this" will
                // be a new instance and the mode change that took place in the ACTION_PHASE will not be known. In
                // this case, the BridgeRequestScope was not maintained from the ACTION_PHASE to the RENDER_PHASE
                // but a navigation-rule. Detecting a mode change is possible though by checking the request to see
                // if it differs from VIEW mode (the default).
                portletModeChanged = true;
            }
            restoreNonExcludedRequestAttributes = false;
        }
    }
    if (((beganInPhase == Bridge.PortletPhase.ACTION_PHASE) || (beganInPhase == Bridge.PortletPhase.EVENT_PHASE) || (beganInPhase == Bridge.PortletPhase.RESOURCE_PHASE))) {
        // Restore the view root that may have been saved during the action/event/render phase of the portlet
        // lifecycle.
        UIViewRoot uiViewRoot = (UIViewRoot) getAttribute(BRIDGE_REQ_SCOPE_ATTR_FACES_VIEW_ROOT);
        if (uiViewRoot != null) {
            facesContext.setViewRoot(uiViewRoot);
            logger.debug("Restored viewId=[{0}] uiViewRoot=[{1}]", uiViewRoot.getViewId(), uiViewRoot);
        } else {
            logger.debug("Did not restore uiViewRoot");
        }
        if (!redirectOcurred) {
            // Restore the faces messages that may have been saved during the action/event/render phase of the
            // portlet lifecycle.
            List<FacesMessageWrapper> facesMessages = (List<FacesMessageWrapper>) getAttribute(BRIDGE_REQ_SCOPE_ATTR_FACES_MESSAGES);
            boolean restoredFacesMessages = false;
            if (facesMessages != null) {
                for (FacesMessageWrapper facesMessageWrapper : facesMessages) {
                    String clientId = facesMessageWrapper.getClientId();
                    FacesMessage facesMessage = facesMessageWrapper.getFacesMessage();
                    facesContext.addMessage(clientId, facesMessage);
                    logger.trace("Restored facesMessage=[{0}]", facesMessage.getSummary());
                    restoredFacesMessages = true;
                }
            }
            if (restoredFacesMessages) {
                logger.debug("Restored facesMessages");
            } else {
                logger.debug("Did not restore any facesMessages");
            }
            // NOTE: PROPOSE-FOR-BRIDGE3-API: https://issues.apache.org/jira/browse/PORTLETBRIDGE-203 Restore the
            // FacesContext attributes that may have been saved during the ACTION_PHASE of the portlet lifecycle.
            restoreJSF2FacesContextAttributes(facesContext);
        }
    }
    if (restoreNonExcludedRequestAttributes) {
        // Restore the non-excluded request attributes.
        List<RequestAttribute> savedRequestAttributes = (List<RequestAttribute>) getAttribute(BRIDGE_REQ_SCOPE_ATTR_REQUEST_ATTRIBUTES);
        boolean restoredNonExcludedRequestAttributes = false;
        if (savedRequestAttributes != null) {
            ExternalContext externalContext = facesContext.getExternalContext();
            Map<String, Object> currentRequestAttributes = externalContext.getRequestMap();
            // If a redirect did not occur, then restore the non-excluded request attributes.
            if (!redirectOcurred) {
                for (RequestAttribute requestAttribute : savedRequestAttributes) {
                    String name = requestAttribute.getName();
                    Object value = requestAttribute.getValue();
                    logger.trace("Restoring non-excluded request attribute name=[{0}] value=[{1}]", name, value);
                    currentRequestAttributes.put(name, value);
                    restoredNonExcludedRequestAttributes = true;
                }
            }
        }
        if (restoredNonExcludedRequestAttributes) {
            logger.debug("Restored non-excluded request attributes");
        } else {
            logger.debug("Did not restore any non-excluded request attributes");
        }
    }
    // If running in the EVENT_PHASE or HEADER_PHASE, then the client window must be restored.
    if ((portletRequestPhase == Bridge.PortletPhase.EVENT_PHASE) || (portletRequestPhase == Bridge.PortletPhase.HEADER_PHASE)) {
        // PROPOSE-FOR-BRIDGE3-API
        restoreClientWindow(facesContext.getExternalContext());
    }
    // If running in the HEADER_PHASE, then the Flash scope must be restored.
    if (portletRequestPhase == Bridge.PortletPhase.HEADER_PHASE) {
        // NOTE: PROPOSED-FOR-BRIDGE3-API: https://issues.apache.org/jira/browse/PORTLETBRIDGE-201
        // Restore the flash scope.
        ExternalContext externalContext = facesContext.getExternalContext();
        restoreFlashState(externalContext);
    }
    // If running in the HEADER_PHASE, then the incongruity context must be restored.
    if (((beganInPhase == Bridge.PortletPhase.ACTION_PHASE) || (beganInPhase == Bridge.PortletPhase.EVENT_PHASE)) && (portletRequestPhase == Bridge.PortletPhase.HEADER_PHASE)) {
        List<IncongruityAttribute> savedIncongruityAttributes = (List<IncongruityAttribute>) getAttribute(BRIDGE_REQ_SCOPE_ATTR_INCONGRUITY_CONTEXT_ATTRIBUTES);
        if (savedIncongruityAttributes != null) {
            ExternalContext externalContext = facesContext.getExternalContext();
            IncongruityContext incongruityContext = (IncongruityContext) externalContext.getRequestMap().get(IncongruityContext.class.getName());
            Map<String, Object> incongruityContextAttributes = incongruityContext.getAttributes();
            for (IncongruityAttribute incongruityAttribute : savedIncongruityAttributes) {
                String key = incongruityAttribute.getName();
                Object value = incongruityAttribute.getValue();
                incongruityContextAttributes.put(key, value);
            }
        }
    }
}
Also used : PortletPhase(javax.portlet.faces.Bridge.PortletPhase) FacesMessageWrapper(com.liferay.faces.bridge.util.internal.FacesMessageWrapper) IncongruityContext(com.liferay.faces.bridge.context.internal.IncongruityContext) PortletRequest(javax.portlet.PortletRequest) ExternalContext(javax.faces.context.ExternalContext) ArrayList(java.util.ArrayList) List(java.util.List) UIViewRoot(javax.faces.component.UIViewRoot) FacesMessage(javax.faces.application.FacesMessage)

Aggregations

IncongruityContext (com.liferay.faces.bridge.context.internal.IncongruityContext)2 FacesMessageWrapper (com.liferay.faces.bridge.util.internal.FacesMessageWrapper)2 ArrayList (java.util.ArrayList)2 FacesMessage (javax.faces.application.FacesMessage)2 ExternalContext (javax.faces.context.ExternalContext)2 PortletRequest (javax.portlet.PortletRequest)2 PortletPhase (javax.portlet.faces.Bridge.PortletPhase)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 UIViewRoot (javax.faces.component.UIViewRoot)1 ActionResponse (javax.portlet.ActionResponse)1 PortletResponse (javax.portlet.PortletResponse)1 Bridge (javax.portlet.faces.Bridge)1