My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package com.vineetmanohar.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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;

/**
* Set the "expires" header on pages which are configured in expires.properties
*
* 1) Create a file "expires.properties" in your src/main/resources. It should
* be accessible via the classpath '/expires.properties'.
*
* 2) Each line in the properties file is a resource path regex mapped to the
* age of the page. Examples:
*
* ^/images/.*$ = 2M # 2 month expiry for all resources in images directory
* ^.*\\.css$ = 1M # 1 month expiry for all pages which have .css extension ^/$
* = 1h # 1 hr expiry for home page .* = 1m # 1 min expiry for all other pages
*
* Left hand side is full regex. Right hand side is the age. The supported units
* are:
*
* m - minutes h - hours d - days w - weeks M - months
*
* 3) Order is important. The first matching regular expression will be used.
*
* @author Vineet Manohar http://www.vineetmanohar.com
*
* Etag: W/"14961-1287895605000"
*
* Expires: Wed, 24 Nov 2010 08:48:33 EST
*/
public class PageExpiryFilter implements javax.servlet.Filter {
private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
.getLog(PageExpiryFilter.class);

private Map<String, Integer> pathRegex;

public void destroy() {
// no resources to be released
}

/**
* The doFilter method of the Filter is called by the container each time a
* request/response pair is passed through the chain due to a client request
* for a resource at the end of the chain.
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (response instanceof HttpServletResponse) {
if (request instanceof HttpServletRequest) {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
String path = httpServletRequest.getServletPath();
HttpServletResponse httpServletResponse = (HttpServletResponse) response;

String expiryDate = null;
String lastModified = null;

for (String regex : pathRegex.keySet()) {
if (path.matches(regex)) {
Integer age = pathRegex.get(regex);
// expiry date
{
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, age);

expiryDate = new SimpleDateFormat(
"EEE, dd MMM yyyy HH:mm:ss z")
.format(calendar.getTime());
httpServletResponse
.setHeader("Expires", expiryDate);
log.debug("expiration date set for path: " + path
+ " = " + expiryDate);
}
// last modified
/*
* { Calendar calendar = Calendar.getInstance();
* calendar.add(Calendar.MINUTE, -age);
*
* lastModified = new SimpleDateFormat(
* "EEE, dd MMM yyyy HH:mm:ss z")
* .format(calendar.getTime());
*
* httpServletResponse.setHeader("Last-Modified",
* lastModified);
* log.debug("last modified date set for path: " + path
* + " = " + lastModified); }
*/

break;
}
}

if (expiryDate == null) {
log.warn("No expiration date set for path: " + path);
}
}
}

// chain.doFilter() should be called after writing the header. If it is
// called _before_ writing the header
chain.doFilter(request, response);
}

/**
* Get the expiry header to set for the given path
*
* @param path
* the relative path of the resource within the application. e.g.
* /images/logo.png
*
* @return the expiry header formatted as "EEE, dd MMM yyyy HH:mm:ss z", or
* null if no expiry date has been specified
*/
private String getExpiryHeaderFor(String path) {
for (String regex : pathRegex.keySet()) {
if (path.matches(regex)) {
Integer age = pathRegex.get(regex);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, age);

return new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z")
.format(calendar.getTime());
}
}

return null;
}

/**
* Called by the web container to indicate to a filter that it is being
* placed into service.
*/
public void init(FilterConfig filterConfig) throws ServletException {
pathRegex = new LinkedHashMap<String, Integer>();

Properties properties = new Properties();

String resourceName = "/expires.properties";

InputStream inputStream = getClass().getResourceAsStream(resourceName);

if (inputStream != null) {
try {
properties.load(inputStream);
} catch (IOException e) {
throw new ServletException(
"Error while opening classpath resource '"
+ resourceName
+ "'. Make sure that it is a valid properties",
e);
}
}

for (Object key : properties.keySet()) {
String regex = (String) key;
String value = properties.getProperty(regex);

if (value != null && value.length() == 0) {
value = null;
}

Integer expInMinutes = getExpirationInMinutes(value);

pathRegex.put(String.valueOf(key), expInMinutes);
}
}

static Integer getExpirationInMinutes(String value) {
if (value == null) {
return null;
}
value = value.trim();
if (value.length() == 0) {
return null;
}
Pattern pattern = Pattern.compile("^(\\d+)(m|M|h|d|w)$");
Matcher m = pattern.matcher(value);
if (m.matches()) {
int val = Integer.parseInt(m.group(1));
String denomination = m.group(2);
int factor;
if (denomination.equals("m")) {
factor = 1;
} else if (denomination.equals("h")) {
factor = 60;
} else if (denomination.equals("d")) {
factor = 60 * 24;
} else if (denomination.equals("M")) {
factor = 60 * 24 * 30;
} else if (denomination.equals("w")) {
factor = 60 * 24 * 7;
} else {
throw new RuntimeException(
"Developer error. Unhandled pattern '" + value + "'"
+ " for '" + value
+ "'. Valid patterns are 2m, 10h, 1d, 1w, 1M");
}

return val * factor;
} else {
throw new RuntimeException(
"Invalid pattern '"
+ value
+ "'"
+ " for '"
+ value
+ "'. Valid patterns are '2m' for 2 minutes, '10h' for 10 hours, '1d' for 1 day, '1w' for 1 week, '1M' for 1 month");
}
}
}

Change log

r19 by vineet.manohar on Nov 30, 2010   Diff
the chain.doFilter() should be called at
the end, otherwise response gets committed
Go to: 
Project members, sign in to write a code review

Older revisions

r18 by vineet.manohar on Oct 23, 2010   Diff
added code for page expiry filter
All revisions of this file

File info

Size: 6726 bytes, 225 lines
Powered by Google Project Hosting