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 url = resolveUrlWithTimestamp(component, url); 058 } 059 ResponseWriter writer = context.getResponseWriter(); 060 if (ResourceType.css.matches(resource)) { 061 writer.startElement("link", component); 062 writer.writeAttribute("type", "text/css", "type"); 063 writer.writeAttribute("rel", "stylesheet", "rel"); 064 writer.writeURIAttribute("href", url, "href"); 065 writer.endElement("link"); 066 } else if (ResourceType.js.matches(resource)) { 067 writer.startElement("script", component); 068 writer.writeAttribute("type", "text/javascript", "type"); 069 writer.writeURIAttribute("src", url, "src"); 070 writer.endElement("script"); 071 } else if (ResourceType.html.matches(resource)) { 072 writer.startElement("link", component); 073 writer.writeAttribute("rel", "import", "rel"); 074 writer.writeURIAttribute("href", url, "href"); 075 writer.endElement("link"); 076 } else { 077 log.error("Unhandled type for resource " + resource); 078 } 079 } 080 081 @Override 082 protected void startElement(ResponseWriter writer, UIComponent component) throws IOException { 083 // NOOP 084 } 085 086 @Override 087 protected void endElement(ResponseWriter writer) throws IOException { 088 // NOOP 089 } 090 091}