KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > web > filter > CacheStampedResourcesFilter


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.web.filter;
25
26 import java.io.IOException JavaDoc;
27 import java.util.regex.Pattern JavaDoc;
28
29 import javax.servlet.FilterChain JavaDoc;
30 import javax.servlet.ServletException JavaDoc;
31 import javax.servlet.ServletRequest JavaDoc;
32 import javax.servlet.ServletResponse JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35
36 import org.riotfamily.common.util.FormatUtils;
37 import org.riotfamily.common.web.util.ServletUtils;
38 import org.springframework.web.filter.GenericFilterBean;
39
40 /**
41  * Servlet filter that sets an Expires header for request URLs that contain
42  * a timestamp. URLs are considered as 'stamped' if they match the configured
43  * pattern.
44  * <p>
45  * The default pattern is <code>(/\\d{14}/)|(\\?[0-9]+$)</code>, this
46  * matches URLs created by the DefaultFileStore and URLs printed via the
47  * <code>common.resource()</code> FreeMarker function.
48  * </p>
49  * @author Felix Gnass [fgnass at neteye dot de]
50  * @since 6.4
51  */

52 public class CacheStampedResourcesFilter extends GenericFilterBean {
53
54     public static final String JavaDoc DEFAULT_EXPIRATION = "1M";
55
56     public static final Pattern JavaDoc DEFAULT_PATTERN =
57             Pattern.compile("(/\\d{14}/)|(\\?[0-9]+$)");
58
59     private static final String JavaDoc EXPIRES_HEADER = "Expires";
60
61     private Pattern JavaDoc stampPattern = DEFAULT_PATTERN;
62
63     private String JavaDoc expiresAfter = DEFAULT_EXPIRATION;
64
65     private long expires;
66
67     public void setExpiresAfter(String JavaDoc expiresAfter) {
68         this.expiresAfter = expiresAfter;
69     }
70
71     public void setStampPattern(Pattern JavaDoc stampPattern) {
72         this.stampPattern = stampPattern;
73     }
74
75     protected void initFilterBean() throws ServletException JavaDoc {
76         expires = System.currentTimeMillis()
77                 + FormatUtils.parseMillis(expiresAfter);
78     }
79
80     public final void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response,
81             FilterChain JavaDoc filterChain) throws IOException JavaDoc, ServletException JavaDoc {
82
83         doFilterInternal((HttpServletRequest JavaDoc) request,
84                 (HttpServletResponse JavaDoc) response, filterChain);
85     }
86
87     protected void doFilterInternal(HttpServletRequest JavaDoc request,
88             HttpServletResponse JavaDoc response, FilterChain JavaDoc filterChain)
89             throws ServletException JavaDoc, IOException JavaDoc {
90
91         if (isStamped(request)) {
92             response.setDateHeader(EXPIRES_HEADER, expires);
93         }
94         filterChain.doFilter(request, response);
95     }
96
97     protected boolean isStamped(HttpServletRequest JavaDoc request) {
98         String JavaDoc url = ServletUtils.getRequestUrlWithQueryString(request);
99         return stampPattern.matcher(url).matches();
100     }
101 }
102
Popular Tags