Search in sources :

Example 1 with AsyncPushProducer

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

the class TerminalAsyncServerFilter method handle.

@Override
public AsyncDataConsumer handle(final HttpRequest request, final EntityDetails entityDetails, final HttpContext context, final AsyncFilterChain.ResponseTrigger responseTrigger, final AsyncFilterChain chain) throws HttpException, IOException {
    final AsyncServerExchangeHandler exchangeHandler = handlerFactory.create(request, context);
    if (exchangeHandler != null) {
        exchangeHandler.handleRequest(request, entityDetails, new ResponseChannel() {

            @Override
            public void sendInformation(final HttpResponse response, final HttpContext httpContext) throws HttpException, IOException {
                responseTrigger.sendInformation(response);
            }

            @Override
            public void sendResponse(final HttpResponse response, final EntityDetails entityDetails, final HttpContext httpContext) throws HttpException, IOException {
                responseTrigger.submitResponse(response, entityDetails != null ? new AsyncEntityProducer() {

                    @Override
                    public void failed(final Exception cause) {
                        exchangeHandler.failed(cause);
                    }

                    @Override
                    public boolean isRepeatable() {
                        return false;
                    }

                    @Override
                    public long getContentLength() {
                        return entityDetails.getContentLength();
                    }

                    @Override
                    public String getContentType() {
                        return entityDetails.getContentType();
                    }

                    @Override
                    public String getContentEncoding() {
                        return entityDetails.getContentEncoding();
                    }

                    @Override
                    public boolean isChunked() {
                        return entityDetails.isChunked();
                    }

                    @Override
                    public Set<String> getTrailerNames() {
                        return entityDetails.getTrailerNames();
                    }

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

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

                    @Override
                    public void releaseResources() {
                        exchangeHandler.releaseResources();
                    }
                } : null);
            }

            @Override
            public void pushPromise(final HttpRequest promise, final AsyncPushProducer pushProducer, final HttpContext httpContext) throws HttpException, IOException {
                responseTrigger.pushPromise(promise, pushProducer);
            }
        }, context);
        return exchangeHandler;
    }
    responseTrigger.submitResponse(new BasicHttpResponse(HttpStatus.SC_NOT_FOUND), AsyncEntityProducers.create("Not found"));
    return null;
}
Also used : HttpRequest(org.apache.hc.core5.http.HttpRequest) Set(java.util.Set) AsyncPushProducer(org.apache.hc.core5.http.nio.AsyncPushProducer) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse) IOException(java.io.IOException) HttpException(org.apache.hc.core5.http.HttpException) IOException(java.io.IOException) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) ResponseChannel(org.apache.hc.core5.http.nio.ResponseChannel) AsyncEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) EntityDetails(org.apache.hc.core5.http.EntityDetails) HttpException(org.apache.hc.core5.http.HttpException) AsyncServerExchangeHandler(org.apache.hc.core5.http.nio.AsyncServerExchangeHandler)

Example 2 with AsyncPushProducer

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

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

the class AsyncServerFilterChainExchangeHandlerFactory method create.

@Override
public AsyncServerExchangeHandler create(final HttpRequest request, final HttpContext context) throws HttpException {
    return new AsyncServerExchangeHandler() {

        private final AtomicReference<AsyncDataConsumer> dataConsumerRef = new AtomicReference<>();

        private final AtomicReference<AsyncResponseProducer> responseProducerRef = new AtomicReference<>();

        @Override
        public void handleRequest(final HttpRequest request, final EntityDetails entityDetails, final ResponseChannel responseChannel, final HttpContext context) throws HttpException, IOException {
            dataConsumerRef.set(filterChain.handle(request, entityDetails, context, new AsyncFilterChain.ResponseTrigger() {

                @Override
                public void sendInformation(final HttpResponse response) throws HttpException, IOException {
                    responseChannel.sendInformation(response, context);
                }

                @Override
                public void submitResponse(final HttpResponse response, final AsyncEntityProducer entityProducer) throws HttpException, IOException {
                    final AsyncResponseProducer responseProducer = new BasicResponseProducer(response, entityProducer);
                    responseProducerRef.set(responseProducer);
                    responseProducer.sendResponse(responseChannel, context);
                }

                @Override
                public void pushPromise(final HttpRequest promise, final AsyncPushProducer responseProducer) throws HttpException, IOException {
                    responseChannel.pushPromise(promise, responseProducer, context);
                }
            }));
        }

        @Override
        public void failed(final Exception cause) {
            if (exceptionCallback != null) {
                exceptionCallback.execute(cause);
            }
            final AsyncResponseProducer handler = responseProducerRef.get();
            if (handler != null) {
                handler.failed(cause);
            }
        }

        @Override
        public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
            final AsyncDataConsumer dataConsumer = dataConsumerRef.get();
            if (dataConsumer != null) {
                dataConsumer.updateCapacity(capacityChannel);
            } else {
                capacityChannel.update(Integer.MAX_VALUE);
            }
        }

        @Override
        public void consume(final ByteBuffer src) throws IOException {
            final AsyncDataConsumer dataConsumer = dataConsumerRef.get();
            if (dataConsumer != null) {
                dataConsumer.consume(src);
            }
        }

        @Override
        public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
            final AsyncDataConsumer dataConsumer = dataConsumerRef.get();
            if (dataConsumer != null) {
                dataConsumer.streamEnd(trailers);
            }
        }

        @Override
        public int available() {
            final AsyncResponseProducer responseProducer = responseProducerRef.get();
            Asserts.notNull(responseProducer, "Response producer");
            return responseProducer.available();
        }

        @Override
        public void produce(final DataStreamChannel channel) throws IOException {
            final AsyncResponseProducer responseProducer = responseProducerRef.get();
            Asserts.notNull(responseProducer, "Response producer");
            responseProducer.produce(channel);
        }

        @Override
        public void releaseResources() {
            final AsyncDataConsumer dataConsumer = dataConsumerRef.getAndSet(null);
            if (dataConsumer != null) {
                dataConsumer.releaseResources();
            }
            final AsyncResponseProducer responseProducer = responseProducerRef.getAndSet(null);
            if (responseProducer != null) {
                responseProducer.releaseResources();
            }
        }
    };
}
Also used : HttpRequest(org.apache.hc.core5.http.HttpRequest) AsyncPushProducer(org.apache.hc.core5.http.nio.AsyncPushProducer) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) HttpResponse(org.apache.hc.core5.http.HttpResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) HttpException(org.apache.hc.core5.http.HttpException) IOException(java.io.IOException) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) ResponseChannel(org.apache.hc.core5.http.nio.ResponseChannel) AsyncResponseProducer(org.apache.hc.core5.http.nio.AsyncResponseProducer) AsyncEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer) CapacityChannel(org.apache.hc.core5.http.nio.CapacityChannel) Header(org.apache.hc.core5.http.Header) EntityDetails(org.apache.hc.core5.http.EntityDetails) AsyncDataConsumer(org.apache.hc.core5.http.nio.AsyncDataConsumer) List(java.util.List) AsyncServerExchangeHandler(org.apache.hc.core5.http.nio.AsyncServerExchangeHandler)

Example 4 with AsyncPushProducer

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

the class AsyncServerFilterExample method main.

public static void main(final String[] args) throws Exception {
    int port = 8080;
    if (args.length >= 1) {
        port = Integer.parseInt(args[0]);
    }
    final IOReactorConfig config = IOReactorConfig.custom().setSoTimeout(15, TimeUnit.SECONDS).setTcpNoDelay(true).build();
    final HttpAsyncServer server = AsyncServerBootstrap.bootstrap().setIOReactorConfig(config).replaceFilter(StandardFilter.EXPECT_CONTINUE.name(), new AbstractAsyncServerAuthFilter<String>(true) {

        @Override
        protected String parseChallengeResponse(final String authorizationValue, final HttpContext context) throws HttpException {
            return authorizationValue;
        }

        @Override
        protected boolean authenticate(final String challengeResponse, final URIAuthority authority, final String requestUri, final HttpContext context) {
            return "let me pass".equals(challengeResponse);
        }

        @Override
        protected String generateChallenge(final String challengeResponse, final URIAuthority authority, final String requestUri, final HttpContext context) {
            return "who goes there?";
        }
    }).addFilterFirst("my-filter", (request, entityDetails, context, responseTrigger, chain) -> {
        if (request.getRequestUri().equals("/back-door")) {
            responseTrigger.submitResponse(new BasicHttpResponse(HttpStatus.SC_OK), AsyncEntityProducers.create("Welcome"));
            return null;
        }
        return chain.proceed(request, entityDetails, context, new AsyncFilterChain.ResponseTrigger() {

            @Override
            public void sendInformation(final HttpResponse response) throws HttpException, IOException {
                responseTrigger.sendInformation(response);
            }

            @Override
            public void submitResponse(final HttpResponse response, final AsyncEntityProducer entityProducer) throws HttpException, IOException {
                response.addHeader("X-Filter", "My-Filter");
                responseTrigger.submitResponse(response, entityProducer);
            }

            @Override
            public void pushPromise(final HttpRequest promise, final AsyncPushProducer responseProducer) throws HttpException, IOException {
                responseTrigger.pushPromise(promise, responseProducer);
            }
        });
    }).register("*", new AsyncServerRequestHandler<Message<HttpRequest, String>>() {

        @Override
        public AsyncRequestConsumer<Message<HttpRequest, String>> prepare(final HttpRequest request, final EntityDetails entityDetails, final HttpContext context) throws HttpException {
            return new BasicRequestConsumer<>(entityDetails != null ? new StringAsyncEntityConsumer() : null);
        }

        @Override
        public void handle(final Message<HttpRequest, String> requestMessage, final ResponseTrigger responseTrigger, final HttpContext context) throws HttpException, IOException {
            // do something useful
            responseTrigger.submitResponse(AsyncResponseBuilder.create(HttpStatus.SC_OK).setEntity("Hello").build(), context);
        }
    }).create();
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        System.out.println("HTTP server shutting down");
        server.close(CloseMode.GRACEFUL);
    }));
    server.start();
    final Future<ListenerEndpoint> future = server.listen(new InetSocketAddress(port), URIScheme.HTTP);
    final ListenerEndpoint listenerEndpoint = future.get();
    System.out.print("Listening on " + listenerEndpoint.getAddress());
    server.awaitShutdown(TimeValue.MAX_VALUE);
}
Also used : URIAuthority(org.apache.hc.core5.net.URIAuthority) Message(org.apache.hc.core5.http.Message) BasicRequestConsumer(org.apache.hc.core5.http.nio.support.BasicRequestConsumer) InetSocketAddress(java.net.InetSocketAddress) IOReactorConfig(org.apache.hc.core5.reactor.IOReactorConfig) AsyncFilterChain(org.apache.hc.core5.http.nio.AsyncFilterChain) EntityDetails(org.apache.hc.core5.http.EntityDetails) HttpException(org.apache.hc.core5.http.HttpException) HttpRequest(org.apache.hc.core5.http.HttpRequest) StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) AsyncPushProducer(org.apache.hc.core5.http.nio.AsyncPushProducer) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) HttpResponse(org.apache.hc.core5.http.HttpResponse) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) IOException(java.io.IOException) ListenerEndpoint(org.apache.hc.core5.reactor.ListenerEndpoint) AsyncServerRequestHandler(org.apache.hc.core5.http.nio.AsyncServerRequestHandler) HttpAsyncServer(org.apache.hc.core5.http.impl.bootstrap.HttpAsyncServer) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) AsyncEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer) ListenerEndpoint(org.apache.hc.core5.reactor.ListenerEndpoint) AbstractAsyncServerAuthFilter(org.apache.hc.core5.http.nio.support.AbstractAsyncServerAuthFilter)

Example 5 with AsyncPushProducer

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

the class ServerPushH2StreamHandler method produceOutput.

@Override
public void produceOutput() throws HttpException, IOException {
    switch(responseState) {
        case IDLE:
            responseState = MessageState.HEADERS;
            pushProducer.produceResponse(new ResponseChannel() {

                @Override
                public void sendInformation(final HttpResponse response, final HttpContext httpContext) throws HttpException, IOException {
                    commitInformation(response);
                }

                @Override
                public void sendResponse(final HttpResponse response, final EntityDetails entityDetails, final HttpContext httpContext) throws HttpException, IOException {
                    commitResponse(response, entityDetails);
                }

                @Override
                public void pushPromise(final HttpRequest promise, final AsyncPushProducer pushProducer, final HttpContext httpContext) throws HttpException, IOException {
                    commitPromise(promise, pushProducer);
                }
            }, context);
            break;
        case BODY:
            pushProducer.produce(dataChannel);
            break;
    }
}
Also used : HttpRequest(org.apache.hc.core5.http.HttpRequest) EntityDetails(org.apache.hc.core5.http.EntityDetails) AsyncPushProducer(org.apache.hc.core5.http.nio.AsyncPushProducer) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) HttpResponse(org.apache.hc.core5.http.HttpResponse) HttpException(org.apache.hc.core5.http.HttpException) IOException(java.io.IOException) ResponseChannel(org.apache.hc.core5.http.nio.ResponseChannel)

Aggregations

HttpResponse (org.apache.hc.core5.http.HttpResponse)5 IOException (java.io.IOException)4 EntityDetails (org.apache.hc.core5.http.EntityDetails)4 HttpException (org.apache.hc.core5.http.HttpException)4 HttpRequest (org.apache.hc.core5.http.HttpRequest)4 AsyncPushProducer (org.apache.hc.core5.http.nio.AsyncPushProducer)4 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)4 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)3 AsyncEntityProducer (org.apache.hc.core5.http.nio.AsyncEntityProducer)3 ResponseChannel (org.apache.hc.core5.http.nio.ResponseChannel)3 AsyncServerExchangeHandler (org.apache.hc.core5.http.nio.AsyncServerExchangeHandler)2 DataStreamChannel (org.apache.hc.core5.http.nio.DataStreamChannel)2 InetSocketAddress (java.net.InetSocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1 List (java.util.List)1 Set (java.util.Set)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Header (org.apache.hc.core5.http.Header)1 Message (org.apache.hc.core5.http.Message)1 HttpAsyncServer (org.apache.hc.core5.http.impl.bootstrap.HttpAsyncServer)1