/*
 * $Id: CacheFilter.java 15 2008-01-01 05:36:30Z samaxes $
 *
 * Last changed on : $Date: 2008-01-01 05:36:30 +0000 (ter, 01 Jan 2008) $
 * Last changed by : $Author: samaxes $
 */
package com.samaxes.cachefilter.presentation;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Filter responsible for browser caching.
 * 
 * @author : Samuel Santos
 * @version : $Revision: 15 $
 */
public class CacheFilter implements Filter {

    private static Logger logger = LoggerFactory.getLogger(CacheFilter.class);

    private FilterConfig filterConfig;

    /**
     * Place this filter into service.
     * 
     * @param filterConfig {@link FilterConfig}
     */
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    /**
     * Take this filter out of service.
     */
    public void destroy() {
        this.filterConfig = null;
    }

    /**
     * Sets cache headers directives.
     * 
     * @param servletRequest {@link ServletRequest}
     * @param servletResponse {@link ServletResponse}
     * @param filterChain {@link FilterChain}
     * @throws IOException {@link FilterChain}
     * @throws ServletException {@link ServletException}
     */
    public void doFilter(ServletRequest servletRequest,
                         ServletResponse servletResponse,
                         FilterChain filterChain) throws IOException, ServletException {
        if (logger.isDebugEnabled()) {
            logger.debug("Setting cache headers for file " + ((HttpServletRequest) servletRequest).getRequestURI());
        }

        String privacy = filterConfig.getInitParameter("privacy");
        String expirationTime = filterConfig.getInitParameter("expirationTime");

        if (StringUtils.isNotBlank(privacy) && StringUtils.isNotBlank(expirationTime)) {
            // set the provided HTTP response parameters
            setCacheExpireDate((HttpServletResponse) servletResponse, privacy, Integer.valueOf(expirationTime)
                    .intValue());
        }

        // pass the request/response on
        filterChain.doFilter(servletRequest, servletResponse);
    }

    private void setCacheExpireDate(HttpServletResponse response,
                                    String privacy,
                                    int seconds) {
        if (response != null) {
            Calendar cal = new GregorianCalendar();
            cal.roll(Calendar.SECOND, seconds);
            response.setHeader("Cache-Control", privacy + ", max-age=" + seconds + ", must-revalidate");
            response.setHeader("Expires", htmlExpiresDateFormat().format(cal.getTime()));
        }
    }

    private DateFormat htmlExpiresDateFormat() {
        DateFormat httpDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
        httpDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
        return httpDateFormat;
    }
}

@@ -7,139 +7,41 @@
  $Id
-: CacheFilter.java 15 2008-01-01 05:36:30Z samaxes $%0D%0A *%0D%0A * Last changed on : $Date: 2008-01-01 05:36:30 +0000 (ter, 01 Jan 2008) 
+$%0D%0A *%0D%0A * Last changed on : $Date
 $%0D%0A 
@@ -71,18 +71,8 @@
 thor
-: samaxes 
 $%0D%0A 
@@ -870,13 +870,8 @@
 sion
-: 15 
 $%0D%0A 

Set svn:keywords properties