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.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
038import org.apache.commons.lang3.time.FastDateFormat;
039import org.nuxeo.ecm.platform.web.common.requestcontroller.filter.NuxeoRequestControllerFilter;
040
041/**
042 * Adding http cache header (Cache-Control : max-age AND Expire) to the response.
043 *
044 * @author <a href="mailto:stan@nuxeo.com">Sun Seng David TAN</a>
045 * @deprecated since 7.10 caching handled by {@link NuxeoRequestControllerFilter}
046 */
047@Deprecated
048public class SimpleCacheFilter implements Filter {
049
050    private String cacheTime = "3599";
051
052    // formatted http Expires: Thu, 01 Dec 1994 16:00:00 GMT
053    public static final FastDateFormat HTTP_EXPIRES_DATE_FORMAT = FastDateFormat.getInstance(
054            "EEE, dd MMM yyyy HH:mm:ss z", TimeZone.getTimeZone("GMT"), Locale.US);
055
056    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
057            ServletException {
058
059        HttpServletRequest httpRequest = (HttpServletRequest) request;
060        HttpServletResponse httpResponse = (HttpServletResponse) response;
061
062        if (httpRequest.getMethod().equals("GET")) {
063            addCacheHeader(httpResponse, cacheTime);
064        }
065        chain.doFilter(request, response);
066    }
067
068    public static void addCacheHeader(HttpServletResponse httpResponse, String cacheTime) {
069        httpResponse.addHeader("Cache-Control", "max-age=" + cacheTime);
070        httpResponse.addHeader("Cache-Control", "public");
071
072        // Generating expires using current date and adding cache time.
073        // we are using the format Expires: Thu, 01 Dec 1994 16:00:00 GMT
074        Date date = new Date();
075        long newDate = date.getTime() + Long.parseLong(cacheTime) * 1000;
076        date.setTime(newDate);
077
078        httpResponse.setHeader("Expires", HTTP_EXPIRES_DATE_FORMAT.format(date));
079    }
080
081    public void destroy() {
082        cacheTime = null;
083    }
084
085    public void init(FilterConfig conf) throws ServletException {
086        cacheTime = conf.getInitParameter("cacheTime");
087    }
088
089}