Browse Source

Remove extra copy in WebClient headers/cookies

See gh-30092
pull/30120/head
James Yuzawa 2 years ago committed by rstoyanchev
parent
commit
cd8955fa72
  1. 33
      spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java
  2. 42
      spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java

33
spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java

@ -370,8 +370,8 @@ class DefaultWebTestClient implements WebTestClient { @@ -370,8 +370,8 @@ class DefaultWebTestClient implements WebTestClient {
private ClientRequest.Builder initRequestBuilder() {
ClientRequest.Builder builder = ClientRequest.create(this.httpMethod, initUri())
.headers(headers -> headers.addAll(initHeaders()))
.cookies(cookies -> cookies.addAll(initCookies()))
.headers(this::initHeaders)
.cookies(this::initCookies)
.attributes(attributes -> attributes.putAll(this.attributes));
if (this.httpRequestConsumer != null) {
builder.httpRequest(this.httpRequestConsumer);
@ -383,28 +383,21 @@ class DefaultWebTestClient implements WebTestClient { @@ -383,28 +383,21 @@ class DefaultWebTestClient implements WebTestClient {
return (this.uri != null ? this.uri : uriBuilderFactory.expand(""));
}
private HttpHeaders initHeaders() {
if (CollectionUtils.isEmpty(defaultHeaders)) {
return this.headers;
private void initHeaders(HttpHeaders out) {
if (!CollectionUtils.isEmpty(defaultHeaders)) {
out.putAll(defaultHeaders);
}
if (!CollectionUtils.isEmpty(this.headers)) {
out.putAll(this.headers);
}
HttpHeaders result = new HttpHeaders();
result.putAll(defaultHeaders);
result.putAll(this.headers);
return result;
}
private MultiValueMap<String, String> initCookies() {
if (CollectionUtils.isEmpty(this.cookies)) {
return (defaultCookies != null ? defaultCookies : new LinkedMultiValueMap<>());
private void initCookies(MultiValueMap<String, String> out) {
if (!CollectionUtils.isEmpty(defaultCookies)) {
out.putAll(defaultCookies);
}
else if (CollectionUtils.isEmpty(defaultCookies)) {
return this.cookies;
}
else {
MultiValueMap<String, String> result = new LinkedMultiValueMap<>();
result.putAll(defaultCookies);
result.putAll(this.cookies);
return result;
if (!CollectionUtils.isEmpty(this.cookies)) {
out.putAll(this.cookies);
}
}
}

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

@ -401,8 +401,6 @@ class DefaultWebClient implements WebClient { @@ -401,8 +401,6 @@ class DefaultWebClient implements WebClient {
return new HttpRequest() {
private final URI uri = initUri();
private final HttpHeaders headers = initHeaders();
@Override
public HttpMethod getMethod() {
return httpMethod;
@ -415,7 +413,9 @@ class DefaultWebClient implements WebClient { @@ -415,7 +413,9 @@ class DefaultWebClient implements WebClient {
@Override
public HttpHeaders getHeaders() {
return this.headers;
HttpHeaders headers = new HttpHeaders();
initHeaders(headers);
return headers;
}
};
}
@ -488,8 +488,8 @@ class DefaultWebClient implements WebClient { @@ -488,8 +488,8 @@ class DefaultWebClient implements WebClient {
defaultRequest.accept(this);
}
ClientRequest.Builder builder = ClientRequest.create(this.httpMethod, initUri())
.headers(headers -> headers.addAll(initHeaders()))
.cookies(cookies -> cookies.addAll(initCookies()))
.headers(this::initHeaders)
.cookies(this::initCookies)
.attributes(attributes -> attributes.putAll(this.attributes));
if (this.httpRequestConsumer != null) {
builder.httpRequest(this.httpRequestConsumer);
@ -501,33 +501,21 @@ class DefaultWebClient implements WebClient { @@ -501,33 +501,21 @@ class DefaultWebClient implements WebClient {
return (this.uri != null ? this.uri : uriBuilderFactory.expand(""));
}
private HttpHeaders initHeaders() {
if (CollectionUtils.isEmpty(this.headers)) {
return (defaultHeaders != null ? defaultHeaders : new HttpHeaders());
}
else if (CollectionUtils.isEmpty(defaultHeaders)) {
return this.headers;
private void initHeaders(HttpHeaders out) {
if (!CollectionUtils.isEmpty(defaultHeaders)) {
out.putAll(defaultHeaders);
}
else {
HttpHeaders result = new HttpHeaders();
result.putAll(defaultHeaders);
result.putAll(this.headers);
return result;
if (!CollectionUtils.isEmpty(this.headers)) {
out.putAll(this.headers);
}
}
private MultiValueMap<String, String> initCookies() {
if (CollectionUtils.isEmpty(this.cookies)) {
return (defaultCookies != null ? defaultCookies : new LinkedMultiValueMap<>());
}
else if (CollectionUtils.isEmpty(defaultCookies)) {
return this.cookies;
private void initCookies(MultiValueMap<String, String> out) {
if (!CollectionUtils.isEmpty(defaultCookies)) {
out.putAll(defaultCookies);
}
else {
MultiValueMap<String, String> result = new LinkedMultiValueMap<>();
result.putAll(defaultCookies);
result.putAll(this.cookies);
return result;
if (!CollectionUtils.isEmpty(this.cookies)) {
out.putAll(this.cookies);
}
}
}

Loading…
Cancel
Save