Skip to content

Commit c95e9e4

Browse files
committed
Update config parameter names
* Use a `HttpServletResponseWrapper` to remove HTTP/1.0 `Pragma` header. * Replace `expirationTime<Long>` parameter with `expiration<Long>`. * Replace `static<Boolean>` parameter with `must-revalidate<Boolean>`.
1 parent 8140ae0 commit c95e9e4

File tree

8 files changed

+79
-42
lines changed

8 files changed

+79
-42
lines changed

CHANGELOG.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Java EE Cache Filter
22

3+
## 2.2.0
4+
5+
* Use a `HttpServletResponseWrapper` to remove HTTP/1.0 `Pragma` header.
6+
* Replace `expirationTime<Long>` parameter with `expiration<Long>`.
7+
* Replace `static<Boolean>` parameter with `must-revalidate<Boolean>`.
8+
39
## 2.1.0
410

511
* Move from Google Project Hosting (https://code.google.com/p/cache-filter/) to GitHub (https://github.com/samaxes/javaee-cache-filter).
@@ -8,23 +14,23 @@
814

915
## 2.0
1016

11-
* Added `NoETagFilter` class to disable HTTP `ETag` header set by the `DefaultServlet` in Tomcat.
12-
* Added `NoCacheFilter` class to completely disable browser caching.
13-
* Replaced `privacy<String>` parameter by `private<Boolean>`. Cache directive to control where the response may be cached.
14-
* Added `static<Boolean>` parameter. Conditional requests are not required for static components.
17+
* Add `NoETagFilter` class to disable HTTP `ETag` header set by the `DefaultServlet` in Tomcat.
18+
* Add `NoCacheFilter` class to completely disable browser caching.
19+
* Replace `privacy<String>` parameter with `private<Boolean>`. Cache directive to control where the response may be cached.
20+
* Add `static<Boolean>` parameter. Conditional requests are not required for static components.
1521
Cache directive `must-revalidate` should be used for non static components to force them to be revalidated once a response becomes stale.
1622

1723
## 1.2.1
1824

19-
* Optimized the configuration process. All the configurations are now done in the init method.
20-
* Added the Sonatype OSS Parent POM.
25+
* Optimize the configuration process. All the configurations are now done in the init method.
26+
* Add the Sonatype OSS Parent POM.
2127

2228
## 1.2
2329

24-
* Changed the default package to `com.samaxes.filter`.
25-
* Updated the distribution repositories to Sonatype Nexus.
30+
* Change the default package to `com.samaxes.filter`.
31+
* Update the distribution repositories to Sonatype Nexus.
2632

2733
## 1.1.0
2834

2935
* Use `response.setDateHeader()` instead of `response.setHeader()` to set `Expires` HTTP cache header.
30-
* Compiled against JDK 1.5 instead of JDK 1.6.
36+
* Compile against JDK 1.5 instead of JDK 1.6.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<groupId>com.samaxes.filter</groupId>
1212
<artifactId>cachefilter</artifactId>
13-
<version>2.1.1-SNAPSHOT</version>
13+
<version>2.2.0-SNAPSHOT</version>
1414
<packaging>jar</packaging>
1515

1616
<name>Java EE Cache Filter</name>

src/main/java/com/samaxes/filter/CacheFilter.java

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import javax.servlet.ServletRequest;
2828
import javax.servlet.ServletResponse;
2929
import javax.servlet.http.HttpServletResponse;
30+
import javax.servlet.http.HttpServletResponseWrapper;
3031

3132
import com.samaxes.filter.util.CacheConfigParameter;
3233
import com.samaxes.filter.util.Cacheability;
@@ -137,61 +138,85 @@
137138
*
138139
* @author Samuel Santos
139140
* @author John Yeary
140-
* @version 2.1.0
141+
* @version 2.2.0
141142
*/
142143
public class CacheFilter implements Filter {
143144

144-
private Cacheability cacheability;
145+
private long expiration;
145146

146-
private boolean isStatic;
147+
private Cacheability cacheability;
147148

148-
private long seconds;
149+
private boolean mustRevalidate;
149150

150151
/**
151152
* {@inheritDoc}
152153
*/
153154
@Override
154155
public void init(FilterConfig filterConfig) throws ServletException {
155-
cacheability = (Boolean.valueOf(filterConfig.getInitParameter(CacheConfigParameter.PRIVATE.getName()))) ? Cacheability.PRIVATE
156-
: Cacheability.PUBLIC;
157-
isStatic = Boolean.valueOf(filterConfig.getInitParameter(CacheConfigParameter.STATIC.getName()));
156+
if (filterConfig.getInitParameter("expirationTime") != null) {
157+
throw new ServletException(new StringBuilder(
158+
"The initialization parameter expirationTime has been replaced with ")
159+
.append(CacheConfigParameter.EXPIRATION.getName()).append(" for the filter ")
160+
.append(filterConfig.getFilterName()).append(".").toString());
161+
}
162+
if (filterConfig.getInitParameter("static") != null) {
163+
throw new ServletException(new StringBuilder("The initialization parameter static has been replaced with ")
164+
.append(CacheConfigParameter.MUST_REVALIDATE.getName()).append(" for the filter ")
165+
.append(filterConfig.getFilterName()).append(".").toString());
166+
}
158167

159168
try {
160-
seconds = Long.valueOf(filterConfig.getInitParameter(CacheConfigParameter.EXPIRATION_TIME.getName()));
169+
expiration = Long.valueOf(filterConfig.getInitParameter(CacheConfigParameter.EXPIRATION.getName()));
161170
} catch (NumberFormatException e) {
162171
throw new ServletException(new StringBuilder("The initialization parameter ")
163-
.append(CacheConfigParameter.EXPIRATION_TIME.getName()).append(" is missing for filter ")
164-
.append(filterConfig.getFilterName()).append(".").toString());
172+
.append(CacheConfigParameter.EXPIRATION.getName())
173+
.append(" is invalid or is missing for the filter ").append(filterConfig.getFilterName())
174+
.append(".").toString());
165175
}
176+
cacheability = Boolean.valueOf(filterConfig.getInitParameter(CacheConfigParameter.PRIVATE.getName())) ? Cacheability.PRIVATE
177+
: Cacheability.PUBLIC;
178+
mustRevalidate = Boolean.valueOf(filterConfig.getInitParameter(CacheConfigParameter.MUST_REVALIDATE.getName()));
166179
}
167180

168181
/**
169-
* Set cache header directives. {@inheritDoc}
182+
* <p>
183+
* Set HTTP cache headers.
184+
* </p>
185+
* {@inheritDoc}
170186
*/
171187
@Override
172188
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
173189
throws IOException, ServletException {
174190
HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
175-
StringBuilder cacheControl = new StringBuilder(cacheability.getValue()).append(", max-age=").append(seconds);
176-
177-
if (!isStatic) {
191+
StringBuilder cacheControl = new StringBuilder(cacheability.getValue()).append(", max-age=").append(expiration);
192+
if (mustRevalidate) {
178193
cacheControl.append(", must-revalidate");
179194
}
180195

181196
// Set cache directives
182197
httpServletResponse.setHeader(HTTPCacheHeader.CACHE_CONTROL.getName(), cacheControl.toString());
183-
httpServletResponse.setDateHeader(HTTPCacheHeader.EXPIRES.getName(), System.currentTimeMillis() + seconds
198+
httpServletResponse.setDateHeader(HTTPCacheHeader.EXPIRES.getName(), System.currentTimeMillis() + expiration
184199
* 1000L);
185200

186201
/*
187-
* By default, some servers (e.g. Tomcat) will set headers on any SSL content to deny caching. Setting the
188-
* Pragma header to null or to an empty string takes care of user-agents implementing HTTP 1.0.
202+
* By default, some servers (e.g. Tomcat) will set headers on any SSL content to deny caching. Omitting the
203+
* Pragma header takes care of user-agents implementing HTTP/1.0.
189204
*/
190-
if (httpServletResponse.containsHeader("Pragma")) {
191-
httpServletResponse.setHeader(HTTPCacheHeader.PRAGMA.getName(), null);
192-
}
205+
filterChain.doFilter(servletRequest, new HttpServletResponseWrapper(httpServletResponse) {
206+
@Override
207+
public void addHeader(String name, String value) {
208+
if (!HTTPCacheHeader.PRAGMA.getName().equalsIgnoreCase(name)) {
209+
super.addHeader(name, value);
210+
}
211+
}
193212

194-
filterChain.doFilter(servletRequest, servletResponse);
213+
@Override
214+
public void setHeader(String name, String value) {
215+
if (!HTTPCacheHeader.PRAGMA.getName().equalsIgnoreCase(name)) {
216+
super.setHeader(name, value);
217+
}
218+
}
219+
});
195220
}
196221

197222
/**

src/main/java/com/samaxes/filter/NoCacheFilter.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
*
6060
* @author Samuel Santos
6161
* @author John Yeary
62-
* @version 2.1.0
62+
* @version 2.2.0
6363
*/
6464
public class NoCacheFilter implements Filter {
6565

@@ -71,7 +71,10 @@ public void init(FilterConfig filterConfig) throws ServletException {
7171
}
7272

7373
/**
74-
* Set cache header directives. {@inheritDoc}
74+
* <p>
75+
* Set HTTP cache headers.
76+
* </p>
77+
* {@inheritDoc}
7578
*/
7679
@Override
7780
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)

src/main/java/com/samaxes/filter/NoETagFilter.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
*
5959
* @author Samuel Santos
6060
* @author John Yeary
61-
* @version 2.1.0
61+
* @version 2.2.0
6262
*/
6363
public class NoETagFilter implements Filter {
6464

@@ -70,7 +70,10 @@ public void init(FilterConfig filterConfig) throws ServletException {
7070
}
7171

7272
/**
73-
* Disables {@code ETag} HTTP header. {@inheritDoc}
73+
* <p>
74+
* Disables {@code ETag} HTTP header.
75+
* </p>
76+
* {@inheritDoc}
7477
*/
7578
@Override
7679
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)

src/main/java/com/samaxes/filter/util/CacheConfigParameter.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,21 @@
2323
*
2424
* @author Samuel Santos
2525
* @author John Yeary
26-
* @version 2.1.0
26+
* @version 2.2.0
2727
*/
2828
public enum CacheConfigParameter {
2929
/**
30-
* Defines whether a component is static or not.
30+
* Cache directive to set an expiration time, in seconds, relative to the current date.
3131
*/
32-
STATIC("static"),
32+
EXPIRATION("expiration"),
3333
/**
3434
* Cache directive to control where the response may be cached.
3535
*/
3636
PRIVATE("private"),
3737
/**
38-
* Cache directive to set an expiration date relative to the current date.
38+
* Cache directive to define whether conditional requests are required or not for stale responses.
3939
*/
40-
EXPIRATION_TIME("expirationTime");
40+
MUST_REVALIDATE("must-revalidate");
4141

4242
private final String name;
4343

src/main/java/com/samaxes/filter/util/Cacheability.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*
2424
* @author Samuel Santos
2525
* @author John Yeary
26-
* @version 2.1.0
26+
* @version 2.2.0
2727
*/
2828
public enum Cacheability {
2929
/**

src/main/java/com/samaxes/filter/util/HTTPCacheHeader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*
2424
* @author Samuel Santos
2525
* @author John Yeary
26-
* @version 2.1.0
26+
* @version 2.2.0
2727
*/
2828
public enum HTTPCacheHeader {
2929
/**

0 commit comments

Comments
 (0)