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.Map; 021 022import javax.faces.component.UIComponent; 023import javax.faces.context.FacesContext; 024import javax.faces.context.ResponseWriter; 025 026import org.apache.commons.logging.Log; 027import org.apache.commons.logging.LogFactory; 028import org.nuxeo.ecm.web.resources.api.Resource; 029import org.nuxeo.ecm.web.resources.api.ResourceType; 030import org.nuxeo.ecm.web.resources.api.service.WebResourceManager; 031 032/** 033 * Renderer for JavaScript, CSS and HTML resources declared on the {@link WebResourceManager} extension point. 034 * 035 * @since 7.3 036 */ 037public class ResourceRenderer extends AbstractResourceRenderer { 038 039 private static final Log log = LogFactory.getLog(ResourceRenderer.class); 040 041 @Override 042 public void encodeEnd(FacesContext context, UIComponent component) throws IOException { 043 Resource resource = null; 044 String url = null; 045 Map<String, Object> attributes = component.getAttributes(); 046 String name = (String) attributes.get("name"); 047 if (name != null) { 048 resource = resolveNuxeoResource(context, component, name); 049 if (resource == null) { 050 log.error("Resource not found: " + name); 051 return; 052 } 053 url = getUrlWithParams(context, component, 054 resolveNuxeoResourceUrl(context, component, resolveNuxeoResourcePath(resource))); 055 } 056 ResponseWriter writer = context.getResponseWriter(); 057 if (ResourceType.css.matches(resource)) { 058 writer.startElement("link", component); 059 writer.writeAttribute("type", "text/css", "type"); 060 writer.writeAttribute("rel", "stylesheet", "rel"); 061 writer.writeURIAttribute("href", url, "href"); 062 writer.endElement("link"); 063 } else if (ResourceType.js.matches(resource)) { 064 writer.startElement("script", component); 065 writer.writeAttribute("type", "text/javascript", "type"); 066 writer.writeURIAttribute("src", url, "src"); 067 writer.endElement("script"); 068 } else if (ResourceType.html.matches(resource)) { 069 writer.startElement("link", component); 070 writer.writeAttribute("rel", "import", "rel"); 071 writer.writeURIAttribute("href", url, "href"); 072 writer.endElement("link"); 073 } else { 074 log.error("Unhandled type for resource " + resource); 075 } 076 } 077 078 @Override 079 protected void startElement(ResponseWriter writer, UIComponent component) throws IOException { 080 // NOOP 081 } 082 083 @Override 084 protected void endElement(ResponseWriter writer) throws IOException { 085 // NOOP 086 } 087 088}