001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS <http://nuxeo.com> and others
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Jean-Marc Orliaguet, Chalmers
011 *
012 * $Id$
013 */
014
015package org.nuxeo.theme.html;
016
017import java.util.Locale;
018
019import javax.servlet.http.HttpServletRequest;
020import javax.servlet.http.HttpServletResponse;
021
022import net.sf.json.JSONObject;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.theme.CachingDef;
027
028public final class Utils {
029
030    private static final Log log = LogFactory.getLog(Utils.class);
031
032    private static final String[] lengthUnits = { "%", "em", "px", "ex", "pt", "in", "cm", "mm", "pc" };
033
034    private Utils() {
035        // This class is not supposed to be instantiated.
036    }
037
038    public static String toJson(final Object object) {
039        return JSONObject.fromObject(object).toString();
040    }
041
042    /* web lengths */
043
044    public static String addWebLengths(final String length1, final String length2) {
045        final WebLength webLength1 = getWebLength(length1);
046        final WebLength webLength2 = getWebLength(length2);
047        if (!webLength1.unit.equals(webLength2.unit)) {
048            return null;
049        }
050        return new WebLength(webLength1.value + webLength2.value, webLength1.unit).toString();
051    }
052
053    public static String substractWebLengths(final String length1, final String length2) {
054        final WebLength webLength1 = getWebLength(length1);
055        final WebLength webLength2 = getWebLength(length2);
056        if (!webLength1.unit.equals(webLength2.unit)) {
057            return null;
058        }
059        return new WebLength(webLength1.value - webLength2.value, webLength1.unit).toString();
060    }
061
062    public static String divideWebLength(final String length, final int divider) {
063        if (divider <= 0) {
064            return null;
065        }
066        final WebLength webLength = getWebLength(length);
067        if (webLength != null) {
068            return new WebLength(webLength.value / divider, webLength.unit).toString();
069        }
070        return null;
071    }
072
073    private static WebLength getWebLength(final String length) {
074        Integer value = null;
075        String unit = null;
076        for (String lengthUnit : lengthUnits) {
077            if (length.endsWith(lengthUnit)) {
078                unit = lengthUnit;
079                try {
080                    value = Integer.valueOf(length.substring(0, length.length() - lengthUnit.length()));
081                } catch (NumberFormatException e) {
082                    log.error("Could not convert web lengths to integers", e);
083                }
084                break;
085            }
086        }
087        if (value != null && unit != null) {
088            return new WebLength(value, unit);
089        }
090        return null;
091    }
092
093    public static boolean supportsGzip(final HttpServletRequest request) {
094        final String encoding = request.getHeader("Accept-Encoding");
095        return encoding != null && encoding.toLowerCase(Locale.ENGLISH).contains("gzip");
096    }
097
098    public static void setCacheHeaders(final HttpServletResponse response, final CachingDef caching) {
099        if (caching != null) {
100            final String lifetime = caching.getLifetime();
101            if (lifetime != null) {
102                final long now = System.currentTimeMillis();
103                response.addHeader("Cache-Control", "max-age=" + lifetime);
104                response.addHeader("Cache-Control", "must-revalidate");
105                response.setDateHeader("Last-Modified", now);
106                response.setDateHeader("Expires", now + new Long(lifetime) * 1000L);
107            }
108        }
109    }
110
111    public static boolean isVirtualHosting(final HttpServletRequest request) {
112        if (request.getHeader("X-Forwarded-Host") != null) {
113            return true;
114        }
115        return false;
116    }
117
118    public static String getImageMimeType(String ext) {
119        ext = ext.toLowerCase();
120        if ("png".equals(ext)) {
121            return "image/png";
122        } else if ("gif".equals(ext)) {
123            return "image/gif";
124        } else if ("jpg".equals(ext) || "jpeg".equals(ext) || "jpe".equals(ext)) {
125            return "image/jpeg";
126        }
127        return "application/octet-stream";
128    }
129}