001/*
002 * (C) Copyright 2014 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.theme.jsf.renderer;
018
019import java.io.IOException;
020import java.net.URL;
021import java.util.HashMap;
022import java.util.Map;
023
024import javax.faces.component.UIComponent;
025import javax.faces.context.ExternalContext;
026import javax.faces.context.FacesContext;
027import javax.faces.context.ResponseWriter;
028import javax.servlet.http.HttpServletRequest;
029
030import org.nuxeo.runtime.api.Framework;
031import org.nuxeo.theme.html.Utils;
032import org.nuxeo.theme.html.ui.ThemeStyles;
033import org.nuxeo.theme.themes.ThemeManager;
034
035import com.sun.faces.renderkit.html_basic.ScriptStyleBaseRenderer;
036
037/**
038 * @since 6.0
039 */
040public class ThemeStylesRenderer extends ScriptStyleBaseRenderer {
041
042    @Override
043    protected void startElement(ResponseWriter writer, UIComponent component) throws IOException {
044    }
045
046    @Override
047    protected void endElement(ResponseWriter writer) throws IOException {
048    }
049
050    @Override
051    public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
052
053        Map<String, Object> attributes = component.getAttributes();
054        String cache = (String) attributes.get("cache");
055        String inline = (String) attributes.get("inline");
056        String theme = (String) attributes.get("theme");
057
058        final ResponseWriter writer = context.getResponseWriter();
059        final ExternalContext externalContext = context.getExternalContext();
060
061        Map<String, Object> requestMap = externalContext.getRequestMap();
062        final URL themeUrl = (URL) requestMap.get("org.nuxeo.theme.url");
063        if (theme == null) {
064            theme = ThemeManager.getThemeNameByUrl(themeUrl);
065        }
066
067        Map<String, String> params = new HashMap<String, String>();
068
069        params.put("themeName", theme);
070        params.put("path", externalContext.getRequestContextPath());
071        // FIXME: use configuration
072        String basePath = Framework.getProperty("org.nuxeo.ecm.contextPath", "/nuxeo");
073        params.put("basepath", basePath);
074        String collectionName = ThemeManager.getCollectionNameByUrl(themeUrl);
075        params.put("collection", collectionName);
076
077        Boolean virtualHosting = Utils.isVirtualHosting((HttpServletRequest) externalContext.getRequest());
078        writer.write(ThemeStyles.render(params, Boolean.parseBoolean(cache), Boolean.parseBoolean(inline),
079                virtualHosting));
080    }
081
082}