Allow default settings of a custom HttpAsyncClient to apply
This is a rework of 71783c5 for SPR-12540 for the async extension that
was not merging the internal RequestConfig as it should.
Issue: SPR-13125
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -76,6 +76,10 @@ final class HttpComponentsAsyncClientHttpRequest extends AbstractBufferingAsyncC
|
||||
return this.httpRequest.getURI();
|
||||
}
|
||||
|
||||
HttpContext getHttpContext() {
|
||||
return this.httpContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ListenableFuture<ClientHttpResponse> executeInternal(HttpHeaders headers, byte[] bufferedOutput)
|
||||
throws IOException {
|
||||
|
||||
@@ -40,6 +40,7 @@ import org.springframework.util.Assert;
|
||||
* HttpAsyncClient 4.0</a> to create requests.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Stephane Nicoll
|
||||
* @since 4.0
|
||||
* @see HttpAsyncClient
|
||||
*/
|
||||
@@ -122,21 +123,41 @@ public class HttpComponentsAsyncClientHttpRequestFactory extends HttpComponentsC
|
||||
if (context == null) {
|
||||
context = HttpClientContext.create();
|
||||
}
|
||||
// Request configuration not set in the context
|
||||
if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {
|
||||
// Use request configuration given by the user, when available
|
||||
RequestConfig config = null;
|
||||
if (httpRequest instanceof Configurable) {
|
||||
config = ((Configurable) httpRequest).getConfig();
|
||||
}
|
||||
if (config == null) {
|
||||
config = RequestConfig.DEFAULT;
|
||||
}
|
||||
context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
|
||||
}
|
||||
// Request configuration not set in the context
|
||||
if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {
|
||||
// Use request configuration given by the user, when available
|
||||
RequestConfig config = null;
|
||||
if (httpRequest instanceof Configurable) {
|
||||
config = ((Configurable) httpRequest).getConfig();
|
||||
}
|
||||
if (config == null) {
|
||||
config = createRequestConfig(asyncClient);
|
||||
}
|
||||
if (config != null) {
|
||||
context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
|
||||
}
|
||||
}
|
||||
return new HttpComponentsAsyncClientHttpRequest(asyncClient, httpRequest, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a default {@link RequestConfig} to use with the given client.
|
||||
* Can return {@code null} to indicate that no custom request config should
|
||||
* be set and the defaults of the {@link HttpAsyncClient} should be used.
|
||||
* <p>The default implementation tries to merge the defaults of the client
|
||||
* with the local customizations of this instance, if any.
|
||||
* @param client the client
|
||||
* @return the RequestConfig to use
|
||||
* @since 4.2
|
||||
*/
|
||||
protected RequestConfig createRequestConfig(HttpAsyncClient client) {
|
||||
if (client instanceof Configurable) {
|
||||
RequestConfig clientRequestConfig = ((Configurable) client).getConfig();
|
||||
return mergeRequestConfig(clientRequestConfig);
|
||||
}
|
||||
return getInternalRequestConfig();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() throws Exception {
|
||||
try {
|
||||
|
||||
@@ -245,7 +245,7 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest
|
||||
return this.requestConfig;
|
||||
}
|
||||
|
||||
private RequestConfig mergeRequestConfig(RequestConfig defaultRequestConfig) {
|
||||
protected RequestConfig mergeRequestConfig(RequestConfig defaultRequestConfig) {
|
||||
if (this.requestConfig == null) { // nothing to merge
|
||||
return defaultRequestConfig;
|
||||
}
|
||||
@@ -265,6 +265,10 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
protected final RequestConfig getInternalRequestConfig() {
|
||||
return this.requestConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Commons HttpMethodBase object for the given HTTP method and URI specification.
|
||||
* @param httpMethod the HTTP method
|
||||
|
||||
Reference in New Issue
Block a user