Polish default headers/attributes in WebClient

This commit is contained in:
Rossen Stoyanchev
2018-07-03 04:43:54 -04:00
parent 43d6ceb6f0
commit da5b705328
4 changed files with 138 additions and 128 deletions

View File

@@ -333,12 +333,8 @@ class DefaultWebClient implements WebClient {
}
else {
HttpHeaders result = new HttpHeaders();
result.putAll(defaultHeaders);
result.putAll(this.headers);
defaultHeaders.forEach((name, values) -> {
if (!this.headers.containsKey(name)) {
values.forEach(value -> result.add(name, value));
}
});
return result;
}
}
@@ -352,8 +348,8 @@ class DefaultWebClient implements WebClient {
}
else {
MultiValueMap<String, String> result = new LinkedMultiValueMap<>();
result.putAll(defaultCookies);
result.putAll(this.cookies);
defaultCookies.forEach(result::putIfAbsent);
return result;
}
}

View File

@@ -77,8 +77,8 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
Assert.notNull(other, "DefaultWebClientBuilder must not be null");
this.baseUrl = other.baseUrl;
this.defaultUriVariables = (other.defaultUriVariables != null ?
new LinkedHashMap<>(other.defaultUriVariables) : null);
this.defaultUriVariables =
other.defaultUriVariables != null ? new LinkedHashMap<>(other.defaultUriVariables) : null;
this.uriBuilderFactory = other.uriBuilderFactory;
if (other.defaultHeaders != null) {
this.defaultHeaders = new HttpHeaders();
@@ -87,9 +87,9 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
else {
this.defaultHeaders = null;
}
this.defaultCookies = (other.defaultCookies != null ?
new LinkedMultiValueMap<>(other.defaultCookies) : null);
this.filters = (other.filters != null ? new ArrayList<>(other.filters) : null);
this.defaultCookies =
other.defaultCookies != null ? new LinkedMultiValueMap<>(other.defaultCookies) : null;
this.filters = other.filters != null ? new ArrayList<>(other.filters) : null;
this.connector = other.connector;
this.exchangeFunction = other.exchangeFunction;
this.exchangeStrategies = other.exchangeStrategies;
@@ -115,11 +115,8 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
}
@Override
public WebClient.Builder defaultHeader(String headerName, String... headerValues) {
HttpHeaders headers = initHeaders();
for (String headerValue : headerValues) {
headers.add(headerName, headerValue);
}
public WebClient.Builder defaultHeader(String header, String... values) {
initHeaders().put(header, Arrays.asList(values));
return this;
}
@@ -137,8 +134,8 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
}
@Override
public WebClient.Builder defaultCookie(String cookieName, String... cookieValues) {
initCookies().addAll(cookieName, Arrays.asList(cookieValues));
public WebClient.Builder defaultCookie(String cookie, String... values) {
initCookies().addAll(cookie, Arrays.asList(values));
return this;
}
@@ -202,38 +199,11 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
.map(filter -> filter.apply(exchange))
.orElse(exchange) : exchange);
return new DefaultWebClient(filteredExchange, initUriBuilderFactory(),
unmodifiableCopy(this.defaultHeaders), unmodifiableCopy(this.defaultCookies),
this.defaultHeaders != null ? unmodifiableCopy(this.defaultHeaders) : null,
this.defaultCookies != null ? unmodifiableCopy(this.defaultCookies) : null,
new DefaultWebClientBuilder(this));
}
private static @Nullable HttpHeaders unmodifiableCopy(@Nullable HttpHeaders original) {
if (original != null) {
return HttpHeaders.readOnlyHttpHeaders(original);
}
else {
return null;
}
}
private static @Nullable <K, V> MultiValueMap<K, V> unmodifiableCopy(@Nullable MultiValueMap<K, V> original) {
if (original != null) {
return CollectionUtils.unmodifiableMultiValueMap(new LinkedMultiValueMap<>(original));
}
else {
return null;
}
}
private UriBuilderFactory initUriBuilderFactory() {
if (this.uriBuilderFactory != null) {
return this.uriBuilderFactory;
}
DefaultUriBuilderFactory factory = (this.baseUrl != null ?
new DefaultUriBuilderFactory(this.baseUrl) : new DefaultUriBuilderFactory());
factory.setDefaultUriVariables(this.defaultUriVariables);
return factory;
}
private ExchangeFunction initExchangeFunction() {
if (this.exchangeFunction != null) {
return this.exchangeFunction;
@@ -246,6 +216,24 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
}
}
private UriBuilderFactory initUriBuilderFactory() {
if (this.uriBuilderFactory != null) {
return this.uriBuilderFactory;
}
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));
}
@Override
public WebClient.Builder clone() {
return new DefaultWebClientBuilder(this);

View File

@@ -226,31 +226,31 @@ public interface WebClient {
Builder uriBuilderFactory(UriBuilderFactory uriBuilderFactory);
/**
* Add the given header to all requests that have not added it.
* @param headerName the header name
* @param headerValues the header values
* A global option to specify a header to be added to every request,
* if the request does not already contain such a header.
* @param header the header name
* @param values the header values
*/
Builder defaultHeader(String headerName, String... headerValues);
Builder defaultHeader(String header, String... values);
/**
* Manipulate the default headers with the given consumer. The
* headers provided to the consumer are "live", so that the consumer
* can be used to overwrite or remove existing values.
* @param headersConsumer the headers consumer
* Provides access to every {@link #defaultHeader(String, String...)}
* declared so far with the possibility to add, replace, or remove.
* @param headersConsumer the consumer
*/
Builder defaultHeaders(Consumer<HttpHeaders> headersConsumer);
/**
* Add the given header to all requests that haven't added it.
* @param cookieName the cookie name
* @param cookieValues the cookie values
* A global option to specify a cookie to be added to every request,
* if the request does not already contain such a cookie.
* @param cookie the cookie name
* @param values the cookie values
*/
Builder defaultCookie(String cookieName, String... cookieValues);
Builder defaultCookie(String cookie, String... values);
/**
* Manipulate the default cookies with the given consumer. The
* cookies provided to the consumer are "live", so that the consumer
* can be used to overwrite or remove existing values.
* Provides access to every {@link #defaultCookie(String, String...)}
* declared so far with the possibility to add, replace, or remove.
* @param cookiesConsumer a function that consumes the cookies map
*/
Builder defaultCookies(Consumer<MultiValueMap<String, String>> cookiesConsumer);
@@ -383,10 +383,9 @@ public interface WebClient {
S cookie(String name, String value);
/**
* Manipulate the default cookies with the given consumer. The
* cookies provided to the consumer are "live", so that the consumer
* can be used to overwrite or remove existing values.
* @param cookiesConsumer a function that consumes the cookies map
* Provides access to every cookie declared so far with the possibility
* to add, replace, or remove values.
* @param cookiesConsumer the consumer to provide access to
* @return this builder
*/
S cookies(Consumer<MultiValueMap<String, String>> cookiesConsumer);
@@ -416,10 +415,9 @@ public interface WebClient {
S header(String headerName, String... headerValues);
/**
* Manipulate the default headers with the given consumer. The
* headers provided to the consumer are "live", so that the consumer
* can be used to overwrite or remove existing values.
* @param headersConsumer a function that consumes the {@code HttpHeaders}
* Provides access to every header declared so far with the possibility
* to add, replace, or remove values.
* @param headersConsumer the consumer to provide access to
* @return this builder
*/
S headers(Consumer<HttpHeaders> headersConsumer);
@@ -433,10 +431,9 @@ public interface WebClient {
S attribute(String name, Object value);
/**
* Manipulate the request attributes with the given consumer. The attributes provided to
* the consumer are "live", so that the consumer can be used to inspect attributes,
* remove attributes, or use any of the other map-provided methods.
* @param attributesConsumer a function that consumes the attributes
* Provides access to every attribute declared so far with the
* possibility to add, replace, or remove values.
* @param attributesConsumer the consumer to provide access to
* @return this builder
*/
S attributes(Consumer<Map<String, Object>> attributesConsumer);