001/* 002 * (C) Copyright 2006-2007 Nuxeo SAS <http://nuxeo.com> and others 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * Jean-Marc Orliaguet, Chalmers 011 * 012 * $Id$ 013 */ 014 015package org.nuxeo.theme.html.ui; 016 017import java.net.MalformedURLException; 018import java.net.URL; 019import java.util.List; 020import java.util.Map; 021 022import org.apache.commons.logging.Log; 023import org.apache.commons.logging.LogFactory; 024import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper; 025import org.nuxeo.theme.Manager; 026import org.nuxeo.theme.resources.ResourceManager; 027 028public class Resources { 029 030 private static final Log log = LogFactory.getLog(Resources.class); 031 032 public static String render(Map<String, String> params, boolean virtualHosting) { 033 StringBuilder sb = new StringBuilder(); 034 035 String resourcePath = VirtualHostHelper.getContextPathProperty() + "/nxthemes-lib/"; 036 final String themeUrl = params.get("themeUrl"); 037 final String path = params.get("path"); 038 final String basepath = params.get("basepath"); 039 String nxthemeBasePath = basepath; 040 041 final ResourceManager resourceManager = Manager.getResourceManager(); 042 043 final StringBuilder combinedStyles = new StringBuilder(); 044 final StringBuilder combinedScripts = new StringBuilder(); 045 combinedStyles.append(resourcePath); 046 combinedScripts.append(resourcePath); 047 048 boolean hasScripts = false; 049 boolean hasStyles = false; 050 051 boolean ignoreLocal = false; 052 if (params.containsKey("ignoreLocal")) { 053 ignoreLocal = Boolean.parseBoolean(params.get("ignoreLocal")); 054 } 055 056 List<String> resourceNames; 057 if (ignoreLocal) { 058 resourceNames = resourceManager.getGlobalResourcesFor(themeUrl); 059 } else { 060 resourceNames = resourceManager.getResourcesFor(themeUrl); 061 } 062 063 for (String resourceName : resourceNames) { 064 if (resourceName.endsWith(".css")) { 065 combinedStyles.append(resourceName).append(","); 066 hasStyles = true; 067 } else if (resourceName.endsWith(".js")) { 068 combinedScripts.append(resourceName).append(","); 069 hasScripts = true; 070 } 071 } 072 073 combinedStyles.deleteCharAt(combinedStyles.length() - 1); 074 combinedScripts.deleteCharAt(combinedScripts.length() - 1); 075 076 long timestamp = getTimestamp(themeUrl); 077 combinedStyles.append("?path=").append(path).append("&basepath=").append(basepath).append("&timestamp=").append( 078 timestamp); 079 combinedScripts.append("?path=").append(path).append("&basepath=").append(basepath).append( 080 "&timestamp=").append(timestamp); 081 082 // styles 083 if (hasStyles) { 084 sb.append(String.format("<link type=\"text/css\" rel=\"stylesheet\" media=\"all\" href=\"%s\"/>", 085 combinedStyles.toString())); 086 } 087 088 final String contextPath = params.get("contextPath"); 089 // scripts 090 sb.append(String.format("<script type=\"text/javascript\"><!--\n" + "var nxthemesPath = \"%s\";\n" 091 + "var nxthemesBasePath = \"%s\";\n" + "var nxContextPath = \"%s\";\n" + "//--></script>\n", path, 092 nxthemeBasePath, contextPath)); 093 if (hasScripts) { 094 sb.append(String.format("<script type=\"text/javascript\" src=\"%s\"></script>", combinedScripts.toString())); 095 } 096 097 // Flush local resources 098 resourceManager.flush(); 099 return sb.toString(); 100 } 101 102 /** 103 * Return the timestamp to use for resources URL, useful to work-around caching when hot-reloading theme resources. 104 * 105 * @since 5.6 106 * @param themeUrl 107 */ 108 protected static long getTimestamp(String themeUrl) { 109 long timestamp = 0; 110 if (themeUrl != null) { 111 try { 112 Long val = Manager.getThemeManager().getLastModified(new URL(themeUrl)); 113 if (val != null) { 114 timestamp = val.longValue(); 115 } 116 } catch (MalformedURLException e) { 117 log.warn(String.format("Error while generating last modified" + " timestamp for theme url '%s'", 118 themeUrl), e); 119 } 120 } 121 return timestamp; 122 } 123}