001/* 002 * (C) Copyright 2006-2009 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 * Jean-Marc Orliaguet, Chalmers 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.theme.webengine.fm.extensions; 023 024import java.io.IOException; 025import java.io.Writer; 026import java.net.URL; 027import java.util.HashMap; 028import java.util.Map; 029 030import javax.servlet.http.HttpServletRequest; 031 032import org.nuxeo.ecm.webengine.WebEngine; 033import org.nuxeo.ecm.webengine.model.WebContext; 034import org.nuxeo.theme.html.ui.Head; 035import org.nuxeo.theme.themes.ThemeManager; 036 037import freemarker.core.Environment; 038import freemarker.template.TemplateDirectiveBody; 039import freemarker.template.TemplateDirectiveModel; 040import freemarker.template.TemplateException; 041import freemarker.template.TemplateModel; 042import freemarker.template.TemplateModelException; 043 044/** 045 * @author <a href="mailto:jmo@chalmers.se">Jean-Marc Orliaguet</a> 046 */ 047public class NXThemesHeadDirective implements TemplateDirectiveModel { 048 049 public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) 050 throws TemplateException, IOException { 051 052 if (!params.isEmpty()) { 053 throw new TemplateModelException("This directive doesn't allow parameters."); 054 } 055 if (loopVars.length != 0) { 056 throw new TemplateModelException("This directive doesn't allow loop variables."); 057 } 058 if (body != null) { 059 throw new TemplateModelException("Didn't expect a body"); 060 } 061 062 Writer writer = env.getOut(); 063 064 WebContext context = WebEngine.getActiveContext(); 065 HttpServletRequest request = context.getRequest(); 066 final URL themeUrl = (URL) request.getAttribute("org.nuxeo.theme.url"); 067 068 String baseUrl = context.head().getURL(); 069 if (!baseUrl.endsWith("/")) { 070 baseUrl += "/"; 071 } 072 073 Map<String, String> attributes = new HashMap<String, String>(); 074 attributes.put("themeName", ThemeManager.getThemeNameByUrl(themeUrl)); 075 attributes.put("path", context.getModulePath()); 076 attributes.put("baseUrl", baseUrl); 077 writer.write(Head.render(attributes)); 078 } 079}