Search in sources :

Example 1 with DataStreamChannel

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

the class ReactiveDataProducer method produce.

@Override
public void produce(final DataStreamChannel channel) throws IOException {
    if (requestChannel.get() == null) {
        requestChannel.set(channel);
        publisher.subscribe(this);
    }
    final Throwable t = exception.get();
    final Subscription s = subscription.get();
    int buffersToReplenish = 0;
    try {
        synchronized (buffers) {
            if (t != null) {
                throw new HttpStreamResetException(t.getMessage(), t);
            } else if (this.complete.get() && buffers.isEmpty()) {
                channel.endStream();
            } else {
                while (!buffers.isEmpty()) {
                    final ByteBuffer nextBuffer = buffers.remove();
                    channel.write(nextBuffer);
                    if (nextBuffer.remaining() > 0) {
                        buffers.push(nextBuffer);
                        break;
                    } else if (s != null) {
                        // We defer the #request call until after we release the buffer lock.
                        buffersToReplenish++;
                    }
                }
            }
        }
    } finally {
        if (s != null && buffersToReplenish > 0) {
            s.request(buffersToReplenish);
        }
    }
}
Also used : Subscription(org.reactivestreams.Subscription) HttpStreamResetException(org.apache.hc.core5.http.HttpStreamResetException) ByteBuffer(java.nio.ByteBuffer)

Example 2 with DataStreamChannel

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

the class ReactiveDataProducer method signalReadiness.

private void signalReadiness() {
    final DataStreamChannel channel = requestChannel.get();
    if (channel == null) {
        throw new IllegalStateException("Output channel is not set");
    }
    channel.requestOutput();
}
Also used : DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel)

Example 3 with DataStreamChannel

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

the class TestReactiveDataProducer method testStreamThatEndsNormally.

@Test
public void testStreamThatEndsNormally() throws Exception {
    final Flowable<ByteBuffer> publisher = Flowable.just(ByteBuffer.wrap(new byte[] { '1', '2', '3' }), ByteBuffer.wrap(new byte[] { '4', '5', '6' }));
    final ReactiveDataProducer producer = new ReactiveDataProducer(publisher);
    final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
    final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
    producer.produce(streamChannel);
    Assertions.assertTrue(byteChannel.isOpen());
    Assertions.assertEquals("123456", byteChannel.dump(StandardCharsets.US_ASCII));
    producer.produce(streamChannel);
    Assertions.assertFalse(byteChannel.isOpen());
    Assertions.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
}
Also used : ByteBuffer(java.nio.ByteBuffer) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) Test(org.junit.jupiter.api.Test)

Example 4 with DataStreamChannel

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

the class TestReactiveDataProducer method testStreamThatEndsWithError.

@Test
public void testStreamThatEndsWithError() throws Exception {
    final Flowable<ByteBuffer> publisher = Flowable.concatArray(Flowable.just(ByteBuffer.wrap(new byte[] { '1' }), ByteBuffer.wrap(new byte[] { '2' }), ByteBuffer.wrap(new byte[] { '3' }), ByteBuffer.wrap(new byte[] { '4' }), ByteBuffer.wrap(new byte[] { '5' }), ByteBuffer.wrap(new byte[] { '6' })), Flowable.error(new RuntimeException()));
    final ReactiveDataProducer producer = new ReactiveDataProducer(publisher);
    final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
    final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
    producer.produce(streamChannel);
    Assertions.assertEquals("12345", byteChannel.dump(StandardCharsets.US_ASCII));
    final HttpStreamResetException exception = Assertions.assertThrows(HttpStreamResetException.class, () -> producer.produce(streamChannel));
    Assertions.assertTrue(exception.getCause() instanceof RuntimeException, "Expected published exception to be rethrown");
    Assertions.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
}
Also used : HttpStreamResetException(org.apache.hc.core5.http.HttpStreamResetException) ByteBuffer(java.nio.ByteBuffer) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) Test(org.junit.jupiter.api.Test)

Example 5 with DataStreamChannel

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

the class TestReactiveEntityProducer method testStreamThatEndsWithError.

@Test
public void testStreamThatEndsWithError() throws Exception {
    final Flowable<ByteBuffer> publisher = Flowable.concatArray(Flowable.just(ByteBuffer.wrap(new byte[] { '1' }), ByteBuffer.wrap(new byte[] { '2' }), ByteBuffer.wrap(new byte[] { '3' }), ByteBuffer.wrap(new byte[] { '4' }), ByteBuffer.wrap(new byte[] { '5' }), ByteBuffer.wrap(new byte[] { '6' })), Flowable.error(new RuntimeException()));
    final ReactiveEntityProducer entityProducer = new ReactiveEntityProducer(publisher, CONTENT_LENGTH, CONTENT_TYPE, GZIP_CONTENT_ENCODING);
    final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
    final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
    entityProducer.produce(streamChannel);
    Assertions.assertEquals("12345", byteChannel.dump(StandardCharsets.US_ASCII));
    final HttpStreamResetException exception = Assertions.assertThrows(HttpStreamResetException.class, () -> entityProducer.produce(streamChannel));
    Assertions.assertTrue(exception.getCause() instanceof RuntimeException, "Expected published exception to be rethrown");
    Assertions.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
    entityProducer.failed(exception);
    Assertions.assertEquals(1, entityProducer.available());
    Assertions.assertTrue(byteChannel.isOpen());
    Assertions.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
    Assertions.assertFalse(entityProducer.isChunked());
    Assertions.assertEquals(GZIP_CONTENT_ENCODING, entityProducer.getContentEncoding());
    Assertions.assertNull(entityProducer.getTrailerNames());
    Assertions.assertEquals(CONTENT_LENGTH, entityProducer.getContentLength());
    Assertions.assertEquals(CONTENT_TYPE.toString(), entityProducer.getContentType());
    Assertions.assertFalse(entityProducer.isRepeatable());
    Assertions.assertEquals(1, entityProducer.available());
    entityProducer.releaseResources();
}
Also used : HttpStreamResetException(org.apache.hc.core5.http.HttpStreamResetException) ByteBuffer(java.nio.ByteBuffer) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) Test(org.junit.jupiter.api.Test)

Aggregations

DataStreamChannel (org.apache.hc.core5.http.nio.DataStreamChannel)37 Test (org.junit.jupiter.api.Test)23 ByteBuffer (java.nio.ByteBuffer)18 WritableByteChannelMock (org.apache.hc.core5.http.WritableByteChannelMock)18 AsyncEntityProducer (org.apache.hc.core5.http.nio.AsyncEntityProducer)18 IOException (java.io.IOException)15 HttpException (org.apache.hc.core5.http.HttpException)15 BasicDataStreamChannel (org.apache.hc.core5.http.nio.BasicDataStreamChannel)15 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)15 EntityDetails (org.apache.hc.core5.http.EntityDetails)14 HttpResponse (org.apache.hc.core5.http.HttpResponse)14 CapacityChannel (org.apache.hc.core5.http.nio.CapacityChannel)14 Header (org.apache.hc.core5.http.Header)10 HttpRequest (org.apache.hc.core5.http.HttpRequest)9 AsyncServerExchangeHandler (org.apache.hc.core5.http.nio.AsyncServerExchangeHandler)8 ResponseChannel (org.apache.hc.core5.http.nio.ResponseChannel)8 StringAsyncEntityConsumer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer)8 BasicHttpRequest (org.apache.hc.core5.http.message.BasicHttpRequest)7 RequestChannel (org.apache.hc.core5.http.nio.RequestChannel)7 InetSocketAddress (java.net.InetSocketAddress)6