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.List;
023import java.util.Map;
024
025import javax.faces.component.UIComponent;
026import javax.faces.context.FacesContext;
027import javax.faces.context.ResponseWriter;
028
029import org.apache.commons.lang3.StringUtils;
030import org.apache.commons.logging.Log;
031import org.apache.commons.logging.LogFactory;
032import org.nuxeo.ecm.web.resources.api.Resource;
033import org.nuxeo.ecm.web.resources.api.ResourceContextImpl;
034import org.nuxeo.ecm.web.resources.api.ResourceType;
035import org.nuxeo.ecm.web.resources.api.service.WebResourceManager;
036import org.nuxeo.runtime.api.Framework;
037
038/**
039 * Renderer for resource bundles, handling several types of resources (css, js, html for now).
040 *
041 * @since 7.3
042 */
043public class ResourceBundleRenderer extends AbstractResourceRenderer {
044
045    public static final String RENDERER_TYPE = "org.nuxeo.ecm.web.resources.jsf.ResourceBundle";
046
047    private static final Log log = LogFactory.getLog(ResourceBundleRenderer.class);
048
049    @Override
050    public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
051        Map<String, Object> attributes = component.getAttributes();
052        String name = (String) attributes.get("name");
053        String type = (String) attributes.get("type");
054        WebResourceManager wrm = Framework.getService(WebResourceManager.class);
055        if (StringUtils.isBlank(type)) {
056            log.error("Cannot encode bundle with empty type at %s" + component.getClientId());
057            return;
058        }
059        if (!ResourceType.css.equals(type) && !ResourceType.js.equals(type) && !ResourceType.html.equals(type)) {
060            log.error("Unsupported type '" + type + "' to encode page '" + name + "' at " + component.getClientId());
061            return;
062        }
063        List<Resource> rs = wrm.getResources(new ResourceContextImpl(), name, type);
064        if (rs != null && !rs.isEmpty()) {
065            if (ResourceType.css.equals(type)) {
066                encodeEnd(context, component, ResourceType.css, BUNDLE_ENDPOINT_PATH + name + ".css");
067            } else if (ResourceType.js.equals(type)) {
068                encodeEnd(context, component, ResourceType.js, BUNDLE_ENDPOINT_PATH + name + ".js");
069            } else if (ResourceType.html.equals(type)) {
070                for (Resource r : rs) {
071                    encodeEnd(context, component, ResourceType.html, COMPONENTS_PATH + r.getPath());
072                }
073            }
074        }
075        super.encodeEnd(context, component);
076    }
077
078    protected void encodeEnd(FacesContext context, UIComponent component, ResourceType type, String base)
079            throws IOException {
080        String url = resolveNuxeoResourceUrl(context, component, base);
081        url = resolveUrlWithTimestamp(component, url);
082        ResponseWriter writer = context.getResponseWriter();
083        if (ResourceType.css.equals(type)) {
084            writer.startElement("link", component);
085            writer.writeAttribute("type", "text/css", "type");
086            writer.writeAttribute("rel", "stylesheet", "rel");
087            writer.writeURIAttribute("href", url, "href");
088            writer.endElement("link");
089        } else if (ResourceType.js.equals(type)) {
090            writer.startElement("script", component);
091            writer.writeAttribute("type", "text/javascript", "type");
092            writer.writeURIAttribute("src", url, "src");
093            writer.endElement("script");
094        } else if (ResourceType.html.equals(type)) {
095            writer.startElement("link", component);
096            writer.writeAttribute("rel", "import", "rel");
097            writer.writeURIAttribute("href", url, "href");
098            writer.endElement("link");
099        }
100    }
101
102    @Override
103    protected void startElement(ResponseWriter writer, UIComponent component) throws IOException {
104        // NOOP
105    }
106
107    @Override
108    protected void endElement(ResponseWriter writer) throws IOException {
109        // NOOP
110    }
111
112}