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;
023
024import java.util.Locale;
025
026import javax.servlet.http.HttpServletRequest;
027import javax.servlet.http.HttpServletResponse;
028
029import net.sf.json.JSONObject;
030
031import org.apache.commons.logging.Log;
032import org.apache.commons.logging.LogFactory;
033import org.nuxeo.theme.CachingDef;
034
035public final class Utils {
036
037    private static final Log log = LogFactory.getLog(Utils.class);
038
039    private static final String[] lengthUnits = { "%", "em", "px", "ex", "pt", "in", "cm", "mm", "pc" };
040
041    private Utils() {
042        // This class is not supposed to be instantiated.
043    }
044
045    public static String toJson(final Object object) {
046        return JSONObject.fromObject(object).toString();
047    }
048
049    /* web lengths */
050
051    public static String addWebLengths(final String length1, final String length2) {
052        final WebLength webLength1 = getWebLength(length1);
053        final WebLength webLength2 = getWebLength(length2);
054        if (!webLength1.unit.equals(webLength2.unit)) {
055            return null;
056        }
057        return new WebLength(webLength1.value + webLength2.value, webLength1.unit).toString();
058    }
059
060    public static String substractWebLengths(final String length1, final String length2) {
061        final WebLength webLength1 = getWebLength(length1);
062        final WebLength webLength2 = getWebLength(length2);
063        if (!webLength1.unit.equals(webLength2.unit)) {
064            return null;
065        }
066        return new WebLength(webLength1.value - webLength2.value, webLength1.unit).toString();
067    }
068
069    public static String divideWebLength(final String length, final int divider) {
070        if (divider <= 0) {
071            return null;
072        }
073        final WebLength webLength = getWebLength(length);
074        if (webLength != null) {
075            return new WebLength(webLength.value / divider, webLength.unit).toString();
076        }
077        return null;
078    }
079
080    private static WebLength getWebLength(final String length) {
081        Integer value = null;
082        String unit = null;
083        for (String lengthUnit : lengthUnits) {
084            if (length.endsWith(lengthUnit)) {
085                unit = lengthUnit;
086                try {
087                    value = Integer.valueOf(length.substring(0, length.length() - lengthUnit.length()));
088                } catch (NumberFormatException e) {
089                    log.error("Could not convert web lengths to integers", e);
090                }
091                break;
092            }
093        }
094        if (value != null && unit != null) {
095            return new WebLength(value, unit);
096        }
097        return null;
098    }
099
100    public static boolean supportsGzip(final HttpServletRequest request) {
101        final String encoding = request.getHeader("Accept-Encoding");
102        return encoding != null && encoding.toLowerCase(Locale.ENGLISH).contains("gzip");
103    }
104
105    public static void setCacheHeaders(final HttpServletResponse response, final CachingDef caching) {
106        if (caching != null) {
107            final String lifetime = caching.getLifetime();
108            if (lifetime != null) {
109                final long now = System.currentTimeMillis();
110                response.addHeader("Cache-Control", "max-age=" + lifetime);
111                response.addHeader("Cache-Control", "must-revalidate");
112                response.setDateHeader("Last-Modified", now);
113                response.setDateHeader("Expires", now + new Long(lifetime) * 1000L);
114            }
115        }
116    }
117
118    public static boolean isVirtualHosting(final HttpServletRequest request) {
119        if (request.getHeader("X-Forwarded-Host") != null) {
120            return true;
121        }
122        return false;
123    }
124
125    public static String getImageMimeType(String ext) {
126        ext = ext.toLowerCase();
127        if ("png".equals(ext)) {
128            return "image/png";
129        } else if ("gif".equals(ext)) {
130            return "image/gif";
131        } else if ("jpg".equals(ext) || "jpeg".equals(ext) || "jpe".equals(ext)) {
132            return "image/jpeg";
133        }
134        return "application/octet-stream";
135    }
136}