Consistent support for if-(un)modified-since as ZonedDateTime/Instant
Includes DefaultRequestBodyUriSpec pre-resolving URI for HttpRequest. Issue: SPR-17571
This commit is contained in:
@@ -19,9 +19,7 @@ package org.springframework.web.reactive.function.client;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -275,9 +273,7 @@ class DefaultWebClient implements WebClient {
|
||||
|
||||
@Override
|
||||
public DefaultRequestBodyUriSpec ifModifiedSince(ZonedDateTime ifModifiedSince) {
|
||||
ZonedDateTime gmt = ifModifiedSince.withZoneSameInstant(ZoneId.of("GMT"));
|
||||
String headerValue = DateTimeFormatter.RFC_1123_DATE_TIME.format(gmt);
|
||||
getHeaders().set(HttpHeaders.IF_MODIFIED_SINCE, headerValue);
|
||||
getHeaders().setIfModifiedSince(ifModifiedSince);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -327,13 +323,16 @@ class DefaultWebClient implements WebClient {
|
||||
if (defaultRequest != null) {
|
||||
defaultRequest.accept(this);
|
||||
}
|
||||
URI uri = (this.uri != null ? this.uri : uriBuilderFactory.expand(""));
|
||||
return ClientRequest.create(this.httpMethod, uri)
|
||||
return ClientRequest.create(this.httpMethod, initUri())
|
||||
.headers(headers -> headers.addAll(initHeaders()))
|
||||
.cookies(cookies -> cookies.addAll(initCookies()))
|
||||
.attributes(attributes -> attributes.putAll(this.attributes));
|
||||
}
|
||||
|
||||
private URI initUri() {
|
||||
return (this.uri != null ? this.uri : uriBuilderFactory.expand(""));
|
||||
}
|
||||
|
||||
private HttpHeaders initHeaders() {
|
||||
if (CollectionUtils.isEmpty(this.headers)) {
|
||||
return (defaultHeaders != null ? defaultHeaders : new HttpHeaders());
|
||||
@@ -371,25 +370,21 @@ class DefaultWebClient implements WebClient {
|
||||
|
||||
private HttpRequest createRequest() {
|
||||
return new HttpRequest() {
|
||||
|
||||
private HttpHeaders headers = initHeaders();
|
||||
|
||||
private final URI uri = initUri();
|
||||
private final HttpHeaders headers = initHeaders();
|
||||
|
||||
@Override
|
||||
public HttpMethod getMethod() {
|
||||
return httpMethod;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMethodValue() {
|
||||
return httpMethod.name();
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getURI() {
|
||||
return uri;
|
||||
return this.uri;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
return this.headers;
|
||||
@@ -410,16 +405,12 @@ class DefaultWebClient implements WebClient {
|
||||
|
||||
private final List<StatusHandler> statusHandlers = new ArrayList<>(1);
|
||||
|
||||
|
||||
DefaultResponseSpec(Mono<ClientResponse> responseMono,
|
||||
Supplier<HttpRequest> requestSupplier) {
|
||||
DefaultResponseSpec(Mono<ClientResponse> responseMono, Supplier<HttpRequest> requestSupplier) {
|
||||
this.responseMono = responseMono;
|
||||
this.requestSupplier = requestSupplier;
|
||||
this.statusHandlers.add(DEFAULT_STATUS_HANDLER);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public ResponseSpec onStatus(Predicate<HttpStatus> statusPredicate,
|
||||
Function<ClientResponse, Mono<? extends Throwable>> exceptionFunction) {
|
||||
@@ -473,8 +464,7 @@ class DefaultWebClient implements WebClient {
|
||||
return bodyPublisher;
|
||||
}
|
||||
else {
|
||||
return errorFunction.apply(createResponseException(response,
|
||||
this.requestSupplier.get()));
|
||||
return errorFunction.apply(createResponseException(response, this.requestSupplier.get()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -486,8 +476,9 @@ class DefaultWebClient implements WebClient {
|
||||
.onErrorResume(ex2 -> Mono.empty()).thenReturn(ex);
|
||||
}
|
||||
|
||||
private static Mono<WebClientResponseException> createResponseException(ClientResponse response,
|
||||
HttpRequest request) {
|
||||
private static Mono<WebClientResponseException> createResponseException(
|
||||
ClientResponse response, HttpRequest request) {
|
||||
|
||||
return DataBufferUtils.join(response.body(BodyExtractors.toDataBuffers()))
|
||||
.map(dataBuffer -> {
|
||||
byte[] bytes = new byte[dataBuffer.readableByteCount()];
|
||||
@@ -527,7 +518,6 @@ class DefaultWebClient implements WebClient {
|
||||
|
||||
private final BiFunction<ClientResponse, HttpRequest, Mono<? extends Throwable>> exceptionFunction;
|
||||
|
||||
|
||||
public StatusHandler(Predicate<HttpStatus> predicate,
|
||||
BiFunction<ClientResponse, HttpRequest, Mono<? extends Throwable>> exceptionFunction) {
|
||||
|
||||
@@ -537,7 +527,6 @@ class DefaultWebClient implements WebClient {
|
||||
this.exceptionFunction = exceptionFunction;
|
||||
}
|
||||
|
||||
|
||||
public boolean test(HttpStatus status) {
|
||||
return this.predicate.test(status);
|
||||
}
|
||||
|
||||
@@ -158,13 +158,13 @@ class DefaultEntityResponseBuilder<T> implements EntityResponse.Builder<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public EntityResponse.Builder<T> lastModified(Instant lastModified) {
|
||||
public EntityResponse.Builder<T> lastModified(ZonedDateTime lastModified) {
|
||||
this.headers.setLastModified(lastModified);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EntityResponse.Builder<T> lastModified(ZonedDateTime lastModified) {
|
||||
public EntityResponse.Builder<T> lastModified(Instant lastModified) {
|
||||
this.headers.setLastModified(lastModified);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -158,13 +158,13 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServerResponse.BodyBuilder lastModified(Instant lastModified) {
|
||||
public ServerResponse.BodyBuilder lastModified(ZonedDateTime lastModified) {
|
||||
this.headers.setLastModified(lastModified);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServerResponse.BodyBuilder lastModified(ZonedDateTime lastModified) {
|
||||
public ServerResponse.BodyBuilder lastModified(Instant lastModified) {
|
||||
this.headers.setLastModified(lastModified);
|
||||
return this;
|
||||
}
|
||||
@@ -315,7 +315,6 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
|
||||
@Override
|
||||
public final Mono<Void> writeTo(ServerWebExchange exchange, Context context) {
|
||||
writeStatusAndHeaders(exchange.getResponse());
|
||||
|
||||
Instant lastModified = Instant.ofEpochMilli(headers().getLastModified());
|
||||
HttpMethod httpMethod = exchange.getRequest().getMethod();
|
||||
if (SAFE_METHODS.contains(httpMethod) && exchange.checkNotModified(headers().getETag(), lastModified)) {
|
||||
|
||||
@@ -190,10 +190,9 @@ public interface EntityResponse<T> extends ServerResponse {
|
||||
* 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);
|
||||
Builder<T> lastModified(ZonedDateTime lastModified);
|
||||
|
||||
/**
|
||||
* Set the time the resource was last changed, as specified by the
|
||||
@@ -202,9 +201,10 @@ public interface EntityResponse<T> extends ServerResponse {
|
||||
* January 1, 1970 GMT.
|
||||
* @param lastModified the last modified date
|
||||
* @return this builder
|
||||
* @since 5.1.4
|
||||
* @see HttpHeaders#setLastModified(long)
|
||||
*/
|
||||
Builder<T> lastModified(ZonedDateTime lastModified);
|
||||
Builder<T> lastModified(Instant lastModified);
|
||||
|
||||
/**
|
||||
* Set the location of a resource, as specified by the {@code Location} header.
|
||||
|
||||
@@ -281,19 +281,19 @@ public interface ServerResponse {
|
||||
* {@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 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(ZonedDateTime lastModified);
|
||||
B lastModified(Instant lastModified);
|
||||
|
||||
/**
|
||||
* Set the location of a resource, as specified by the {@code Location} header.
|
||||
|
||||
Reference in New Issue
Block a user