Search in sources :

Example 11 with ResourceHandler

use of javax.faces.application.ResourceHandler 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)

Example 12 with ResourceHandler

use of javax.faces.application.ResourceHandler in project liferay-faces-alloy by liferay.

the class VideoRenderer method encodeCustomMediaAttributes.

@Override
protected void encodeCustomMediaAttributes(FacesContext facesContext, ResponseWriter responseWriter, Media media) throws IOException {
    Video video = (Video) media;
    Object poster = video.getPoster();
    if (poster != null) {
        Application application = facesContext.getApplication();
        ResourceHandler resourceHandler = application.getResourceHandler();
        String posterResourceURL = getEncodedResourceURL(facesContext, resourceHandler, application, poster);
        responseWriter.writeAttribute(POSTER, posterResourceURL, POSTER);
    }
}
Also used : Video(com.liferay.faces.alloy.component.video.Video) ResourceHandler(javax.faces.application.ResourceHandler) Application(javax.faces.application.Application)

Example 13 with ResourceHandler

use of javax.faces.application.ResourceHandler in project liferay-faces-alloy by liferay.

the class FilteredResourceExpressionImpl method filter.

@Override
protected String filter(String text) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ResourceHandler resourceHandlerChain = facesContext.getApplication().getResourceHandler();
    ExternalContext externalContext = facesContext.getExternalContext();
    String comboModulePathsDelimiter = getModulePathsDelimiter("combo", resourceHandlerChain, externalContext);
    String scriptModulePathsDelimiter = getModulePathsDelimiter("script", resourceHandlerChain, externalContext);
    text = text.replace(COMBO_MODULE_PATHS_DELIMITER, comboModulePathsDelimiter);
    text = text.replace(SCRIPT_MODULE_PATHS_DELIMITER, scriptModulePathsDelimiter);
    return ExpressionUtil.filterResourceExpressions(text, resourceHandlerChain, externalContext);
}
Also used : FacesContext(javax.faces.context.FacesContext) ExternalContext(javax.faces.context.ExternalContext) ResourceHandler(javax.faces.application.ResourceHandler)

Example 14 with ResourceHandler

use of javax.faces.application.ResourceHandler in project liferay-faces-alloy by liferay.

the class MediaRenderer method encodeChildren.

@Override
@SuppressWarnings("unchecked")
public void encodeChildren(FacesContext facesContext, UIComponent uiComponent) throws IOException {
    Media media = (Media) uiComponent;
    String name = media.getName();
    Object value = media.getValue();
    Application application = facesContext.getApplication();
    ResourceHandler resourceHandler = application.getResourceHandler();
    String firstMediaResourceURL = null;
    ResponseWriter responseWriter = facesContext.getResponseWriter();
    if (name != null) {
        String library = media.getLibrary();
        if ((name.length() > 0) && (library != null) && (library.length() > 0)) {
            Resource mediaResource = resourceHandler.createResource(name, library);
            String contentType = media.getContentType();
            if (mediaResource != null) {
                String mediaRequestPath = mediaResource.getRequestPath();
                ExternalContext externalContext = facesContext.getExternalContext();
                firstMediaResourceURL = externalContext.encodeResourceURL(mediaRequestPath);
                if (contentType == null) {
                    contentType = mediaResource.getContentType();
                }
            } else {
                firstMediaResourceURL = RES_NOT_FOUND;
                logger.error(RES_NOT_FOUND_ERROR_MSG, resourceHandler, name, library);
            }
            encodeMediaSource(responseWriter, firstMediaResourceURL, contentType);
        }
    } else if (value != null) {
        if ((value instanceof Collection) || (value instanceof FacesResource[])) {
            Collection<FacesResource> facesResources;
            if (value instanceof Collection) {
                facesResources = (Collection<FacesResource>) value;
            } else {
                FacesResource[] resourcesAsArray = (FacesResource[]) value;
                facesResources = Arrays.asList(resourcesAsArray);
            }
            for (FacesResource facesResource : facesResources) {
                String facesResourceName = facesResource.getName();
                String facesResourceLibrary = facesResource.getLibrary();
                Resource mediaResource = resourceHandler.createResource(facesResourceName, facesResourceLibrary);
                String mediaResourceURL;
                String contentType = facesResource.getContentType();
                if (mediaResource != null) {
                    String mediaRequestPath = mediaResource.getRequestPath();
                    ExternalContext externalContext = facesContext.getExternalContext();
                    mediaResourceURL = externalContext.encodeResourceURL(mediaRequestPath);
                    if (contentType == null) {
                        contentType = mediaResource.getContentType();
                    }
                } else {
                    mediaResourceURL = RES_NOT_FOUND;
                    logger.error(RES_NOT_FOUND_ERROR_MSG, resourceHandler, facesResourceName, facesResourceLibrary);
                }
                if (firstMediaResourceURL == null) {
                    firstMediaResourceURL = mediaResourceURL;
                }
                encodeMediaSource(responseWriter, mediaResourceURL, contentType);
            }
        } else {
            String contentType = media.getContentType();
            if (value instanceof FacesResource) {
                FacesResource facesResource = (FacesResource) value;
                String facesResourceName = facesResource.getName();
                String facesResourceLibrary = facesResource.getLibrary();
                Resource mediaResource = resourceHandler.createResource(facesResourceName, facesResourceLibrary);
                if (contentType == null) {
                    contentType = facesResource.getContentType();
                }
                if (mediaResource != null) {
                    String mediaRequestPath = mediaResource.getRequestPath();
                    ExternalContext externalContext = facesContext.getExternalContext();
                    firstMediaResourceURL = externalContext.encodeResourceURL(mediaRequestPath);
                    if (contentType == null) {
                        contentType = mediaResource.getContentType();
                    }
                } else {
                    firstMediaResourceURL = RES_NOT_FOUND;
                    logger.error(RES_NOT_FOUND_ERROR_MSG, resourceHandler, facesResourceName, facesResourceLibrary);
                }
            } else {
                String valueAsString = value.toString();
                firstMediaResourceURL = getEncodedResourceURL(facesContext, resourceHandler, application, valueAsString);
            }
            encodeMediaSource(responseWriter, firstMediaResourceURL, contentType);
        }
    }
    List<UIComponent> children = uiComponent.getChildren();
    for (UIComponent child : children) {
        if (child instanceof com.liferay.faces.alloy.component.resource.Resource) {
            String mediaResourceURL;
            com.liferay.faces.alloy.component.resource.Resource resource = (com.liferay.faces.alloy.component.resource.Resource) child;
            String contentType = resource.getContentType();
            String resourceName = resource.getName();
            String resourceLibrary = resource.getLibrary();
            Resource mediaResource = resourceHandler.createResource(resourceName, resourceLibrary);
            if (mediaResource != null) {
                String mediaRequestPath = mediaResource.getRequestPath();
                ExternalContext externalContext = facesContext.getExternalContext();
                mediaResourceURL = externalContext.encodeResourceURL(mediaRequestPath);
                if (contentType == null) {
                    contentType = mediaResource.getContentType();
                }
            } else {
                mediaResourceURL = RES_NOT_FOUND;
                logger.error(RES_NOT_FOUND_ERROR_MSG, resourceHandler, resourceName, resourceLibrary);
            }
            encodeMediaSource(responseWriter, mediaResourceURL, contentType);
            if (firstMediaResourceURL == null) {
                firstMediaResourceURL = mediaResourceURL;
            }
        } else {
            child.encodeAll(facesContext);
        }
    }
    // If no media has been specified, then warn the developer.
    if (firstMediaResourceURL == null) {
        logger.warn("No {0} has been specified for <alloy:{0} id=[{1}] />.", getMediaType(), media.getClientId());
    } else // first media URL.
    if (media.isDegrade()) {
        encodeFlashPlayer(facesContext, responseWriter, media, firstMediaResourceURL);
    }
}
Also used : Media(com.liferay.faces.alloy.component.media.Media) Resource(javax.faces.application.Resource) FacesResource(com.liferay.faces.util.application.FacesResource) UIComponent(javax.faces.component.UIComponent) ResourceHandler(javax.faces.application.ResourceHandler) ResponseWriter(javax.faces.context.ResponseWriter) ExternalContext(javax.faces.context.ExternalContext) FacesResource(com.liferay.faces.util.application.FacesResource) Collection(java.util.Collection) Application(javax.faces.application.Application)

Example 15 with ResourceHandler

use of javax.faces.application.ResourceHandler in project liferay-faces-alloy by liferay.

the class MediaRenderer method encodeFlashPlayer.

protected void encodeFlashPlayer(FacesContext facesContext, ResponseWriter responseWriter, Media media, String mediaResourceURL) throws IOException {
    BrowserSniffer browserSniffer = BrowserSnifferFactory.getBrowserSnifferInstance(facesContext.getExternalContext());
    boolean browserIE = browserSniffer.isIe();
    responseWriter.startElement("object", null);
    encodeMediaSize(responseWriter, media);
    Application application = facesContext.getApplication();
    ResourceHandler resourceHandler = application.getResourceHandler();
    Object flashPlayer = media.getFlashPlayer();
    String flashPlayerURL;
    // If the developer has specified a Flash player, then
    if (flashPlayer != null) {
        flashPlayerURL = getEncodedResourceURL(facesContext, resourceHandler, application, flashPlayer);
    } else // Otherwise, get the default Alloy Flash player.
    {
        Resource defaultFlashPlayerResource = resourceHandler.createResource(getDefaultFlashPlayerName(), "liferay-faces-alloy");
        String defaultFlashPlayerRequestPath = defaultFlashPlayerResource.getRequestPath();
        ExternalContext externalContext = facesContext.getExternalContext();
        flashPlayerURL = externalContext.encodeResourceURL(defaultFlashPlayerRequestPath);
    }
    if (browserIE) {
        responseWriter.writeAttribute("classid", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000", null);
        String flashPlayerVersion = media.getFlashPlayerVersion();
        String codebaseURL = "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=".concat(flashPlayerVersion);
        responseWriter.writeAttribute("codebase", codebaseURL, null);
        responseWriter.startElement("param", null);
        responseWriter.writeAttribute("name", "movie", null);
        responseWriter.writeAttribute("value", flashPlayerURL, null);
        responseWriter.endElement("param");
    } else {
        responseWriter.writeAttribute("data", flashPlayerURL, null);
        responseWriter.writeAttribute("type", "application/x-shockwave-flash", null);
    }
    encodeFlashPlayerChildren(facesContext, responseWriter, media, mediaResourceURL, resourceHandler, application, (flashPlayer == null));
    responseWriter.endElement("object");
}
Also used : BrowserSniffer(com.liferay.faces.util.client.BrowserSniffer) ExternalContext(javax.faces.context.ExternalContext) Resource(javax.faces.application.Resource) FacesResource(com.liferay.faces.util.application.FacesResource) ResourceHandler(javax.faces.application.ResourceHandler) Application(javax.faces.application.Application)

Aggregations

ResourceHandler (javax.faces.application.ResourceHandler)15 Resource (javax.faces.application.Resource)9 ExternalContext (javax.faces.context.ExternalContext)8 Application (javax.faces.application.Application)7 FacesContext (javax.faces.context.FacesContext)6 FacesResource (com.liferay.faces.util.application.FacesResource)4 UIComponent (javax.faces.component.UIComponent)2 ResponseWriter (javax.faces.context.ResponseWriter)2 Media (com.liferay.faces.alloy.component.media.Media)1 Video (com.liferay.faces.alloy.component.video.Video)1 ResourceHandlerOuterImpl (com.liferay.faces.bridge.application.internal.ResourceHandlerOuterImpl)1 ResourceComponent (com.liferay.faces.bridge.component.internal.ResourceComponent)1 InlineScript (com.liferay.faces.bridge.renderkit.html_basic.internal.InlineScript)1 BrowserSniffer (com.liferay.faces.util.client.BrowserSniffer)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1