Merged HttpClient defaults with local customizations
Update HttpComponents wrapper to merge local customizations with the default of the current HttpClient instead of overriding everything. This is available as from HttpComponents 4.4. that exposes the default request config from the client via the Configurable interface. If the client does not implement such interface, the previous behaviour is applied Issue: SPR-12583
This commit is contained in:
@@ -217,7 +217,7 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest
|
||||
config = ((Configurable) httpRequest).getConfig();
|
||||
}
|
||||
if (config == null) {
|
||||
config = this.requestConfig;
|
||||
config = createRequestConfig(client);
|
||||
}
|
||||
if (config != null) {
|
||||
context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
|
||||
@@ -231,6 +231,43 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 HttpClient} 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
|
||||
*/
|
||||
protected RequestConfig createRequestConfig(CloseableHttpClient client) {
|
||||
if (client instanceof Configurable) {
|
||||
RequestConfig clientRequestConfig = ((Configurable) client).getConfig();
|
||||
return mergeRequestConfig(clientRequestConfig);
|
||||
}
|
||||
return this.requestConfig;
|
||||
}
|
||||
|
||||
private RequestConfig mergeRequestConfig(RequestConfig defaultRequestConfig) {
|
||||
if (this.requestConfig == null) { // nothing to merge
|
||||
return defaultRequestConfig;
|
||||
}
|
||||
RequestConfig.Builder builder = RequestConfig.copy(defaultRequestConfig);
|
||||
int connectTimeout = this.requestConfig.getConnectTimeout();
|
||||
if (connectTimeout >= 0) {
|
||||
builder.setConnectTimeout(connectTimeout);
|
||||
}
|
||||
int connectionRequestTimeout = this.requestConfig.getConnectionRequestTimeout();
|
||||
if (connectionRequestTimeout >= 0) {
|
||||
builder.setConnectionRequestTimeout(connectionRequestTimeout);
|
||||
}
|
||||
int socketTimeout = this.requestConfig.getSocketTimeout();
|
||||
if (socketTimeout >= 0) {
|
||||
builder.setSocketTimeout(socketTimeout);
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Commons HttpMethodBase object for the given HTTP method and URI specification.
|
||||
* @param httpMethod the HTTP method
|
||||
|
||||
@@ -28,6 +28,7 @@ import org.apache.http.NoHttpResponseException;
|
||||
import org.apache.http.StatusLine;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.Configurable;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.config.Registry;
|
||||
import org.apache.http.config.RegistryBuilder;
|
||||
@@ -269,12 +270,39 @@ public class HttpComponentsHttpInvokerRequestExecutor extends AbstractHttpInvoke
|
||||
* Create a {@link RequestConfig} for the given configuration. Can return {@code null}
|
||||
* to indicate that no custom request config should be set and the defaults of the
|
||||
* {@link HttpClient} should be used.
|
||||
* <p>The default implementation tries to merge the defaults of the client with the
|
||||
* local customizations of the instance, if any.
|
||||
* @param config the HTTP invoker configuration that specifies the
|
||||
* target service
|
||||
* @return the RequestConfig to use
|
||||
*/
|
||||
protected RequestConfig createRequestConfig(HttpInvokerClientConfiguration config) {
|
||||
return (this.requestConfig != null ? this.requestConfig : null);
|
||||
HttpClient client = getHttpClient();
|
||||
if (client instanceof Configurable) {
|
||||
RequestConfig clientRequestConfig = ((Configurable) client).getConfig();
|
||||
return mergeRequestConfig(clientRequestConfig);
|
||||
}
|
||||
return this.requestConfig;
|
||||
}
|
||||
|
||||
private RequestConfig mergeRequestConfig(RequestConfig defaultRequestConfig) {
|
||||
if (this.requestConfig == null) { // nothing to merge
|
||||
return defaultRequestConfig;
|
||||
}
|
||||
RequestConfig.Builder builder = RequestConfig.copy(defaultRequestConfig);
|
||||
int connectTimeout = this.requestConfig.getConnectTimeout();
|
||||
if (connectTimeout >= 0) {
|
||||
builder.setConnectTimeout(connectTimeout);
|
||||
}
|
||||
int connectionRequestTimeout = this.requestConfig.getConnectionRequestTimeout();
|
||||
if (connectionRequestTimeout >= 0) {
|
||||
builder.setConnectionRequestTimeout(connectionRequestTimeout);
|
||||
}
|
||||
int socketTimeout = this.requestConfig.getSocketTimeout();
|
||||
if (socketTimeout >= 0) {
|
||||
builder.setSocketTimeout(socketTimeout);
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user