From 9352e3d047e27a91149e471bac7584ff17fa6b92 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 1 Mar 2018 17:06:24 -0500 Subject: [PATCH] Add ClientRequest attribute for URI template Issue: SPR-16537 --- .../function/client/DefaultWebClient.java | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java index f62b83218f..cce7514861 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java +++ b/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 { + private static final String URI_TEMPLATE_ATTRIBUTE = WebClient.class.getName() + ".uriTemplate"; + private static final Mono NO_HTTP_CLIENT_RESPONSE_ERROR = Mono.error( new IllegalStateException("The underlying HTTP client completed without emitting a response.")); @@ -161,8 +163,8 @@ class DefaultWebClient implements WebClient { @Nullable private BodyInserter inserter; - @Nullable - private Map attributes; + private final Map attributes = new LinkedHashMap<>(4); + DefaultRequestBodyUriSpec(HttpMethod httpMethod) { this.httpMethod = httpMethod; @@ -170,11 +172,13 @@ class DefaultWebClient implements WebClient { @Override public RequestBodySpec uri(String uriTemplate, Object... uriVariables) { + attribute(URI_TEMPLATE_ATTRIBUTE, uriTemplate); return uri(uriBuilderFactory.expand(uriTemplate, uriVariables)); } @Override public RequestBodySpec uri(String uriTemplate, Map uriVariables) { + attribute(URI_TEMPLATE_ATTRIBUTE, uriTemplate); return uri(uriBuilderFactory.expand(uriTemplate, uriVariables)); } @@ -203,13 +207,6 @@ class DefaultWebClient implements WebClient { return this.cookies; } - private Map getAttributes() { - if (this.attributes == null) { - this.attributes = new LinkedHashMap<>(4); - } - return this.attributes; - } - @Override public DefaultRequestBodyUriSpec header(String headerName, String... headerValues) { for (String headerValue : headerValues) { @@ -227,14 +224,14 @@ class DefaultWebClient implements WebClient { @Override public RequestBodySpec attribute(String name, Object value) { - getAttributes().put(name, value); + this.attributes.put(name, value); return this; } @Override public RequestBodySpec attributes(Consumer> attributesConsumer) { Assert.notNull(attributesConsumer, "'attributesConsumer' must not be null"); - attributesConsumer.accept(getAttributes()); + attributesConsumer.accept(this.attributes); return this; } @@ -330,7 +327,7 @@ class DefaultWebClient implements WebClient { return ClientRequest.method(this.httpMethod, uri) .headers(headers -> headers.addAll(initHeaders())) .cookies(cookies -> cookies.addAll(initCookies())) - .attributes(attributes -> attributes.putAll(getAttributes())); + .attributes(attributes -> attributes.putAll(this.attributes)); } private HttpHeaders initHeaders() {