String url = getUrl(searchRequest);
LOG.trace().log("Performing a search query: [Query: ", searchRequest.getQuery(), ", Index: ",
searchRequest.getIndex(), ", NumResults: ", searchRequest.getNumResults(), "]");
RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, url);
requestBuilder.setCallback(new RequestCallback() {
@Override
public void onResponseReceived(Request request, Response response) {
LOG.trace().log("Search response received: ", response.getText());
if (response.getStatusCode() != Response.SC_OK) {
callback.onFailure("Got back status code " + response.getStatusCode());
} else if (!response.getHeader("Content-Type").startsWith("application/json")) {
callback.onFailure("Search service did not return json");
} else {
SearchResponseJsoImpl searchResponse;
try {
searchResponse = JsonMessage.parse(response.getText());
} catch (JsonException e) {
callback.onFailure(e.getMessage());
return;
}
List<DigestSnapshot> digestSnapshots =
SearchBuilderUtils.deserializeSearchResponse(searchResponse);
callback.onSuccess(searchResponse.getTotalResults(), digestSnapshots);
}
}
@Override
public void onError(Request request, Throwable exception) {
LOG.error().log("Search error: ", exception);
callback.onFailure(exception.getMessage());
}
});
try {
return requestBuilder.send();
} catch (RequestException e) {
callback.onFailure(e.getMessage());
return null;
}
}