added filters for resource caching and gzip encoding

This commit is contained in:
Sebastian Sdorra
2010-09-14 08:52:57 +02:00
parent d92b0932b3
commit c4b8936b79
10 changed files with 957 additions and 11 deletions

View File

@@ -0,0 +1,279 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package sonia.scm.util;
//~--- JDK imports ------------------------------------------------------------
import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Sebastian Sdorra
*/
public class WebUtil
{
/** Field description */
public static final String CACHE_CONTROL_PREVENT =
"no-cache, must-revalidate";
/** Field description */
public static final String DATE_PREVENT_CACHE =
"Tue, 09 Apr 1985 10:00:00 GMT";
/** Field description */
public static final String HEADER_CACHECONTROL = "Cache-Control";
/** Field description */
public static final String HEADER_ETAG = "Etag";
/** Field description */
public static final String HEADER_EXPIRES = "Expires";
/** Field description */
public static final String HEADER_IFMS = "If-Modified-Since";
/** Field description */
public static final String HEADER_INM = "If-None-Match";
/** Field description */
public static final String HEADER_LASTMODIFIED = "Last-Modified";
/** Field description */
public static final String HEADER_PRAGMA = "Pragma";
/** Field description */
public static final String PRAGMA_NOCACHE = "no-cache";
/** Field description */
public static final long TIME_DAY = 60 * 60 * 24;
/** Field description */
public static final long TIME_MONTH = 60 * 60 * 24 * 30;
/** Field description */
public static final long TIME_YEAR = 60 * 60 * 24 * 365;
/** Field description */
private static final String HTTP_DATE_FORMAT =
"EEE, dd MMM yyyy HH:mm:ss zzz";
/** Field description */
private static final Logger logger =
Logger.getLogger(WebUtil.class.getName());
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param response
* @param file
*/
public static void addETagHeader(HttpServletResponse response, File file)
{
response.addHeader(HEADER_ETAG, getETag(file));
}
/**
* Method description
*
*
* @param response
*/
public static void addPreventCacheHeaders(HttpServletResponse response)
{
response.addDateHeader(HEADER_LASTMODIFIED, new Date().getTime());
response.addHeader(HEADER_CACHECONTROL, CACHE_CONTROL_PREVENT);
response.addHeader(HEADER_PRAGMA, PRAGMA_NOCACHE);
response.addHeader(HEADER_EXPIRES, DATE_PREVENT_CACHE);
}
/**
* Method description
*
*
* @param response
* @param seconds
*/
public static void addStaticCacheControls(HttpServletResponse response,
long seconds)
{
long time = new Date().getTime();
response.addDateHeader(HEADER_EXPIRES, time + (seconds * 1000));
String cc = "max-age=".concat(Long.toString(seconds)).concat(", public");
// use public for https
response.addHeader(HEADER_CACHECONTROL, cc.toString());
}
/**
* Method description
*
*
* @param date
*
* @return
*/
public static String formatHttpDate(Date date)
{
return getHttpDateFormat().format(date);
}
/**
* Method description
*
*
* @param dateString
*
* @return
*
* @throws ParseException
*/
public static Date parseHttpDate(String dateString) throws ParseException
{
return getHttpDateFormat().parse(dateString);
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param file
*
* @return
*/
public static String getETag(File file)
{
return new StringBuilder("W/\"").append(file.length()).append(
file.lastModified()).append("\"").toString();
}
/**
* Method description
*
*
* @return
*/
public static DateFormat getHttpDateFormat()
{
SimpleDateFormat dateFormat = new SimpleDateFormat(HTTP_DATE_FORMAT,
Locale.ENGLISH);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
return dateFormat;
}
/**
* Method description
*
*
* @param request
*
* @return
*/
public static Date getIfModifiedSinceDate(HttpServletRequest request)
{
Date date = null;
String dateString = request.getHeader(HEADER_IFMS);
if ((dateString != null) && (dateString.length() > 0))
{
try
{
date = parseHttpDate(dateString);
}
catch (ParseException ex)
{
if (logger.isLoggable(Level.WARNING))
{
logger.log(Level.WARNING, null, ex);
}
}
catch (NumberFormatException ex)
{
logger.warning(dateString);
if (logger.isLoggable(Level.WARNING))
{
logger.log(Level.WARNING, dateString, ex);
}
}
}
return date;
}
/**
* Method description
*
*
* @param request
*
* @return
*/
public static boolean isGzipSupported(HttpServletRequest request)
{
String enc = request.getHeader("Accept-Encoding");
return (enc != null) && enc.contains("gzip");
}
/**
* Method description
*
*
* @param request
* @param file
*
* @return
*/
public static boolean isModified(HttpServletRequest request, File file)
{
boolean result = true;
Date modifiedSince = getIfModifiedSinceDate(request);
if ((modifiedSince != null)
&& (modifiedSince.getTime() == file.lastModified()))
{
result = false;
}
if (result)
{
String inmEtag = request.getHeader(HEADER_INM);
if ((inmEtag != null) && (inmEtag.length() > 0)
&& inmEtag.equals(getETag(file)))
{
result = false;
}
}
return result;
}
}