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 *     Nuxeo - initial API and implementation
018 *
019 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
020 */
021
022package org.nuxeo.ecm.platform.ui.web.cache;
023
024import java.io.IOException;
025import java.text.DateFormat;
026import java.text.SimpleDateFormat;
027import java.util.Date;
028import java.util.Locale;
029import java.util.TimeZone;
030
031import javax.servlet.Filter;
032import javax.servlet.FilterChain;
033import javax.servlet.FilterConfig;
034import javax.servlet.ServletException;
035import javax.servlet.ServletRequest;
036import javax.servlet.ServletResponse;
037import javax.servlet.http.HttpServletRequest;
038import javax.servlet.http.HttpServletResponse;
039
040import org.nuxeo.ecm.platform.web.common.requestcontroller.filter.NuxeoRequestControllerFilter;
041
042/**
043 * Adding http cache header (Cache-Control : max-age AND Expire) to the response.
044 *
045 * @author <a href="mailto:stan@nuxeo.com">Sun Seng David TAN</a>
046 * @deprecated: caching handled by {@link NuxeoRequestControllerFilter}
047 */
048@Deprecated
049public class SimpleCacheFilter implements Filter {
050
051    private String cacheTime = "3599";
052
053    public static final DateFormat HTTP_EXPIRES_DATE_FORMAT = httpExpiresDateFormat();
054
055    private static DateFormat httpExpiresDateFormat() {
056        // formatted http Expires: Thu, 01 Dec 1994 16:00:00 GMT
057        DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
058        df.setTimeZone(TimeZone.getTimeZone("GMT"));
059        return df;
060    }
061
062    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
063            ServletException {
064
065        HttpServletRequest httpRequest = (HttpServletRequest) request;
066        HttpServletResponse httpResponse = (HttpServletResponse) response;
067
068        if (httpRequest.getMethod().equals("GET")) {
069            addCacheHeader(httpResponse, cacheTime);
070        }
071        chain.doFilter(request, response);
072    }
073
074    public static void addCacheHeader(HttpServletResponse httpResponse, String cacheTime) {
075        httpResponse.addHeader("Cache-Control", "max-age=" + cacheTime);
076        httpResponse.addHeader("Cache-Control", "public");
077
078        // Generating expires using current date and adding cache time.
079        // we are using the format Expires: Thu, 01 Dec 1994 16:00:00 GMT
080        Date date = new Date();
081        long newDate = date.getTime() + new Long(cacheTime) * 1000;
082        date.setTime(newDate);
083
084        httpResponse.setHeader("Expires", HTTP_EXPIRES_DATE_FORMAT.format(date));
085    }
086
087    public void destroy() {
088        cacheTime = null;
089    }
090
091    public void init(FilterConfig conf) throws ServletException {
092        cacheTime = conf.getInitParameter("cacheTime");
093    }
094
095}