001/*
002 * (C) Copyright 2006-2007 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.html.ui;
023
024import java.util.HashMap;
025import java.util.Map;
026import java.util.Properties;
027
028import org.apache.commons.lang.StringUtils;
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.nuxeo.theme.Manager;
032import org.nuxeo.theme.elements.ElementFormatter;
033import org.nuxeo.theme.elements.ThemeElement;
034import org.nuxeo.theme.formats.widgets.Widget;
035
036public class Head {
037
038    private static final Log log = LogFactory.getLog(Head.class);
039
040    protected static Map<String, String> iconsMime = new HashMap<>();
041
042    public static String render(Map<String, String> params) {
043        StringBuilder sb = new StringBuilder();
044
045        String themeName = params.get("themeName");
046        final ThemeElement theme = Manager.getThemeManager().getThemeByName(themeName);
047        final Widget widget = (Widget) ElementFormatter.getFormatFor(theme, "widget");
048
049        if (widget == null) {
050            log.warn("Theme " + themeName + " has no widget.");
051        } else {
052            final Properties properties = widget.getProperties();
053
054            // Charset
055            final String charset = properties.getProperty("charset", "utf-8");
056            sb.append(String.format("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>", charset));
057
058            // Site icon
059            String icon = properties.getProperty("icon", "/favicon.ico");
060            String mimeType = getMimeType(icon);
061            sb.append(String.format("<link rel=\"icon\" href=\"%s\" type=\"" + mimeType + "\"/>", icon));
062
063            // If specified use a real .ico file for IE
064            final String iconIco = properties.getProperty("iconIco", null);
065            if (StringUtils.isNotEmpty(iconIco)) {
066                icon = iconIco;
067            }
068            sb.append(String.format("<link rel=\"shortcut icon\" href=\"%s\" type=\"image/x-icon\"/>", icon));
069        }
070
071        // Base URL
072        final String baseUrl = params.get("baseUrl");
073        if (baseUrl != null) {
074            sb.append(String.format("<base href=\"%s\" />", baseUrl));
075        }
076
077        return sb.toString();
078    }
079
080    protected static String getMimeType(String ico) {
081        // Use a map to cache mimetype for ico
082        if (!iconsMime.containsKey(ico)) {
083            iconsMime.put(ico, resolveMimeType(ico));
084        }
085        return iconsMime.get(ico);
086    }
087
088    protected static String resolveMimeType(String ico) {
089        int index = ico.lastIndexOf(".");
090        if (index > 0) {
091            // Handle only gif and png
092            String ext = ico.substring(1 + index);
093            switch (ext) {
094            case "gif":
095                return "image/gif";
096            case "png":
097                return "image/png";
098            }
099        }
100        return "image/x-icon";
101    }
102
103}