Consistent support for last-modified argument as Instant/ZonedDateTime

Issue: SPR-17571
This commit is contained in:
Juergen Hoeller
2018-12-06 15:53:47 +01:00
parent 9abd4ed33d
commit 6a012147c4
6 changed files with 69 additions and 34 deletions

View File

@@ -1148,6 +1148,7 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
/**
* Set the time the resource was last changed, as specified by the
* {@code Last-Modified} header.
* @since 5.1.4
*/
public void setLastModified(Instant lastModified) {
setInstant(LAST_MODIFIED, lastModified);
@@ -1156,9 +1157,10 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
/**
* Set the time the resource was last changed, as specified by the
* {@code Last-Modified} header.
* @since 5.1.4
*/
public void setLastModified(ZonedDateTime lastModified) {
setZonedDateTime(LAST_MODIFIED, lastModified);
setZonedDateTime(LAST_MODIFIED, lastModified.withZoneSameInstant(ZoneId.of("GMT")));
}
/**
@@ -1272,6 +1274,16 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
return getValuesAsList(VARY);
}
/**
* Set the given date under the given header name after formatting it as a string
* using the RFC-1123 date-time formatter. The equivalent of
* {@link #set(String, String)} but for date headers.
* @since 5.1.4
*/
public void setInstant(String headerName, Instant date) {
setZonedDateTime(headerName, ZonedDateTime.ofInstant(date, GMT));
}
/**
* Set the given date under the given header name after formatting it as a string
* using the RFC-1123 date-time formatter. The equivalent of
@@ -1282,15 +1294,6 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
set(headerName, DATE_FORMATTERS[0].format(date));
}
/**
* Set the given date under the given header name after formatting it as a string
* using the RFC-1123 date-time formatter. The equivalent of
* {@link #set(String, String)} but for date headers.
*/
public void setInstant(String headerName, Instant date) {
setZonedDateTime(headerName, ZonedDateTime.ofInstant(date, GMT));
}
/**
* Set the given date under the given header name after formatting it as a string
* using the RFC-1123 date-time formatter. The equivalent of
@@ -1299,14 +1302,7 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
* @see #setZonedDateTime(String, ZonedDateTime)
*/
public void setDate(String headerName, long date) {
set(headerName, formatDate(date));
}
// Package private: also used in ResponseCookie..
static String formatDate(long date) {
Instant instant = Instant.ofEpochMilli(date);
ZonedDateTime time = ZonedDateTime.ofInstant(instant, GMT);
return DATE_FORMATTERS[0].format(time);
setInstant(headerName, Instant.ofEpochMilli(date));
}
/**
@@ -1656,4 +1652,11 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
}
}
// Package-private: used in ResponseCookie
static String formatDate(long date) {
Instant instant = Instant.ofEpochMilli(date);
ZonedDateTime time = ZonedDateTime.ofInstant(instant, GMT);
return DATE_FORMATTERS[0].format(time);
}
}

View File

@@ -366,18 +366,20 @@ public class ResponseEntity<T> extends HttpEntity<T> {
* {@code Last-Modified} header.
* @param lastModified the last modified date
* @return this builder
* @since 5.1.4
* @see HttpHeaders#setLastModified(long)
*/
B lastModified(ZonedDateTime lastModified);
B lastModified(Instant lastModified);
/**
* Set the time the resource was last changed, as specified by the
* {@code Last-Modified} header.
* @param lastModified the last modified date
* @return this builder
* @since 5.1.4
* @see HttpHeaders#setLastModified(long)
*/
B lastModified(Instant lastModified);
B lastModified(ZonedDateTime lastModified);
/**
* Set the location of a resource, as specified by the {@code Location} header.
@@ -516,13 +518,13 @@ public class ResponseEntity<T> extends HttpEntity<T> {
}
@Override
public BodyBuilder lastModified(ZonedDateTime date) {
public BodyBuilder lastModified(Instant date) {
this.headers.setLastModified(date);
return this;
}
@Override
public BodyBuilder lastModified(Instant date) {
public BodyBuilder lastModified(ZonedDateTime date) {
this.headers.setLastModified(date);
return this;
}