Search in sources :

Example 16 with HttpProcessor

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

the class RequesterBootstrap method create.

public HttpRequester create() {
    final HttpRequestExecutor requestExecutor = new HttpRequestExecutor(HttpRequestExecutor.DEFAULT_WAIT_FOR_CONTINUE, connReuseStrategy != null ? connReuseStrategy : DefaultConnectionReuseStrategy.INSTANCE, streamListener);
    final ManagedConnPool<HttpHost, HttpClientConnection> connPool;
    switch(poolConcurrencyPolicy != null ? poolConcurrencyPolicy : PoolConcurrencyPolicy.STRICT) {
        case LAX:
            connPool = new LaxConnPool<>(defaultMaxPerRoute > 0 ? defaultMaxPerRoute : 20, timeToLive, poolReusePolicy, new DefaultDisposalCallback<>(), connPoolListener);
            break;
        case STRICT:
        default:
            connPool = new StrictConnPool<>(defaultMaxPerRoute > 0 ? defaultMaxPerRoute : 20, maxTotal > 0 ? maxTotal : 50, timeToLive, poolReusePolicy, new DefaultDisposalCallback<>(), connPoolListener);
            break;
    }
    return new HttpRequester(requestExecutor, httpProcessor != null ? httpProcessor : HttpProcessors.client(), connPool, socketConfig != null ? socketConfig : SocketConfig.DEFAULT, connectFactory != null ? connectFactory : new DefaultBHttpClientConnectionFactory(Http1Config.DEFAULT, CharCodingConfig.DEFAULT), sslSocketFactory, sslSetupHandler != null ? sslSetupHandler : new DefaultTlsSetupHandler(), sslSessionVerifier, DefaultAddressResolver.INSTANCE);
}
Also used : HttpRequestExecutor(org.apache.hc.core5.http.impl.io.HttpRequestExecutor) DefaultDisposalCallback(org.apache.hc.core5.pool.DefaultDisposalCallback) HttpHost(org.apache.hc.core5.http.HttpHost) HttpClientConnection(org.apache.hc.core5.http.io.HttpClientConnection) DefaultBHttpClientConnectionFactory(org.apache.hc.core5.http.impl.io.DefaultBHttpClientConnectionFactory) DefaultTlsSetupHandler(org.apache.hc.core5.http.io.ssl.DefaultTlsSetupHandler)

Example 17 with HttpProcessor

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

the class TestHttpRequestExecutor method testExecutionNoResponseBody.

@Test
public void testExecutionNoResponseBody() throws Exception {
    final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
    final HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
    final HttpRequestExecutor executor = new HttpRequestExecutor();
    final HttpCoreContext context = HttpCoreContext.create();
    final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
    executor.preProcess(request, httprocessor, context);
    Mockito.verify(httprocessor).process(request, request.getEntity(), context);
    Mockito.when(conn.receiveResponseHeader()).thenReturn(new BasicClassicHttpResponse(204, "OK"));
    final ClassicHttpResponse response = executor.execute(request, conn, context);
    Mockito.verify(conn).sendRequestHeader(request);
    Mockito.verify(conn).flush();
    Mockito.verify(conn).receiveResponseHeader();
    Mockito.verify(conn, Mockito.never()).receiveResponseEntity(response);
    executor.postProcess(response, httprocessor, context);
    Mockito.verify(httprocessor).process(response, response.getEntity(), context);
}
Also used : BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) HttpProcessor(org.apache.hc.core5.http.protocol.HttpProcessor) HttpClientConnection(org.apache.hc.core5.http.io.HttpClientConnection) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) Test(org.junit.jupiter.api.Test)

Example 18 with HttpProcessor

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

the class TestHttpRequestExecutor method testExecutionSkipIntermediateResponses.

@Test
public void testExecutionSkipIntermediateResponses() throws Exception {
    final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
    final HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
    final HttpRequestExecutor executor = new HttpRequestExecutor();
    final HttpCoreContext context = HttpCoreContext.create();
    final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
    executor.preProcess(request, httprocessor, context);
    Mockito.verify(httprocessor).process(request, request.getEntity(), context);
    Mockito.when(conn.receiveResponseHeader()).thenReturn(new BasicClassicHttpResponse(100, "Continue"), new BasicClassicHttpResponse(110, "Huh?"), new BasicClassicHttpResponse(111, "Huh?"), new BasicClassicHttpResponse(200, "OK"));
    final HttpResponseInformationCallback callback = Mockito.mock(HttpResponseInformationCallback.class);
    final ClassicHttpResponse response = executor.execute(request, conn, callback, context);
    Mockito.verify(conn).sendRequestHeader(request);
    Mockito.verify(conn).flush();
    Mockito.verify(conn, Mockito.times(4)).receiveResponseHeader();
    Mockito.verify(conn, Mockito.times(1)).receiveResponseEntity(response);
    final ArgumentCaptor<HttpResponse> responseCaptor = ArgumentCaptor.forClass(HttpResponse.class);
    Mockito.verify(callback, Mockito.times(2)).execute(responseCaptor.capture(), ArgumentMatchers.eq(conn), ArgumentMatchers.eq(context));
    final List<HttpResponse> infos = responseCaptor.getAllValues();
    Assertions.assertNotNull(infos);
    Assertions.assertEquals(2, infos.size());
    final HttpResponse info1 = infos.get(0);
    Assertions.assertNotNull(info1);
    Assertions.assertEquals(110, info1.getCode());
    final HttpResponse info2 = infos.get(1);
    Assertions.assertNotNull(info2);
    Assertions.assertEquals(111, info2.getCode());
    executor.postProcess(response, httprocessor, context);
    Mockito.verify(httprocessor).process(response, response.getEntity(), context);
    Assertions.assertSame(request, context.getRequest());
    Assertions.assertSame(response, context.getResponse());
}
Also used : BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) HttpResponseInformationCallback(org.apache.hc.core5.http.io.HttpResponseInformationCallback) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) HttpProcessor(org.apache.hc.core5.http.protocol.HttpProcessor) HttpClientConnection(org.apache.hc.core5.http.io.HttpClientConnection) HttpCoreContext(org.apache.hc.core5.http.protocol.HttpCoreContext) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) Test(org.junit.jupiter.api.Test)

Example 19 with HttpProcessor

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

the class JSSEProviderIntegrationTest method testSimpleGetIdentityTransfer.

@Test
public void testSimpleGetIdentityTransfer() throws Exception {
    server.register("/hello", () -> new SingleLineResponseHandler("Hi there"));
    final HttpProcessor httpProcessor = new DefaultHttpProcessor(new RequestValidateHost());
    final InetSocketAddress serverEndpoint = server.start(httpProcessor, Http1Config.DEFAULT);
    client.start();
    for (int i = 0; i < REQ_NUM; i++) {
        final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
        try (final ClientSessionEndpoint streamEndpoint = connectFuture.get()) {
            final Future<Message<HttpResponse, String>> future = streamEndpoint.execute(new BasicRequestProducer(Method.GET, createRequestURI(serverEndpoint, "/hello")), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
            final Message<HttpResponse, String> result = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
            Assertions.assertNotNull(result);
            final HttpResponse response = result.getHead();
            final String entity = result.getBody();
            Assertions.assertNotNull(response);
            Assertions.assertEquals(200, response.getCode());
            Assertions.assertEquals("Hi there", entity);
        }
    }
}
Also used : StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) Message(org.apache.hc.core5.http.Message) HttpProcessor(org.apache.hc.core5.http.protocol.HttpProcessor) DefaultHttpProcessor(org.apache.hc.core5.http.protocol.DefaultHttpProcessor) InetSocketAddress(java.net.InetSocketAddress) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) HttpResponse(org.apache.hc.core5.http.HttpResponse) DefaultHttpProcessor(org.apache.hc.core5.http.protocol.DefaultHttpProcessor) RequestValidateHost(org.apache.hc.core5.http.protocol.RequestValidateHost) Test(org.junit.Test)

Example 20 with HttpProcessor

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

the class Http1IntegrationTest method testSimpleGetIdentityTransfer.

@Test
public void testSimpleGetIdentityTransfer() throws Exception {
    server.register("/hello", () -> new SingleLineResponseHandler("Hi there"));
    final HttpProcessor httpProcessor = new DefaultHttpProcessor(new RequestValidateHost());
    final InetSocketAddress serverEndpoint = server.start(httpProcessor, Http1Config.DEFAULT);
    client.start();
    final int reqNo = 5;
    for (int i = 0; i < reqNo; i++) {
        final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
        final ClientSessionEndpoint streamEndpoint = connectFuture.get();
        final Future<Message<HttpResponse, String>> future = streamEndpoint.execute(new BasicRequestProducer(Method.GET, createRequestURI(serverEndpoint, "/hello")), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
        final Message<HttpResponse, String> result = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
        streamEndpoint.close();
        Assertions.assertNotNull(result);
        final HttpResponse response = result.getHead();
        final String entity = result.getBody();
        Assertions.assertNotNull(response);
        Assertions.assertEquals(200, response.getCode());
        Assertions.assertEquals("Hi there", entity);
    }
}
Also used : StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) Message(org.apache.hc.core5.http.Message) HttpProcessor(org.apache.hc.core5.http.protocol.HttpProcessor) DefaultHttpProcessor(org.apache.hc.core5.http.protocol.DefaultHttpProcessor) InetSocketAddress(java.net.InetSocketAddress) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) HttpResponse(org.apache.hc.core5.http.HttpResponse) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) DefaultHttpProcessor(org.apache.hc.core5.http.protocol.DefaultHttpProcessor) RequestValidateHost(org.apache.hc.core5.http.protocol.RequestValidateHost) Test(org.junit.Test)

Aggregations

HttpProcessor (org.apache.hc.core5.http.protocol.HttpProcessor)17 HttpCoreContext (org.apache.hc.core5.http.protocol.HttpCoreContext)16 HttpClientConnection (org.apache.hc.core5.http.io.HttpClientConnection)12 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)11 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)10 Test (org.junit.jupiter.api.Test)10 BasicClassicHttpRequest (org.apache.hc.core5.http.message.BasicClassicHttpRequest)8 BasicClassicHttpResponse (org.apache.hc.core5.http.message.BasicClassicHttpResponse)8 InetSocketAddress (java.net.InetSocketAddress)7 HttpResponse (org.apache.hc.core5.http.HttpResponse)7 Timeout (org.apache.hc.core5.util.Timeout)7 HttpEntity (org.apache.hc.core5.http.HttpEntity)5 HttpHost (org.apache.hc.core5.http.HttpHost)5 Message (org.apache.hc.core5.http.Message)5 IOSession (org.apache.hc.core5.reactor.IOSession)5 Supplier (org.apache.hc.core5.function.Supplier)4 DefaultAsyncResponseExchangeHandlerFactory (org.apache.hc.core5.http.nio.support.DefaultAsyncResponseExchangeHandlerFactory)4 RequestHandlerRegistry (org.apache.hc.core5.http.protocol.RequestHandlerRegistry)4 IOEventHandlerFactory (org.apache.hc.core5.reactor.IOEventHandlerFactory)4 List (java.util.List)3