001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.web.resources.jsf;
020
021import java.io.IOException;
022import java.util.Map;
023
024import javax.faces.component.UIComponent;
025import javax.faces.context.FacesContext;
026import javax.faces.context.ResponseWriter;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.ecm.web.resources.api.Resource;
031import org.nuxeo.ecm.web.resources.api.ResourceType;
032import org.nuxeo.ecm.web.resources.api.service.WebResourceManager;
033
034/**
035 * Renderer for JavaScript, CSS and HTML resources declared on the {@link WebResourceManager} extension point.
036 *
037 * @since 7.3
038 */
039public class ResourceRenderer extends AbstractResourceRenderer {
040
041    private static final Log log = LogFactory.getLog(ResourceRenderer.class);
042
043    @Override
044    public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
045        Resource resource = null;
046        String url = null;
047        Map<String, Object> attributes = component.getAttributes();
048        String name = (String) attributes.get("name");
049        if (name != null) {
050            resource = resolveNuxeoResource(context, component, name);
051            if (resource == null) {
052                log.error("Resource not found: " + name);
053                return;
054            }
055            url = getUrlWithParams(context, component,
056                    resolveNuxeoResourceUrl(context, component, resolveNuxeoResourcePath(resource)));
057        }
058        ResponseWriter writer = context.getResponseWriter();
059        if (ResourceType.css.matches(resource)) {
060            writer.startElement("link", component);
061            writer.writeAttribute("type", "text/css", "type");
062            writer.writeAttribute("rel", "stylesheet", "rel");
063            writer.writeURIAttribute("href", url, "href");
064            writer.endElement("link");
065        } else if (ResourceType.js.matches(resource)) {
066            writer.startElement("script", component);
067            writer.writeAttribute("type", "text/javascript", "type");
068            writer.writeURIAttribute("src", url, "src");
069            writer.endElement("script");
070        } else if (ResourceType.html.matches(resource)) {
071            writer.startElement("link", component);
072            writer.writeAttribute("rel", "import", "rel");
073            writer.writeURIAttribute("href", url, "href");
074            writer.endElement("link");
075        } else {
076            log.error("Unhandled type for resource " + resource);
077        }
078    }
079
080    @Override
081    protected void startElement(ResponseWriter writer, UIComponent component) throws IOException {
082        // NOOP
083    }
084
085    @Override
086    protected void endElement(ResponseWriter writer) throws IOException {
087        // NOOP
088    }
089
090}