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

@@ -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.