Search in sources :

Example 1 with FileEntityProducer

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

the class TestFileAsyncEntityProducer method testTextContentRepeatable.

@Test
public void testTextContentRepeatable() throws Exception {
    final AsyncEntityProducer producer = new FileEntityProducer(tempFile, ContentType.TEXT_PLAIN);
    Assertions.assertEquals(6, 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);
        producer.produce(streamChannel);
        Assertions.assertFalse(byteChannel.isOpen());
        Assertions.assertEquals("abcdef", 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) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) BasicDataStreamChannel(org.apache.hc.core5.http.nio.BasicDataStreamChannel) Test(org.junit.jupiter.api.Test)

Example 2 with FileEntityProducer

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

the class TestFileAsyncEntityProducer method testTextContent.

@Test
public void testTextContent() throws Exception {
    final AsyncEntityProducer producer = new FileEntityProducer(tempFile, ContentType.TEXT_PLAIN);
    Assertions.assertEquals(6, 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);
    producer.produce(streamChannel);
    Assertions.assertFalse(byteChannel.isOpen());
    Assertions.assertEquals("abcdef", 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) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) BasicDataStreamChannel(org.apache.hc.core5.http.nio.BasicDataStreamChannel) Test(org.junit.jupiter.api.Test)

Example 3 with FileEntityProducer

use of org.apache.hc.core5.http.nio.entity.FileEntityProducer 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

AsyncEntityProducer (org.apache.hc.core5.http.nio.AsyncEntityProducer)3 DataStreamChannel (org.apache.hc.core5.http.nio.DataStreamChannel)3 WritableByteChannelMock (org.apache.hc.core5.http.WritableByteChannelMock)2 BasicDataStreamChannel (org.apache.hc.core5.http.nio.BasicDataStreamChannel)2 Test (org.junit.jupiter.api.Test)2 IOException (java.io.IOException)1 HttpException (org.apache.hc.core5.http.HttpException)1 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)1 BasicHttpRequest (org.apache.hc.core5.http.message.BasicHttpRequest)1 AsyncClientEndpoint (org.apache.hc.core5.http.nio.AsyncClientEndpoint)1 AsyncRequestProducer (org.apache.hc.core5.http.nio.AsyncRequestProducer)1 RequestChannel (org.apache.hc.core5.http.nio.RequestChannel)1 BasicAsyncEntityProducer (org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer)1 FileEntityProducer (org.apache.hc.core5.http.nio.entity.FileEntityProducer)1 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)1