users@glassfish.java.net

adding an expires header to response for caching static resources

From: <glassfish_at_javadesktop.org>
Date: Mon, 02 Feb 2009 09:52:39 PST

Hello,

I have successfully implemented a filter following the blog entry by Chris Webster

http://blogs.sun.com/cwebster/entry/caching_static_resources_in_glassfish

The java implementation for the filter can be found at:

http://blogs.sun.com/cwebster/resource/ExpiresFilter.java

it calculates the expires well, but look at:

        private void addCacheHeaders(ServletRequest request, ServletResponse response)
                throws IOException, ServletException {
                
                HttpServletResponse sr = (HttpServletResponse) response;
                sr.setHeader("Expires", expires);
                long now = (new Date()).getTime();
                
                long expireTime = nextDeploymentTime - now;
                expireTime %= 1000;
                sr.setHeader("Cache-Control", "max-age="+
                        Long.toString(expireTime)+";public;must-revalidate;");
        }

the expireTime is put to max-age. however:

expireTime %= 1000; results in a short expire time. (max-age is specified in seconds)

I wonder if that should have been:

expireTime = expireTime/1000

or maybe it is correct and something that i did not understand.

PS: anyone knows of a different filter implementation for caching static resources?

Best.
[Message sent by forum member 'cambazz' (cambazz)]

http://forums.java.net/jive/thread.jspa?messageID=329674