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