Search in sources :

Example 6 with PortalContext

use of javax.portlet.PortalContext in project liferay-faces-bridge-ext by liferay.

the class BridgePortletRequestFactoryLiferayImpl method getActionRequest.

@Override
public ActionRequest getActionRequest(ActionRequest actionRequest, ActionResponse actionResponse, PortletConfig portletConfig, BridgeConfig bridgeConfig) {
    actionRequest = getWrapped().getActionRequest(actionRequest, actionResponse, portletConfig, bridgeConfig);
    PortalContext portalContext = new PortalContextBridgeLiferayImpl(actionRequest);
    return new ActionRequestBridgeLiferayImpl(actionRequest, actionResponse.getNamespace(), portletConfig, portalContext);
}
Also used : PortalContext(javax.portlet.PortalContext) PortalContextBridgeLiferayImpl(com.liferay.faces.bridge.ext.context.internal.PortalContextBridgeLiferayImpl)

Example 7 with PortalContext

use of javax.portlet.PortalContext in project liferay-faces-bridge-ext by liferay.

the class BridgePortletRequestFactoryLiferayImpl method getRenderRequest.

@Override
public RenderRequest getRenderRequest(RenderRequest renderRequest, RenderResponse renderResponse, PortletConfig portletConfig, BridgeConfig bridgeConfig) {
    renderRequest = getWrapped().getRenderRequest(renderRequest, renderResponse, portletConfig, bridgeConfig);
    PortalContext portalContext = new PortalContextBridgeLiferayImpl(renderRequest);
    return new RenderRequestBridgeLiferayImpl(renderRequest, renderResponse.getNamespace(), portletConfig, portalContext);
}
Also used : PortalContext(javax.portlet.PortalContext) PortalContextBridgeLiferayImpl(com.liferay.faces.bridge.ext.context.internal.PortalContextBridgeLiferayImpl)

Example 8 with PortalContext

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

the class ExternalContextImpl method redirect.

@Override
public void redirect(String url) throws IOException {
    if (url != null) {
        logger.debug("redirect url=[{0}]", url);
        // lifecycle, then
        if ((portletPhase == Bridge.PortletPhase.ACTION_PHASE) || (portletPhase == Bridge.PortletPhase.EVENT_PHASE) || isHeaderPhase(portletPhase) || (portletPhase == Bridge.PortletPhase.RENDER_PHASE)) {
            // "javax.portlet.faces.DirectLink" parameter value of "true", then
            try {
                FacesContext facesContext = FacesContext.getCurrentInstance();
                ExternalContext externalContext = facesContext.getExternalContext();
                FacesURLEncoder facesURLEncoder = FacesURLEncoderFactory.getFacesURLEncoderInstance(externalContext);
                ResponseWriter responseWriter = facesContext.getResponseWriter();
                String urlCharacterEncoding = URLUtil.getURLCharacterEncoding(portletPhase, externalContext, responseWriter, "UTF-8");
                BridgeURI bridgeURI = new BridgeURI(url, portletResponse.getNamespace(), facesURLEncoder, urlCharacterEncoding);
                String directLink = bridgeURI.getParameter(Bridge.DIRECT_LINK);
                String contextPath = externalContext.getRequestContextPath();
                if ((portletPhase == Bridge.PortletPhase.ACTION_PHASE) && (url.startsWith("#") || bridgeURI.isExternal(contextPath) || "true".equals(directLink))) {
                    if (bridgeRequestScope != null) {
                        bridgeRequestScope.setRedirectOccurred(true);
                    }
                    // TCK: requestNoScopeOnRedirectTest
                    ActionResponse actionResponse = (ActionResponse) portletResponse;
                    actionResponse.sendRedirect(bridgeURI.toString());
                } else // Otherwise,
                {
                    // If running in the ACTION_PHASE of the portlet lifecycle and the portlet container has the
                    // ability to create a render URL during the ACTION_PHASE, then assume that the specified URL is
                    // an encoded RenderURL and issue a redirect.
                    PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
                    PortalContext portalContext = portletRequest.getPortalContext();
                    String createRenderUrlDuringActionPhaseSupport = portalContext.getProperty(BridgePortalContext.CREATE_RENDER_URL_DURING_ACTION_PHASE_SUPPORT);
                    if ((portletPhase == Bridge.PortletPhase.ACTION_PHASE) && (createRenderUrlDuringActionPhaseSupport != null)) {
                        // Redirect to the targeted view.
                        // TCK: redirectActionTest (Liferay Portal)
                        BridgeURL bridgeRedirectURL = bridgeURLFactory.getBridgeRedirectURL(facesContext, bridgeURI.toString(), null);
                        ActionResponse actionResponse = (ActionResponse) portletResponse;
                        actionResponse.sendRedirect(bridgeRedirectURL.toString());
                    } else // redirect).
                    if ((portletPhase == Bridge.PortletPhase.ACTION_PHASE) || (portletPhase == Bridge.PortletPhase.EVENT_PHASE)) {
                        // TCK: redirectActionTest (Pluto)
                        // TCK: redirectEventTest
                        String newViewId = bridgeURI.getContextRelativePath(contextPath);
                        // If redirecting to a different view, then create the target view and place it into the
                        // FacesContext.
                        UIViewRoot viewRoot = facesContext.getViewRoot();
                        String currentFacesViewId = viewRoot.getViewId();
                        if (!currentFacesViewId.equals(newViewId)) {
                            ViewHandler viewHandler = facesContext.getApplication().getViewHandler();
                            UIViewRoot newViewRoot = viewHandler.createView(facesContext, newViewId);
                            facesContext.setViewRoot(newViewRoot);
                        }
                        // Set the "_facesViewIdRender" parameter on the URL to the new viewId so that the call
                        // to BridgeNavigationUtil.navigate(...) below will cause a render parameter to be set
                        // which will inform containers that implement POST-REDIRECT-GET (like Pluto) that the
                        // 302 redirect URL needs to specify the new viewId in order for redirection to work in
                        // the subsequent RENDER_PHASE.
                        bridgeURI.setParameter(bridgeConfig.getViewIdRenderParameterName(), newViewId);
                        // Update the PartialViewContext.
                        partialViewContextRenderAll(facesContext);
                        // Set the response as "complete" in the FacesContext.
                        facesContext.responseComplete();
                        // occurred which means that the request attributes should not be preserved.
                        if (bridgeRequestScope != null) {
                            bridgeRequestScope.setRedirectOccurred(true);
                        }
                        // Apply the PortletMode, WindowState, etc. that may be present in the URL to the response.
                        try {
                            StateAwareResponse stateAwareResponse = (StateAwareResponse) portletResponse;
                            BridgeNavigationUtil.navigate(portletRequest, stateAwareResponse, bridgeURI.getParameterMap());
                        } catch (PortletException e) {
                            logger.error(e.getMessage());
                        }
                    } else // lifecycle, then
                    if (isHeaderPhase(portletPhase) || (portletPhase == Bridge.PortletPhase.RENDER_PHASE)) {
                        // If the specified URL is for a JSF viewId, then prepare for a render-redirect.
                        BridgeURL bridgeRedirectURL = bridgeURLFactory.getBridgeRedirectURL(facesContext, url, null);
                        String redirectURLViewId = bridgeRedirectURL.getViewId();
                        if (redirectURLViewId != null) {
                            // TCK: renderRedirectTest
                            // TCK: redirectRenderTest
                            // TCK: redirectRenderPRP2Test
                            portletRequest.setAttribute(BridgeExt.RENDER_REDIRECT, Boolean.TRUE);
                            portletRequest.setAttribute(BridgeExt.RENDER_REDIRECT_VIEW_ID, redirectURLViewId);
                        } else // Otherwise,
                        {
                            // If there is a URL parameter specifying a JSF viewId, then prepare for a
                            // render-redirect.
                            String viewIdRenderParameterName = bridgeConfig.getViewIdRenderParameterName();
                            String viewIdRenderParameterValue = bridgeRedirectURL.getParameter(viewIdRenderParameterName);
                            // FACES-2978: Support render-redirect.
                            if (viewIdRenderParameterValue == null) {
                                Map<String, Object> requestMap = externalContext.getRequestMap();
                                String requestMapKey = Bridge.VIEW_ID + url;
                                viewIdRenderParameterValue = (String) requestMap.remove(requestMapKey);
                            }
                            if (viewIdRenderParameterValue != null) {
                                // TCK: redirectRenderPRP1Test
                                portletRequest.setAttribute(BridgeExt.RENDER_REDIRECT, Boolean.TRUE);
                                portletRequest.setAttribute(BridgeExt.RENDER_REDIRECT_VIEW_ID, viewIdRenderParameterValue);
                            } else // Otherwise, throw an IllegalStateException according to Section 6.1.3.1 of the Spec.
                            {
                                throw new IllegalStateException("6.1.3.1: Unable to redirect to a non-Faces view during the RENDER_PHASE.");
                            }
                        }
                    }
                }
            } catch (URISyntaxException e) {
                logger.error(e);
            }
        } else // Otherwise, since executing the RESOURCE_PHASE of the portlet lifecycle:
        {
            // NOTE: The Bridge Spec indicates that the redirect is to be ignored, but JSF 2 has the ability to
            // redirect during Ajax.
            FacesContext facesContext = FacesContext.getCurrentInstance();
            if (isJSF2PartialRequest(facesContext)) {
                try {
                    redirectJSF2PartialResponse(facesContext, (ResourceResponse) portletResponse, url);
                } catch (Exception e) {
                    logger.error(e);
                    throw new IOException(e.getMessage());
                }
            } else {
                throw new UnsupportedEncodingException("Can only redirect during RESOURCE_PHASE if a JSF partial/Ajax request has been triggered");
            }
        }
    } else {
        logger.error("redirect url=null");
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) PortletException(javax.portlet.PortletException) ViewHandler(javax.faces.application.ViewHandler) UnsupportedEncodingException(java.io.UnsupportedEncodingException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ActionResponse(javax.portlet.ActionResponse) BridgeDefaultViewNotSpecifiedException(javax.portlet.faces.BridgeDefaultViewNotSpecifiedException) URISyntaxException(java.net.URISyntaxException) BridgeException(javax.portlet.faces.BridgeException) FacesException(javax.faces.FacesException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) BridgeInvalidViewPathException(javax.portlet.faces.BridgeInvalidViewPathException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) PortletException(javax.portlet.PortletException) StateAwareResponse(javax.portlet.StateAwareResponse) PortletRequest(javax.portlet.PortletRequest) ResponseWriter(javax.faces.context.ResponseWriter) ExternalContext(javax.faces.context.ExternalContext) PortalContext(javax.portlet.PortalContext) BridgePortalContext(com.liferay.faces.bridge.context.BridgePortalContext) FacesURLEncoder(com.liferay.faces.util.render.FacesURLEncoder) BridgeURI(com.liferay.faces.bridge.internal.BridgeURI) UIViewRoot(javax.faces.component.UIViewRoot) BridgeURL(javax.portlet.faces.BridgeURL)

Example 9 with PortalContext

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

the class ContextMapFactoryImpl method getFacesRequestParameterMap.

protected FacesRequestParameterMap getFacesRequestParameterMap(PortletRequest portletRequest, String responseNamespace, PortletConfig portletConfig, BridgeRequestScope bridgeRequestScope, String defaultRenderKitId, String facesViewQueryString) {
    FacesRequestParameterMap facesRequestParameterMap = null;
    Map<String, String> facesViewParameterMap = getFacesViewParameterMap(facesViewQueryString);
    PortalContext portalContext = portletRequest.getPortalContext();
    boolean strictParameterNamespacingSupported = FacesRuntimeUtil.isStrictParameterNamespacingSupported(portalContext);
    PortletContext portletContext = portletConfig.getPortletContext();
    boolean namespaceViewState = strictParameterNamespacingSupported && FacesRuntimeUtil.isNamespaceViewState(strictParameterNamespacingSupported, portletContext);
    if (portletRequest instanceof ClientDataRequest) {
        ClientDataRequest clientDataRequest = (ClientDataRequest) portletRequest;
        String contentType = clientDataRequest.getContentType();
        // Note: af:inputFile (ADF Faces) ace:fileEntry (ICEfaces) rely on their own mechanisms for handling file
        // uploads.
        ProductFactory productFactory = (ProductFactory) BridgeFactoryFinder.getFactory(portletContext, ProductFactory.class);
        Product ADF_FACES_RICH_CLIENT = productFactory.getProductInfo(Product.Name.ADF_FACES);
        Product ICEFACES = productFactory.getProductInfo(Product.Name.ICEFACES);
        if (!ADF_FACES_RICH_CLIENT.isDetected() && !ICEFACES.isDetected() && (contentType != null) && contentType.toLowerCase().startsWith("multipart/")) {
            MultiPartFormData multiPartFormData = (MultiPartFormData) portletRequest.getAttribute(MULTIPART_FORM_DATA_FQCN);
            if (multiPartFormData == null) {
                facesRequestParameterMap = new FacesRequestParameterMapImpl(responseNamespace, bridgeRequestScope, facesViewParameterMap, defaultRenderKitId, getSeparatorChar(), strictParameterNamespacingSupported, namespaceViewState);
                MultiPartFormDataProcessor multiPartFormDataProcessor = new MultiPartFormDataProcessorImpl();
                Map<String, List<com.liferay.faces.util.model.UploadedFile>> uploadedFileMap = multiPartFormDataProcessor.process(clientDataRequest, portletConfig, facesRequestParameterMap);
                multiPartFormData = new MultiPartFormDataImpl(facesRequestParameterMap, uploadedFileMap);
                // Save the multipart/form-data in a request attribute so that it can be referenced later-on in the
                // JSF lifecycle by file upload component renderers.
                portletRequest.setAttribute(MULTIPART_FORM_DATA_FQCN, multiPartFormData);
            } else {
                facesRequestParameterMap = multiPartFormData.getFacesRequestParameterMap();
            }
        }
    }
    if (facesRequestParameterMap == null) {
        Map<String, String[]> parameterMap = portletRequest.getParameterMap();
        facesRequestParameterMap = new FacesRequestParameterMapImpl(parameterMap, responseNamespace, bridgeRequestScope, facesViewParameterMap, defaultRenderKitId, getSeparatorChar(), strictParameterNamespacingSupported, namespaceViewState);
    }
    return facesRequestParameterMap;
}
Also used : FacesRequestParameterMap(com.liferay.faces.util.context.map.FacesRequestParameterMap) ClientDataRequest(javax.portlet.ClientDataRequest) ProductFactory(com.liferay.faces.util.product.ProductFactory) Product(com.liferay.faces.util.product.Product) PortalContext(javax.portlet.PortalContext) PortletContext(javax.portlet.PortletContext) MultiPartFormData(com.liferay.faces.util.context.map.MultiPartFormData) ArrayList(java.util.ArrayList) List(java.util.List)

Example 10 with PortalContext

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

the class HeadRendererBridgeImpl method encodeChildren.

@Override
public void encodeChildren(FacesContext facesContext, UIComponent uiComponent) throws IOException {
    // Build up a list of components that are intended for the <head> section of the portal page.
    UIViewRoot uiViewRoot = facesContext.getViewRoot();
    List<UIComponent> headResources = new ArrayList<UIComponent>();
    // Add the list of components that are to appear first.
    List<UIComponent> firstResources = getFirstResources(facesContext, uiComponent);
    if (firstResources != null) {
        headResources.addAll(firstResources);
    }
    // Sort the components that are in the view root into stylesheets, scripts, and other.
    List<UIComponent> headComponentResources = uiViewRoot.getComponentResources(facesContext, "head");
    ExternalContext externalContext = facesContext.getExternalContext();
    final Product BOOTSFACES = ProductFactory.getProductInstance(externalContext, Product.Name.BOOTSFACES);
    final boolean BOOTSFACES_DETECTED = BOOTSFACES.isDetected();
    List<UIComponent> styleSheetResources = new ArrayList<UIComponent>();
    List<UIComponent> scriptResources = new ArrayList<UIComponent>();
    List<UIComponent> otherHeadResources = new ArrayList<UIComponent>();
    for (UIComponent headComponentResource : headComponentResources) {
        if (RenderKitUtil.isStyleSheetResource(headComponentResource, BOOTSFACES_DETECTED) || isInlineStyleSheet(headComponentResource)) {
            styleSheetResources.add(headComponentResource);
        } else if (RenderKitUtil.isScriptResource(headComponentResource, BOOTSFACES_DETECTED) || isInlineScript(headComponentResource)) {
            scriptResources.add(headComponentResource);
        } else {
            // Other head resources include <base>, <meta>, and <noscript> elments as well as passthrough <link>,
            // <style>, and <script> elements.
            otherHeadResources.add(headComponentResource);
        }
    }
    // Sort children into stylesheets, scripts, and other.
    List<UIComponent> children = uiComponent.getChildren();
    for (UIComponent child : children) {
        if (RenderKitUtil.isStyleSheetResource(child, BOOTSFACES_DETECTED) || isInlineStyleSheet(child)) {
            styleSheetResources.add(child);
        } else if (RenderKitUtil.isScriptResource(child, BOOTSFACES_DETECTED) || isInlineScript(child)) {
            scriptResources.add(child);
        } else {
            // Other head resources include <base>, <meta>, and <noscript> elments as well as passthrough <link>,
            // <style>, and <script> elements.
            otherHeadResources.add(child);
        }
    }
    if (!otherHeadResources.isEmpty()) {
        headResources.addAll(otherHeadResources);
    }
    // Add the list of stylesheet components that are in the view root.
    if (!styleSheetResources.isEmpty()) {
        headResources.addAll(styleSheetResources);
    }
    // Add the list of components that are to appear in the middle.
    List<UIComponent> middleResources = getMiddleResources(facesContext, uiComponent);
    if (middleResources != null) {
        headResources.addAll(middleResources);
    }
    // Add the list of script components that are in the view root.
    if (!scriptResources.isEmpty()) {
        headResources.addAll(scriptResources);
    }
    // Add the list of components that are to appear last.
    List<UIComponent> lastResources = getLastResources(facesContext, uiComponent);
    if (lastResources != null) {
        headResources.addAll(lastResources);
    }
    List<UIComponent> headResourcesToRenderInBody = new ArrayList<UIComponent>();
    PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
    PortalContext portalContext = portletRequest.getPortalContext();
    Iterator<UIComponent> iterator = headResources.iterator();
    while (iterator.hasNext()) {
        UIComponent headResource = iterator.next();
        // portal page, then
        if (!ableToAddResourceToHead(portalContext, headResource, BOOTSFACES_DETECTED)) {
            // Add it to the list of resources that are to be rendered in the body section by the body renderer.
            headResourcesToRenderInBody.add(headResource);
            // Remove it from the list of resources that are to be rendered in the head section by this renderer.
            iterator.remove();
            if (logger.isDebugEnabled()) {
                Map<String, Object> componentResourceAttributes = headResource.getAttributes();
                logger.debug("Relocating resource to body: name=[{0}] library=[{1}] rendererType=[{2}] value=[{3}] className=[{4}]", componentResourceAttributes.get("name"), componentResourceAttributes.get("library"), headResource.getRendererType(), ComponentUtil.getComponentValue(headResource), headResource.getClass().getName());
            }
        }
    }
    // Save the list of resources that are to be rendered in the body section so that the body renderer can find it.
    Map<Object, Object> facesContextAttributes = facesContext.getAttributes();
    facesContextAttributes.put(RenderKitUtil.HEAD_RESOURCES_TO_RENDER_IN_BODY, headResourcesToRenderInBody);
    if (!headResources.isEmpty()) {
        // Save a temporary reference to the ResponseWriter provided by the FacesContext.
        ResponseWriter responseWriterBackup = facesContext.getResponseWriter();
        // Replace the ResponseWriter in the FacesContext with a HeadResponseWriter that knows how to write to
        // the <head>...</head> section of the rendered portal page.
        ResponseWriter headResponseWriter = (ResponseWriter) portletRequest.getAttribute("com.liferay.faces.bridge.HeadResponseWriter");
        if (headResponseWriter == null) {
            PortletResponse portletResponse = (PortletResponse) externalContext.getResponse();
            PortletContext portletContext = (PortletContext) externalContext.getContext();
            headResponseWriter = HeadResponseWriterFactory.getHeadResponseWriterInstance(responseWriterBackup, portletContext, portletResponse);
        }
        portletRequest.setAttribute("com.liferay.faces.bridge.HeadResponseWriter", headResponseWriter);
        facesContext.setResponseWriter(headResponseWriter);
        Set<String> headResourceIds = RenderKitUtil.getHeadResourceIds(facesContext);
        for (UIComponent headResource : headResources) {
            headResource.encodeAll(facesContext);
            if (RenderKitUtil.isScriptResource(headResource, BOOTSFACES_DETECTED) || RenderKitUtil.isStyleSheetResource(headResource, BOOTSFACES_DETECTED)) {
                headResourceIds.add(ResourceUtil.getResourceId(headResource));
            }
        }
        // Restore the temporary ResponseWriter reference.
        facesContext.setResponseWriter(responseWriterBackup);
    }
}
Also used : PortletResponse(javax.portlet.PortletResponse) UIComponent(javax.faces.component.UIComponent) ArrayList(java.util.ArrayList) Product(com.liferay.faces.util.product.Product) PortletRequest(javax.portlet.PortletRequest) ResponseWriter(javax.faces.context.ResponseWriter) ExternalContext(javax.faces.context.ExternalContext) PortalContext(javax.portlet.PortalContext) BridgePortalContext(com.liferay.faces.bridge.context.BridgePortalContext) PortletContext(javax.portlet.PortletContext) UIViewRoot(javax.faces.component.UIViewRoot)

Aggregations

PortalContext (javax.portlet.PortalContext)13 PortalContextBridgeLiferayImpl (com.liferay.faces.bridge.ext.context.internal.PortalContextBridgeLiferayImpl)5 BridgePortalContext (com.liferay.faces.bridge.context.BridgePortalContext)4 ExternalContext (javax.faces.context.ExternalContext)4 PortletRequest (javax.portlet.PortletRequest)4 UIViewRoot (javax.faces.component.UIViewRoot)3 PortalContextBridgeImpl (com.liferay.faces.bridge.context.internal.PortalContextBridgeImpl)2 Product (com.liferay.faces.util.product.Product)2 ArrayList (java.util.ArrayList)2 FacesContext (javax.faces.context.FacesContext)2 ResponseWriter (javax.faces.context.ResponseWriter)2 PortletContext (javax.portlet.PortletContext)2 Test (org.junit.Test)2 BridgeURI (com.liferay.faces.bridge.internal.BridgeURI)1 BridgeTest (com.liferay.faces.bridge.tck.annotation.BridgeTest)1 AnnotatedExcludedBean (com.liferay.faces.bridge.tck.beans.AnnotatedExcludedBean)1 FacesRequestParameterMap (com.liferay.faces.util.context.map.FacesRequestParameterMap)1 MultiPartFormData (com.liferay.faces.util.context.map.MultiPartFormData)1 ProductFactory (com.liferay.faces.util.product.ProductFactory)1 FacesURLEncoder (com.liferay.faces.util.render.FacesURLEncoder)1