Search in sources :

Example 1 with BasicAsyncEntityProducer

use of org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer in project httpcomponents-core by apache.

the class TestBasicAsyncEntityProducer method testTextContentRepeatable.

@Test
public void testTextContentRepeatable() throws Exception {
    final AsyncEntityProducer producer = new BasicAsyncEntityProducer("abc", ContentType.TEXT_PLAIN);
    Assertions.assertEquals(3, producer.getContentLength());
    Assertions.assertEquals(ContentType.TEXT_PLAIN.toString(), producer.getContentType());
    Assertions.assertNull(producer.getContentEncoding());
    for (int i = 0; i < 3; i++) {
        final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
        final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
        producer.produce(streamChannel);
        Assertions.assertFalse(byteChannel.isOpen());
        Assertions.assertEquals("abc", byteChannel.dump(StandardCharsets.US_ASCII));
        producer.releaseResources();
    }
}
Also used : AsyncEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer) WritableByteChannelMock(org.apache.hc.core5.http.WritableByteChannelMock) BasicDataStreamChannel(org.apache.hc.core5.http.nio.BasicDataStreamChannel) BasicDataStreamChannel(org.apache.hc.core5.http.nio.BasicDataStreamChannel) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) Test(org.junit.jupiter.api.Test)

Example 2 with BasicAsyncEntityProducer

use of org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer in project mercury by yellow013.

the class AsyncClientFullDuplexExchange method main.

public static void main(final String[] args) throws Exception {
    final IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setSoTimeout(Timeout.ofSeconds(5)).build();
    final MinimalHttpAsyncClient client = HttpAsyncClients.createMinimal(HttpVersionPolicy.NEGOTIATE, H2Config.DEFAULT, Http1Config.DEFAULT, ioReactorConfig);
    client.start();
    final BasicHttpRequest request = BasicRequestBuilder.post("http://httpbin.org/post").build();
    final BasicRequestProducer requestProducer = new BasicRequestProducer(request, new BasicAsyncEntityProducer("stuff", ContentType.TEXT_PLAIN));
    final BasicResponseConsumer<String> responseConsumer = new BasicResponseConsumer<>(new StringAsyncEntityConsumer());
    System.out.println("Executing request " + request);
    final CountDownLatch latch = new CountDownLatch(1);
    client.execute(new AsyncClientExchangeHandler() {

        @Override
        public void releaseResources() {
            requestProducer.releaseResources();
            responseConsumer.releaseResources();
            latch.countDown();
        }

        @Override
        public void cancel() {
            System.out.println(request + " cancelled");
        }

        @Override
        public void failed(final Exception cause) {
            System.out.println(request + "->" + cause);
        }

        @Override
        public void produceRequest(final RequestChannel channel, final HttpContext context) throws HttpException, IOException {
            requestProducer.sendRequest(channel, context);
        }

        @Override
        public int available() {
            return requestProducer.available();
        }

        @Override
        public void produce(final DataStreamChannel channel) throws IOException {
            requestProducer.produce(channel);
        }

        @Override
        public void consumeInformation(final HttpResponse response, final HttpContext context) throws HttpException, IOException {
            System.out.println(request + "->" + new StatusLine(response));
        }

        @Override
        public void consumeResponse(final HttpResponse response, final EntityDetails entityDetails, final HttpContext context) throws HttpException, IOException {
            System.out.println(request + "->" + new StatusLine(response));
            responseConsumer.consumeResponse(response, entityDetails, context, null);
        }

        @Override
        public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
            responseConsumer.updateCapacity(capacityChannel);
        }

        @Override
        public void consume(final ByteBuffer src) throws IOException {
            responseConsumer.consume(src);
        }

        @Override
        public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
            responseConsumer.streamEnd(trailers);
        }
    });
    latch.await(1, TimeUnit.MINUTES);
    System.out.println("Shutting down");
    client.close(CloseMode.GRACEFUL);
}
Also used : StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) MinimalHttpAsyncClient(org.apache.hc.client5.http.impl.async.MinimalHttpAsyncClient) AsyncClientExchangeHandler(org.apache.hc.core5.http.nio.AsyncClientExchangeHandler) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) HttpResponse(org.apache.hc.core5.http.HttpResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpException(org.apache.hc.core5.http.HttpException) IOException(java.io.IOException) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) IOReactorConfig(org.apache.hc.core5.reactor.IOReactorConfig) StatusLine(org.apache.hc.core5.http.message.StatusLine) CapacityChannel(org.apache.hc.core5.http.nio.CapacityChannel) EntityDetails(org.apache.hc.core5.http.EntityDetails) BasicAsyncEntityProducer(org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer) BasicResponseConsumer(org.apache.hc.core5.http.nio.support.BasicResponseConsumer) HttpException(org.apache.hc.core5.http.HttpException) RequestChannel(org.apache.hc.core5.http.nio.RequestChannel)

Example 3 with BasicAsyncEntityProducer

use of org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer in project httpcomponents-core by apache.

the class TestBasicAsyncEntityProducer method testTextContent.

@Test
public void testTextContent() throws Exception {
    final AsyncEntityProducer producer = new BasicAsyncEntityProducer("abc", ContentType.TEXT_PLAIN);
    Assertions.assertEquals(3, producer.getContentLength());
    Assertions.assertEquals(ContentType.TEXT_PLAIN.toString(), producer.getContentType());
    Assertions.assertNull(producer.getContentEncoding());
    final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
    final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
    producer.produce(streamChannel);
    Assertions.assertFalse(byteChannel.isOpen());
    Assertions.assertEquals("abc", byteChannel.dump(StandardCharsets.US_ASCII));
}
Also used : AsyncEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer) WritableByteChannelMock(org.apache.hc.core5.http.WritableByteChannelMock) BasicDataStreamChannel(org.apache.hc.core5.http.nio.BasicDataStreamChannel) BasicDataStreamChannel(org.apache.hc.core5.http.nio.BasicDataStreamChannel) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) Test(org.junit.jupiter.api.Test)

Example 4 with BasicAsyncEntityProducer

use of org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer in project httpcomponents-core by apache.

the class TestBasicAsyncEntityProducer method testBinaryContent.

@Test
public void testBinaryContent() throws Exception {
    final AsyncEntityProducer producer = new BasicAsyncEntityProducer(new byte[] { 'a', 'b', 'c' }, ContentType.DEFAULT_BINARY);
    Assertions.assertEquals(3, producer.getContentLength());
    Assertions.assertEquals(ContentType.DEFAULT_BINARY.toString(), producer.getContentType());
    Assertions.assertNull(producer.getContentEncoding());
    final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
    final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
    producer.produce(streamChannel);
    Assertions.assertFalse(byteChannel.isOpen());
    Assertions.assertEquals("abc", byteChannel.dump(StandardCharsets.US_ASCII));
}
Also used : AsyncEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer) WritableByteChannelMock(org.apache.hc.core5.http.WritableByteChannelMock) BasicDataStreamChannel(org.apache.hc.core5.http.nio.BasicDataStreamChannel) BasicDataStreamChannel(org.apache.hc.core5.http.nio.BasicDataStreamChannel) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) Test(org.junit.jupiter.api.Test)

Example 5 with BasicAsyncEntityProducer

use of org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer in project httpcomponents-core by apache.

the class BenchmarkWorker method createRequestProducer.

private AsyncRequestProducer createRequestProducer() {
    String method = config.getMethod();
    if (method == null) {
        method = config.isHeadInsteadOfGet() ? Method.HEAD.name() : Method.GET.name();
    }
    final BasicHttpRequest request = new BasicHttpRequest(method, config.getUri());
    final String[] headers = config.getHeaders();
    if (headers != null) {
        for (final String s : headers) {
            final int pos = s.indexOf(':');
            if (pos != -1) {
                request.addHeader(new BasicHeader(s.substring(0, pos).trim(), s.substring(pos + 1)));
            }
        }
    }
    if (!config.isKeepAlive() && !config.isForceHttp2()) {
        request.addHeader(new BasicHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE));
    }
    if (config.isUseAcceptGZip()) {
        request.addHeader(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "gzip"));
    }
    if (config.getSoapAction() != null && config.getSoapAction().length() > 0) {
        request.addHeader(new BasicHeader("SOAPAction", config.getSoapAction()));
    }
    final AsyncEntityProducer entityProducer;
    if (config.getPayloadFile() != null) {
        entityProducer = new FileEntityProducer(config.getPayloadFile(), config.getContentType(), config.isUseChunking());
    } else if (config.getPayloadText() != null) {
        entityProducer = new BasicAsyncEntityProducer(config.getPayloadText(), config.getContentType(), config.isUseChunking());
    } else {
        entityProducer = null;
    }
    return new AsyncRequestProducer() {

        @Override
        public void sendRequest(final RequestChannel channel, final HttpContext context) throws HttpException, IOException {
            channel.sendRequest(request, entityProducer, context);
        }

        @Override
        public boolean isRepeatable() {
            return entityProducer == null || entityProducer.isRepeatable();
        }

        @Override
        public int available() {
            return entityProducer != null ? entityProducer.available() : 0;
        }

        @Override
        public void produce(final DataStreamChannel channel) throws IOException {
            if (entityProducer != null) {
                entityProducer.produce(channel);
            }
        }

        @Override
        public void failed(final Exception cause) {
            if (config.getVerbosity() >= 1) {
                System.out.println("Failed HTTP request: " + cause.getMessage());
            }
        }

        @Override
        public void releaseResources() {
            if (entityProducer != null) {
                entityProducer.releaseResources();
            }
        }
    };
}
Also used : FileEntityProducer(org.apache.hc.core5.http.nio.entity.FileEntityProducer) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) AsyncClientEndpoint(org.apache.hc.core5.http.nio.AsyncClientEndpoint) AsyncRequestProducer(org.apache.hc.core5.http.nio.AsyncRequestProducer) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) HttpException(org.apache.hc.core5.http.HttpException) IOException(java.io.IOException) BasicAsyncEntityProducer(org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer) AsyncEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer) BasicAsyncEntityProducer(org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer) RequestChannel(org.apache.hc.core5.http.nio.RequestChannel) BasicHeader(org.apache.hc.core5.http.message.BasicHeader)

Aggregations

DataStreamChannel (org.apache.hc.core5.http.nio.DataStreamChannel)6 AsyncEntityProducer (org.apache.hc.core5.http.nio.AsyncEntityProducer)4 IOException (java.io.IOException)3 HttpException (org.apache.hc.core5.http.HttpException)3 WritableByteChannelMock (org.apache.hc.core5.http.WritableByteChannelMock)3 BasicHttpRequest (org.apache.hc.core5.http.message.BasicHttpRequest)3 BasicDataStreamChannel (org.apache.hc.core5.http.nio.BasicDataStreamChannel)3 RequestChannel (org.apache.hc.core5.http.nio.RequestChannel)3 BasicAsyncEntityProducer (org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer)3 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)3 Test (org.junit.jupiter.api.Test)3 ByteBuffer (java.nio.ByteBuffer)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 MinimalHttpAsyncClient (org.apache.hc.client5.http.impl.async.MinimalHttpAsyncClient)2 EntityDetails (org.apache.hc.core5.http.EntityDetails)2 HttpResponse (org.apache.hc.core5.http.HttpResponse)2 StatusLine (org.apache.hc.core5.http.message.StatusLine)2 AsyncClientExchangeHandler (org.apache.hc.core5.http.nio.AsyncClientExchangeHandler)2 CapacityChannel (org.apache.hc.core5.http.nio.CapacityChannel)2 StringAsyncEntityConsumer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer)2