Introduce request attributes in RestClient

This commit introduces request attributes in the RestClient and
underlying infrastructure (i.e. HttpRequest).

Closes gh-32027
This commit is contained in:
Arjen Poutsma
2024-06-10 10:06:52 +02:00
parent c36e270481
commit 60b5bbe334
21 changed files with 385 additions and 9 deletions

View File

@@ -18,6 +18,8 @@ package org.springframework.web.reactive.function.client;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
@@ -65,6 +67,11 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder {
public HttpHeaders getHeaders() {
return HttpHeaders.EMPTY;
}
@Override
public Map<String, Object> getAttributes() {
return Collections.emptyMap();
}
};

View File

@@ -17,6 +17,7 @@
package org.springframework.web.reactive.function.client;
import java.net.URI;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -149,6 +150,11 @@ public abstract class ExchangeFunctions {
return request.url();
}
@Override
public Map<String, Object> getAttributes() {
return request.attributes();
}
@Override
public HttpHeaders getHeaders() {
return request.headers();

View File

@@ -193,7 +193,7 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder {
@Override
public ServerRequest build() {
ServerHttpRequest serverHttpRequest = new BuiltServerHttpRequest(this.exchange.getRequest().getId(),
this.method, this.uri, this.contextPath, this.headers, this.cookies, this.body);
this.method, this.uri, this.contextPath, this.headers, this.cookies, this.body, this.attributes);
ServerWebExchange exchange = new DelegatingServerWebExchange(
serverHttpRequest, this.attributes, this.exchange, this.messageReaders);
return new DefaultServerRequest(exchange, this.messageReaders);
@@ -220,8 +220,10 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder {
private final Flux<DataBuffer> body;
private final Map<String, Object> attributes;
public BuiltServerHttpRequest(String id, HttpMethod method, URI uri, @Nullable String contextPath,
HttpHeaders headers, MultiValueMap<String, HttpCookie> cookies, Flux<DataBuffer> body) {
HttpHeaders headers, MultiValueMap<String, HttpCookie> cookies, Flux<DataBuffer> body, Map<String, Object> attributes) {
this.id = id;
this.method = method;
@@ -231,6 +233,7 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder {
this.cookies = unmodifiableCopy(cookies);
this.queryParams = parseQueryParams(uri);
this.body = body;
this.attributes = attributes;
}
private static <K, V> MultiValueMap<K, V> unmodifiableCopy(MultiValueMap<K, V> original) {
@@ -273,6 +276,11 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder {
return this.uri;
}
@Override
public Map<String, Object> getAttributes() {
return this.attributes;
}
@Override
public RequestPath getPath() {
return this.path;

View File

@@ -19,6 +19,7 @@ package org.springframework.web.reactive.function.client;
import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.OptionalLong;
@@ -324,6 +325,11 @@ class DefaultClientResponseTests {
public HttpHeaders getHeaders() {
return HttpHeaders.EMPTY;
}
@Override
public Map<String, Object> getAttributes() {
return Collections.emptyMap();
}
};
given(mockExchangeStrategies.messageReaders()).willReturn(