Search in sources :

Example 1 with BasicPushProducer

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

the class H2IntegrationTest method testPushRefused.

@Test
public void testPushRefused() throws Exception {
    final BlockingQueue<Exception> pushResultQueue = new LinkedBlockingDeque<>();
    final InetSocketAddress serverEndpoint = server.start();
    server.register("/hello", new Supplier<AsyncServerExchangeHandler>() {

        @Override
        public AsyncServerExchangeHandler get() {
            return new MessageExchangeHandler<Void>(new DiscardingEntityConsumer<>()) {

                @Override
                protected void handle(final Message<HttpRequest, Void> request, final AsyncServerRequestHandler.ResponseTrigger responseTrigger, final HttpContext context) throws IOException, HttpException {
                    responseTrigger.pushPromise(new BasicHttpRequest(Method.GET, createRequestURI(serverEndpoint, "/stuff")), context, new BasicPushProducer(AsyncEntityProducers.create("Pushing all sorts of stuff")) {

                        @Override
                        public void failed(final Exception cause) {
                            pushResultQueue.add(cause);
                            super.failed(cause);
                        }
                    });
                    responseTrigger.pushPromise(new BasicHttpRequest(Method.GET, createRequestURI(serverEndpoint, "/more-stuff")), context, new BasicPushProducer(new MultiLineEntityProducer("Pushing lots of stuff", 500)) {

                        @Override
                        public void failed(final Exception cause) {
                            pushResultQueue.add(cause);
                            super.failed(cause);
                        }
                    });
                    responseTrigger.submitResponse(new BasicResponseProducer(HttpStatus.SC_OK, AsyncEntityProducers.create("Hi there")), context);
                }
            };
        }
    });
    client.start(H2Config.custom().setPushEnabled(true).build());
    final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
    final ClientSessionEndpoint streamEndpoint = connectFuture.get();
    final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(new BasicRequestProducer(Method.GET, createRequestURI(serverEndpoint, "/hello")), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
    final Message<HttpResponse, String> result1 = future1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
    Assertions.assertNotNull(result1);
    final HttpResponse response1 = result1.getHead();
    final String entity1 = result1.getBody();
    Assertions.assertNotNull(response1);
    Assertions.assertEquals(200, response1.getCode());
    Assertions.assertEquals("Hi there", entity1);
    final Object result2 = pushResultQueue.poll(5, TimeUnit.SECONDS);
    Assertions.assertNotNull(result2);
    Assertions.assertTrue(result2 instanceof H2StreamResetException);
    Assertions.assertEquals(H2Error.REFUSED_STREAM.getCode(), ((H2StreamResetException) result2).getCode());
    final Object result3 = pushResultQueue.poll(5, TimeUnit.SECONDS);
    Assertions.assertNotNull(result3);
    Assertions.assertTrue(result3 instanceof H2StreamResetException);
    Assertions.assertEquals(H2Error.REFUSED_STREAM.getCode(), ((H2StreamResetException) result3).getCode());
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) Message(org.apache.hc.core5.http.Message) InetSocketAddress(java.net.InetSocketAddress) H2StreamResetException(org.apache.hc.core5.http2.H2StreamResetException) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpException(org.apache.hc.core5.http.HttpException) AsyncServerExchangeHandler(org.apache.hc.core5.http.nio.AsyncServerExchangeHandler) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) BasicPushProducer(org.apache.hc.core5.http.nio.support.BasicPushProducer) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) HttpResponse(org.apache.hc.core5.http.HttpResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) URISyntaxException(java.net.URISyntaxException) H2StreamResetException(org.apache.hc.core5.http2.H2StreamResetException) HttpException(org.apache.hc.core5.http.HttpException) ProtocolException(org.apache.hc.core5.http.ProtocolException) DiscardingEntityConsumer(org.apache.hc.core5.http.nio.entity.DiscardingEntityConsumer) AsyncServerRequestHandler(org.apache.hc.core5.http.nio.AsyncServerRequestHandler) BasicResponseProducer(org.apache.hc.core5.http.nio.support.BasicResponseProducer) Test(org.junit.Test)

Example 2 with BasicPushProducer

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

the class AsyncPushBuilder method build.

@Override
public AsyncPushProducer build() {
    final HttpResponse response = new BasicHttpResponse(getStatus());
    response.setVersion(getVersion());
    response.setHeaders(getHeaders());
    return new BasicPushProducer(response, entityProducer);
}
Also used : BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse)

Example 3 with BasicPushProducer

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

the class H2IntegrationTest method testPush.

@Test
public void testPush() throws Exception {
    final InetSocketAddress serverEndpoint = server.start();
    server.register("/hello", () -> new MessageExchangeHandler<Void>(new DiscardingEntityConsumer<>()) {

        @Override
        protected void handle(final Message<HttpRequest, Void> request, final AsyncServerRequestHandler.ResponseTrigger responseTrigger, final HttpContext context) throws IOException, HttpException {
            responseTrigger.pushPromise(new BasicHttpRequest(Method.GET, createRequestURI(serverEndpoint, "/stuff")), context, new BasicPushProducer(new MultiLineEntityProducer("Pushing lots of stuff", 500)));
            responseTrigger.submitResponse(AsyncResponseBuilder.create(HttpStatus.SC_OK).setEntity("Hi there", ContentType.TEXT_PLAIN).build(), context);
        }
    });
    client.start(H2Config.custom().setPushEnabled(true).build());
    final BlockingQueue<Message<HttpResponse, String>> pushMessageQueue = new LinkedBlockingDeque<>();
    final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
    final ClientSessionEndpoint streamEndpoint = connectFuture.get();
    final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(new BasicRequestProducer(Method.GET, createRequestURI(serverEndpoint, "/hello")), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), (request, context) -> new AbstractAsyncPushHandler<Message<HttpResponse, String>>(new BasicResponseConsumer<>(new StringAsyncEntityConsumer())) {

        @Override
        protected void handleResponse(final HttpRequest promise, final Message<HttpResponse, String> responseMessage) throws IOException, HttpException {
            try {
                pushMessageQueue.put(responseMessage);
            } catch (final InterruptedException ex) {
                Thread.currentThread().interrupt();
                throw new InterruptedIOException(ex.getMessage());
            }
        }
    }, null, null);
    final Message<HttpResponse, String> result1 = future1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
    Assertions.assertNotNull(result1);
    final HttpResponse response1 = result1.getHead();
    final String entity1 = result1.getBody();
    Assertions.assertNotNull(response1);
    Assertions.assertEquals(200, response1.getCode());
    Assertions.assertEquals("Hi there", entity1);
    final Message<HttpResponse, String> result2 = pushMessageQueue.poll(5, TimeUnit.SECONDS);
    Assertions.assertNotNull(result2);
    final HttpResponse response2 = result2.getHead();
    final String entity2 = result2.getBody();
    Assertions.assertEquals(200, response2.getCode());
    Assertions.assertNotNull(entity2);
    final StringTokenizer t1 = new StringTokenizer(entity2, "\r\n");
    while (t1.hasMoreTokens()) {
        Assertions.assertEquals("Pushing lots of stuff", t1.nextToken());
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) Message(org.apache.hc.core5.http.Message) InetSocketAddress(java.net.InetSocketAddress) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) BasicResponseConsumer(org.apache.hc.core5.http.nio.support.BasicResponseConsumer) HttpException(org.apache.hc.core5.http.HttpException) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) BasicPushProducer(org.apache.hc.core5.http.nio.support.BasicPushProducer) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) HttpResponse(org.apache.hc.core5.http.HttpResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) DiscardingEntityConsumer(org.apache.hc.core5.http.nio.entity.DiscardingEntityConsumer) AsyncServerRequestHandler(org.apache.hc.core5.http.nio.AsyncServerRequestHandler) StringTokenizer(java.util.StringTokenizer) Test(org.junit.Test)

Aggregations

HttpResponse (org.apache.hc.core5.http.HttpResponse)3 IOException (java.io.IOException)2 InterruptedIOException (java.io.InterruptedIOException)2 InetSocketAddress (java.net.InetSocketAddress)2 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)2 HttpException (org.apache.hc.core5.http.HttpException)2 HttpRequest (org.apache.hc.core5.http.HttpRequest)2 Message (org.apache.hc.core5.http.Message)2 BasicHttpRequest (org.apache.hc.core5.http.message.BasicHttpRequest)2 AsyncServerRequestHandler (org.apache.hc.core5.http.nio.AsyncServerRequestHandler)2 DiscardingEntityConsumer (org.apache.hc.core5.http.nio.entity.DiscardingEntityConsumer)2 StringAsyncEntityConsumer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer)2 BasicPushProducer (org.apache.hc.core5.http.nio.support.BasicPushProducer)2 BasicRequestProducer (org.apache.hc.core5.http.nio.support.BasicRequestProducer)2 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)2 Test (org.junit.Test)2 URISyntaxException (java.net.URISyntaxException)1 StringTokenizer (java.util.StringTokenizer)1 ExecutionException (java.util.concurrent.ExecutionException)1 ProtocolException (org.apache.hc.core5.http.ProtocolException)1