Allow default settings of a custom HttpClient to apply

Previously the default settings of a custom HttpClient were always
ignored since a RequestConfig instance was always set no matter if
some customizations were applied or not.

This commit keeps an internal RequestConfig object instance that is
only initialized if the user applies a customization. If he does not, the
default settings of the HttpClient are used as it should.

Note that if the HttpComponents API exposed the default RequestConfig
of a given HttpClient, we would be able to merge our customizations with
the one specified by the client. Unfortunately, such API does not exist
and the "defaultSettingsOfHttpClientLostOnExecutorCustomization" test
illustrates that limitation.

Issue: SPR-12540
This commit is contained in:
Stephane Nicoll
2014-12-23 17:36:07 +01:00
parent 961574bd17
commit 71783c5d86
4 changed files with 152 additions and 49 deletions

View File

@@ -16,10 +16,6 @@
package org.springframework.http.client;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.net.URI;
import org.apache.http.HttpEntityEnclosingRequest;
@@ -27,12 +23,13 @@ import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.params.CoreConnectionPNames;
import org.junit.Test;
import org.springframework.http.HttpMethod;
import static org.junit.Assert.*;
public class HttpComponentsClientHttpRequestFactoryTests extends AbstractHttpRequestFactoryTestCase {
@Override
@@ -49,13 +46,15 @@ public class HttpComponentsClientHttpRequestFactoryTests extends AbstractHttpReq
@SuppressWarnings("deprecation")
@Test
public void assertLegacyCustomConfig() {
HttpClient httpClient = new DefaultHttpClient(); // Does not support RequestConfig
HttpClient httpClient = new org.apache.http.impl.client.DefaultHttpClient(); // Does not support RequestConfig
HttpComponentsClientHttpRequestFactory hrf = new HttpComponentsClientHttpRequestFactory(httpClient);
hrf.setConnectTimeout(1234);
assertEquals(1234, httpClient.getParams().getIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 0));
assertEquals(1234, httpClient.getParams().getIntParameter(
org.apache.http.params.CoreConnectionPNames.CONNECTION_TIMEOUT, 0));
hrf.setReadTimeout(4567);
assertEquals(4567, httpClient.getParams().getIntParameter(CoreConnectionPNames.SO_TIMEOUT, 0));
assertEquals(4567, httpClient.getParams().getIntParameter(
org.apache.http.params.CoreConnectionPNames.SO_TIMEOUT, 0));
}
@Test
@@ -80,6 +79,45 @@ public class HttpComponentsClientHttpRequestFactoryTests extends AbstractHttpReq
assertEquals("Wrong custom socket timeout", 4567, requestConfig.getSocketTimeout());
}
@Test
public void customHttpClientUsesItsDefault() throws Exception {
HttpComponentsClientHttpRequestFactory hrf =
new HttpComponentsClientHttpRequestFactory(HttpClientBuilder.create().build());
URI uri = new URI(baseUrl + "/status/ok");
HttpComponentsClientHttpRequest request = (HttpComponentsClientHttpRequest)
hrf.createRequest(uri, HttpMethod.GET);
assertNull("No custom config should be set with a custom HttpClient",
request.getHttpContext().getAttribute(HttpClientContext.REQUEST_CONFIG));
}
@Test
public void defaultSettingsOfHttpClientLostOnExecutorCustomization() throws Exception {
CloseableHttpClient client = HttpClientBuilder.create()
.setDefaultRequestConfig(RequestConfig.custom().setConnectTimeout(1234).build())
.build();
HttpComponentsClientHttpRequestFactory hrf = new HttpComponentsClientHttpRequestFactory(client);
URI uri = new URI(baseUrl + "/status/ok");
HttpComponentsClientHttpRequest request = (HttpComponentsClientHttpRequest)
hrf.createRequest(uri, HttpMethod.GET);
assertNull("No custom config should be set with a custom HttpClient",
request.getHttpContext().getAttribute(HttpClientContext.REQUEST_CONFIG));
hrf.setConnectionRequestTimeout(4567);
HttpComponentsClientHttpRequest request2 = (HttpComponentsClientHttpRequest)
hrf.createRequest(uri, HttpMethod.GET);
Object requestConfigAttribute = request2.getHttpContext().getAttribute(HttpClientContext.REQUEST_CONFIG);
assertNotNull(requestConfigAttribute);
RequestConfig requestConfig = (RequestConfig) requestConfigAttribute;
assertEquals(4567, requestConfig.getConnectionRequestTimeout());
// No way to access the request config of the HTTP client so no way to "merge" our customizations
assertEquals(-1, requestConfig.getConnectTimeout());
}
@Test
public void createHttpUriRequest() throws Exception {
URI uri = new URI("http://example.com");

View File

@@ -1,3 +1,19 @@
/*
* Copyright 2002-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.remoting.httpinvoker;
import java.io.IOException;
@@ -63,6 +79,36 @@ public class HttpComponentsHttpInvokerRequestExecutorTests {
assertEquals(10000, httpPost.getConfig().getSocketTimeout());
}
@Test
public void customHttpClientUsesItsDefault() throws IOException {
HttpComponentsHttpInvokerRequestExecutor executor =
new HttpComponentsHttpInvokerRequestExecutor(HttpClientBuilder.create().build());
HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("http://fake-service");
HttpPost httpPost = executor.createHttpPost(config);
assertNull("No custom config should be set with a custom HttpClient", httpPost.getConfig());
}
@Test
public void defaultSettingsOfHttpClientLostOnExecutorCustomization() throws IOException {
CloseableHttpClient client = HttpClientBuilder.create()
.setDefaultRequestConfig(RequestConfig.custom().setConnectTimeout(1234).build())
.build();
HttpComponentsHttpInvokerRequestExecutor executor =
new HttpComponentsHttpInvokerRequestExecutor(client);
HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("http://fake-service");
HttpPost httpPost = executor.createHttpPost(config);
assertNull("No custom config should be set with a custom HttpClient", httpPost.getConfig());
executor.setConnectionRequestTimeout(4567);
HttpPost httpPost2 = executor.createHttpPost(config);
assertNotNull(httpPost2.getConfig());
assertEquals(4567, httpPost2.getConfig().getConnectionRequestTimeout());
// No way to access the request config of the HTTP client so no way to "merge" our customizations
assertEquals(-1, httpPost2.getConfig().getConnectTimeout());
}
@Test
public void ignoreFactorySettings() throws IOException {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();