This blog post was originally posted in the ATG Developer Community by Spark::red’s founding partner Devon Hillard in June, 2010.
Having control over the HTTP response headers allows you to set cache related headers in responses for various content you’d like cached on the browser (or an intermediary proxy). I created the ATG Cache Control DAS pipeline Servlet a year ago, but when you’re using JBoss you need another solution.
Since the DAF pipeline is only executed for JSPs the pipeline Servlet it doesn’t allow you to set headers for the static media items you’re more likely to want to cache. I created a Servlet Filter which allows you to set cache headers in JBoss based on URI patterns. It doesn’t allow the same fine grained control that the old pipeline Servlet does, but it should work for most situations.
Servlet Filters are very similar to ATG pipeline Servlets in that they are executed within the lifecycle of a request and can read the request and modify the response. The filter I created gets configured from the web.xml and sets the response headers relating to caching. You can configure different instances of the filter for each cache time you need, an hour, a day, or a week, and map different URL patterns to the appropriate instances.
I’ve added the filter into Foundation, the open source ATG e-commerce framework project, which is hosted at the Spark::red Development Community.
The Servlet filter code looks like this:
package org.foundation.servlet.filter; import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; 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.HttpServletResponse; /** * The Class CacheHeaderFilter. * * @author Devon Hillard */ public class CacheHeaderFilter implements Filter { /** * The Constant MILLISECONDS_IN_SECOND. */ private static final int MILLISECONDS_IN_SECOND = 1000 ; /** The Constant POST_CHECK_VALUE. */ private static final String POST_CHECK_VALUE = "post-check=" ; /** The Constant PRE_CHECK_VALUE. */ private static final String PRE_CHECK_VALUE = "pre-check=" ; /** The Constant MAX_AGE_VALUE. */ private static final String MAX_AGE_VALUE = "max-age=" ; /** The Constant ZERO_STRING_VALUE. */ private static final String ZERO_STRING_VALUE = "0" ; /** The Constant NO_STORE_VALUE. */ private static final String NO_STORE_VALUE = "no-store" ; /** The Constant NO_CACHE_VALUE. */ private static final String NO_CACHE_VALUE = "no-cache" ; /** The Constant PRAGMA_HEADER. */ private static final String PRAGMA_HEADER = "Pragma" ; /** The Constant CACHE_CONTROL_HEADER. */ private static final String CACHE_CONTROL_HEADER = "Cache-Control" ; /** The Constant EXPIRES_HEADER. */ private static final String EXPIRES_HEADER = "Expires" ; /** The Constant LAST_MODIFIED_HEADER. */ private static final String LAST_MODIFIED_HEADER = "Last-Modified" ; /** The Constant CACHE_TIME_PARAM_NAME. */ private static final String CACHE_TIME_PARAM_NAME = "CacheTime" ; /** The Static HTTP_DATE_FORMAT object. */ private static final DateFormat HTTP_DATE_FORMAT = new SimpleDateFormat( "EEE, dd MMM yyyy HH:mm:ss z" , Locale.US); static { HTTP_DATE_FORMAT.setTimeZone(TimeZone.getTimeZone( "GMT" )); } /** The reply headers. */ private String |
And the web.xml configuration looks like this:
<filter> <filter-name>CacheFilterOneWeek</filter-name> <filter- class >org.foundation.servlet.filter.CacheHeaderFilter</filter- class > <init-param> <param-name>CacheTime</param-name> <param-value> 604800 </param-value> </init-param> </filter> <filter> <filter-name>CacheFilterOneDay</filter-name> <filter- class >org.foundation.servlet.filter.CacheHeaderFilter</filter- class > <init-param> <param-name>CacheTime</param-name> <param-value> 86400 </param-value> </init-param> </filter> <filter> <filter-name>CacheFilterOneHour</filter-name> <filter- class >org.foundation.servlet.filter.CacheHeaderFilter</filter- class > <init-param> <param-name>CacheTime</param-name> <param-value> 3600 </param-value> </init-param> </filter> ....... <filter-mapping> <filter-name>CacheFilterOneDay</filter-name> <url-pattern>*.js</url-pattern> </filter-mapping> <filter-mapping> <filter-name>CacheFilterOneDay</filter-name> <url-pattern>*.css</url-pattern> </filter-mapping> <filter-mapping> <filter-name>CacheFilterOneWeek</filter-name> <url-pattern>*.jpg</url-pattern> </filter-mapping> <filter-mapping> <filter-name>CacheFilterOneWeek</filter-name> <url-pattern>*.gif</url-pattern> </filter-mapping> |
Leave A Comment
You must be logged in to post a comment.