Search in sources :

Example 11 with Bridge

use of javax.portlet.faces.Bridge 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 12 with Bridge

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

the class Tests method cdiRequestScopedBeanExtensionTest.

@BridgeTest(test = "cdiRequestScopedBeanExtensionTest")
public String cdiRequestScopedBeanExtensionTest(TestBean testBean) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    Bridge.PortletPhase portletPhase = BridgeUtil.getPortletRequestPhase(facesContext);
    if (portletPhase == Bridge.PortletPhase.ACTION_PHASE) {
        cdiRequestScopedBeanExtension.setFoo("setInActionPhase");
        return "multiRequestTestResultRenderCheck";
    } else if (portletPhase == Bridge.PortletPhase.HEADER_PHASE) {
        testBean.setTestComplete(true);
        if ("setInActionPhase".equals(cdiRequestScopedBeanExtension.getFoo())) {
            testBean.setTestResult(true, "CDI @RequestScoped is behaving like faces-config &lt;managed-bean&gt; &lt;scope&gt;request&lt/scope&gt; (bridge request scope)");
            return Constants.TEST_SUCCESS;
        } else {
            testBean.setTestResult(false, "CDI @RequestScoped is behaving like @PortletRequestScoped rather than @BridgeRequestScoped");
            return Constants.TEST_FAILED;
        }
    }
    testBean.setTestResult(false, "Unexpected portletPhase=" + portletPhase);
    return Constants.TEST_FAILED;
}
Also used : FacesContext(javax.faces.context.FacesContext) Bridge(javax.portlet.faces.Bridge) BridgeTest(com.liferay.faces.bridge.tck.annotation.BridgeTest)

Example 13 with Bridge

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

the class ScopeCommonTests method requestRenderRedisplayTest.

// Test is MultiRequest -- Render/Action
// Test #5.8
@BridgeTest(test = "requestRenderRedisplayTest")
public String requestRenderRedisplayTest(TestBean testBean) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    Map<String, Object> requestMap = externalContext.getRequestMap();
    Bridge.PortletPhase portletRequestPhase = BridgeUtil.getPortletRequestPhase(facesContext);
    // HEADER_PHASE, provided that the BRIDGE_REQUEST_SCOPE_ACTION_ENABLED context initialization parameter is true.
    if (portletRequestPhase == Bridge.PortletPhase.ACTION_PHASE) {
        requestMap.put("com.liferay.faces.bridge.tck.TestRequestScope_a", REQUEST_ATTR_VALUE);
    } else // Otherwise, if this method is reached in the HEADER_PHASE of the portlet lifecycle, then
    if (portletRequestPhase == Bridge.PortletPhase.HEADER_PHASE) {
        // If the page is being redisplayed due to clicking the "Run Test" link, then
        if (externalContext.getRequestParameterMap().get("org.apache.portlet.faces.tck.redisplay") != null) {
            testBean.setTestComplete(true);
            String bridgeRequestScopeActionEnabledParam = externalContext.getInitParameter(Bridge.BRIDGE_REQUEST_SCOPE_ACTION_ENABLED);
            boolean bridgeRequestScopeActionEnabled = "true".equalsIgnoreCase(bridgeRequestScopeActionEnabledParam);
            String requestScopedAttribute = (String) requestMap.get("com.liferay.faces.bridge.tck.TestRequestScope_a");
            if (requestScopedAttribute != null) {
                if (bridgeRequestScopeActionEnabled) {
                    testBean.setTestResult(true, "Request attribute retained (AS EXPECTED) through a redisplay since the bridge request " + "scope spans ACTION_PHASE -> ACTION_PHASE.");
                    return Constants.TEST_SUCCESS;
                } else {
                    testBean.setTestResult(false, "Request attribute retained through a redisplay EVEN THOUGH the bridge " + "request scope DOES NOT span ACTION_PHASE -> ACTION_PHASE.");
                    return Constants.TEST_FAILED;
                }
            } else {
                if (bridgeRequestScopeActionEnabled) {
                    testBean.setTestResult(false, "Request attribute NOT retained through a redisplay EVEN THOUGH the bridge " + "request scope spans ACTION_PHASE -> ACTION_PHASE.");
                    return Constants.TEST_FAILED;
                } else {
                    testBean.setTestResult(true, "Request attribute NOT retained (AS EXPECTED) through a redisplay since the bridge " + "request scope does not span ACTION_PHASE -> ACTION_PHASE.");
                    return Constants.TEST_SUCCESS;
                }
            }
        }
    }
    return "requestRenderRedisplayTest";
}
Also used : FacesContext(javax.faces.context.FacesContext) ExternalContext(javax.faces.context.ExternalContext) Bridge(javax.portlet.faces.Bridge) BridgeTest(com.liferay.faces.bridge.tck.annotation.BridgeTest)

Example 14 with Bridge

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

the class Ch5TestEventHandler method handleEvent.

@Override
public EventNavigationResult handleEvent(FacesContext facesContext, Event event) {
    Map<String, Object> requestMap = facesContext.getExternalContext().getRequestMap();
    Map<String, Object> sessionMap = facesContext.getExternalContext().getSessionMap();
    String testName = (String) event.getValue();
    String portletTestName = (String) requestMap.get(Constants.TEST_NAME);
    if (testName == null) {
        requestMap.put(EVENT_TEST_FAILED, "Event test failed because the payload is null instead of being the name of the test this event pertains to.");
    }
    if ((testName == null) || (portletTestName == null) || !testName.equals(portletTestName)) {
        // test and ignore for all the other tests/portlets
        return null;
    }
    sessionMap.put(EVENT_RECEIVED, event);
    if (testName.equals("eventScopeRestoredTest")) {
        // test -- that the request attr set in action is restored
        String payload = (String) requestMap.get("portlet.bridge.tck.testAttr");
        if ((payload == null) || !payload.equals(testName)) {
            sessionMap.put(EVENT_TEST_FAILED, "Event received but request scope wasn't restored.");
            return null;
        }
        return null;
    } else if (testName.equals("eventScopeNotRestoredRedirectTest")) {
        // test -- that the request attr set in action is restored
        String payload = (String) requestMap.get("portlet.bridge.tck.testAttr");
        if ((payload == null) || !payload.equals(testName)) {
            sessionMap.put(EVENT_TEST_FAILED, "Event received but request scope wasn't restored.");
        }
        return new EventNavigationResult(null, testName + "EventNavigation");
    } else if (testName.equals("eventScopeNotRestoredModeChangedTest")) {
        // test -- that the request attr set in action is restored
        String payload = (String) requestMap.get("portlet.bridge.tck.testAttr");
        if ((payload == null) || !payload.equals(testName)) {
            sessionMap.put(EVENT_TEST_FAILED, "Event received but request scope wasn't restored.");
        }
        return new EventNavigationResult(null, testName + "EventNavigation");
    } else if (testName.equals("eventControllerTest")) {
        // Verify the event phase attribute is set
        Bridge.PortletPhase phase = (Bridge.PortletPhase) requestMap.get(Bridge.PORTLET_LIFECYCLE_PHASE);
        requestMap.put("tck.eventPhaseCheck", new Boolean((phase != null) && (phase == Bridge.PortletPhase.EVENT_PHASE)));
        // Now verify that a change to a public render parameter is carried forward
        String currentValue = (String) requestMap.get("modelPRP");
        if (currentValue == null) {
            currentValue = "1";
        } else {
            currentValue = currentValue.concat("1");
        }
        // Config is setup to exclude this value from bridge request scope -- so only get carried forward
        // if received in render request
        requestMap.put("modelPRP", currentValue);
        // Stash copy of value in an attr that is carried forward to compare.
        requestMap.put("tck.compareModelPRPValue", currentValue);
        // Verify that event navigation works
        return new EventNavigationResult(null, testName + "EventNavigation");
    }
    return null;
}
Also used : EventNavigationResult(javax.portlet.faces.event.EventNavigationResult) Bridge(javax.portlet.faces.Bridge)

Example 15 with Bridge

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

the class BridgeDestroyTestPortlet method runRenderDestroyTest.

private void runRenderDestroyTest(RenderRequest request, RenderResponse response) throws PortletException, IOException {
    response.setContentType("text/html");
    PrintWriter responsePrintWriter = response.getWriter();
    BridgeTCKResultWriter resultWriter = new BridgeTCKResultWriter(DESTROY_RENDER_TEST);
    // Run test
    Bridge bridge = getFacesBridge(request, response);
    bridge.destroy();
    try {
        bridge.doFacesRequest(request, response);
        resultWriter.setStatus(BridgeTCKResultWriter.FAIL);
        resultWriter.setDetail("Didn't throw the BridgeUninitializedException from doFacesRequest(render) when passed a destroyed bridge. Instead the request completed without an exception.");
    } catch (BridgeUninitializedException bue) {
        resultWriter.setStatus(BridgeTCKResultWriter.PASS);
        resultWriter.setDetail("Correctly threw BridgeUninitializedException from doFacesRequest(render) when passed a destroyed bridge.");
    } catch (Exception e) {
        resultWriter.setStatus(BridgeTCKResultWriter.FAIL);
        resultWriter.setDetail("Didn't throw the BridgeUninitializedException from doFacesRequest(render) when passed a destroyed bridge. Instead it threw: " + e.toString());
    }
    responsePrintWriter.println(resultWriter.toString());
}
Also used : BridgeTCKResultWriter(com.liferay.faces.bridge.tck.common.util.BridgeTCKResultWriter) BridgeUninitializedException(javax.portlet.faces.BridgeUninitializedException) Bridge(javax.portlet.faces.Bridge) IOException(java.io.IOException) BridgeUninitializedException(javax.portlet.faces.BridgeUninitializedException) PortletException(javax.portlet.PortletException) PrintWriter(java.io.PrintWriter)

Aggregations

Bridge (javax.portlet.faces.Bridge)18 IOException (java.io.IOException)9 PortletException (javax.portlet.PortletException)9 BridgeTCKResultWriter (com.liferay.faces.bridge.tck.common.util.BridgeTCKResultWriter)7 BridgeUninitializedException (javax.portlet.faces.BridgeUninitializedException)7 BridgeTest (com.liferay.faces.bridge.tck.annotation.BridgeTest)6 FacesContext (javax.faces.context.FacesContext)6 ExternalContext (javax.faces.context.ExternalContext)5 PrintWriter (java.io.PrintWriter)3 FacesMessage (javax.faces.application.FacesMessage)2 ActionResponse (javax.portlet.ActionResponse)2 PortletRequest (javax.portlet.PortletRequest)2 PortletResponse (javax.portlet.PortletResponse)2 IncongruityContext (com.liferay.faces.bridge.context.internal.IncongruityContext)1 ViewBean (com.liferay.faces.bridge.tck.beans.ViewBean)1 FacesMessageWrapper (com.liferay.faces.bridge.util.internal.FacesMessageWrapper)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 BaseURL (javax.portlet.BaseURL)1