use of org.apache.http.client.config.RequestConfig in project spring-framework by spring-projects.
the class HttpComponentsHttpInvokerRequestExecutor method createHttpPost.
/**
* Create a HttpPost for the given configuration.
* <p>The default implementation creates a standard HttpPost with
* "application/x-java-serialized-object" as "Content-Type" header.
* @param config the HTTP invoker configuration that specifies the
* target service
* @return the HttpPost instance
* @throws java.io.IOException if thrown by I/O methods
*/
protected HttpPost createHttpPost(HttpInvokerClientConfiguration config) throws IOException {
HttpPost httpPost = new HttpPost(config.getServiceUrl());
RequestConfig requestConfig = createRequestConfig(config);
if (requestConfig != null) {
httpPost.setConfig(requestConfig);
}
LocaleContext localeContext = LocaleContextHolder.getLocaleContext();
if (localeContext != null) {
Locale locale = localeContext.getLocale();
if (locale != null) {
httpPost.addHeader(HTTP_HEADER_ACCEPT_LANGUAGE, StringUtils.toLanguageTag(locale));
}
}
if (isAcceptGzipEncoding()) {
httpPost.addHeader(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
}
return httpPost;
}
use of org.apache.http.client.config.RequestConfig in project spring-framework by spring-projects.
the class HttpComponentsAsyncClientHttpRequestFactory method createAsyncRequest.
@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);
}
use of org.apache.http.client.config.RequestConfig in project spring-framework by spring-projects.
the class HttpComponentsClientHttpRequestFactory method createRequest.
@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);
}
}
use of org.apache.http.client.config.RequestConfig in project spring-framework by spring-projects.
the class HttpComponentsAsyncClientHttpRequestFactoryTests method defaultSettingsOfHttpAsyncClientLostOnExecutorCustomization.
@Test
public void defaultSettingsOfHttpAsyncClientLostOnExecutorCustomization() throws Exception {
CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create().setDefaultRequestConfig(RequestConfig.custom().setConnectTimeout(1234).build()).build();
HttpComponentsAsyncClientHttpRequestFactory factory = new HttpComponentsAsyncClientHttpRequestFactory(client);
URI uri = new URI(baseUrl + "/status/ok");
HttpComponentsAsyncClientHttpRequest request = (HttpComponentsAsyncClientHttpRequest) factory.createAsyncRequest(uri, HttpMethod.GET);
assertNull("No custom config should be set with a custom HttpClient", request.getHttpContext().getAttribute(HttpClientContext.REQUEST_CONFIG));
factory.setConnectionRequestTimeout(4567);
HttpComponentsAsyncClientHttpRequest request2 = (HttpComponentsAsyncClientHttpRequest) factory.createAsyncRequest(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());
}
use of org.apache.http.client.config.RequestConfig in project spring-framework by spring-projects.
the class HttpComponentsClientHttpRequestFactoryTests method localSettingsOverrideClientDefaultSettings.
@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());
}
Aggregations