Example usage for org.apache.http.nio.protocol HttpAsyncRequestProducer generateRequest

List of usage examples for org.apache.http.nio.protocol HttpAsyncRequestProducer generateRequest

Introduction

In this page you can find the example usage for org.apache.http.nio.protocol HttpAsyncRequestProducer generateRequest.

Prototype

HttpRequest generateRequest() throws IOException, HttpException;

Source Link

Document

Invoked to generate a HTTP request message head.

Usage

From source file:co.paralleluniverse.fibers.dropwizard.InstrumentedNHttpClientBuilder.java

@Override
public CloseableHttpAsyncClient build() {

    final CloseableHttpAsyncClient ac = super.build();
    return new CloseableHttpAsyncClient() {

        @Override//w  w  w .  j a  v  a 2s . c om
        public boolean isRunning() {
            return ac.isRunning();
        }

        @Override
        public void start() {
            ac.start();
        }

        @Override
        public <T> Future<T> execute(HttpAsyncRequestProducer requestProducer,
                HttpAsyncResponseConsumer<T> responseConsumer, HttpContext context,
                FutureCallback<T> callback) {
            final Timer.Context timerContext;
            try {
                timerContext = timer(requestProducer.generateRequest()).time();
            } catch (IOException | HttpException ex) {
                throw new AssertionError();
            }
            try {
                return ac.execute(requestProducer, responseConsumer, context, callback);
            } finally {
                timerContext.stop();
            }
        }

        @Override
        public void close() throws IOException {
            ac.close();
        }
    };
}

From source file:com.navercorp.pinpoint.profiler.modifier.connector.httpclient4.interceptor.AsyncInternalClientExecuteInterceptor.java

@Override
protected org.apache.http.HttpRequest getHttpRequest(final Object[] args) {
    if (!(args[0] instanceof org.apache.http.nio.protocol.HttpAsyncRequestProducer)) {
        return null;
    }//from  w  ww. j a  v a 2 s.c  o  m
    final org.apache.http.nio.protocol.HttpAsyncRequestProducer producer = (org.apache.http.nio.protocol.HttpAsyncRequestProducer) args[0];
    try {
        /**
         * FIXME Implementations other than org.apache.http.nio.protocol.BasicAsyncRequestProducer.generateRequest() can cause some trouble.
         */
        return producer.generateRequest();
    } catch (Exception e) {
        return null;
    }
}

From source file:org.elasticsearch.client.RestClientMultipleHostsTests.java

@Before
@SuppressWarnings("unchecked")
public void createRestClient() throws IOException {
    CloseableHttpAsyncClient httpClient = mock(CloseableHttpAsyncClient.class);
    when(httpClient.<HttpResponse>execute(any(HttpAsyncRequestProducer.class),
            any(HttpAsyncResponseConsumer.class), any(HttpClientContext.class), any(FutureCallback.class)))
                    .thenAnswer(new Answer<Future<HttpResponse>>() {
                        @Override
                        public Future<HttpResponse> answer(InvocationOnMock invocationOnMock) throws Throwable {
                            HttpAsyncRequestProducer requestProducer = (HttpAsyncRequestProducer) invocationOnMock
                                    .getArguments()[0];
                            HttpUriRequest request = (HttpUriRequest) requestProducer.generateRequest();
                            HttpHost httpHost = requestProducer.getTarget();
                            HttpClientContext context = (HttpClientContext) invocationOnMock.getArguments()[2];
                            assertThat(context.getAuthCache().get(httpHost), instanceOf(BasicScheme.class));
                            FutureCallback<HttpResponse> futureCallback = (FutureCallback<HttpResponse>) invocationOnMock
                                    .getArguments()[3];
                            //return the desired status code or exception depending on the path
                            if (request.getURI().getPath().equals("/soe")) {
                                futureCallback.failed(new SocketTimeoutException(httpHost.toString()));
                            } else if (request.getURI().getPath().equals("/coe")) {
                                futureCallback.failed(new ConnectTimeoutException(httpHost.toString()));
                            } else if (request.getURI().getPath().equals("/ioe")) {
                                futureCallback.failed(new IOException(httpHost.toString()));
                            } else {
                                int statusCode = Integer.parseInt(request.getURI().getPath().substring(1));
                                StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("http", 1, 1),
                                        statusCode, "");
                                futureCallback.completed(new BasicHttpResponse(statusLine));
                            }/*from ww w  .  ja v a  2  s . c o  m*/
                            return null;
                        }
                    });
    int numHosts = RandomNumbers.randomIntBetween(getRandom(), 2, 5);
    httpHosts = new HttpHost[numHosts];
    for (int i = 0; i < numHosts; i++) {
        httpHosts[i] = new HttpHost("localhost", 9200 + i);
    }
    failureListener = new HostsTrackingFailureListener();
    restClient = new RestClient(httpClient, 10000, new Header[0], httpHosts, null, failureListener);
}

From source file:com.navercorp.pinpoint.plugin.httpclient4.interceptor.DefaultClientExchangeHandlerImplStartMethodInterceptor.java

private HttpRequest getHttpRequest(final Object target) {
    try {/*from  w w w  .  ja va  2 s.co m*/
        if (!(target instanceof RequestProducerGetter)) {
            return null;
        }

        final HttpAsyncRequestProducer requestProducer = ((RequestProducerGetter) target)
                ._$PINPOINT$_getRequestProducer();
        return requestProducer.generateRequest();
    } catch (Exception e) {
        return null;
    }
}

From source file:org.elasticsearch.client.RestClientSingleHostTests.java

@Before
@SuppressWarnings("unchecked")
public void createRestClient() throws IOException {
    httpClient = mock(CloseableHttpAsyncClient.class);
    when(httpClient.<HttpResponse>execute(any(HttpAsyncRequestProducer.class),
            any(HttpAsyncResponseConsumer.class), any(HttpClientContext.class), any(FutureCallback.class)))
                    .thenAnswer(new Answer<Future<HttpResponse>>() {
                        @Override
                        public Future<HttpResponse> answer(InvocationOnMock invocationOnMock) throws Throwable {
                            HttpAsyncRequestProducer requestProducer = (HttpAsyncRequestProducer) invocationOnMock
                                    .getArguments()[0];
                            HttpClientContext context = (HttpClientContext) invocationOnMock.getArguments()[2];
                            assertThat(context.getAuthCache().get(httpHost), instanceOf(BasicScheme.class));
                            FutureCallback<HttpResponse> futureCallback = (FutureCallback<HttpResponse>) invocationOnMock
                                    .getArguments()[3];
                            HttpUriRequest request = (HttpUriRequest) requestProducer.generateRequest();
                            //return the desired status code or exception depending on the path
                            if (request.getURI().getPath().equals("/soe")) {
                                futureCallback.failed(new SocketTimeoutException());
                            } else if (request.getURI().getPath().equals("/coe")) {
                                futureCallback.failed(new ConnectTimeoutException());
                            } else {
                                int statusCode = Integer.parseInt(request.getURI().getPath().substring(1));
                                StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("http", 1, 1),
                                        statusCode, "");

                                HttpResponse httpResponse = new BasicHttpResponse(statusLine);
                                //return the same body that was sent
                                if (request instanceof HttpEntityEnclosingRequest) {
                                    HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
                                    if (entity != null) {
                                        assertTrue(
                                                "the entity is not repeatable, cannot set it to the response directly",
                                                entity.isRepeatable());
                                        httpResponse.setEntity(entity);
                                    }/*w ww.  j  av a  2  s . co  m*/
                                }
                                //return the same headers that were sent
                                httpResponse.setHeaders(request.getAllHeaders());
                                futureCallback.completed(httpResponse);
                            }
                            return null;
                        }
                    });

    defaultHeaders = RestClientTestUtil.randomHeaders(getRandom(), "Header-default");
    httpHost = new HttpHost("localhost", 9200);
    failureListener = new HostsTrackingFailureListener();
    restClient = new RestClient(httpClient, 10000, defaultHeaders, new HttpHost[] { httpHost }, null,
            failureListener);
}

From source file:org.apache.http.impl.nio.client.PipeliningClientExchangeHandlerImpl.java

@Override
public HttpRequest generateRequest() throws IOException, HttpException {
    verifytRoute();/*  w  ww .  j  a  v  a 2s .c om*/
    if (!isRouteEstablished()) {
        onRouteToTarget();
        onRouteComplete();
    }
    final NHttpClientConnection localConn = getConnection();
    this.localContext.setAttribute(HttpCoreContext.HTTP_CONNECTION, localConn);

    Asserts.check(this.requestProducerRef.get() == null,
            "Inconsistent state: currentRequest producer is not null");
    final HttpAsyncRequestProducer requestProducer = this.requestProducerQueue.poll();
    if (requestProducer == null) {
        return null;
    }
    this.requestProducerRef.set(requestProducer);

    final HttpRequest original = requestProducer.generateRequest();
    final HttpRequestWrapper currentRequest = HttpRequestWrapper.wrap(original);
    final RequestConfig config = this.localContext.getRequestConfig();
    if (config.getSocketTimeout() > 0) {
        localConn.setSocketTimeout(config.getSocketTimeout());
    }

    this.httpProcessor.process(currentRequest, this.localContext);

    this.requestQueue.add(currentRequest);
    setCurrentRequest(currentRequest);

    return currentRequest;
}