Search in sources :

Example 1 with ResourceComponent

use of com.liferay.faces.bridge.component.internal.ResourceComponent in project liferay-faces-bridge-impl by liferay.

the class HeadRendererICEfacesImpl method getFirstResources.

@Override
protected List<UIComponent> getFirstResources(FacesContext facesContext, UIComponent uiComponent) {
    List<UIComponent> resources = super.getFirstResources(facesContext, uiComponent);
    // ICEfaces Theme
    ExternalContext externalContext = facesContext.getExternalContext();
    String iceFacesThemeName = externalContext.getInitParameter(ICEFACES_THEME_PARAM);
    if (iceFacesThemeName != null) {
        ELContext elContext = facesContext.getELContext();
        ExpressionFactory expressionFactory = facesContext.getApplication().getExpressionFactory();
        ValueExpression valueExpression = expressionFactory.createValueExpression(elContext, iceFacesThemeName, String.class);
        iceFacesThemeName = (String) valueExpression.getValue(elContext);
    } else {
        iceFacesThemeName = ICEFACES_THEME_DEFAULT;
    }
    if ((iceFacesThemeName != null) && !iceFacesThemeName.equals(ICEFACES_THEME_NONE)) {
        if (resources == null) {
            resources = new ArrayList<UIComponent>();
        }
        String resourceName = ICEFACES_THEME_RESOURCE_NAME;
        String resourceLibrary = ICEFACES_THEME_PREFIX + iceFacesThemeName;
        if (iceFacesThemeName.equals(ICEFACES_THEME_NAME_SAM) || iceFacesThemeName.equals(ICEFACES_THEME_NAME_RIME)) {
            StringBuilder buf = new StringBuilder();
            buf.append(ICEFACES_THEME_DIR);
            buf.append("/");
            buf.append(iceFacesThemeName);
            buf.append("/");
            buf.append(ICEFACES_THEME_RESOURCE_NAME);
            resourceName = buf.toString();
            resourceLibrary = ICEFACES_LIBRARY_NAME_ACE;
        }
        ResourceComponent iceFacesStyleSheet = new ResourceComponent(facesContext, resourceName, resourceLibrary, "head");
        resources.add(iceFacesStyleSheet);
    }
    return resources;
}
Also used : ELContext(javax.el.ELContext) ExpressionFactory(javax.el.ExpressionFactory) ResourceComponent(com.liferay.faces.bridge.component.internal.ResourceComponent) ExternalContext(javax.faces.context.ExternalContext) ValueExpression(javax.el.ValueExpression) UIComponent(javax.faces.component.UIComponent)

Example 2 with ResourceComponent

use of com.liferay.faces.bridge.component.internal.ResourceComponent in project liferay-faces-bridge-impl by liferay.

the class HeadRendererPrimeFacesImpl method getFirstResources.

@Override
protected List<UIComponent> getFirstResources(FacesContext facesContext, UIComponent uiComponent) {
    List<UIComponent> firstResources = super.getFirstResources(facesContext, uiComponent);
    if (firstResources == null) {
        firstResources = new ArrayList<UIComponent>();
    }
    Map<Object, Object> facesContextAttributes = facesContext.getAttributes();
    ResourceComponent primefacesThemeResource = (ResourceComponent) facesContextAttributes.remove("primefacesTheme");
    if (primefacesThemeResource != null) {
        firstResources.add(primefacesThemeResource);
    }
    return firstResources;
}
Also used : ResourceComponent(com.liferay.faces.bridge.component.internal.ResourceComponent) UIComponent(javax.faces.component.UIComponent)

Example 3 with ResourceComponent

use of com.liferay.faces.bridge.component.internal.ResourceComponent in project liferay-faces-bridge-impl by liferay.

the class HeadRendererPrimeFacesImpl method encodeBegin.

@Override
public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) throws IOException {
    UIViewRoot originalUIViewRoot = facesContext.getViewRoot();
    if (isMobile(facesContext)) {
        List<UIComponent> componentResources = originalUIViewRoot.getComponentResources(facesContext, "head");
        List<UIComponent> resourcesToRemove = new ArrayList<UIComponent>();
        for (UIComponent componentResource : componentResources) {
            // https://github.com/primefaces/primefaces/blob/6_0/src/main/java/org/primefaces/mobile/renderkit/HeadRenderer.java#L96-L104.
            if (isComponentResourceSuppressedWhenMobile(componentResource)) {
                componentResource.setRendered(false);
            } else // https://github.com/primefaces/primefaces/blob/6_0/src/main/java/org/primefaces/mobile/renderkit/HeadRenderer.java#L68-L87.
            if (isMobileComponentResource(componentResource)) {
                resourcesToRemove.add(componentResource);
            }
        }
        for (UIComponent resourceToRemove : resourcesToRemove) {
            originalUIViewRoot.removeComponentResource(facesContext, resourceToRemove, "head");
        }
    }
    // Invoke the PrimeFaces HeadRenderer so that it has the opportunity to add css and/or script resources to the
    // view root. However, the PrimeFaces HeadRenderer must be captured (and thus prevented from actually rendering
    // any resources) so that they can instead be rendered by the superclass.
    FacesContext primeFacesContext = new FacesContextPrimeFacesHeadImpl(facesContext);
    ResponseWriter origResponseWriter = primeFacesContext.getResponseWriter();
    PrimeFacesHeadResponseWriter primeFacesHeadResponseWriter = new PrimeFacesHeadResponseWriter();
    primeFacesContext.setResponseWriter(primeFacesHeadResponseWriter);
    ResourceCapturingUIViewRoot resourceCapturingUIViewRoot = new ResourceCapturingUIViewRoot();
    primeFacesContext.setViewRoot(resourceCapturingUIViewRoot);
    Renderer primeFacesHeadRenderer = getPrimeFacesHeadRenderer(facesContext);
    primeFacesHeadRenderer.encodeBegin(primeFacesContext, uiComponent);
    primeFacesContext.setViewRoot(originalUIViewRoot);
    primeFacesContext.setResponseWriter(origResponseWriter);
    // Get the list of captured resources.
    List<UIComponent> capturedResources = resourceCapturingUIViewRoot.getCapturedComponentResources("head");
    List<UIComponent> capturedMobileResources = new ArrayList<UIComponent>();
    // The PrimeFaces 5.1+ HeadRenderer properly adds resources like "validation/validation.js" to the view root,
    // which makes it possible to easily capture the resources that it wants to add to the head. However, the
    // PrimeFaces 5.0/4.0 HeadRenderer does not add resources to the view root. Instead, it encodes a <script>
    // element to the response writer with a "src" attribute containing a URL (an external script). When this
    // occurs, it is necessary to reverse-engineer the URL of each external script in order to determine the
    // name/library of the corresponding JSF2 resource.
    List<String> externalResourceURLs = primeFacesHeadResponseWriter.getExternalResourceURLs();
    // For each external script URL:
    if (externalResourceURLs.size() > 0) {
        ExternalContext externalContext = facesContext.getExternalContext();
        String resourceNameParam = externalContext.encodeNamespace("javax.faces.resource");
        String libraryNameParam = externalContext.encodeNamespace("ln");
        for (String externalResourceURL : externalResourceURLs) {
            // Determine the value of the "javax.faces.resource" and "ln" parameters from the URL.
            String resourceName = null;
            String libraryName = null;
            ResponseWriter responseWriter = facesContext.getResponseWriter();
            String characterEncoding = responseWriter.getCharacterEncoding();
            Map<String, String[]> parsedParameterMapValuesArray = URLUtil.parseParameterMapValuesArray(externalResourceURL, characterEncoding);
            if (parsedParameterMapValuesArray != null) {
                String[] resourceNameParamValues = parsedParameterMapValuesArray.get(resourceNameParam);
                if ((resourceNameParamValues == null) || (resourceNameParamValues.length < 1)) {
                    resourceNameParamValues = parsedParameterMapValuesArray.get("javax.faces.resource");
                }
                if ((resourceNameParamValues != null) && (resourceNameParamValues.length > 0)) {
                    resourceName = resourceNameParamValues[0];
                }
                if (resourceName == null) {
                    int indexOfResource = externalResourceURL.indexOf("javax.faces.resource/");
                    int indexOfQuery = externalResourceURL.indexOf("?");
                    if (indexOfResource > -1) {
                        int indexOfResourceName = indexOfResource + "javax.faces.resource/".length();
                        if (indexOfQuery > -1) {
                            resourceName = externalResourceURL.substring(indexOfResourceName, indexOfQuery);
                        } else {
                            resourceName = externalResourceURL.substring(indexOfResourceName);
                        }
                    }
                }
                String[] libraryNameParamValues = parsedParameterMapValuesArray.get(libraryNameParam);
                if ((libraryNameParamValues == null) || (libraryNameParamValues.length < 1)) {
                    libraryNameParamValues = parsedParameterMapValuesArray.get("ln");
                }
                if ((libraryNameParamValues != null) && (libraryNameParamValues.length > 0)) {
                    libraryName = libraryNameParamValues[0];
                }
            }
            // resource and add it to the view root.
            if ((resourceName != null) && (libraryName != null)) {
                if (resourceName.equals(PRIMEFACES_THEME_RESOURCE_NAME) && libraryName.startsWith(PRIMEFACES_THEME_PREFIX)) {
                    ResourceComponent primefacesThemeResource = new ResourceComponent(facesContext, resourceName, libraryName, externalContext.encodeNamespace(""));
                    Map<Object, Object> facesContextAttributes = facesContext.getAttributes();
                    facesContextAttributes.put("primefacesTheme", primefacesThemeResource);
                } else {
                    Application application = facesContext.getApplication();
                    ResourceHandler resourceHandler = application.getResourceHandler();
                    UIComponent resource = application.createComponent(UIOutput.COMPONENT_TYPE);
                    String rendererType = resourceHandler.getRendererTypeForResourceName(resourceName);
                    resource.setRendererType(rendererType);
                    resource.setTransient(true);
                    resource.getAttributes().put("name", resourceName);
                    resource.getAttributes().put("library", libraryName);
                    resource.getAttributes().put("target", "head");
                    if (isMobile(facesContext)) {
                        if (isMobileComponentResource(resourceName, libraryName)) {
                            capturedMobileResources.add(resource);
                        } else {
                            if (isComponentResourceSuppressedWhenMobile(resourceName, libraryName)) {
                                resource.setRendered(false);
                            }
                            capturedResources.add(resource);
                        }
                    } else {
                        capturedResources.add(resource);
                    }
                }
            }
        }
    }
    // superclass.
    for (UIComponent componentResource : capturedResources) {
        originalUIViewRoot.addComponentResource(facesContext, componentResource, "head");
    }
    // FACES-2061: If the PrimeFaces HeadRenderer attempted to render an inline script (as is the case when
    // PrimeFaces client side validation is activated) then add a component that can render the script to the view
    // root.
    List<InlineScript> inlineScripts = primeFacesHeadResponseWriter.getInlineScripts();
    if (!inlineScripts.isEmpty()) {
        // https://github.com/primefaces/primefaces/blob/6_0/src/main/java/org/primefaces/mobile/renderkit/HeadRenderer.java#L68-L87.
        if (isMobile(facesContext)) {
            InlineScript mobileInlineScript = inlineScripts.remove(0);
            ListIterator<UIComponent> listIterator = capturedMobileResources.listIterator();
            while (listIterator.hasNext()) {
                UIComponent mobileComponentResource = listIterator.next();
                Map<String, Object> attributes = mobileComponentResource.getAttributes();
                String name = (String) attributes.get("name");
                if (name.equals("jquery/jquery.js")) {
                    listIterator.add(mobileInlineScript);
                    break;
                }
            }
        }
        for (InlineScript primeFacesInlineScript : inlineScripts) {
            originalUIViewRoot.addComponentResource(facesContext, primeFacesInlineScript, "head");
        }
    }
    if (isMobile(facesContext)) {
        // Save captured mobile resources so that they can be rendered in as middle resources before other scripts.
        // For more information, see HeadRendererBridgeImpl.encodeChildren(),
        // http://demos.jquerymobile.com/1.0/docs/api/globalconfig.html, and
        // https://github.com/primefaces/primefaces/blob/6_0/src/main/java/org/primefaces/mobile/renderkit/HeadRenderer.java#L68-L87.
        Map<Object, Object> attributes = facesContext.getAttributes();
        attributes.put(MOBILE_COMPONENT_RESOURCES_KEY, capturedMobileResources);
    }
    // Delegate rendering to the superclass so that it can write resources found in the view root to the head
    // section of the portal page.
    super.encodeBegin(facesContext, uiComponent);
}
Also used : FacesContext(javax.faces.context.FacesContext) ArrayList(java.util.ArrayList) ResourceHandler(javax.faces.application.ResourceHandler) ExternalContext(javax.faces.context.ExternalContext) UIViewRoot(javax.faces.component.UIViewRoot) UIComponent(javax.faces.component.UIComponent) ResponseWriter(javax.faces.context.ResponseWriter) ResourceComponent(com.liferay.faces.bridge.component.internal.ResourceComponent) Renderer(javax.faces.render.Renderer) Application(javax.faces.application.Application) InlineScript(com.liferay.faces.bridge.renderkit.html_basic.internal.InlineScript)

Aggregations

ResourceComponent (com.liferay.faces.bridge.component.internal.ResourceComponent)3 UIComponent (javax.faces.component.UIComponent)3 ExternalContext (javax.faces.context.ExternalContext)2 InlineScript (com.liferay.faces.bridge.renderkit.html_basic.internal.InlineScript)1 ArrayList (java.util.ArrayList)1 ELContext (javax.el.ELContext)1 ExpressionFactory (javax.el.ExpressionFactory)1 ValueExpression (javax.el.ValueExpression)1 Application (javax.faces.application.Application)1 ResourceHandler (javax.faces.application.ResourceHandler)1 UIViewRoot (javax.faces.component.UIViewRoot)1 FacesContext (javax.faces.context.FacesContext)1 ResponseWriter (javax.faces.context.ResponseWriter)1 Renderer (javax.faces.render.Renderer)1