Search in sources :

Example 6 with AsyncEntityProducer

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

the class TestAsyncEntityProducers method testPathEntityProducer.

@Test
public void testPathEntityProducer() throws IOException {
    final Path path = Paths.get("src/test/resources/test-ssl.txt");
    final AsyncEntityProducer producer = AsyncEntityProducers.create(path, ContentType.APPLICATION_OCTET_STREAM, StandardOpenOption.READ, LinkOption.NOFOLLOW_LINKS);
    try {
        Assertions.assertFalse(producer.isChunked());
        Assertions.assertEquals(Files.size(path), producer.getContentLength());
        Assertions.assertEquals(ContentType.APPLICATION_OCTET_STREAM.toString(), producer.getContentType());
    } finally {
        producer.releaseResources();
    }
}
Also used : Path(java.nio.file.Path) AsyncEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer) Test(org.junit.jupiter.api.Test)

Example 7 with AsyncEntityProducer

use of org.apache.hc.core5.http.nio.AsyncEntityProducer 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 8 with AsyncEntityProducer

use of org.apache.hc.core5.http.nio.AsyncEntityProducer 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 9 with AsyncEntityProducer

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

the class TestPathAsyncEntityProducer method testTextContent.

@Test
public void testTextContent() throws Exception {
    final Path tempPath = tempFile.toPath();
    final AsyncEntityProducer producer = new PathEntityProducer(tempPath, ContentType.TEXT_PLAIN, StandardOpenOption.READ);
    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 : Path(java.nio.file.Path) 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 10 with AsyncEntityProducer

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

the class AbstractAsyncServerAuthFilter method handle.

@Override
public final AsyncDataConsumer handle(final HttpRequest request, final EntityDetails entityDetails, final HttpContext context, final AsyncFilterChain.ResponseTrigger responseTrigger, final AsyncFilterChain chain) throws HttpException, IOException {
    final Header h = request.getFirstHeader(HttpHeaders.AUTHORIZATION);
    final T challengeResponse = h != null ? parseChallengeResponse(h.getValue(), context) : null;
    final URIAuthority authority = request.getAuthority();
    final String requestUri = request.getRequestUri();
    final boolean authenticated = authenticate(challengeResponse, authority, requestUri, context);
    final Header expect = request.getFirstHeader(HttpHeaders.EXPECT);
    final boolean expectContinue = expect != null && HeaderElements.CONTINUE.equalsIgnoreCase(expect.getValue());
    if (authenticated) {
        if (expectContinue) {
            responseTrigger.sendInformation(new BasicClassicHttpResponse(HttpStatus.SC_CONTINUE));
        }
        return chain.proceed(request, entityDetails, context, responseTrigger);
    }
    final HttpResponse unauthorized = new BasicHttpResponse(HttpStatus.SC_UNAUTHORIZED);
    unauthorized.addHeader(HttpHeaders.WWW_AUTHENTICATE, generateChallenge(challengeResponse, authority, requestUri, context));
    final AsyncEntityProducer responseContentProducer = generateResponseContent(unauthorized);
    if (respondImmediately || expectContinue || entityDetails == null) {
        responseTrigger.submitResponse(unauthorized, responseContentProducer);
        return null;
    }
    return new AsyncDataConsumer() {

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

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

        @Override
        public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
            responseTrigger.submitResponse(unauthorized, responseContentProducer);
        }

        @Override
        public void releaseResources() {
            if (responseContentProducer != null) {
                responseContentProducer.releaseResources();
            }
        }
    };
}
Also used : URIAuthority(org.apache.hc.core5.net.URIAuthority) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse) ByteBuffer(java.nio.ByteBuffer) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) AsyncEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer) Header(org.apache.hc.core5.http.Header) CapacityChannel(org.apache.hc.core5.http.nio.CapacityChannel) AsyncDataConsumer(org.apache.hc.core5.http.nio.AsyncDataConsumer) List(java.util.List) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse)

Aggregations

AsyncEntityProducer (org.apache.hc.core5.http.nio.AsyncEntityProducer)27 DataStreamChannel (org.apache.hc.core5.http.nio.DataStreamChannel)18 Test (org.junit.jupiter.api.Test)16 WritableByteChannelMock (org.apache.hc.core5.http.WritableByteChannelMock)14 BasicDataStreamChannel (org.apache.hc.core5.http.nio.BasicDataStreamChannel)14 IOException (java.io.IOException)8 HttpException (org.apache.hc.core5.http.HttpException)8 HttpResponse (org.apache.hc.core5.http.HttpResponse)8 HttpRequest (org.apache.hc.core5.http.HttpRequest)7 Header (org.apache.hc.core5.http.Header)6 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)6 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)6 EntityDetails (org.apache.hc.core5.http.EntityDetails)5 ByteBuffer (java.nio.ByteBuffer)4 Path (java.nio.file.Path)4 BasicHttpRequest (org.apache.hc.core5.http.message.BasicHttpRequest)4 List (java.util.List)3 AsyncDataConsumer (org.apache.hc.core5.http.nio.AsyncDataConsumer)3 AsyncServerExchangeHandler (org.apache.hc.core5.http.nio.AsyncServerExchangeHandler)3 CapacityChannel (org.apache.hc.core5.http.nio.CapacityChannel)3