Search in sources :

Example 1 with StateAwareResponse

use of javax.portlet.StateAwareResponse in project liferay-faces-bridge-impl by liferay.

the class ExternalContextImpl method getResponseCharacterEncoding.

@Override
public String getResponseCharacterEncoding() {
    if (portletResponse instanceof MimeResponse) {
        MimeResponse mimeResponse = (MimeResponse) portletResponse;
        String characterEncoding = mimeResponse.getCharacterEncoding();
        if (manageIncongruities) {
            incongruityContext.setResponseCharacterEncoding(characterEncoding);
        }
        return characterEncoding;
    } else {
        if (manageIncongruities) {
            return incongruityContext.getResponseCharacterEncoding();
        } else {
            if (portletResponse instanceof StateAwareResponse) {
                FacesContext facesContext = FacesContext.getCurrentInstance();
                ExternalContext externalContext = facesContext.getExternalContext();
                String characterEncoding = (String) externalContext.getRequestMap().get(BridgeExt.RESPONSE_CHARACTER_ENCODING);
                if (characterEncoding != null) {
                    // Workaround for patch applied to Mojarra in JAVASERVERFACES-3023
                    return characterEncoding;
                } else {
                    // TCK: getResponseCharacterEncodingEventTest
                    throw new IllegalStateException();
                }
            } else {
                return null;
            }
        }
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) StateAwareResponse(javax.portlet.StateAwareResponse) ExternalContext(javax.faces.context.ExternalContext) MimeResponse(javax.portlet.MimeResponse)

Example 2 with StateAwareResponse

use of javax.portlet.StateAwareResponse in project liferay-faces-bridge-impl by liferay.

the class BridgePhaseBaseImpl method initBridgeRequestScope.

protected void initBridgeRequestScope(PortletRequest portletRequest, PortletResponse portletResponse, Bridge.PortletPhase portletPhase) {
    boolean bridgeRequestScopeEnabled = true;
    if (portletPhase == Bridge.PortletPhase.RESOURCE_PHASE) {
        String facesAjaxParameter = portletRequest.getParameter(Bridge.FACES_AJAX_PARAMETER);
        if (BooleanHelper.isTrueToken(facesAjaxParameter)) {
            bridgeRequestScopeEnabled = PortletConfigParam.BridgeRequestScopeAjaxEnabled.getBooleanValue(portletConfig);
        } else {
            String facesExportComponentParameter = portletRequest.getParameter(BridgeExt.FACES_EXPORT_COMPONENT_PARAMETER);
            if (BooleanHelper.isTrueToken(facesExportComponentParameter)) {
                bridgeRequestScopeEnabled = false;
            }
        }
    }
    if (bridgeRequestScopeEnabled) {
        // Determine if there is a bridge request scope "id" saved as a render parameter. Note that in order to
        // avoid collisions with bridge request scopes for other portlets, the render parameter name has to be
        // namespaced with the portlet name.
        String portletName = portletConfig.getPortletName();
        String bridgeRequestScopeKey = portletName + PARAM_BRIDGE_REQUEST_SCOPE_ID;
        // If there is a render parameter value found for the "id", then return the cached bridge request scope
        // associated with the "id".
        String bridgeRequestScopeId = portletRequest.getParameter(bridgeRequestScopeKey);
        if (bridgeRequestScopeId != null) {
            bridgeRequestScope = bridgeRequestScopeCache.getValue(bridgeRequestScopeId);
            if (bridgeRequestScope != null) {
                logger.debug("Found render parameter name=[{0}] value=[{1}] and cached bridgeRequestScope=[{2}]", bridgeRequestScopeKey, bridgeRequestScopeId, bridgeRequestScope);
            } else {
                if (bridgeRequestScopeActionEnabled) {
                    logger.error("Found render parameter name=[{0}] value=[{1}] BUT bridgeRequestScope is NOT in the cache", bridgeRequestScopeKey, bridgeRequestScopeId);
                }
            }
        }
        // non-excluded request attributes can be picked up by a subsequent RenderRequest.
        if (bridgeRequestScope == null) {
            // TCK: nonFacesResourceTest
            // TCK: resourceAttrRetainedAfterRedisplayPPRTest
            PortletSession portletSession = portletRequest.getPortletSession();
            bridgeRequestScopeId = (String) portletSession.getAttribute(bridgeRequestScopeKey);
            if (bridgeRequestScopeId != null) {
                portletSession.removeAttribute(bridgeRequestScopeKey);
                bridgeRequestScope = bridgeRequestScopeCache.getValue(bridgeRequestScopeId);
                if (bridgeRequestScope != null) {
                    logger.debug("Found (and removed) session-attribute name=[{0}] value=[{1}] and cached bridgeRequestScope=[{2}]", bridgeRequestScopeKey, bridgeRequestScopeId, bridgeRequestScope);
                    if (portletResponse instanceof StateAwareResponse) {
                        logger.debug("Setting former session-attribute as render parameter name=[{0}] value=[{1}]", bridgeRequestScopeKey, bridgeRequestScopeId);
                        StateAwareResponse stateAwareResponse = (StateAwareResponse) portletResponse;
                        stateAwareResponse.setRenderParameter(bridgeRequestScopeKey, bridgeRequestScopeId);
                    }
                } else {
                    logger.error("Found session attribute name=[{0}] value=[{1}] but bridgeRequestScope is not in the cache", bridgeRequestScopeKey, bridgeRequestScopeId);
                }
            }
        }
        // Otherwise, return a new factory created instance.
        if (bridgeRequestScope == null) {
            bridgeRequestScope = BridgeRequestScopeFactory.getBridgeRequestScopeInstance(portletRequest, portletConfig, bridgeConfig);
        }
    }
}
Also used : StateAwareResponse(javax.portlet.StateAwareResponse) PortletSession(javax.portlet.PortletSession)

Example 3 with StateAwareResponse

use of javax.portlet.StateAwareResponse in project liferay-faces-bridge-impl by liferay.

the class IPCPhaseListener method processOutgoingPublicRenderParameters.

/**
 * This method processes the "outgoing" Public Render Parameters in accordance with Section 5.3.3 of the Spec.
 */
public void processOutgoingPublicRenderParameters(FacesContext facesContext) {
    try {
        // since this phase listener is being executed within the portlet lifecycle.
        if (facesContext != null) {
            ExternalContext externalContext = facesContext.getExternalContext();
            PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
            StateAwareResponse stateAwareResponse = (StateAwareResponse) externalContext.getResponse();
            // Section 5.3.3 requires the phase listener to re-examine the public render parameters. For each one
            // that has been changed in the model, its new value must be set in the response, so that when the
            // RENDER_PHASE of the Portlet 2.0 lifecycle executes, this phase listener will be able to inject the
            // new value into the model of other portlets that are participating in the IPC.
            BridgeConfig bridgeConfig = RequestMapUtil.getBridgeConfig(portletRequest);
            Map<String, String[]> publicParameterMappings = bridgeConfig.getPublicParameterMappings();
            if (publicParameterMappings != null) {
                PortletConfig portletConfig = RequestMapUtil.getPortletConfig(portletRequest);
                String portletName = portletConfig.getPortletName();
                Map<String, String[]> publicParameterMap = portletRequest.getPublicParameterMap();
                Set<String> publicRenderParameterNames = publicParameterMappings.keySet();
                // For each of the public render parameters found in the WEB-INF/faces-config.xml file:
                for (String prefixedParameterName : publicRenderParameterNames) {
                    String[] modelExpressions = publicParameterMappings.get(prefixedParameterName);
                    if (modelExpressions != null) {
                        String parameterPrefix;
                        String nonPrefixedParameterName;
                        int colonPos = prefixedParameterName.indexOf(":");
                        if (colonPos > 0) {
                            parameterPrefix = prefixedParameterName.substring(0, colonPos);
                            nonPrefixedParameterName = prefixedParameterName.substring(colonPos + 1);
                        } else {
                            parameterPrefix = null;
                            nonPrefixedParameterName = prefixedParameterName;
                        }
                        for (String originalModelEL : modelExpressions) {
                            String[] parameterValues = publicParameterMap.get(nonPrefixedParameterName);
                            String parameterValue = null;
                            if ((parameterValues != null) && (parameterValues.length > 0)) {
                                parameterValue = parameterValues[0];
                            }
                            PublicRenderParameter publicRenderParameter = new PublicRenderParameterImpl(parameterPrefix, parameterValue, originalModelEL, portletName);
                            if (publicRenderParameter.isForThisPortlet()) {
                                String modelValue = publicRenderParameter.getModelValue(facesContext);
                                boolean modelValueHasChanged = publicRenderParameter.isModelValueChanged(facesContext);
                                if (logger.isTraceEnabled()) {
                                    logger.trace("portletName=[{0}] public render parameter=[{1}] parameterValue=[{2}] modelValue=[{3}] modelValueHasChanged=[{4}]", portletName, nonPrefixedParameterName, parameterValue, modelValue, modelValueHasChanged);
                                }
                                if (modelValueHasChanged) {
                                    logger.debug("Setting render parameter=[{0}] in response because modelValue=[{1}] has changed", nonPrefixedParameterName, modelValue);
                                    stateAwareResponse.setRenderParameter(nonPrefixedParameterName, modelValue);
                                } else {
                                    logger.debug("NOT setting render parameter=[{0}] in response because modelValue=[{1}] has NOT changed", nonPrefixedParameterName, modelValue);
                                }
                            } else {
                                logger.debug("NOT setting render parameter=[{0}] in response because it is NOT for this portletName=[{1}]", nonPrefixedParameterName, portletName);
                            }
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        // There's no point in throwing a RuntimeException of any kind like FacesException because the Faces
        // runtime will swallow it. So the best we can do is log the exception.
        logger.error(e);
    }
}
Also used : StateAwareResponse(javax.portlet.StateAwareResponse) PortletRequest(javax.portlet.PortletRequest) ExternalContext(javax.faces.context.ExternalContext) PortletConfig(javax.portlet.PortletConfig) BridgeConfig(javax.portlet.faces.BridgeConfig)

Example 4 with StateAwareResponse

use of javax.portlet.StateAwareResponse in project liferay-faces-bridge-impl by liferay.

the class TestsCDI1 method eventResponseAlternativeTest.

@BridgeTest(test = "eventResponseAlternativeTest")
public String eventResponseAlternativeTest(TestBean testBean) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    Bridge.PortletPhase portletPhase = BridgeUtil.getPortletRequestPhase(facesContext);
    if (portletPhase == Bridge.PortletPhase.ACTION_PHASE) {
        // Publish an event that gets handled by Ch7TestEventHandler.handleEvent(FacesContext,Event)
        StateAwareResponse stateAwareResponse = (StateAwareResponse) facesContext.getExternalContext().getResponse();
        stateAwareResponse.setEvent(new QName(Constants.EVENT_QNAME, Constants.EVENT_NAME), testBean.getTestName());
        return "multiRequestTestResultRenderCheck";
    } else if (portletPhase == Bridge.PortletPhase.EVENT_PHASE) {
        ELContext elContext = facesContext.getELContext();
        ELResolver elResolver = elContext.getELResolver();
        EventBean eventBean = (EventBean) elResolver.getValue(elContext, null, "eventBean");
        bridgeRequestScopedBean.setFoo(eventBean.getInjectedEventResponseFQCN());
        return null;
    } else if (portletPhase == Bridge.PortletPhase.HEADER_PHASE) {
        String foo = bridgeRequestScopedBean.getFoo();
        if ((foo != null) && foo.endsWith("EventResponseTCKImpl")) {
            testBean.setTestResult(true, "The bridge's alternative producer for EventResponse was properly invoked");
            return Constants.TEST_SUCCESS;
        } else {
            testBean.setTestResult(false, "The bridge's alternative producer for EventResponse was not invoked");
            return Constants.TEST_FAILED;
        }
    }
    testBean.setTestResult(false, "Unexpected portletPhase=" + portletPhase);
    return Constants.TEST_FAILED;
}
Also used : FacesContext(javax.faces.context.FacesContext) StateAwareResponse(javax.portlet.StateAwareResponse) ELContext(javax.el.ELContext) ELResolver(javax.el.ELResolver) QName(javax.xml.namespace.QName) Bridge(javax.portlet.faces.Bridge) BridgeTest(com.liferay.faces.bridge.tck.annotation.BridgeTest)

Example 5 with StateAwareResponse

use of javax.portlet.StateAwareResponse in project liferay-faces-bridge-impl by liferay.

the class TestsCDI1 method eventRequestAlternativeTest.

@BridgeTest(test = "eventRequestAlternativeTest")
public String eventRequestAlternativeTest(TestBean testBean) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    Bridge.PortletPhase portletPhase = BridgeUtil.getPortletRequestPhase(facesContext);
    if (portletPhase == Bridge.PortletPhase.ACTION_PHASE) {
        // Publish an event that gets handled by Ch7TestEventHandler.handleEvent(FacesContext,Event)
        StateAwareResponse stateAwareResponse = (StateAwareResponse) facesContext.getExternalContext().getResponse();
        stateAwareResponse.setEvent(new QName(Constants.EVENT_QNAME, Constants.EVENT_NAME), testBean.getTestName());
        return "multiRequestTestResultRenderCheck";
    } else if (portletPhase == Bridge.PortletPhase.EVENT_PHASE) {
        ELContext elContext = facesContext.getELContext();
        ELResolver elResolver = elContext.getELResolver();
        EventBean eventBean = (EventBean) elResolver.getValue(elContext, null, "eventBean");
        bridgeRequestScopedBean.setFoo(eventBean.getInjectedEventRequestFQCN());
        return null;
    } else if (portletPhase == Bridge.PortletPhase.HEADER_PHASE) {
        String foo = bridgeRequestScopedBean.getFoo();
        if ((foo != null) && foo.endsWith("EventRequestTCKImpl")) {
            testBean.setTestResult(true, "The bridge's alternative producer for EventRequest was properly invoked");
            return Constants.TEST_SUCCESS;
        } else {
            testBean.setTestResult(false, "The bridge's alternative producer for EventRequest was not invoked");
            return Constants.TEST_FAILED;
        }
    }
    testBean.setTestResult(false, "Unexpected portletPhase=" + portletPhase);
    return Constants.TEST_FAILED;
}
Also used : FacesContext(javax.faces.context.FacesContext) StateAwareResponse(javax.portlet.StateAwareResponse) ELContext(javax.el.ELContext) ELResolver(javax.el.ELResolver) QName(javax.xml.namespace.QName) Bridge(javax.portlet.faces.Bridge) BridgeTest(com.liferay.faces.bridge.tck.annotation.BridgeTest)

Aggregations

StateAwareResponse (javax.portlet.StateAwareResponse)34 ExternalContext (javax.faces.context.ExternalContext)29 FacesContext (javax.faces.context.FacesContext)29 QName (javax.xml.namespace.QName)27 BridgeTest (com.liferay.faces.bridge.tck.annotation.BridgeTest)26 PortletRequest (javax.portlet.PortletRequest)9 ActionEvent (javax.faces.event.ActionEvent)3 PhaseEvent (javax.faces.event.PhaseEvent)3 Event (javax.portlet.Event)3 ELContext (javax.el.ELContext)2 ELResolver (javax.el.ELResolver)2 UIViewRoot (javax.faces.component.UIViewRoot)2 PortletMode (javax.portlet.PortletMode)2 PortletSession (javax.portlet.PortletSession)2 WindowState (javax.portlet.WindowState)2 Bridge (javax.portlet.faces.Bridge)2 BridgeURL (javax.portlet.faces.BridgeURL)2 BridgePortalContext (com.liferay.faces.bridge.context.BridgePortalContext)1 BridgeURI (com.liferay.faces.bridge.internal.BridgeURI)1 FacesURLEncoder (com.liferay.faces.util.render.FacesURLEncoder)1