Search in sources :

Example 11 with HandlerFactory

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

the class HttpAsyncRequester method execute.

public final <T> Future<T> execute(final AsyncRequestProducer requestProducer, final AsyncResponseConsumer<T> responseConsumer, final HandlerFactory<AsyncPushConsumer> pushHandlerFactory, final Timeout timeout, final HttpContext context, final FutureCallback<T> callback) {
    Args.notNull(requestProducer, "Request producer");
    Args.notNull(responseConsumer, "Response consumer");
    Args.notNull(timeout, "Timeout");
    final BasicFuture<T> future = new BasicFuture<>(callback);
    final AsyncClientExchangeHandler exchangeHandler = new BasicClientExchangeHandler<>(requestProducer, responseConsumer, new FutureContribution<T>(future) {

        @Override
        public void completed(final T result) {
            future.completed(result);
        }
    });
    execute(exchangeHandler, pushHandlerFactory, timeout, context != null ? context : HttpCoreContext.create());
    return future;
}
Also used : AsyncClientExchangeHandler(org.apache.hc.core5.http.nio.AsyncClientExchangeHandler) BasicFuture(org.apache.hc.core5.concurrent.BasicFuture) BasicClientExchangeHandler(org.apache.hc.core5.http.nio.support.BasicClientExchangeHandler)

Example 12 with HandlerFactory

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

the class HttpServer method registerProxy.

/**
 * Register proxy request handler.
 *
 * The proxy handler is used for all requests, which contains a
 * {@code absoluteURI}, which is not related to one of the used virtual
 * handlers.
 *
 * @param <T> request presentation
 * @param requestHandler request handler to register
 * @throws NullPointerException if request handler is {@code null}
 * @throws IllegalStateException if server was already started
 * @see #registerVirtual(String, String, AsyncServerRequestHandler)
 * @see <a href="https://tools.ietf.org/html/rfc2616#section-5.1.2" target=
 *      "_blank"> RFC2616, HTTP/1.1 - 5.1.2 Request-URI</a>
 * @since 3.0
 */
public <T> void registerProxy(final AsyncServerRequestHandler<T> requestHandler) {
    if (server != null) {
        throw new IllegalStateException("http server already started!");
    }
    Args.notNull(requestHandler, "Request handler");
    proxyServerFilter = new TerminalAsyncServerFilter(new HandlerFactory<AsyncServerExchangeHandler>() {

        @Override
        public AsyncServerExchangeHandler create(HttpRequest request, HttpContext context) throws HttpException {
            return new BasicServerExchangeHandler<>(requestHandler);
        }
    });
}
Also used : HttpRequest(org.apache.hc.core5.http.HttpRequest) HandlerFactory(org.apache.hc.core5.http.nio.HandlerFactory) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) TerminalAsyncServerFilter(org.apache.hc.core5.http.nio.support.TerminalAsyncServerFilter) BasicServerExchangeHandler(org.apache.hc.core5.http.nio.support.BasicServerExchangeHandler)

Example 13 with HandlerFactory

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

the class HttpAsyncRequester method execute.

public void execute(final AsyncClientExchangeHandler exchangeHandler, final HandlerFactory<AsyncPushConsumer> pushHandlerFactory, final Timeout timeout, final HttpContext executeContext) {
    Args.notNull(exchangeHandler, "Exchange handler");
    Args.notNull(timeout, "Timeout");
    Args.notNull(executeContext, "Context");
    try {
        exchangeHandler.produceRequest((request, entityDetails, requestContext) -> {
            final String scheme = request.getScheme();
            final URIAuthority authority = request.getAuthority();
            if (authority == null) {
                throw new ProtocolException("Request authority not specified");
            }
            final HttpHost target = new HttpHost(scheme, authority);
            connect(target, timeout, null, new FutureCallback<AsyncClientEndpoint>() {

                @Override
                public void completed(final AsyncClientEndpoint endpoint) {
                    endpoint.execute(new AsyncClientExchangeHandler() {

                        @Override
                        public void releaseResources() {
                            endpoint.releaseAndDiscard();
                            exchangeHandler.releaseResources();
                        }

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

                        @Override
                        public void cancel() {
                            endpoint.releaseAndDiscard();
                            exchangeHandler.cancel();
                        }

                        @Override
                        public void produceRequest(final RequestChannel channel, final HttpContext httpContext) throws HttpException, IOException {
                            channel.sendRequest(request, entityDetails, httpContext);
                        }

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

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

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

                        @Override
                        public void consumeResponse(final HttpResponse response, final EntityDetails entityDetails, final HttpContext httpContext) throws HttpException, IOException {
                            if (entityDetails == null) {
                                endpoint.releaseAndReuse();
                            }
                            exchangeHandler.consumeResponse(response, entityDetails, httpContext);
                        }

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

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

                        @Override
                        public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
                            endpoint.releaseAndReuse();
                            exchangeHandler.streamEnd(trailers);
                        }
                    }, pushHandlerFactory, executeContext);
                }

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

                @Override
                public void cancelled() {
                    exchangeHandler.cancel();
                }
            });
        }, executeContext);
    } catch (final IOException | HttpException ex) {
        exchangeHandler.failed(ex);
    }
}
Also used : ProtocolException(org.apache.hc.core5.http.ProtocolException) URIAuthority(org.apache.hc.core5.net.URIAuthority) AsyncClientExchangeHandler(org.apache.hc.core5.http.nio.AsyncClientExchangeHandler) AsyncClientEndpoint(org.apache.hc.core5.http.nio.AsyncClientEndpoint) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) HttpResponse(org.apache.hc.core5.http.HttpResponse) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) HttpException(org.apache.hc.core5.http.HttpException) ProtocolException(org.apache.hc.core5.http.ProtocolException) IOException(java.io.IOException) ConnectionClosedException(org.apache.hc.core5.http.ConnectionClosedException) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) CapacityChannel(org.apache.hc.core5.http.nio.CapacityChannel) Header(org.apache.hc.core5.http.Header) HttpHost(org.apache.hc.core5.http.HttpHost) EntityDetails(org.apache.hc.core5.http.EntityDetails) List(java.util.List) HttpException(org.apache.hc.core5.http.HttpException) RequestChannel(org.apache.hc.core5.http.nio.RequestChannel)

Aggregations

AsyncClientExchangeHandler (org.apache.hc.core5.http.nio.AsyncClientExchangeHandler)5 List (java.util.List)4 HandlerFactory (org.apache.hc.core5.http.nio.HandlerFactory)4 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)4 IOSession (org.apache.hc.core5.reactor.IOSession)4 IOException (java.io.IOException)3 ByteBuffer (java.nio.ByteBuffer)3 ConnectionClosedException (org.apache.hc.core5.http.ConnectionClosedException)3 EntityDetails (org.apache.hc.core5.http.EntityDetails)3 HttpException (org.apache.hc.core5.http.HttpException)3 HttpHost (org.apache.hc.core5.http.HttpHost)3 HttpResponse (org.apache.hc.core5.http.HttpResponse)3 ProtocolException (org.apache.hc.core5.http.ProtocolException)3 CharCodingConfig (org.apache.hc.core5.http.config.CharCodingConfig)3 Http1Config (org.apache.hc.core5.http.config.Http1Config)3 DefaultConnectionReuseStrategy (org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy)3 Http1StreamListener (org.apache.hc.core5.http.impl.Http1StreamListener)3 HttpProcessors (org.apache.hc.core5.http.impl.HttpProcessors)3 AsyncServerExchangeHandler (org.apache.hc.core5.http.nio.AsyncServerExchangeHandler)3 AsyncServerRequestHandler (org.apache.hc.core5.http.nio.AsyncServerRequestHandler)3