001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
018 */
019
020package org.nuxeo.ecm.platform.ui.web.cache;
021
022import java.io.IOException;
023import java.text.DateFormat;
024import java.text.SimpleDateFormat;
025import java.util.Date;
026import java.util.Locale;
027import java.util.TimeZone;
028
029import javax.servlet.Filter;
030import javax.servlet.FilterChain;
031import javax.servlet.FilterConfig;
032import javax.servlet.ServletException;
033import javax.servlet.ServletRequest;
034import javax.servlet.ServletResponse;
035import javax.servlet.http.HttpServletRequest;
036import javax.servlet.http.HttpServletResponse;
037
038/**
039 * Adding http cache header (Cache-Control : max-age AND Expire) to the response.
040 *
041 * @author <a href="mailto:stan@nuxeo.com">Sun Seng David TAN</a>
042 */
043@Deprecated
044public class SimpleCacheFilter implements Filter {
045
046    private String cacheTime = "3599";
047
048    public static final DateFormat HTTP_EXPIRES_DATE_FORMAT = httpExpiresDateFormat();
049
050    private static DateFormat httpExpiresDateFormat() {
051        // formatted http Expires: Thu, 01 Dec 1994 16:00:00 GMT
052        DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
053        df.setTimeZone(TimeZone.getTimeZone("GMT"));
054        return df;
055    }
056
057    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
058            ServletException {
059
060        HttpServletRequest httpRequest = (HttpServletRequest) request;
061        HttpServletResponse httpResponse = (HttpServletResponse) response;
062
063        if (httpRequest.getMethod().equals("GET")) {
064            addCacheHeader(httpResponse, cacheTime);
065        }
066        chain.doFilter(request, response);
067    }
068
069    public static void addCacheHeader(HttpServletResponse httpResponse, String cacheTime) {
070        httpResponse.addHeader("Cache-Control", "max-age=" + cacheTime);
071        httpResponse.addHeader("Cache-Control", "public");
072
073        // Generating expires using current date and adding cache time.
074        // we are using the format Expires: Thu, 01 Dec 1994 16:00:00 GMT
075        Date date = new Date();
076        long newDate = date.getTime() + new Long(cacheTime) * 1000;
077        date.setTime(newDate);
078
079        httpResponse.setHeader("Expires", HTTP_EXPIRES_DATE_FORMAT.format(date));
080    }
081
082    public void destroy() {
083        cacheTime = null;
084    }
085
086    public void init(FilterConfig conf) throws ServletException {
087        cacheTime = conf.getInitParameter("cacheTime");
088    }
089
090}