Search in sources :

Example 1 with BridgeException

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

the class BridgePhaseActionImpl method execute.

@Override
public void execute() throws BridgeDefaultViewNotSpecifiedException, BridgeException {
    logger.debug(Logger.SEPARATOR);
    logger.debug("execute(ActionRequest, ActionResponse) portletName=[{0}]", portletName);
    try {
        init(actionRequest, actionResponse, Bridge.PortletPhase.ACTION_PHASE);
        // PROPOSED-FOR-BRIDGE3-API: https://issues.apache.org/jira/browse/PORTLETBRIDGE-202
        bridgeRequestScope.setPortletMode(actionRequest.getPortletMode());
        // If the "javax.portlet.faces.PortletMode" request parameter has a value, then the developer probably
        // specified a URL like <h:outputLink value="portlet:render"> using f:param to set the request parameter
        // for switching modes. This is one of the tests in the TCK.
        String portletModeParam = actionRequest.getParameter(Bridge.PORTLET_MODE_PARAMETER);
        if (portletModeParam != null) {
            try {
                actionResponse.setPortletMode(new PortletMode(portletModeParam));
            } catch (PortletModeException e) {
                logger.error("Invalid parameter value {0}=[{1}]", Bridge.PORTLET_MODE_PARAMETER, portletModeParam);
            }
        }
        // Attach the JSF 2.2 client window to the JSF lifecycle so that Faces Flows can be utilized.
        attachClientWindowToLifecycle(facesContext, facesLifecycle);
        // Execute all the phases of the JSF lifecycle except for RENDER_RESPONSE since that can only be executed
        // during the RENDER_PHASE of the portlet lifecycle.
        facesLifecycle.execute(facesContext);
        throwQueuedExceptionIfNecessary(facesContext);
        // Set a flag on the bridge request scope indicating that the Faces Lifecycle has executed.
        bridgeRequestScope.setFacesLifecycleExecuted(true);
        // Since this is a full-page postback, the <head>...</head> section of the portal page needs to be
        // completely re-rendered during the subsequent RENDER_PHASE of the portlet lifecycle. Because of this, the
        // resources in the HeadManagedBean must be cleared so that the bridge will not recognize any resources as
        // already being present in the <head>...</head> section.
        clearHeadManagedBeanResources(facesContext);
        // redirect and portlet mode change has not occurred.
        if (bridgeRequestScope.isRedirectOccurred() || bridgeRequestScope.isPortletModeChanged()) {
            bridgeRequestScope.release(facesContext);
        } else {
            // Save the faces view root and any messages in the faces context so that they can be restored during
            // the RENDER_PHASE of the portlet lifecycle.
            bridgeRequestScope.saveState(facesContext);
            maintainBridgeRequestScope(actionRequest, actionResponse, BridgeRequestScope.Transport.RENDER_PARAMETER);
        }
        // Spec 6.6 (Namespacing)
        indicateNamespacingToConsumers(facesContext.getViewRoot(), actionResponse);
    } catch (Throwable t) {
        throw new BridgeException(t);
    } finally {
        cleanup(actionRequest);
    }
}
Also used : BridgeException(javax.portlet.faces.BridgeException) PortletMode(javax.portlet.PortletMode) PortletModeException(javax.portlet.PortletModeException)

Example 2 with BridgeException

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

the class BridgePhaseEventImpl method execute.

// Java 1.6+ @Override
@Override
public void execute() throws BridgeDefaultViewNotSpecifiedException, BridgeException {
    logger.debug(Logger.SEPARATOR);
    logger.debug("execute(EventRequest, EventResponse) portletName=[{0}]", portletName);
    String bridgeEventHandlerAttributeName = Bridge.BRIDGE_PACKAGE_PREFIX + portletName + "." + Bridge.BRIDGE_EVENT_HANDLER;
    BridgeEventHandler bridgeEventHandler = (BridgeEventHandler) portletContext.getAttribute(bridgeEventHandlerAttributeName);
    if (bridgeEventHandler == null) {
        bridgeEventHandler = BridgeEventHandlerFactory.getBridgeEventHandlerInstance(portletConfig);
    }
    try {
        // If there is a bridgeEventHandler registered in portlet.xml, then
        if (bridgeEventHandler != null) {
            init(eventRequest, eventResponse, Bridge.PortletPhase.EVENT_PHASE);
            // Restore the BridgeRequestScope that may have started during the ACTION_PHASE.
            bridgeRequestScope.restoreState(facesContext);
            // PROPOSED-FOR-BRIDGE3-API: https://issues.apache.org/jira/browse/PORTLETBRIDGE-202
            bridgeRequestScope.setPortletMode(eventRequest.getPortletMode());
            // Execute the JSF lifecycle so that ONLY the RESTORE_VIEW phase executes (note that this this is
            // accomplished by the IPCPhaseListener).
            facesLifecycle.execute(facesContext);
            throwQueuedExceptionIfNecessary(facesContext);
            // Set a flag on the bridge request scope indicating that the Faces Lifecycle has executed.
            bridgeRequestScope.setFacesLifecycleExecuted(true);
            // If there is a bridgeEventHandler registered in portlet.xml, then invoke the handler so that it
            // can process the event payload.
            logger.debug("Invoking {0} for class=[{1}]", bridgeEventHandlerAttributeName, bridgeEventHandler.getClass());
            Event event = eventRequest.getEvent();
            EventNavigationResult eventNavigationResult = bridgeEventHandler.handleEvent(facesContext, event);
            if (eventNavigationResult != null) {
                String oldViewId = facesContext.getViewRoot().getViewId();
                String fromAction = eventNavigationResult.getFromAction();
                String outcome = eventNavigationResult.getOutcome();
                logger.debug("Invoking navigationHandler fromAction=[{0}] outcome=[{1}]", fromAction, outcome);
                NavigationHandler navigationHandler = facesContext.getApplication().getNavigationHandler();
                navigationHandler.handleNavigation(facesContext, fromAction, outcome);
                String newViewId = facesContext.getViewRoot().getViewId();
                bridgeRequestScope.setNavigationOccurred(!oldViewId.equals(newViewId));
            }
            // Process the outgoing public render parameters.
            // TCK (eventControllerTest)
            processOutgoingPublicRenderParameters(facesLifecycle);
            // Save the faces view root and any messages in the faces context so that they can be restored during
            // the RENDER_PHASE of the portlet lifecycle.
            bridgeRequestScope.saveState(facesContext);
            // In accordance with Section 5.2.5 of the Spec, if a portlet mode change has occurred, then the
            // bridge request scope must not be maintained.
            // 
            // NOTE: THIS REQUIREMENT IS HANDLED BY THE RENDER PHASE since the current design relies on
            // BridgeRequestScope.restoreState(FacesContext) to detect portlet mode changes in some cases.
            // 
            // PROPOSE-FOR-BRIDGE3-SPEC: Although the spec does not mention the redirect case, the bridge request
            // scope must not be maintained if a redirect has occurred.
            // 
            // REDIRECT:
            // ---------
            // TCK (eventScopeNotRestoredRedirectTest)
            // TCK (redirectEventTest)
            // 
            // PORTLET MODE CHANGED:
            // ---------------------
            // TCK (eventScopeNotRestoredModeChangedTest)
            // TCK (encodeActionURLWithModeEventTest)
            // 
            // NON-REDIRECT / SAME PORTLET MODE:
            // ---------------------------------
            // TCK (eventPhaseListenerTest)
            // TCK (eventScopeRestoredTest)
            // TCK (eventControllerTest)
            // TCK (encodeActionURLJSFViewEventTest)
            // TCK (encodeActionURLWithParamEventTest)
            // TCK (encodeActionURLWithInvalidModeEventTest),
            // TCK (encodeActionURLWithWindowStateEventTest)
            // TCK (encodeActionURLWithInvalidWindowStateEventTest)
            // TCK (getRequestHeaderMapEventTest)
            // TCK (getRequestHeaderValuesMapEventTest)
            // TCK (getRequestCharacterEncodingEventTest)
            // TCK (getRequestContentTypeEventTest)
            // TCK (getResponseCharacterEncodingEventTest)
            // TCK (getResponseContentTypeEventTest)
            // Assume that the bridge request scope should be maintained from the EVENT_PHASE into the
            // RENDER_PHASE by utilizing render parameters.
            BridgeRequestScope.Transport bridgeRequestScopeTransport = BridgeRequestScope.Transport.RENDER_PARAMETER;
            Serializable eventPayload = event.getValue();
            // redirect.
            if ((eventPayload != null) && (eventPayload instanceof EventPayloadWrapper)) {
                EventPayloadWrapper eventPayloadWrapper = (EventPayloadWrapper) eventPayload;
                if (eventPayloadWrapper.isRedirect()) {
                    bridgeRequestScopeTransport = BridgeRequestScope.Transport.PORTLET_SESSION_ATTRIBUTE;
                }
            }
            maintainBridgeRequestScope(eventRequest, eventResponse, bridgeRequestScopeTransport);
        }
        // Maintain the render parameters set in the ACTION_PHASE so that they carry over to the RENDER_PHASE.
        maintainRenderParameters(eventRequest, eventResponse);
        // Spec 6.6 (Namespacing)
        if (bridgeEventHandler != null) {
            indicateNamespacingToConsumers(facesContext.getViewRoot(), eventResponse);
        }
    } catch (Throwable t) {
        throw new BridgeException(t);
    } finally {
        if (bridgeEventHandler != null) {
            cleanup(eventRequest);
        }
    }
}
Also used : BridgeException(javax.portlet.faces.BridgeException) Serializable(java.io.Serializable) EventPayloadWrapper(com.liferay.faces.bridge.event.EventPayloadWrapper) EventNavigationResult(javax.portlet.faces.event.EventNavigationResult) BridgeEventHandler(javax.portlet.faces.BridgeEventHandler) Event(javax.portlet.Event) NavigationHandler(javax.faces.application.NavigationHandler) BridgeRequestScope(com.liferay.faces.bridge.scope.internal.BridgeRequestScope)

Example 3 with BridgeException

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

the class BridgePhaseHeaderImpl method execute.

@Override
public void execute() throws BridgeException {
    logger.debug(Logger.SEPARATOR);
    logger.debug("execute(RenderRequest, RenderResponse) portletName=[{0}] portletMode=[{1}]", portletName, headerRequest.getPortletMode());
    try {
        executeRender(null, PortletPhase.HEADER_PHASE);
    } catch (BridgeException e) {
        throw e;
    } catch (Throwable t) {
        throw new BridgeException(t);
    } finally {
        cleanup(headerRequest);
    }
    logger.debug(Logger.SEPARATOR);
}
Also used : BridgeException(javax.portlet.faces.BridgeException)

Example 4 with BridgeException

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

the class HeaderRequestPhaseListener method beforePhase.

@Override
public void beforePhase(PhaseEvent phaseEvent) {
    FacesContext facesContext = phaseEvent.getFacesContext();
    ExternalContext externalContext = facesContext.getExternalContext();
    String viewId = externalContext.getRequestPathInfo();
    if (viewId == null) {
        viewId = externalContext.getRequestServletPath();
    }
    if (viewId != null) {
        PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
        PortletConfig portletConfig = RequestMapUtil.getPortletConfig(portletRequest);
        Map<String, String> defaultViewIdMap = ViewUtil.getDefaultViewIdMap(portletConfig);
        if (viewId.equals(defaultViewIdMap.get(PortletMode.EDIT.toString()))) {
            boolean portletModeAllowed = portletRequest.isPortletModeAllowed(PortletMode.EDIT);
            if (!portletModeAllowed) {
                throw new BridgeException("Portlet EDIT_MODE is not allowed");
            }
        }
    }
}
Also used : BridgeException(javax.portlet.faces.BridgeException) FacesContext(javax.faces.context.FacesContext) PortletRequest(javax.portlet.PortletRequest) ExternalContext(javax.faces.context.ExternalContext) PortletConfig(javax.portlet.PortletConfig)

Example 5 with BridgeException

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

the class BridgePhaseHeaderRenderCommon method executeRender.

protected void executeRender(String renderRedirectViewId, Bridge.PortletPhase portletPhase) throws BridgeException, IOException {
    RenderRequest renderRequest = getRenderRequest();
    MimeResponse mimeResponse = getMimeResponse();
    init(renderRequest, mimeResponse, portletPhase);
    // Determine whether or not the Faces lifecycle was already executed.
    boolean facesLifecycleExecuted = bridgeRequestScope.isFacesLifecycleExecuted();
    // Restore the faces view root and messages that would have been saved during the ACTION_PHASE.
    bridgeRequestScope.restoreState(facesContext);
    // scope must not be maintained if a redirect has occurred.
    if (bridgeRequestScope.isPortletModeChanged() || bridgeRequestScope.isRedirectOccurred()) {
        bridgeRequestScopeCache.removeValue(bridgeRequestScope.getId());
    }
    // in the FacesContext.
    if (renderRedirectViewId != null) {
        renderRequest.setAttribute(BridgeExt.RENDER_REDIRECT_AFTER_DISPATCH, Boolean.TRUE);
        ViewHandler viewHandler = facesContext.getApplication().getViewHandler();
        UIViewRoot uiViewRoot = viewHandler.createView(facesContext, renderRedirectViewId);
        facesContext.setViewRoot(uiViewRoot);
        logger.debug("Performed render-redirect to viewId=[{0}]", renderRedirectViewId);
    } else // _facesViewIdRender request parameter.
    if (bridgeRequestScope.isRedirectOccurred()) {
        // TCK (redirectEventTest)
        facesLifecycleExecuted = false;
    }
    // http://javaserverfaces.java.net/nonav/docs/2.0/javadocs/javax/faces/context/FacesContext.html#isPostback()
    if (bridgeRequestScope.getBeganInPhase() == Bridge.PortletPhase.ACTION_PHASE) {
        ExternalContext externalContext = facesContext.getExternalContext();
        externalContext.getRequestMap().put(Bridge.IS_POSTBACK_ATTRIBUTE, Boolean.TRUE);
    }
    logger.debug("portletName=[{0}] facesLifecycleExecuted=[{1}]", portletName, facesLifecycleExecuted);
    // If the JSF lifecycle executed back in the ACTION_PHASE of the portlet lifecycle, then
    if (facesLifecycleExecuted) {
        // TCK: prpUpdatedFromActionTest
        PhaseEvent restoreViewPhaseEvent = new PhaseEvent(facesContext, PhaseId.RESTORE_VIEW, facesLifecycle);
        PhaseListener[] phaseListeners = facesLifecycle.getPhaseListeners();
        for (PhaseListener phaseListener : phaseListeners) {
            if (phaseListener instanceof IPCPhaseListener) {
                phaseListener.afterPhase(restoreViewPhaseEvent);
                break;
            }
        }
    } else // Otherwise, in accordance with Section 5.2.6 of the Spec, execute the JSF lifecycle so that ONLY the
    // RESTORE_VIEW phase executes. Note that this is accomplished by the HeaderRequestPhaseListener.
    {
        ExternalContext externalContext = facesContext.getExternalContext();
        try {
            String viewId = getFacesViewId(externalContext);
            logger.debug("Executing Faces lifecycle for viewId=[{0}]", viewId);
        }// lifecycle has already happened in the HEADER_PHASE.
         catch (BridgeException e) {
            // TCK (portletSetsInvalidViewPathTest)
            // TCK (exceptionThrownWhenNoDefaultViewIdTest)
            Writer responseOutputWriter = getResponseOutputWriter(externalContext);
            if (responseOutputWriter instanceof CapturingWriter) {
                CapturingWriter capturingWriter = (CapturingWriter) responseOutputWriter;
                renderRequest.setAttribute(Bridge.RENDER_RESPONSE_OUTPUT, capturingWriter.getWriterOperations());
            }
            logger.error("Unable to get viewId due to {0}", e.getClass().getSimpleName());
            if (e instanceof BridgeInvalidViewPathException) {
                renderRequest.setAttribute(HANDLING_BRIDGE_INVALID_VIEW_PATH_EXCEPTION, true);
                queueHandleableException(renderRequest, facesContext, e);
            } else {
                throw e;
            }
        }
        // Attach the JSF 2.2 client window to the JSF lifecycle so that Faces Flows can be utilized.
        attachClientWindowToLifecycle(facesContext, facesLifecycle);
        // Execute the JSF lifecycle.
        if (isHandlingBridgeInvalidViewPathException(renderRequest)) {
            try {
                facesLifecycle.execute(facesContext);
            } catch (FacesException e) {
                Throwable cause = e.getCause();
                if ((cause != null) && (cause instanceof BridgeInvalidViewPathException)) {
                    throw (BridgeException) cause;
                } else {
                    throw e;
                }
            } finally {
                renderRequest.removeAttribute(HANDLING_BRIDGE_INVALID_VIEW_PATH_EXCEPTION);
            }
        } else {
            facesLifecycle.execute(facesContext);
        }
    }
    throwQueuedExceptionIfNecessary(facesContext);
    // UIViewRoot.
    if (bridgeRequestScope.isPortletModeChanged() && !bridgeRequestScope.isNavigationOccurred()) {
        BridgeNavigationHandler bridgeNavigationHandler = getBridgeNavigationHandler(facesContext);
        PortletMode fromPortletMode = bridgeRequestScope.getPortletMode();
        PortletMode toPortletMode = renderRequest.getPortletMode();
        bridgeNavigationHandler.handleNavigation(facesContext, fromPortletMode, toPortletMode);
    }
    // Determines whether or not lifecycle incongruities should be managed.
    boolean manageIncongruities = PortletConfigParam.ManageIncongruities.getBooleanValue(portletConfig);
    // lifecycle.
    if (manageIncongruities) {
        incongruityContext.makeCongruous(facesContext);
    }
    // Execute the RENDER_RESPONSE phase of the faces lifecycle.
    logger.debug("Executing Faces render");
    facesLifecycle.render(facesContext);
    // Set the view history according to Section 5.4.3 of the Bridge Spec.
    setViewHistory(facesContext.getViewRoot().getViewId());
    // Spec 6.6 (Namespacing)
    indicateNamespacingToConsumers(facesContext.getViewRoot(), mimeResponse);
    // If a render-redirect occurred, then
    ExternalContext externalContext = facesContext.getExternalContext();
    Map<String, Object> requestMap = externalContext.getRequestMap();
    Boolean renderRedirect = (Boolean) requestMap.remove(BridgeExt.RENDER_REDIRECT);
    Writer responseOutputWriter = getResponseOutputWriter(externalContext);
    if ((renderRedirect != null) && renderRedirect) {
        // Cleanup the old FacesContext since a new one will be created in the recursive method call below.
        facesContext.responseComplete();
        facesContext.release();
        // redirect URL.
        if (responseOutputWriter instanceof CapturingWriter) {
            CapturingWriter capturingWriter = (CapturingWriter) responseOutputWriter;
            capturingWriter.getWriterOperations().clear();
        }
        // Recursively call this method with the render-redirect URL so that the RENDER_RESPONSE phase of the
        // JSF lifecycle will be re-executed according to the new Faces viewId found in the redirect URL.
        renderRedirectViewId = (String) requestMap.remove(BridgeExt.RENDER_REDIRECT_VIEW_ID);
        executeRender(renderRedirectViewId, portletPhase);
    } else // Otherwise,
    {
        // HEADER_PHASE or that a render-redirect executed in the HEADER_PHASE or RESOURCE_PHASE.
        if (responseOutputWriter instanceof CapturingWriter) {
            CapturingWriter capturingWriter = (CapturingWriter) responseOutputWriter;
            List<WriterOperation> writerOperations = capturingWriter.getWriterOperations();
            if (writerOperations != null) {
                // that the markup will be written to the response.
                if (portletPhase == Bridge.PortletPhase.RENDER_PHASE) {
                    for (WriterOperation writerOperation : writerOperations) {
                        writerOperation.invoke(responseOutputWriter);
                    }
                } else // Otherwise, since running in the HEADER_PHASE, save the list of writer operations so that the
                // markup will be rendered in the subsequent RENDER_PHASE.
                {
                    renderRequest.setAttribute(Bridge.RENDER_RESPONSE_OUTPUT, writerOperations);
                }
            }
        }
    }
}
Also used : PhaseEvent(javax.faces.event.PhaseEvent) ViewHandler(javax.faces.application.ViewHandler) MimeResponse(javax.portlet.MimeResponse) RenderRequest(javax.portlet.RenderRequest) BridgeNavigationHandler(com.liferay.faces.bridge.application.internal.BridgeNavigationHandler) FacesException(javax.faces.FacesException) PortletMode(javax.portlet.PortletMode) BridgeException(javax.portlet.faces.BridgeException) CapturingWriter(com.liferay.faces.bridge.context.internal.CapturingWriter) BridgeInvalidViewPathException(javax.portlet.faces.BridgeInvalidViewPathException) ExternalContext(javax.faces.context.ExternalContext) IPCPhaseListener(com.liferay.faces.bridge.event.internal.IPCPhaseListener) PhaseListener(javax.faces.event.PhaseListener) WriterOperation(com.liferay.faces.bridge.context.internal.WriterOperation) UIViewRoot(javax.faces.component.UIViewRoot) IPCPhaseListener(com.liferay.faces.bridge.event.internal.IPCPhaseListener) CapturingWriter(com.liferay.faces.bridge.context.internal.CapturingWriter) Writer(java.io.Writer)

Aggregations

BridgeException (javax.portlet.faces.BridgeException)14 ExternalContext (javax.faces.context.ExternalContext)8 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 URISyntaxException (java.net.URISyntaxException)4 ViewHandler (javax.faces.application.ViewHandler)2 PortletMode (javax.portlet.PortletMode)2 BridgeInvalidViewPathException (javax.portlet.faces.BridgeInvalidViewPathException)2 BridgeNavigationHandler (com.liferay.faces.bridge.application.internal.BridgeNavigationHandler)1 CapturingWriter (com.liferay.faces.bridge.context.internal.CapturingWriter)1 WriterOperation (com.liferay.faces.bridge.context.internal.WriterOperation)1 EventPayloadWrapper (com.liferay.faces.bridge.event.EventPayloadWrapper)1 IPCPhaseListener (com.liferay.faces.bridge.event.internal.IPCPhaseListener)1 BridgeRequestScope (com.liferay.faces.bridge.scope.internal.BridgeRequestScope)1 ResourceValidator (com.liferay.faces.util.application.ResourceValidator)1 ConfiguredServletMapping (com.liferay.faces.util.config.ConfiguredServletMapping)1 Product (com.liferay.faces.util.product.Product)1 Serializable (java.io.Serializable)1 Writer (java.io.Writer)1 FacesException (javax.faces.FacesException)1 NavigationHandler (javax.faces.application.NavigationHandler)1