Example usage for org.apache.http.impl.nio.client HttpAsyncClientBuilder addInterceptorFirst

List of usage examples for org.apache.http.impl.nio.client HttpAsyncClientBuilder addInterceptorFirst

Introduction

In this page you can find the example usage for org.apache.http.impl.nio.client HttpAsyncClientBuilder addInterceptorFirst.

Prototype

public final HttpAsyncClientBuilder addInterceptorFirst(final HttpRequestInterceptor itcp) 

Source Link

Document

Adds this protocol interceptor to the head of the protocol processing list.

Usage

From source file:io.opentracing.contrib.elasticsearch.common.TracingHttpClientConfigCallback.java

@Override
public HttpAsyncClientBuilder customizeHttpClient(final HttpAsyncClientBuilder httpClientBuilder) {

    httpClientBuilder.addInterceptorFirst(new HttpRequestInterceptor() {
        @Override/*from   w w  w  . j  a v a  2  s.c  o  m*/
        public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
            Tracer.SpanBuilder spanBuilder = tracer.buildSpan(spanNameProvider.apply(request))
                    .ignoreActiveSpan().withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT);

            SpanContext parentContext = extract(request);

            if (parentContext != null) {
                spanBuilder.asChildOf(parentContext);
            }

            Span span = spanBuilder.start();
            SpanDecorator.onRequest(request, span);

            tracer.inject(span.context(), Format.Builtin.HTTP_HEADERS, new HttpTextMapInjectAdapter(request));

            context.setAttribute("span", span);
        }
    });

    httpClientBuilder.addInterceptorFirst(new HttpResponseInterceptor() {
        @Override
        public void process(HttpResponse response, HttpContext context) throws HttpException, IOException {
            Object spanObject = context.getAttribute("span");
            if (spanObject instanceof Span) {
                Span span = (Span) spanObject;
                SpanDecorator.onResponse(response, span);
                span.finish();
            }
        }
    });

    return httpClientBuilder;
}

From source file:org.apache.nifi.remote.util.SiteToSiteRestApiClient.java

private void setupAsyncClient() {
    final HttpAsyncClientBuilder clientBuilder = HttpAsyncClients.custom();

    if (sslContext != null) {
        clientBuilder.setSSLContext(sslContext);
        clientBuilder.addInterceptorFirst(new HttpsResponseInterceptor());
    }// w  ww  .ja v  a2  s.  co  m

    httpAsyncClient = clientBuilder.setDefaultCredentialsProvider(getCredentialsProvider()).build();
    httpAsyncClient.start();
}