Search in sources :

Example 1 with AsyncExecChainHandler

use of org.apache.hc.client5.http.async.AsyncExecChainHandler in project mercury by yellow013.

the class AsyncClientMessageTrailers method main.

public static final void main(final String[] args) throws Exception {
    final IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setSoTimeout(Timeout.ofSeconds(5)).build();
    final CloseableHttpAsyncClient client = HttpAsyncClients.custom().setIOReactorConfig(ioReactorConfig).addExecInterceptorAfter(ChainElement.PROTOCOL.name(), "custom", new AsyncExecChainHandler() {

        @Override
        public void execute(final HttpRequest request, final AsyncEntityProducer entityProducer, final AsyncExecChain.Scope scope, final AsyncExecChain chain, final AsyncExecCallback asyncExecCallback) throws HttpException, IOException {
            // Send MD5 hash in a trailer by decorating the original entity producer
            chain.proceed(request, entityProducer != null ? new DigestingEntityProducer("MD5", entityProducer) : null, scope, asyncExecCallback);
        }
    }).build();
    client.start();
    final SimpleHttpRequest request = SimpleRequestBuilder.post("http://httpbin.org/post").setBody("some stuff", ContentType.TEXT_PLAIN).build();
    System.out.println("Executing request " + request);
    final Future<SimpleHttpResponse> future = client.execute(SimpleRequestProducer.create(request), SimpleResponseConsumer.create(), new FutureCallback<SimpleHttpResponse>() {

        @Override
        public void completed(final SimpleHttpResponse response) {
            System.out.println(request + "->" + new StatusLine(response));
            System.out.println(response.getBody());
        }

        @Override
        public void failed(final Exception ex) {
            System.out.println(request + "->" + ex);
        }

        @Override
        public void cancelled() {
            System.out.println(request + " cancelled");
        }
    });
    future.get();
    System.out.println("Shutting down");
    client.close(CloseMode.GRACEFUL);
}
Also used : HttpRequest(org.apache.hc.core5.http.HttpRequest) SimpleHttpRequest(org.apache.hc.client5.http.async.methods.SimpleHttpRequest) AsyncExecCallback(org.apache.hc.client5.http.async.AsyncExecCallback) DigestingEntityProducer(org.apache.hc.core5.http.nio.entity.DigestingEntityProducer) AsyncExecChain(org.apache.hc.client5.http.async.AsyncExecChain) SimpleHttpRequest(org.apache.hc.client5.http.async.methods.SimpleHttpRequest) AsyncExecChainHandler(org.apache.hc.client5.http.async.AsyncExecChainHandler) SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse) HttpException(org.apache.hc.core5.http.HttpException) IOException(java.io.IOException) IOReactorConfig(org.apache.hc.core5.reactor.IOReactorConfig) StatusLine(org.apache.hc.core5.http.message.StatusLine) AsyncEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer) CloseableHttpAsyncClient(org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient)

Example 2 with AsyncExecChainHandler

use of org.apache.hc.client5.http.async.AsyncExecChainHandler in project mercury by yellow013.

the class AsyncClientInterceptors method main.

public static void main(final String[] args) throws Exception {
    final IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setSoTimeout(Timeout.ofSeconds(5)).build();
    final CloseableHttpAsyncClient client = HttpAsyncClients.custom().setIOReactorConfig(ioReactorConfig).addRequestInterceptorFirst(new HttpRequestInterceptor() {

        private final AtomicLong count = new AtomicLong(0);

        @Override
        public void process(final HttpRequest request, final EntityDetails entity, final HttpContext context) throws HttpException, IOException {
            request.setHeader("request-id", Long.toString(count.incrementAndGet()));
        }
    }).addExecInterceptorAfter(ChainElement.PROTOCOL.name(), "custom", new AsyncExecChainHandler() {

        @Override
        public void execute(final HttpRequest request, final AsyncEntityProducer requestEntityProducer, final AsyncExecChain.Scope scope, final AsyncExecChain chain, final AsyncExecCallback asyncExecCallback) throws HttpException, IOException {
            final Header idHeader = request.getFirstHeader("request-id");
            if (idHeader != null && "13".equalsIgnoreCase(idHeader.getValue())) {
                final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_NOT_FOUND, "Oppsie");
                final ByteBuffer content = ByteBuffer.wrap("bad luck".getBytes(StandardCharsets.US_ASCII));
                final AsyncDataConsumer asyncDataConsumer = asyncExecCallback.handleResponse(response, new BasicEntityDetails(content.remaining(), ContentType.TEXT_PLAIN));
                asyncDataConsumer.consume(content);
                asyncDataConsumer.streamEnd(null);
            } else {
                chain.proceed(request, requestEntityProducer, scope, asyncExecCallback);
            }
        }
    }).build();
    client.start();
    final String requestUri = "http://httpbin.org/get";
    for (int i = 0; i < 20; i++) {
        final SimpleHttpRequest request = SimpleRequestBuilder.get(requestUri).build();
        System.out.println("Executing request " + request);
        final Future<SimpleHttpResponse> future = client.execute(request, new FutureCallback<SimpleHttpResponse>() {

            @Override
            public void completed(final SimpleHttpResponse response) {
                System.out.println(request + "->" + new StatusLine(response));
                System.out.println(response.getBody());
            }

            @Override
            public void failed(final Exception ex) {
                System.out.println(request + "->" + ex);
            }

            @Override
            public void cancelled() {
                System.out.println(request + " cancelled");
            }
        });
        future.get();
    }
    System.out.println("Shutting down");
    client.close(CloseMode.GRACEFUL);
}
Also used : AsyncExecCallback(org.apache.hc.client5.http.async.AsyncExecCallback) AsyncExecChain(org.apache.hc.client5.http.async.AsyncExecChain) IOReactorConfig(org.apache.hc.core5.reactor.IOReactorConfig) EntityDetails(org.apache.hc.core5.http.EntityDetails) BasicEntityDetails(org.apache.hc.core5.http.impl.BasicEntityDetails) CloseableHttpAsyncClient(org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient) AsyncDataConsumer(org.apache.hc.core5.http.nio.AsyncDataConsumer) HttpException(org.apache.hc.core5.http.HttpException) BasicEntityDetails(org.apache.hc.core5.http.impl.BasicEntityDetails) SimpleHttpRequest(org.apache.hc.client5.http.async.methods.SimpleHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) SimpleHttpRequest(org.apache.hc.client5.http.async.methods.SimpleHttpRequest) IOException(java.io.IOException) AsyncExecChainHandler(org.apache.hc.client5.http.async.AsyncExecChainHandler) ByteBuffer(java.nio.ByteBuffer) SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse) HttpException(org.apache.hc.core5.http.HttpException) IOException(java.io.IOException) StatusLine(org.apache.hc.core5.http.message.StatusLine) AtomicLong(java.util.concurrent.atomic.AtomicLong) AsyncEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) Header(org.apache.hc.core5.http.Header) HttpRequestInterceptor(org.apache.hc.core5.http.HttpRequestInterceptor)

Aggregations

IOException (java.io.IOException)2 AsyncExecCallback (org.apache.hc.client5.http.async.AsyncExecCallback)2 AsyncExecChain (org.apache.hc.client5.http.async.AsyncExecChain)2 AsyncExecChainHandler (org.apache.hc.client5.http.async.AsyncExecChainHandler)2 SimpleHttpRequest (org.apache.hc.client5.http.async.methods.SimpleHttpRequest)2 SimpleHttpResponse (org.apache.hc.client5.http.async.methods.SimpleHttpResponse)2 CloseableHttpAsyncClient (org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient)2 HttpException (org.apache.hc.core5.http.HttpException)2 HttpRequest (org.apache.hc.core5.http.HttpRequest)2 StatusLine (org.apache.hc.core5.http.message.StatusLine)2 AsyncEntityProducer (org.apache.hc.core5.http.nio.AsyncEntityProducer)2 IOReactorConfig (org.apache.hc.core5.reactor.IOReactorConfig)2 ByteBuffer (java.nio.ByteBuffer)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 EntityDetails (org.apache.hc.core5.http.EntityDetails)1 Header (org.apache.hc.core5.http.Header)1 HttpRequestInterceptor (org.apache.hc.core5.http.HttpRequestInterceptor)1 HttpResponse (org.apache.hc.core5.http.HttpResponse)1 BasicEntityDetails (org.apache.hc.core5.http.impl.BasicEntityDetails)1 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)1