private String callElasticSearch() {
try {
build();
HttpPost post = new HttpPost(uri);
final String requestBody = qb.build();
logger.trace(String.format("executing elasticsearch query as:\n%s", requestBody));
StringEntity body = new StringEntity(requestBody);
body.setChunked(false);
post.setEntity(body);
ResponseHandler<String> rspHandler = new ResponseHandler<String>() {
@Override
public String handleResponse(HttpResponse rsp) throws ClientProtocolException, IOException {
String res;
if (rsp.getStatusLine().getStatusCode() != HttpStatus.SC_OK && rsp.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) {
logger.warn(String.format("Failed to search index[%s] , because: \nstatus line: %s\nbody: %s\nrequest body: %s",
inventoryClass.getSimpleName(), rsp.getStatusLine(), EntityUtils.toString(rsp.getEntity()), requestBody));
throw new IOException(String.format("Failed to search index[%s] because %s", inventoryClass.getSimpleName(), rsp.getStatusLine()));
} else {
res = EntityUtils.toString(rsp.getEntity());
logger.trace(String.format("Successfully search index[%s], %s", inventoryClass.getSimpleName(), res));
}
return res;
}
};
String res = mgr.getHttpClient().execute(post, rspHandler);
return res;
} catch (Exception e) {
throw new CloudRuntimeException(e.getMessage(), e);
}
}