Apply attributes to the underlying HTTP request

See gh-29958
This commit is contained in:
PhilKes
2023-02-11 14:12:18 +01:00
committed by rstoyanchev
parent 282ee02419
commit 052b6357c8
26 changed files with 374 additions and 24 deletions

View File

@@ -265,6 +265,12 @@ final class DefaultClientRequestBuilder implements ClientRequest.Builder {
requestCookies.add(name, cookie);
}));
}
Map<String, Object> requestAttributes = request.getAttributes();
if (!this.attributes.isEmpty()) {
this.attributes.forEach((key, value) -> requestAttributes.put(key, value));
}
if (this.httpRequestConsumer != null) {
this.httpRequestConsumer.accept(request);
}

View File

@@ -89,6 +89,8 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
@Nullable
private MultiValueMap<String, String> defaultCookies;
private boolean applyAttributes;
@Nullable
private Consumer<WebClient.RequestHeadersSpec<?>> defaultRequest;
@@ -137,6 +139,7 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
this.defaultCookies = (other.defaultCookies != null ?
new LinkedMultiValueMap<>(other.defaultCookies) : null);
this.applyAttributes = other.applyAttributes;
this.defaultRequest = other.defaultRequest;
this.statusHandlers = (other.statusHandlers != null ? new LinkedHashMap<>(other.statusHandlers) : null);
this.filters = (other.filters != null ? new ArrayList<>(other.filters) : null);
@@ -200,6 +203,12 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
return this;
}
@Override
public WebClient.Builder applyAttributes(boolean applyAttributes) {
this.applyAttributes = applyAttributes;
return this;
}
private MultiValueMap<String, String> initCookies() {
if (this.defaultCookies == null) {
this.defaultCookies = new LinkedMultiValueMap<>(3);
@@ -335,21 +344,24 @@ final class DefaultWebClientBuilder implements WebClient.Builder {
}
private ClientHttpConnector initConnector() {
final ClientHttpConnector connector;
if (reactorNettyClientPresent) {
return new ReactorClientHttpConnector();
connector = new ReactorClientHttpConnector();
}
else if (reactorNetty2ClientPresent) {
return new ReactorNetty2ClientHttpConnector();
}
else if (jettyClientPresent) {
return new JettyClientHttpConnector();
connector = new JettyClientHttpConnector();
}
else if (httpComponentsClientPresent) {
return new HttpComponentsClientHttpConnector();
connector = new HttpComponentsClientHttpConnector();
}
else {
return new JdkClientHttpConnector();
connector = new JdkClientHttpConnector();
}
connector.setApplyAttributes(this.applyAttributes);
return connector;
}
private ExchangeStrategies initExchangeStrategies() {

View File

@@ -250,6 +250,13 @@ public interface WebClient {
*/
Builder defaultCookies(Consumer<MultiValueMap<String, String>> cookiesConsumer);
/**
* Global option to specify whether or not the request attributes should be applied
* to the underlying http-client request, if the used {@link ClientHttpConnector} allows it.
* @param applyAttributes whether or not to apply the attributes
*/
Builder applyAttributes(boolean applyAttributes);
/**
* Provide a consumer to customize every request being built.
* @param defaultRequest the consumer to use for modifying requests