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.application.ResourceDependencies;
027import javax.faces.application.ResourceDependency;
028import javax.faces.component.UIComponent;
029import javax.faces.context.ExternalContext;
030import javax.faces.context.FacesContext;
031import javax.faces.context.ResponseWriter;
032import javax.servlet.http.HttpServletRequest;
033
034import org.nuxeo.ecm.platform.ui.web.util.BaseURL;
035import org.nuxeo.theme.html.Utils;
036import org.nuxeo.theme.html.ui.Resources;
037import org.nuxeo.theme.jsf.component.UIResources;
038
039import com.sun.faces.renderkit.html_basic.ScriptStyleBaseRenderer;
040
041/**
042 * Renderer for {@link UIResources} component.
043 *
044 * @since 6.0
045 */
046@ResourceDependencies({ @ResourceDependency(library = "javax.faces", name = "jsf.js"),
047        @ResourceDependency(library = "org.richfaces", name = "jquery.js"),
048        @ResourceDependency(library = "org.richfaces", name = "richfaces.js"),
049        @ResourceDependency(library = "org.richfaces", name = "richfaces-queue.js"),
050        @ResourceDependency(library = "org.nuxeo", name = "widget-utils.js"),
051        @ResourceDependency(library = "org.nuxeo.select2", name = "select2.js") })
052public class ResourcesRenderer extends ScriptStyleBaseRenderer {
053
054    @Override
055    protected void startElement(ResponseWriter writer, UIComponent component) throws IOException {
056    }
057
058    @Override
059    protected void endElement(ResponseWriter writer) throws IOException {
060    }
061
062    @Override
063    public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
064        final ResponseWriter writer = context.getResponseWriter();
065        final ExternalContext externalContext = context.getExternalContext();
066
067        Map<String, String> params = new HashMap<String, String>();
068
069        Map<String, Object> requestMap = externalContext.getRequestMap();
070        URL themeUrl = (URL) requestMap.get("org.nuxeo.theme.url");
071        final Map<String, Object> attributes = component.getAttributes();
072
073        String contextPath = BaseURL.getContextPath();
074        params.put("contextPath", contextPath);
075        params.put("themeUrl", themeUrl.toString());
076        params.put("path", contextPath);
077        params.put("ignoreLocal", (String) attributes.get("ignoreLocal"));
078
079        String basePath = contextPath + "/site";
080        params.put("basepath", basePath);
081
082        Boolean virtualHosting = Utils.isVirtualHosting((HttpServletRequest) externalContext.getRequest());
083        writer.write(Resources.render(params, virtualHosting));
084    }
085
086}