public void executeAsync( final Action clientRequest, final JestResultHandler<JestResult> resultHandler) throws ExecutionException, InterruptedException, IOException { synchronized (this) { if (asyncClient.getStatus() == IOReactorStatus.INACTIVE) { asyncClient.start(); } } String elasticSearchRestUrl = getRequestURL(getElasticSearchServer(), clientRequest.getURI()); final HttpUriRequest request = constructHttpMethod( clientRequest.getRestMethodName(), elasticSearchRestUrl, clientRequest.getData()); // add headers added to action if (!clientRequest.getHeaders().isEmpty()) { for (Entry<String, Object> header : clientRequest.getHeaders().entrySet()) { request.addHeader(header.getKey(), header.getValue() + ""); } } asyncClient.execute( request, new FutureCallback<HttpResponse>() { @Override public void completed(final HttpResponse response) { try { JestResult jestResult = deserializeResponse( response, clientRequest.getName(), clientRequest.getPathToResult()); resultHandler.completed(jestResult); } catch (IOException e) { log.error( "Exception occurred while serializing the response. Exception: " + e.getMessage()); } } @Override public void failed(final Exception ex) { resultHandler.failed(ex); } @Override public void cancelled() {} }); }
public JestResult execute(Action clientRequest) throws IOException { String elasticSearchRestUrl = getRequestURL(getElasticSearchServer(), clientRequest.getURI()); HttpUriRequest request = constructHttpMethod( clientRequest.getRestMethodName(), elasticSearchRestUrl, clientRequest.getData()); // add headers added to action if (!clientRequest.getHeaders().isEmpty()) { for (Entry<String, Object> header : clientRequest.getHeaders().entrySet()) { request.addHeader(header.getKey(), header.getValue() + ""); } } HttpResponse response = httpClient.execute(request); // If head method returns no content, it is added according to response code thanks to // https://github.com/hlassiege if (request.getMethod().equalsIgnoreCase("HEAD")) { if (response.getEntity() == null) { if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { response.setEntity(new StringEntity("{\"ok\" : true, \"found\" : true}")); } else if (response.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) { response.setEntity(new StringEntity("{\"ok\" : false, \"found\" : false}")); } } } return deserializeResponse(response, clientRequest.getName(), clientRequest.getPathToResult()); }