Browse Source

Add ClientRequest attribute for URI template

Issue: SPR-16537
pull/1713/head
Rossen Stoyanchev 7 years ago
parent
commit
9352e3d047
  1. 21
      spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java

21
spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java

@ -65,6 +65,8 @@ import org.springframework.web.util.UriBuilderFactory;
*/ */
class DefaultWebClient implements WebClient { class DefaultWebClient implements WebClient {
private static final String URI_TEMPLATE_ATTRIBUTE = WebClient.class.getName() + ".uriTemplate";
private static final Mono<ClientResponse> NO_HTTP_CLIENT_RESPONSE_ERROR = Mono.error( private static final Mono<ClientResponse> NO_HTTP_CLIENT_RESPONSE_ERROR = Mono.error(
new IllegalStateException("The underlying HTTP client completed without emitting a response.")); new IllegalStateException("The underlying HTTP client completed without emitting a response."));
@ -161,8 +163,8 @@ class DefaultWebClient implements WebClient {
@Nullable @Nullable
private BodyInserter<?, ? super ClientHttpRequest> inserter; private BodyInserter<?, ? super ClientHttpRequest> inserter;
@Nullable private final Map<String, Object> attributes = new LinkedHashMap<>(4);
private Map<String, Object> attributes;
DefaultRequestBodyUriSpec(HttpMethod httpMethod) { DefaultRequestBodyUriSpec(HttpMethod httpMethod) {
this.httpMethod = httpMethod; this.httpMethod = httpMethod;
@ -170,11 +172,13 @@ class DefaultWebClient implements WebClient {
@Override @Override
public RequestBodySpec uri(String uriTemplate, Object... uriVariables) { public RequestBodySpec uri(String uriTemplate, Object... uriVariables) {
attribute(URI_TEMPLATE_ATTRIBUTE, uriTemplate);
return uri(uriBuilderFactory.expand(uriTemplate, uriVariables)); return uri(uriBuilderFactory.expand(uriTemplate, uriVariables));
} }
@Override @Override
public RequestBodySpec uri(String uriTemplate, Map<String, ?> uriVariables) { public RequestBodySpec uri(String uriTemplate, Map<String, ?> uriVariables) {
attribute(URI_TEMPLATE_ATTRIBUTE, uriTemplate);
return uri(uriBuilderFactory.expand(uriTemplate, uriVariables)); return uri(uriBuilderFactory.expand(uriTemplate, uriVariables));
} }
@ -203,13 +207,6 @@ class DefaultWebClient implements WebClient {
return this.cookies; return this.cookies;
} }
private Map<String, Object> getAttributes() {
if (this.attributes == null) {
this.attributes = new LinkedHashMap<>(4);
}
return this.attributes;
}
@Override @Override
public DefaultRequestBodyUriSpec header(String headerName, String... headerValues) { public DefaultRequestBodyUriSpec header(String headerName, String... headerValues) {
for (String headerValue : headerValues) { for (String headerValue : headerValues) {
@ -227,14 +224,14 @@ class DefaultWebClient implements WebClient {
@Override @Override
public RequestBodySpec attribute(String name, Object value) { public RequestBodySpec attribute(String name, Object value) {
getAttributes().put(name, value); this.attributes.put(name, value);
return this; return this;
} }
@Override @Override
public RequestBodySpec attributes(Consumer<Map<String, Object>> attributesConsumer) { public RequestBodySpec attributes(Consumer<Map<String, Object>> attributesConsumer) {
Assert.notNull(attributesConsumer, "'attributesConsumer' must not be null"); Assert.notNull(attributesConsumer, "'attributesConsumer' must not be null");
attributesConsumer.accept(getAttributes()); attributesConsumer.accept(this.attributes);
return this; return this;
} }
@ -330,7 +327,7 @@ class DefaultWebClient implements WebClient {
return ClientRequest.method(this.httpMethod, uri) return ClientRequest.method(this.httpMethod, uri)
.headers(headers -> headers.addAll(initHeaders())) .headers(headers -> headers.addAll(initHeaders()))
.cookies(cookies -> cookies.addAll(initCookies())) .cookies(cookies -> cookies.addAll(initCookies()))
.attributes(attributes -> attributes.putAll(getAttributes())); .attributes(attributes -> attributes.putAll(this.attributes));
} }
private HttpHeaders initHeaders() { private HttpHeaders initHeaders() {

Loading…
Cancel
Save