List of usage examples for org.apache.http.impl.nio.client CloseableHttpAsyncClient execute
@Override public Future<HttpResponse> execute(final HttpHost target, final HttpRequest request, final HttpContext context, final FutureCallback<HttpResponse> callback)
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 2 s . 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:org.openqa.selenium.remote.internal.ApacheHttpAsyncClient.java
private org.apache.http.HttpResponse followRedirects(CloseableHttpAsyncClient client, HttpContext context, org.apache.http.HttpResponse response, int redirectCount) { if (!isRedirect(response)) { return response; }//from www .ja v a 2 s . c o m try { // Make sure that the previous connection is freed. HttpEntity httpEntity = response.getEntity(); if (httpEntity != null) { EntityUtils.consume(httpEntity); } } catch (IOException e) { throw new WebDriverException(e); } if (redirectCount > MAX_REDIRECTS) { throw new WebDriverException("Maximum number of redirects exceeded. Aborting"); } String location = response.getFirstHeader("location").getValue(); URI uri; try { uri = buildUri(context, location); } catch (URISyntaxException e) { throw new WebDriverException(e); } HttpGet get = new HttpGet(uri); get.setHeader("Accept", "application/json; charset=utf-8"); Future<org.apache.http.HttpResponse> future = client.execute(targetHost, get, context, null); try { org.apache.http.HttpResponse newResponse = future.get(); return followRedirects(client, context, newResponse, redirectCount + 1); } catch (InterruptedException | ExecutionException e) { throw new WebDriverException(e); } }
From source file:com.baidubce.http.BceHttpClient.java
/** * Executes the request and returns the result. * * @param request The BCE request to send to the remote server * @param responseClass A response handler to accept a successful response from the remote server * @param responseHandlers A response handler to accept an unsuccessful response from the remote server * * @throws com.baidubce.BceClientException If any errors are encountered on the client while making the * request or handling the response. * @throws com.baidubce.BceServiceException If any errors occurred in BCE while processing the request. *//*from www . j ava 2s. c o m*/ public <T extends AbstractBceResponse> T execute(InternalRequest request, Class<T> responseClass, HttpResponseHandler[] responseHandlers) { // Apply whatever request options we know how to handle, such as user-agent. request.addHeader(Headers.USER_AGENT, this.config.getUserAgent()); BceCredentials credentials = config.getCredentials(); if (request.getCredentials() != null) { credentials = request.getCredentials(); } long delayForNextRetryInMillis = 0; for (int attempt = 1;; ++attempt) { HttpRequestBase httpRequest = null; CloseableHttpResponse httpResponse = null; CloseableHttpAsyncClient httpAsyncClient = null; try { // Sign the request if credentials were provided if (credentials != null) { this.signer.sign(request, credentials); } requestLogger.debug("Sending Request: {}", request); httpRequest = this.createHttpRequest(request); HttpContext httpContext = this.createHttpContext(request); if (this.isHttpAsyncPutEnabled && httpRequest.getMethod().equals("PUT")) { httpAsyncClient = this.createHttpAsyncClient(this.createNHttpClientConnectionManager()); httpAsyncClient.start(); Future<HttpResponse> future = httpAsyncClient.execute(HttpAsyncMethods.create(httpRequest), new BasicAsyncResponseConsumer(), httpContext, null); httpResponse = new BceCloseableHttpResponse(future.get()); } else { httpResponse = this.httpClient.execute(httpRequest, httpContext); } HttpUtils.printRequest(httpRequest); BceHttpResponse bceHttpResponse = new BceHttpResponse(httpResponse); T response = responseClass.newInstance(); for (HttpResponseHandler handler : responseHandlers) { if (handler.handle(bceHttpResponse, response)) { break; } } // everything is ok return response; } catch (Exception e) { if (logger.isInfoEnabled()) { logger.info("Unable to execute HTTP request", e); } BceClientException bce; if (e instanceof BceClientException) { bce = (BceClientException) e; } else { bce = new BceClientException("Unable to execute HTTP request", e); } delayForNextRetryInMillis = this.getDelayBeforeNextRetryInMillis(httpRequest, bce, attempt, this.config.getRetryPolicy()); if (delayForNextRetryInMillis < 0) { throw bce; } logger.debug("Retriable error detected, will retry in {} ms, attempt number: {}", delayForNextRetryInMillis, attempt); try { Thread.sleep(delayForNextRetryInMillis); } catch (InterruptedException e1) { throw new BceClientException("Delay interrupted", e1); } if (request.getContent() != null) { request.getContent().restart(); } if (httpResponse != null) { try { HttpEntity entity = httpResponse.getEntity(); if (entity != null && entity.isStreaming()) { final InputStream instream = entity.getContent(); if (instream != null) { instream.close(); } } } catch (IOException e1) { logger.debug("Fail to consume entity.", e1); try { httpResponse.close(); } catch (IOException e2) { logger.debug("Fail to close connection.", e2); } } } } finally { try { if (httpAsyncClient != null) { httpAsyncClient.close(); } } catch (IOException e) { logger.debug("Fail to close HttpAsyncClient", e); } } } }