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