try {
XContentBuilder builder = restContentBuilder(request);
response.toXContent(builder, request);
if (!response.exists()) {
channel.sendResponse(new XContentRestResponse(request, NOT_FOUND, builder));
} else {
channel.sendResponse(new XContentRestResponse(request, OK, builder));
}
} catch (Exception e) {
onFailure(e);
}
}