Search in sources :

Example 1 with RequestConnControl

use of org.apache.hc.core5.http.protocol.RequestConnControl in project californium by eclipse.

the class HttpClientFactory method createClient.

/**
 * Create the pooled asynchronous http client.
 *
 * @param config configuration for the http client
 * @return asynchronous http client
 * @since 3.0 (changed parameter to Configuration)
 */
public static CloseableHttpAsyncClient createClient(Configuration config) {
    int connectionIdleSecs = config.getTimeAsInt(Proxy2Config.HTTP_CONNECTION_IDLE_TIMEOUT, TimeUnit.SECONDS);
    final CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create().disableCookieManagement().setDefaultRequestConfig(createCustomRequestConfig(config)).setConnectionManager(createPoolingConnManager(config)).setVersionPolicy(HttpVersionPolicy.NEGOTIATE).setIOReactorConfig(IOReactorConfig.custom().setSoTimeout(Timeout.ofSeconds(connectionIdleSecs)).build()).addRequestInterceptorFirst(new RequestConnControl()).addRequestInterceptorFirst(new RequestDate()).addRequestInterceptorFirst(new RequestExpectContinue()).addRequestInterceptorFirst(new RequestTargetHost()).addRequestInterceptorFirst(new RequestUserAgent()).setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy() {

        @Override
        public TimeValue getKeepAliveDuration(HttpResponse response, HttpContext context) {
            TimeValue keepAlive = super.getKeepAliveDuration(response, context);
            if (keepAlive == null || keepAlive.getDuration() < 0) {
                // Keep connections alive if a keep-alive value
                // has not be explicitly set by the server
                keepAlive = KEEP_ALIVE;
            }
            return keepAlive;
        }
    }).build();
    client.start();
    return client;
}
Also used : RequestConnControl(org.apache.hc.core5.http.protocol.RequestConnControl) RequestDate(org.apache.hc.core5.http.protocol.RequestDate) RequestTargetHost(org.apache.hc.core5.http.protocol.RequestTargetHost) RequestExpectContinue(org.apache.hc.core5.http.protocol.RequestExpectContinue) DefaultConnectionKeepAliveStrategy(org.apache.hc.client5.http.impl.DefaultConnectionKeepAliveStrategy) RequestUserAgent(org.apache.hc.core5.http.protocol.RequestUserAgent) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) CloseableHttpAsyncClient(org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient) HttpResponse(org.apache.hc.core5.http.HttpResponse) TimeValue(org.apache.hc.core5.util.TimeValue)

Example 2 with RequestConnControl

use of org.apache.hc.core5.http.protocol.RequestConnControl in project httpcomponents-core by apache.

the class ClassicIntegrationTest method testAbsentHostHeader.

@Test
public void testAbsentHostHeader() throws Exception {
    // Initialize the server-side request handler
    this.server.registerHandler("*", (request, response, context) -> response.setEntity(new StringEntity("All is well", StandardCharsets.US_ASCII)));
    this.server.start();
    this.client.start(new DefaultHttpProcessor(RequestContent.INSTANCE, new RequestConnControl()));
    final HttpCoreContext context = HttpCoreContext.create();
    final HttpHost host = new HttpHost(scheme.id, "localhost", this.server.getPort());
    final BasicClassicHttpRequest get1 = new BasicClassicHttpRequest(Method.GET, "/");
    get1.setVersion(HttpVersion.HTTP_1_0);
    try (final ClassicHttpResponse response1 = this.client.execute(host, get1, context)) {
        Assertions.assertEquals(200, response1.getCode());
        EntityUtils.consume(response1.getEntity());
    }
    final BasicClassicHttpRequest get2 = new BasicClassicHttpRequest(Method.GET, "/");
    try (final ClassicHttpResponse response2 = this.client.execute(host, get2, context)) {
        Assertions.assertEquals(400, response2.getCode());
        EntityUtils.consume(response2.getEntity());
    }
}
Also used : DefaultHttpProcessor(org.apache.hc.core5.http.protocol.DefaultHttpProcessor) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) RequestConnControl(org.apache.hc.core5.http.protocol.RequestConnControl) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) HttpHost(org.apache.hc.core5.http.HttpHost) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) Test(org.junit.Test)

Aggregations

RequestConnControl (org.apache.hc.core5.http.protocol.RequestConnControl)2 DefaultConnectionKeepAliveStrategy (org.apache.hc.client5.http.impl.DefaultConnectionKeepAliveStrategy)1 CloseableHttpAsyncClient (org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient)1 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)1 HttpHost (org.apache.hc.core5.http.HttpHost)1 HttpResponse (org.apache.hc.core5.http.HttpResponse)1 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)1 BasicClassicHttpRequest (org.apache.hc.core5.http.message.BasicClassicHttpRequest)1 BasicClassicHttpResponse (org.apache.hc.core5.http.message.BasicClassicHttpResponse)1 DefaultHttpProcessor (org.apache.hc.core5.http.protocol.DefaultHttpProcessor)1 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)1 HttpCoreContext (org.apache.hc.core5.http.protocol.HttpCoreContext)1 RequestDate (org.apache.hc.core5.http.protocol.RequestDate)1 RequestExpectContinue (org.apache.hc.core5.http.protocol.RequestExpectContinue)1 RequestTargetHost (org.apache.hc.core5.http.protocol.RequestTargetHost)1 RequestUserAgent (org.apache.hc.core5.http.protocol.RequestUserAgent)1 TimeValue (org.apache.hc.core5.util.TimeValue)1 Test (org.junit.Test)1