Search in sources :

Example 1 with PortletNamingContainerUIViewRoot

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

the class BodyRendererBridgeImpl method encodeBegin.

/**
 * It is forbidden for a portlet to render the &<body&> element, so instead, render a
 * &<div&>element.
 *
 * @see  Renderer#encodeBegin(FacesContext, UIComponent)
 */
@Override
@SuppressWarnings("unchecked")
public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) throws IOException {
    ResponseWriter originalResponseWriter = facesContext.getResponseWriter();
    ResponseWriter responseWriter = new ResponseWriterBridgeBodyImpl(originalResponseWriter);
    facesContext.setResponseWriter(responseWriter);
    super.encodeBegin(facesContext, uiComponent);
    PortletNamingContainerUIViewRoot viewRoot = (PortletNamingContainerUIViewRoot) facesContext.getViewRoot();
    String clientId = viewRoot.getContainerClientId(facesContext);
    responseWriter.writeAttribute("id", clientId, null);
    String styleClass = (String) uiComponent.getAttributes().get("styleClass");
    // ResponseWriterBridgeBodyImpl will append STYLE_CLASS_PORTLET_BODY to the specified styleClass.
    if (styleClass == null) {
        responseWriter.writeAttribute("class", RenderKitUtil.STYLE_CLASS_PORTLET_BODY, "styleClass");
    }
    // Render each of the head resources that were not renderable in the head section into the top of the portlet
    // body (the outer <div> of the portlet markup). This happens when the portlet container may not support adding
    // resources to the <head> section. For example, Pluto does not support the feature of adding resources to the
    // <head> section. Liferay supports it with the exception of "runtime" and WSRP portlets. See
    // PortalContextBridgeImpl and PortalContextBridgeLiferayImpl (in bridge-ext) for more information. Script
    // resources are rendered at the top of the portlet body instead of the bottom because elements and scripts in
    // the portlet body may depend on these resources (for example jquery.js), so head resource scripts must be
    // loaded before the portlet body is rendered.
    Map<Object, Object> facesContextAttributes = facesContext.getAttributes();
    List<UIComponent> headResourcesToRenderInBody = (List<UIComponent>) facesContextAttributes.get(RenderKitUtil.HEAD_RESOURCES_TO_RENDER_IN_BODY);
    Set<String> headResourceIds = RenderKitUtil.getHeadResourceIds(facesContext);
    // https://html.spec.whatwg.org/multipage/semantics.html#the-link-element
    if (headResourcesToRenderInBody != null) {
        ExternalContext externalContext = facesContext.getExternalContext();
        final Product BOOTSFACES = ProductFactory.getProductInstance(externalContext, Product.Name.BOOTSFACES);
        final boolean BOOTSFACES_DETECTED = BOOTSFACES.isDetected();
        for (UIComponent headResource : headResourcesToRenderInBody) {
            headResource.encodeAll(facesContext);
            // tracked as if they were rendered in the <head> section so that they are not loaded multiple times.
            if (RenderKitUtil.isScriptResource(headResource, BOOTSFACES_DETECTED)) {
                headResourceIds.add(ResourceUtil.getResourceId(headResource));
            }
        }
    }
    facesContext.setResponseWriter(originalResponseWriter);
}
Also used : UIComponent(javax.faces.component.UIComponent) Product(com.liferay.faces.util.product.Product) ResponseWriter(javax.faces.context.ResponseWriter) ExternalContext(javax.faces.context.ExternalContext) List(java.util.List) PortletNamingContainerUIViewRoot(javax.portlet.faces.component.PortletNamingContainerUIViewRoot)

Example 2 with PortletNamingContainerUIViewRoot

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

the class Tests method portletNamingContainerUIViewRootClientIdTest.

// Test is SingleRequest -- Render
// Test #6.96
@BridgeTest(test = "portletNamingContainerUIViewRootClientIdTest")
public String portletNamingContainerUIViewRootClientIdTest(TestBean testBean) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    String namespace = ((MimeResponse) facesContext.getExternalContext().getResponse()).getNamespace();
    testBean.setTestComplete(true);
    try {
        PortletNamingContainerUIViewRoot vr = new PortletNamingContainerUIViewRoot();
        // ensure it has an id
        vr.setId("nc");
        if (vr.getContainerClientId(facesContext).indexOf(namespace) >= 0) {
            testBean.setTestResult(true, "class PortletNamingContainerUIViewRoot correctly encodes portlet namespace id in the result from getContainerClientId");
            return Constants.TEST_SUCCESS;
        } else {
            testBean.setTestResult(false, "class PortletNamingContainerUIViewRoot doesn't encodes portlet namespace id in the result from getContainerClientId. namespaceId: " + namespace + " getContainerClientId returned: " + vr.getContainerClientId(facesContext));
            return Constants.TEST_FAILED;
        }
    } catch (Exception e) {
        testBean.setTestResult(false, "class PortletNamingContainerUIViewRoot doesn't exist or can't be instantiated. Received exception: " + e.getMessage());
        return Constants.TEST_FAILED;
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) MimeResponse(javax.portlet.MimeResponse) PortletNamingContainerUIViewRoot(javax.portlet.faces.component.PortletNamingContainerUIViewRoot) BridgeTest(com.liferay.faces.bridge.tck.annotation.BridgeTest)

Aggregations

PortletNamingContainerUIViewRoot (javax.portlet.faces.component.PortletNamingContainerUIViewRoot)2 BridgeTest (com.liferay.faces.bridge.tck.annotation.BridgeTest)1 Product (com.liferay.faces.util.product.Product)1 List (java.util.List)1 UIComponent (javax.faces.component.UIComponent)1 ExternalContext (javax.faces.context.ExternalContext)1 FacesContext (javax.faces.context.FacesContext)1 ResponseWriter (javax.faces.context.ResponseWriter)1 MimeResponse (javax.portlet.MimeResponse)1