Java源码示例:org.apache.http.client.methods.Configurable
示例1
@Test
public void defaultSettingsOfHttpClientMergedOnExecutorCustomization() throws IOException {
RequestConfig defaultConfig = RequestConfig.custom().setConnectTimeout(1234).build();
CloseableHttpClient client = mock(CloseableHttpClient.class,
withSettings().extraInterfaces(Configurable.class));
Configurable configurable = (Configurable) client;
given(configurable.getConfig()).willReturn(defaultConfig);
HttpComponentsHttpInvokerRequestExecutor executor =
new HttpComponentsHttpInvokerRequestExecutor(client);
HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("https://fake-service");
HttpPost httpPost = executor.createHttpPost(config);
assertSame("Default client configuration is expected", defaultConfig, httpPost.getConfig());
executor.setConnectionRequestTimeout(4567);
HttpPost httpPost2 = executor.createHttpPost(config);
assertNotNull(httpPost2.getConfig());
assertEquals(4567, httpPost2.getConfig().getConnectionRequestTimeout());
// Default connection timeout merged
assertEquals(1234, httpPost2.getConfig().getConnectTimeout());
}
示例2
@Test
public void localSettingsOverrideClientDefaultSettings() throws Exception {
RequestConfig defaultConfig = RequestConfig.custom()
.setConnectTimeout(1234).setConnectionRequestTimeout(6789).build();
CloseableHttpClient client = mock(CloseableHttpClient.class,
withSettings().extraInterfaces(Configurable.class));
Configurable configurable = (Configurable) client;
given(configurable.getConfig()).willReturn(defaultConfig);
HttpComponentsHttpInvokerRequestExecutor executor =
new HttpComponentsHttpInvokerRequestExecutor(client);
executor.setConnectTimeout(5000);
HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("https://fake-service");
HttpPost httpPost = executor.createHttpPost(config);
RequestConfig requestConfig = httpPost.getConfig();
assertEquals(5000, requestConfig.getConnectTimeout());
assertEquals(6789, requestConfig.getConnectionRequestTimeout());
assertEquals(-1, requestConfig.getSocketTimeout());
}
示例3
@Test
public void defaultSettingsOfHttpClientMergedOnExecutorCustomization() throws Exception {
RequestConfig defaultConfig = RequestConfig.custom().setConnectTimeout(1234).build();
CloseableHttpClient client = mock(CloseableHttpClient.class,
withSettings().extraInterfaces(Configurable.class));
Configurable configurable = (Configurable) client;
given(configurable.getConfig()).willReturn(defaultConfig);
HttpComponentsClientHttpRequestFactory hrf = new HttpComponentsClientHttpRequestFactory(client);
assertSame("Default client configuration is expected", defaultConfig, retrieveRequestConfig(hrf));
hrf.setConnectionRequestTimeout(4567);
RequestConfig requestConfig = retrieveRequestConfig(hrf);
assertNotNull(requestConfig);
assertEquals(4567, requestConfig.getConnectionRequestTimeout());
// Default connection timeout merged
assertEquals(1234, requestConfig.getConnectTimeout());
}
示例4
@Test
public void localSettingsOverrideClientDefaultSettings() throws Exception {
RequestConfig defaultConfig = RequestConfig.custom()
.setConnectTimeout(1234).setConnectionRequestTimeout(6789).build();
CloseableHttpClient client = mock(CloseableHttpClient.class,
withSettings().extraInterfaces(Configurable.class));
Configurable configurable = (Configurable) client;
given(configurable.getConfig()).willReturn(defaultConfig);
HttpComponentsClientHttpRequestFactory hrf = new HttpComponentsClientHttpRequestFactory(client);
hrf.setConnectTimeout(5000);
RequestConfig requestConfig = retrieveRequestConfig(hrf);
assertEquals(5000, requestConfig.getConnectTimeout());
assertEquals(6789, requestConfig.getConnectionRequestTimeout());
assertEquals(-1, requestConfig.getSocketTimeout());
}
示例5
@Test
public void defaultSettingsOfHttpClientMergedOnExecutorCustomization() throws IOException {
RequestConfig defaultConfig = RequestConfig.custom().setConnectTimeout(1234).build();
CloseableHttpClient client = mock(CloseableHttpClient.class,
withSettings().extraInterfaces(Configurable.class));
Configurable configurable = (Configurable) client;
when(configurable.getConfig()).thenReturn(defaultConfig);
HttpComponentsHttpInvokerRequestExecutor executor =
new HttpComponentsHttpInvokerRequestExecutor(client);
HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("http://fake-service");
HttpPost httpPost = executor.createHttpPost(config);
assertSame("Default client configuration is expected", defaultConfig, httpPost.getConfig());
executor.setConnectionRequestTimeout(4567);
HttpPost httpPost2 = executor.createHttpPost(config);
assertNotNull(httpPost2.getConfig());
assertEquals(4567, httpPost2.getConfig().getConnectionRequestTimeout());
// Default connection timeout merged
assertEquals(1234, httpPost2.getConfig().getConnectTimeout());
}
示例6
@Test
public void localSettingsOverrideClientDefaultSettings() throws Exception {
RequestConfig defaultConfig = RequestConfig.custom()
.setConnectTimeout(1234).setConnectionRequestTimeout(6789).build();
CloseableHttpClient client = mock(CloseableHttpClient.class,
withSettings().extraInterfaces(Configurable.class));
Configurable configurable = (Configurable) client;
when(configurable.getConfig()).thenReturn(defaultConfig);
HttpComponentsHttpInvokerRequestExecutor executor =
new HttpComponentsHttpInvokerRequestExecutor(client);
executor.setConnectTimeout(5000);
HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("http://fake-service");
HttpPost httpPost = executor.createHttpPost(config);
RequestConfig requestConfig = httpPost.getConfig();
assertEquals(5000, requestConfig.getConnectTimeout());
assertEquals(6789, requestConfig.getConnectionRequestTimeout());
assertEquals(-1, requestConfig.getSocketTimeout());
}
示例7
@Test
public void defaultSettingsOfHttpClientMergedOnExecutorCustomization() throws Exception {
RequestConfig defaultConfig = RequestConfig.custom().setConnectTimeout(1234).build();
CloseableHttpClient client = mock(CloseableHttpClient.class,
withSettings().extraInterfaces(Configurable.class));
Configurable configurable = (Configurable) client;
when(configurable.getConfig()).thenReturn(defaultConfig);
HttpComponentsClientHttpRequestFactory hrf = new HttpComponentsClientHttpRequestFactory(client);
assertSame("Default client configuration is expected", defaultConfig, retrieveRequestConfig(hrf));
hrf.setConnectionRequestTimeout(4567);
RequestConfig requestConfig = retrieveRequestConfig(hrf);
assertNotNull(requestConfig);
assertEquals(4567, requestConfig.getConnectionRequestTimeout());
// Default connection timeout merged
assertEquals(1234, requestConfig.getConnectTimeout());
}
示例8
@Test
public void localSettingsOverrideClientDefaultSettings() throws Exception {
RequestConfig defaultConfig = RequestConfig.custom()
.setConnectTimeout(1234).setConnectionRequestTimeout(6789).build();
CloseableHttpClient client = mock(CloseableHttpClient.class,
withSettings().extraInterfaces(Configurable.class));
Configurable configurable = (Configurable) client;
when(configurable.getConfig()).thenReturn(defaultConfig);
HttpComponentsClientHttpRequestFactory hrf = new HttpComponentsClientHttpRequestFactory(client);
hrf.setConnectTimeout(5000);
RequestConfig requestConfig = retrieveRequestConfig(hrf);
assertEquals(5000, requestConfig.getConnectTimeout());
assertEquals(6789, requestConfig.getConnectionRequestTimeout());
assertEquals(-1, requestConfig.getSocketTimeout());
}
示例9
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException {
HttpAsyncClient asyncClient = getHttpAsyncClient();
startAsyncClient();
HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri);
postProcessHttpRequest(httpRequest);
HttpContext context = createHttpContext(httpMethod, uri);
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 = createRequestConfig(asyncClient);
}
if (config != null) {
context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
}
}
return new HttpComponentsAsyncClientHttpRequest(asyncClient, httpRequest, context);
}
示例10
@Test
public void defaultSettingsOfHttpClientMergedOnExecutorCustomization() throws IOException {
RequestConfig defaultConfig = RequestConfig.custom().setConnectTimeout(1234).build();
CloseableHttpClient client = mock(CloseableHttpClient.class,
withSettings().extraInterfaces(Configurable.class));
Configurable configurable = (Configurable) client;
when(configurable.getConfig()).thenReturn(defaultConfig);
HttpComponentsHttpInvokerRequestExecutor executor =
new HttpComponentsHttpInvokerRequestExecutor(client);
HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("http://fake-service");
HttpPost httpPost = executor.createHttpPost(config);
assertSame("Default client configuration is expected", defaultConfig, httpPost.getConfig());
executor.setConnectionRequestTimeout(4567);
HttpPost httpPost2 = executor.createHttpPost(config);
assertNotNull(httpPost2.getConfig());
assertEquals(4567, httpPost2.getConfig().getConnectionRequestTimeout());
// Default connection timeout merged
assertEquals(1234, httpPost2.getConfig().getConnectTimeout());
}
示例11
@Test
public void localSettingsOverrideClientDefaultSettings() throws Exception {
RequestConfig defaultConfig = RequestConfig.custom()
.setConnectTimeout(1234).setConnectionRequestTimeout(6789).build();
CloseableHttpClient client = mock(CloseableHttpClient.class,
withSettings().extraInterfaces(Configurable.class));
Configurable configurable = (Configurable) client;
when(configurable.getConfig()).thenReturn(defaultConfig);
HttpComponentsHttpInvokerRequestExecutor executor =
new HttpComponentsHttpInvokerRequestExecutor(client);
executor.setConnectTimeout(5000);
HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("http://fake-service");
HttpPost httpPost = executor.createHttpPost(config);
RequestConfig requestConfig = httpPost.getConfig();
assertEquals(5000, requestConfig.getConnectTimeout());
assertEquals(6789, requestConfig.getConnectionRequestTimeout());
assertEquals(-1, requestConfig.getSocketTimeout());
}
示例12
@Test
public void defaultSettingsOfHttpClientMergedOnExecutorCustomization() throws Exception {
RequestConfig defaultConfig = RequestConfig.custom().setConnectTimeout(1234).build();
CloseableHttpClient client = mock(CloseableHttpClient.class,
withSettings().extraInterfaces(Configurable.class));
Configurable configurable = (Configurable) client;
when(configurable.getConfig()).thenReturn(defaultConfig);
HttpComponentsClientHttpRequestFactory hrf = new HttpComponentsClientHttpRequestFactory(client);
assertSame("Default client configuration is expected", defaultConfig, retrieveRequestConfig(hrf));
hrf.setConnectionRequestTimeout(4567);
RequestConfig requestConfig = retrieveRequestConfig(hrf);
assertNotNull(requestConfig);
assertEquals(4567, requestConfig.getConnectionRequestTimeout());
// Default connection timeout merged
assertEquals(1234, requestConfig.getConnectTimeout());
}
示例13
@Test
public void localSettingsOverrideClientDefaultSettings() throws Exception {
RequestConfig defaultConfig = RequestConfig.custom()
.setConnectTimeout(1234).setConnectionRequestTimeout(6789).build();
CloseableHttpClient client = mock(CloseableHttpClient.class,
withSettings().extraInterfaces(Configurable.class));
Configurable configurable = (Configurable) client;
when(configurable.getConfig()).thenReturn(defaultConfig);
HttpComponentsClientHttpRequestFactory hrf = new HttpComponentsClientHttpRequestFactory(client);
hrf.setConnectTimeout(5000);
RequestConfig requestConfig = retrieveRequestConfig(hrf);
assertEquals(5000, requestConfig.getConnectTimeout());
assertEquals(6789, requestConfig.getConnectionRequestTimeout());
assertEquals(-1, requestConfig.getSocketTimeout());
}
示例14
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException {
HttpAsyncClient client = startAsyncClient();
HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri);
postProcessHttpRequest(httpRequest);
HttpContext context = createHttpContext(httpMethod, uri);
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 = createRequestConfig(client);
}
if (config != null) {
context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
}
}
return new HttpComponentsAsyncClientHttpRequest(client, httpRequest, context);
}
示例15
@Override
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
HttpClient client = getHttpClient();
HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri);
postProcessHttpRequest(httpRequest);
HttpContext context = createHttpContext(httpMethod, uri);
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 = createRequestConfig(client);
}
if (config != null) {
context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
}
}
if (this.bufferRequestBody) {
return new HttpComponentsClientHttpRequest(client, httpRequest, context);
}
else {
return new HttpComponentsStreamingClientHttpRequest(client, httpRequest, context);
}
}
示例16
@Test
public void mergeBasedOnCurrentHttpClient() throws Exception {
RequestConfig defaultConfig = RequestConfig.custom()
.setSocketTimeout(1234).build();
final CloseableHttpClient client = mock(CloseableHttpClient.class,
withSettings().extraInterfaces(Configurable.class));
Configurable configurable = (Configurable) client;
given(configurable.getConfig()).willReturn(defaultConfig);
HttpComponentsHttpInvokerRequestExecutor executor =
new HttpComponentsHttpInvokerRequestExecutor() {
@Override
public HttpClient getHttpClient() {
return client;
}
};
executor.setReadTimeout(5000);
HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("https://fake-service");
HttpPost httpPost = executor.createHttpPost(config);
RequestConfig requestConfig = httpPost.getConfig();
assertEquals(-1, requestConfig.getConnectTimeout());
assertEquals(-1, requestConfig.getConnectionRequestTimeout());
assertEquals(5000, requestConfig.getSocketTimeout());
// Update the Http client so that it returns an updated config
RequestConfig updatedDefaultConfig = RequestConfig.custom()
.setConnectTimeout(1234).build();
given(configurable.getConfig()).willReturn(updatedDefaultConfig);
executor.setReadTimeout(7000);
HttpPost httpPost2 = executor.createHttpPost(config);
RequestConfig requestConfig2 = httpPost2.getConfig();
assertEquals(1234, requestConfig2.getConnectTimeout());
assertEquals(-1, requestConfig2.getConnectionRequestTimeout());
assertEquals(7000, requestConfig2.getSocketTimeout());
}
示例17
@Test
public void mergeBasedOnCurrentHttpClient() throws Exception {
RequestConfig defaultConfig = RequestConfig.custom()
.setSocketTimeout(1234).build();
final CloseableHttpClient client = mock(CloseableHttpClient.class,
withSettings().extraInterfaces(Configurable.class));
Configurable configurable = (Configurable) client;
given(configurable.getConfig()).willReturn(defaultConfig);
HttpComponentsClientHttpRequestFactory hrf = new HttpComponentsClientHttpRequestFactory() {
@Override
public HttpClient getHttpClient() {
return client;
}
};
hrf.setReadTimeout(5000);
RequestConfig requestConfig = retrieveRequestConfig(hrf);
assertEquals(-1, requestConfig.getConnectTimeout());
assertEquals(-1, requestConfig.getConnectionRequestTimeout());
assertEquals(5000, requestConfig.getSocketTimeout());
// Update the Http client so that it returns an updated config
RequestConfig updatedDefaultConfig = RequestConfig.custom()
.setConnectTimeout(1234).build();
given(configurable.getConfig()).willReturn(updatedDefaultConfig);
hrf.setReadTimeout(7000);
RequestConfig requestConfig2 = retrieveRequestConfig(hrf);
assertEquals(1234, requestConfig2.getConnectTimeout());
assertEquals(-1, requestConfig2.getConnectionRequestTimeout());
assertEquals(7000, requestConfig2.getSocketTimeout());
}
示例18
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException {
HttpAsyncClient client = startAsyncClient();
HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri);
postProcessHttpRequest(httpRequest);
HttpContext context = createHttpContext(httpMethod, uri);
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 = createRequestConfig(client);
}
if (config != null) {
context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
}
}
return new HttpComponentsAsyncClientHttpRequest(client, httpRequest, context);
}
示例19
@Override
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
HttpClient client = getHttpClient();
HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri);
postProcessHttpRequest(httpRequest);
HttpContext context = createHttpContext(httpMethod, uri);
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 = createRequestConfig(client);
}
if (config != null) {
context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
}
}
if (this.bufferRequestBody) {
return new HttpComponentsClientHttpRequest(client, httpRequest, context);
}
else {
return new HttpComponentsStreamingClientHttpRequest(client, httpRequest, context);
}
}
示例20
@Test
public void mergeBasedOnCurrentHttpClient() throws Exception {
RequestConfig defaultConfig = RequestConfig.custom()
.setSocketTimeout(1234).build();
final CloseableHttpClient client = mock(CloseableHttpClient.class,
withSettings().extraInterfaces(Configurable.class));
Configurable configurable = (Configurable) client;
when(configurable.getConfig()).thenReturn(defaultConfig);
HttpComponentsHttpInvokerRequestExecutor executor =
new HttpComponentsHttpInvokerRequestExecutor() {
@Override
public HttpClient getHttpClient() {
return client;
}
};
executor.setReadTimeout(5000);
HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("http://fake-service");
HttpPost httpPost = executor.createHttpPost(config);
RequestConfig requestConfig = httpPost.getConfig();
assertEquals(-1, requestConfig.getConnectTimeout());
assertEquals(-1, requestConfig.getConnectionRequestTimeout());
assertEquals(5000, requestConfig.getSocketTimeout());
// Update the Http client so that it returns an updated config
RequestConfig updatedDefaultConfig = RequestConfig.custom()
.setConnectTimeout(1234).build();
when(configurable.getConfig()).thenReturn(updatedDefaultConfig);
executor.setReadTimeout(7000);
HttpPost httpPost2 = executor.createHttpPost(config);
RequestConfig requestConfig2 = httpPost2.getConfig();
assertEquals(1234, requestConfig2.getConnectTimeout());
assertEquals(-1, requestConfig2.getConnectionRequestTimeout());
assertEquals(7000, requestConfig2.getSocketTimeout());
}
示例21
@Test
public void mergeBasedOnCurrentHttpClient() throws Exception {
RequestConfig defaultConfig = RequestConfig.custom()
.setSocketTimeout(1234).build();
final CloseableHttpClient client = mock(CloseableHttpClient.class,
withSettings().extraInterfaces(Configurable.class));
Configurable configurable = (Configurable) client;
when(configurable.getConfig()).thenReturn(defaultConfig);
HttpComponentsClientHttpRequestFactory hrf = new HttpComponentsClientHttpRequestFactory() {
@Override
public HttpClient getHttpClient() {
return client;
}
};
hrf.setReadTimeout(5000);
RequestConfig requestConfig = retrieveRequestConfig(hrf);
assertEquals(-1, requestConfig.getConnectTimeout());
assertEquals(-1, requestConfig.getConnectionRequestTimeout());
assertEquals(5000, requestConfig.getSocketTimeout());
// Update the Http client so that it returns an updated config
RequestConfig updatedDefaultConfig = RequestConfig.custom()
.setConnectTimeout(1234).build();
when(configurable.getConfig()).thenReturn(updatedDefaultConfig);
hrf.setReadTimeout(7000);
RequestConfig requestConfig2 = retrieveRequestConfig(hrf);
assertEquals(1234, requestConfig2.getConnectTimeout());
assertEquals(-1, requestConfig2.getConnectionRequestTimeout());
assertEquals(7000, requestConfig2.getSocketTimeout());
}
示例22
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException {
startAsyncClient();
HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri);
postProcessHttpRequest(httpRequest);
HttpContext context = createHttpContext(httpMethod, uri);
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 = createRequestConfig(getAsyncClient());
}
if (config != null) {
context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
}
}
return new HttpComponentsAsyncClientHttpRequest(getAsyncClient(), httpRequest, context);
}
示例23
@Override
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri);
postProcessHttpRequest(httpRequest);
HttpContext context = createHttpContext(httpMethod, uri);
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 = createRequestConfig(getHttpClient());
}
if (config != null) {
context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
}
}
if (this.bufferRequestBody) {
return new HttpComponentsClientHttpRequest(getHttpClient(), httpRequest, context);
}
else {
return new HttpComponentsStreamingClientHttpRequest(getHttpClient(), httpRequest, context);
}
}
示例24
public static RequestConfig getRequestConfig(Object client) {
if (client == null)
return null;
if (client instanceof Configurable)
return ((Configurable) client).getConfig();
return getRequestConfigFromField(client);
}
示例25
@Override
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
HttpClient client = getHttpClient();
Assert.state(client != null, "Synchronous execution requires an HttpClient to be set");
HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri);
postProcessHttpRequest(httpRequest);
HttpContext context = createHttpContext(httpMethod, uri);
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 = createRequestConfig(client);
}
if (config != null) {
context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
}
}
if (this.bufferRequestBody) {
return new HttpComponentsClientHttpRequest(client, httpRequest, context);
}
else {
return new HttpComponentsStreamingClientHttpRequest(client, httpRequest, context);
}
}
示例26
@Test
public void mergeBasedOnCurrentHttpClient() throws Exception {
RequestConfig defaultConfig = RequestConfig.custom()
.setSocketTimeout(1234).build();
final CloseableHttpClient client = mock(CloseableHttpClient.class,
withSettings().extraInterfaces(Configurable.class));
Configurable configurable = (Configurable) client;
when(configurable.getConfig()).thenReturn(defaultConfig);
HttpComponentsHttpInvokerRequestExecutor executor =
new HttpComponentsHttpInvokerRequestExecutor() {
@Override
public HttpClient getHttpClient() {
return client;
}
};
executor.setReadTimeout(5000);
HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("http://fake-service");
HttpPost httpPost = executor.createHttpPost(config);
RequestConfig requestConfig = httpPost.getConfig();
assertEquals(-1, requestConfig.getConnectTimeout());
assertEquals(-1, requestConfig.getConnectionRequestTimeout());
assertEquals(5000, requestConfig.getSocketTimeout());
// Update the Http client so that it returns an updated config
RequestConfig updatedDefaultConfig = RequestConfig.custom()
.setConnectTimeout(1234).build();
when(configurable.getConfig()).thenReturn(updatedDefaultConfig);
executor.setReadTimeout(7000);
HttpPost httpPost2 = executor.createHttpPost(config);
RequestConfig requestConfig2 = httpPost2.getConfig();
assertEquals(1234, requestConfig2.getConnectTimeout());
assertEquals(-1, requestConfig2.getConnectionRequestTimeout());
assertEquals(7000, requestConfig2.getSocketTimeout());
}
示例27
@Test
public void mergeBasedOnCurrentHttpClient() throws Exception {
RequestConfig defaultConfig = RequestConfig.custom()
.setSocketTimeout(1234).build();
final CloseableHttpClient client = mock(CloseableHttpClient.class,
withSettings().extraInterfaces(Configurable.class));
Configurable configurable = (Configurable) client;
when(configurable.getConfig()).thenReturn(defaultConfig);
HttpComponentsClientHttpRequestFactory hrf = new HttpComponentsClientHttpRequestFactory() {
@Override
public HttpClient getHttpClient() {
return client;
}
};
hrf.setReadTimeout(5000);
RequestConfig requestConfig = retrieveRequestConfig(hrf);
assertEquals(-1, requestConfig.getConnectTimeout());
assertEquals(-1, requestConfig.getConnectionRequestTimeout());
assertEquals(5000, requestConfig.getSocketTimeout());
// Update the Http client so that it returns an updated config
RequestConfig updatedDefaultConfig = RequestConfig.custom()
.setConnectTimeout(1234).build();
when(configurable.getConfig()).thenReturn(updatedDefaultConfig);
hrf.setReadTimeout(7000);
RequestConfig requestConfig2 = retrieveRequestConfig(hrf);
assertEquals(1234, requestConfig2.getConnectTimeout());
assertEquals(-1, requestConfig2.getConnectionRequestTimeout());
assertEquals(7000, requestConfig2.getSocketTimeout());
}
示例28
@Test
void shouldApplyTimeouts() {
assertThat("Configurable http client expected", exampleHttpClient, is(instanceOf(Configurable.class)));
final RequestConfig config = ((Configurable) exampleHttpClient).getConfig();
assertThat(config.getSocketTimeout(), is(34 * 60 * 60 * 1000));
assertThat(config.getConnectTimeout(), is(12 * 60 * 1000));
}
示例29
@Test
public void createClient() throws Exception {
final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(100)
.setConnectTimeout(200).setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
CloseableHttpClient httpClient = new DefaultApacheHttpClientFactory(
HttpClientBuilder.create()).createBuilder()
.setConnectionManager(mock(HttpClientConnectionManager.class))
.setDefaultRequestConfig(requestConfig).build();
BDDAssertions.then(httpClient).isInstanceOf(Configurable.class);
RequestConfig config = ((Configurable) httpClient).getConfig();
then(config.getSocketTimeout()).isEqualTo(100);
then(config.getConnectTimeout()).isEqualTo(200);
then(config.getCookieSpec()).isEqualTo(CookieSpecs.IGNORE_COOKIES);
}
示例30
/**
* 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
*/
@Nullable
protected RequestConfig createRequestConfig(HttpInvokerClientConfiguration config) {
HttpClient client = getHttpClient();
if (client instanceof Configurable) {
RequestConfig clientRequestConfig = ((Configurable) client).getConfig();
return mergeRequestConfig(clientRequestConfig);
}
return this.requestConfig;
}