Skip to content

Commit

Permalink
Avoid unnecessary copy of cookies map in DefaultWebClientBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller authored and zx20110729 committed Feb 18, 2022
1 parent 59af197 commit 8e5de1e
Showing 1 changed file with 12 additions and 16 deletions.
Expand Up @@ -101,20 +101,24 @@ public DefaultWebClientBuilder(DefaultWebClientBuilder other) {
this.defaultUriVariables = (other.defaultUriVariables != null ?
new LinkedHashMap<>(other.defaultUriVariables) : null);
this.uriBuilderFactory = other.uriBuilderFactory;

if (other.defaultHeaders != null) {
this.defaultHeaders = new HttpHeaders();
this.defaultHeaders.putAll(other.defaultHeaders);
}
else {
this.defaultHeaders = null;
}

this.defaultCookies = (other.defaultCookies != null ?
new LinkedMultiValueMap<>(other.defaultCookies) : null);
this.defaultRequest = other.defaultRequest;
this.filters = other.filters != null ? new ArrayList<>(other.filters) : null;
this.filters = (other.filters != null ? new ArrayList<>(other.filters) : null);

this.connector = other.connector;
this.strategies = other.strategies;
this.strategiesConfigurers = other.strategiesConfigurers != null ? new ArrayList<>(other.strategiesConfigurers) : null;
this.strategiesConfigurers = (other.strategiesConfigurers != null ?
new ArrayList<>(other.strategiesConfigurers) : null);
this.exchangeFunction = other.exchangeFunction;
}

Expand Down Expand Up @@ -260,8 +264,8 @@ public WebClient build() {
.map(filter -> filter.apply(exchange))
.orElse(exchange) : exchange);
return new DefaultWebClient(filteredExchange, initUriBuilderFactory(),
this.defaultHeaders != null ? unmodifiableCopy(this.defaultHeaders) : null,
this.defaultCookies != null ? unmodifiableCopy(this.defaultCookies) : null,
this.defaultHeaders != null ? HttpHeaders.readOnlyHttpHeaders(this.defaultHeaders) : null,
this.defaultCookies != null ? CollectionUtils.unmodifiableMultiValueMap(this.defaultCookies) : null,
this.defaultRequest, new DefaultWebClientBuilder(this));
}

Expand All @@ -280,10 +284,10 @@ else if (jettyClientPresent) {

private ExchangeStrategies initExchangeStrategies() {
if (CollectionUtils.isEmpty(this.strategiesConfigurers)) {
return this.strategies != null ? this.strategies : ExchangeStrategies.withDefaults();
return (this.strategies != null ? this.strategies : ExchangeStrategies.withDefaults());
}
ExchangeStrategies.Builder builder =
this.strategies != null ? this.strategies.mutate() : ExchangeStrategies.builder();
(this.strategies != null ? this.strategies.mutate() : ExchangeStrategies.builder());
this.strategiesConfigurers.forEach(configurer -> configurer.accept(builder));
return builder.build();
}
Expand All @@ -292,18 +296,10 @@ private UriBuilderFactory initUriBuilderFactory() {
if (this.uriBuilderFactory != null) {
return this.uriBuilderFactory;
}
DefaultUriBuilderFactory factory = this.baseUrl != null ?
new DefaultUriBuilderFactory(this.baseUrl) : new DefaultUriBuilderFactory();
DefaultUriBuilderFactory factory = (this.baseUrl != null ?
new DefaultUriBuilderFactory(this.baseUrl) : new DefaultUriBuilderFactory());
factory.setDefaultUriVariables(this.defaultUriVariables);
return factory;
}

private static HttpHeaders unmodifiableCopy(HttpHeaders headers) {
return HttpHeaders.readOnlyHttpHeaders(headers);
}

private static <K, V> MultiValueMap<K, V> unmodifiableCopy(MultiValueMap<K, V> map) {
return CollectionUtils.unmodifiableMultiValueMap(new LinkedMultiValueMap<>(map));
}

}

0 comments on commit 8e5de1e

Please sign in to comment.