Improve Last-Modified & ETag support

Prior to this change, the `"Last-Modified"` and "`Etag`" support had
been improved with SPR-11324: HTTP response headers are now
automatically added for conditional requests and more.

This commit fixes the format of the "`Last-Modified`" and "`ETag`"
values, which were using an epoch timestamp rather than an HTTP-date
format defined in RFC 7231 section 7.1.1.1.

Also, Conditional responses are only applied when the given response
applies, i.e. when it has an compatible HTTP status (2xx).

Issue: SPR-13090
This commit is contained in:
Brian Clozel
2015-06-05 14:30:09 +02:00
parent 75edb3979e
commit 0175068cab
9 changed files with 129 additions and 49 deletions

View File

@@ -16,9 +16,12 @@
package org.springframework.web.servlet.support;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletException;
@@ -91,6 +94,8 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport {
private boolean usePreviousHttpCachingBehavior = false;
private final SimpleDateFormat dateFormat;
private CacheControl cacheControl;
@@ -115,6 +120,8 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport {
this.supportedMethods.add(METHOD_HEAD);
this.supportedMethods.add(METHOD_POST);
}
dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
}
/**
@@ -123,6 +130,8 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport {
*/
public WebContentGenerator(String... supportedMethods) {
this.supportedMethods = new HashSet<String>(Arrays.asList(supportedMethods));
dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
}
@@ -404,7 +413,7 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport {
protected final void cacheForSeconds(HttpServletResponse response, int seconds, boolean mustRevalidate) {
if (this.useExpiresHeader) {
// HTTP 1.0 header
response.setDateHeader(HEADER_EXPIRES, System.currentTimeMillis() + seconds * 1000L);
response.setHeader(HEADER_EXPIRES, dateFormat.format(System.currentTimeMillis() + seconds * 1000L));
}
if (this.useCacheControlHeader) {
// HTTP 1.1 header
@@ -424,7 +433,7 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport {
response.setHeader(HEADER_PRAGMA, "no-cache");
if (this.useExpiresHeader) {
// HTTP 1.0 header
response.setDateHeader(HEADER_EXPIRES, 1L);
response.setHeader(HEADER_EXPIRES, dateFormat.format(System.currentTimeMillis()));
}
if (this.useCacheControlHeader) {
// HTTP 1.1 header: "no-cache" is the standard value,