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;
}

View File

@ -17,9 +17,8 @@
package org.springframework.web.reactive.function.server;
import java.net.URI;
import java.time.ZoneId;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashSet;
@ -158,11 +157,15 @@ class DefaultEntityResponseBuilder<T> implements EntityResponse.Builder<T> {
return this;
}
@Override
public EntityResponse.Builder<T> lastModified(Instant lastModified) {
this.headers.setLastModified(lastModified);
return this;
}
@Override
public EntityResponse.Builder<T> lastModified(ZonedDateTime lastModified) {
ZonedDateTime gmt = lastModified.withZoneSameInstant(ZoneId.of("GMT"));
String headerValue = DateTimeFormatter.RFC_1123_DATE_TIME.format(gmt);
this.headers.set(HttpHeaders.LAST_MODIFIED, headerValue);
this.headers.setLastModified(lastModified);
return this;
}
@ -174,10 +177,7 @@ class DefaultEntityResponseBuilder<T> implements EntityResponse.Builder<T> {
@Override
public EntityResponse.Builder<T> cacheControl(CacheControl cacheControl) {
String ccValue = cacheControl.getHeaderValue();
if (ccValue != null) {
this.headers.setCacheControl(cacheControl.getHeaderValue());
}
this.headers.setCacheControl(cacheControl);
return this;
}

View File

@ -157,9 +157,15 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
return this;
}
@Override
public ServerResponse.BodyBuilder lastModified(Instant lastModified) {
this.headers.setLastModified(lastModified);
return this;
}
@Override
public ServerResponse.BodyBuilder lastModified(ZonedDateTime lastModified) {
this.headers.setZonedDateTime(HttpHeaders.LAST_MODIFIED, lastModified);
this.headers.setLastModified(lastModified);
return this;
}

View File

@ -17,6 +17,7 @@
package org.springframework.web.reactive.function.server;
import java.net.URI;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.Set;
import java.util.function.Consumer;
@ -176,11 +177,23 @@ public interface EntityResponse<T> extends ServerResponse {
/**
* Set the entity tag of the body, as specified by the {@code ETag} header.
* @param eTag the new entity tag
* @param etag the new entity tag
* @return this builder
* @see HttpHeaders#setETag(String)
*/
Builder<T> eTag(String eTag);
Builder<T> eTag(String etag);
/**
* Set the time the resource was last changed, as specified by the
* {@code Last-Modified} header.
* <p>The date should be specified as the number of milliseconds since
* January 1, 1970 GMT.
* @param lastModified the last modified date
* @return this builder
* @since 5.1.4
* @see HttpHeaders#setLastModified(long)
*/
Builder<T> lastModified(Instant lastModified);
/**
* Set the time the resource was last changed, as specified by the

View File

@ -17,6 +17,7 @@
package org.springframework.web.reactive.function.server;
import java.net.URI;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.Collection;
import java.util.List;
@ -275,6 +276,16 @@ public interface ServerResponse {
*/
B eTag(String eTag);
/**
* 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);
/**
* Set the time the resource was last changed, as specified by the
* {@code Last-Modified} header.