001/* 002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Anahide Tchertchian 016 */ 017package org.nuxeo.ecm.web.resources.jsf; 018 019import java.io.IOException; 020import java.util.List; 021import java.util.Map; 022 023import javax.faces.component.UIComponent; 024import javax.faces.context.FacesContext; 025import javax.faces.context.ResponseWriter; 026 027import org.apache.commons.lang.StringUtils; 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.ResourceContextImpl; 032import org.nuxeo.ecm.web.resources.api.ResourceType; 033import org.nuxeo.ecm.web.resources.api.service.WebResourceManager; 034import org.nuxeo.runtime.api.Framework; 035 036/** 037 * Renderer for resource bundles, handling several types of resources (css, js, html for now). 038 * 039 * @since 7.3 040 */ 041public class ResourceBundleRenderer extends AbstractResourceRenderer { 042 043 public static final String RENDERER_TYPE = "org.nuxeo.ecm.web.resources.jsf.ResourceBundle"; 044 045 private static final Log log = LogFactory.getLog(ResourceBundleRenderer.class); 046 047 @Override 048 public void encodeEnd(FacesContext context, UIComponent component) throws IOException { 049 Map<String, Object> attributes = component.getAttributes(); 050 String name = (String) attributes.get("name"); 051 String type = (String) attributes.get("type"); 052 WebResourceManager wrm = Framework.getService(WebResourceManager.class); 053 if (StringUtils.isBlank(type)) { 054 log.error(String.format("Cannot encode bundle with empty type at %s", component.getClientId())); 055 return; 056 } 057 if (!ResourceType.css.equals(type) && !ResourceType.js.equals(type) && !ResourceType.html.equals(type)) { 058 log.error(String.format("Unsupported type '%s' to encode bundle '%s' at %s", type, name, 059 component.getClientId())); 060 return; 061 } 062 List<Resource> rs = wrm.getResources(new ResourceContextImpl(), name, type); 063 if (rs != null && !rs.isEmpty()) { 064 if (ResourceType.css.equals(type)) { 065 encodeEnd(context, component, ResourceType.css, BUNDLE_ENDPOINT_PATH + name + ".css"); 066 } else if (ResourceType.js.equals(type)) { 067 encodeEnd(context, component, ResourceType.js, BUNDLE_ENDPOINT_PATH + name + ".js"); 068 } else if (ResourceType.html.equals(type)) { 069 for (Resource r : rs) { 070 encodeEnd(context, component, ResourceType.html, COMPONENTS_PATH + r.getPath()); 071 } 072 } 073 } 074 super.encodeEnd(context, component); 075 } 076 077 protected void encodeEnd(FacesContext context, UIComponent component, ResourceType type, String base) 078 throws IOException { 079 String url = resolveNuxeoResourceUrl(context, component, base); 080 ResponseWriter writer = context.getResponseWriter(); 081 if (ResourceType.css.equals(type)) { 082 writer.startElement("link", component); 083 writer.writeAttribute("type", "text/css", "type"); 084 writer.writeAttribute("rel", "stylesheet", "rel"); 085 writer.writeURIAttribute("href", url, "href"); 086 writer.endElement("link"); 087 } else if (ResourceType.js.equals(type)) { 088 writer.startElement("script", component); 089 writer.writeAttribute("type", "text/javascript", "type"); 090 writer.writeURIAttribute("src", url, "src"); 091 writer.endElement("script"); 092 } else if (ResourceType.html.equals(type)) { 093 writer.startElement("link", component); 094 writer.writeAttribute("rel", "import", "rel"); 095 writer.writeURIAttribute("href", url, "href"); 096 writer.endElement("link"); 097 } 098 } 099 100 @Override 101 protected void startElement(ResponseWriter writer, UIComponent component) throws IOException { 102 // NOOP 103 } 104 105 @Override 106 protected void endElement(ResponseWriter writer) throws IOException { 107 // NOOP 108 } 109 110}