Search in sources :

Example 61 with RequestConfig

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;
}
Also used : Locale(java.util.Locale) HttpPost(org.apache.http.client.methods.HttpPost) RequestConfig(org.apache.http.client.config.RequestConfig) LocaleContext(org.springframework.context.i18n.LocaleContext)

Example 62 with RequestConfig

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);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) RequestConfig(org.apache.http.client.config.RequestConfig) HttpContext(org.apache.http.protocol.HttpContext) CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient) HttpAsyncClient(org.apache.http.nio.client.HttpAsyncClient) Configurable(org.apache.http.client.methods.Configurable)

Example 63 with RequestConfig

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);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) RequestConfig(org.apache.http.client.config.RequestConfig) HttpClient(org.apache.http.client.HttpClient) HttpContext(org.apache.http.protocol.HttpContext) Configurable(org.apache.http.client.methods.Configurable)

Example 64 with RequestConfig

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());
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient) URI(java.net.URI) Test(org.junit.Test)

Example 65 with RequestConfig

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());
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Configurable(org.apache.http.client.methods.Configurable) Test(org.junit.Test)

Aggregations

RequestConfig (org.apache.http.client.config.RequestConfig)91 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)33 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)31 HttpGet (org.apache.http.client.methods.HttpGet)29 HttpPost (org.apache.http.client.methods.HttpPost)19 Test (org.junit.Test)16 IOException (java.io.IOException)14 StringEntity (org.apache.http.entity.StringEntity)14 WxErrorException (me.chanjar.weixin.common.exception.WxErrorException)13 HttpResponse (org.apache.http.HttpResponse)12 WxError (me.chanjar.weixin.common.bean.result.WxError)11 HttpEntity (org.apache.http.HttpEntity)11 URI (java.net.URI)10 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)10 InputStream (java.io.InputStream)9 HttpHost (org.apache.http.HttpHost)9 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)9 Configurable (org.apache.http.client.methods.Configurable)7 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)7 Header (org.apache.http.Header)6