Search in sources :

Example 1 with AsyncDataConsumer

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

Example 2 with AsyncDataConsumer

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

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

the class AsyncServerExpectationFilter 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 {
    if (entityDetails != null) {
        final Header h = request.getFirstHeader(HttpHeaders.EXPECT);
        if (h != null && HeaderElements.CONTINUE.equalsIgnoreCase(h.getValue())) {
            final boolean verified = verify(request, context);
            if (verified) {
                responseTrigger.sendInformation(new BasicHttpResponse(HttpStatus.SC_CONTINUE));
            } else {
                final HttpResponse expectationFailed = new BasicHttpResponse(HttpStatus.SC_EXPECTATION_FAILED);
                final AsyncEntityProducer responseContentProducer = generateResponseContent(expectationFailed);
                responseTrigger.submitResponse(expectationFailed, responseContentProducer);
                return null;
            }
        }
    }
    return chain.proceed(request, entityDetails, context, responseTrigger);
}
Also used : BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) AsyncEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer) Header(org.apache.hc.core5.http.Header) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse)

Example 4 with AsyncDataConsumer

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

use of org.apache.hc.core5.http.nio.AsyncDataConsumer in project californium by eclipse.

the class HttpServer method start.

/**
 * Start http server.
 */
public void start() {
    if (proxyServerFilter != null && server == null) {
        bootstrap.addFilterBefore(StandardFilter.MAIN_HANDLER.name(), "proxy", new AsyncFilterHandler() {

            @Override
            public AsyncDataConsumer handle(HttpRequest request, EntityDetails entityDetails, HttpContext context, org.apache.hc.core5.http.nio.AsyncFilterChain.ResponseTrigger responseTrigger, AsyncFilterChain chain) throws HttpException, IOException {
                try {
                    URI uri = request.getUri();
                    if (uri.getScheme() != null && !virtualHosts.contains(uri.getHost())) {
                        LOGGER.warn("proxy filter {}", uri);
                        return proxyServerFilter.handle(request, entityDetails, context, responseTrigger, chain);
                    }
                } catch (URISyntaxException e) {
                    e.printStackTrace();
                }
                return chain.proceed(request, entityDetails, context, responseTrigger);
            }
        });
    }
    server = bootstrap.create();
    LOGGER.info("HttpServer listening on {} started.", StringUtil.toLog(httpInterface));
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            LOGGER.info("HTTP server shutting down");
            HttpServer.this.stop();
        }
    });
    server.start();
    final Future<ListenerEndpoint> future = server.listen(httpInterface, URIScheme.HTTP);
    try {
        final ListenerEndpoint listenerEndpoint = future.get();
        LOGGER.info("Listening on {}", listenerEndpoint.getAddress());
    } catch (InterruptedException ex) {
        LOGGER.info("interrupted", ex);
    } catch (ExecutionException ex) {
        LOGGER.error("unexpected error:", ex);
    }
}
Also used : HttpRequest(org.apache.hc.core5.http.HttpRequest) AsyncFilterHandler(org.apache.hc.core5.http.nio.AsyncFilterHandler) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) AsyncFilterChain(org.apache.hc.core5.http.nio.AsyncFilterChain) ListenerEndpoint(org.apache.hc.core5.reactor.ListenerEndpoint) EntityDetails(org.apache.hc.core5.http.EntityDetails) AsyncDataConsumer(org.apache.hc.core5.http.nio.AsyncDataConsumer) HttpException(org.apache.hc.core5.http.HttpException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

HttpResponse (org.apache.hc.core5.http.HttpResponse)5 AsyncEntityProducer (org.apache.hc.core5.http.nio.AsyncEntityProducer)5 IOException (java.io.IOException)4 EntityDetails (org.apache.hc.core5.http.EntityDetails)4 Header (org.apache.hc.core5.http.Header)4 HttpException (org.apache.hc.core5.http.HttpException)4 HttpRequest (org.apache.hc.core5.http.HttpRequest)4 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)4 AsyncDataConsumer (org.apache.hc.core5.http.nio.AsyncDataConsumer)4 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)4 ByteBuffer (java.nio.ByteBuffer)3 List (java.util.List)2 AsyncPushProducer (org.apache.hc.core5.http.nio.AsyncPushProducer)2 AsyncServerExchangeHandler (org.apache.hc.core5.http.nio.AsyncServerExchangeHandler)2 CapacityChannel (org.apache.hc.core5.http.nio.CapacityChannel)2 DataStreamChannel (org.apache.hc.core5.http.nio.DataStreamChannel)2 ResponseChannel (org.apache.hc.core5.http.nio.ResponseChannel)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Set (java.util.Set)1