Skip to content

Instantly share code, notes, and snippets.

@martijnvg
Created June 23, 2016 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martijnvg/6a28e57e6b39be2cdf4044a3dbc85669 to your computer and use it in GitHub Desktop.
Save martijnvg/6a28e57e6b39be2cdf4044a3dbc85669 to your computer and use it in GitHub Desktop.
package org.elasticsearch.rest.action.get;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.action.support.RestResponseListener;
import static org.elasticsearch.rest.RestRequest.Method.HEAD;
import static org.elasticsearch.rest.RestStatus.NOT_FOUND;
import static org.elasticsearch.rest.RestStatus.OK;
public abstract class RestHeadAction extends BaseRestHandler {
public RestHeadAction(Settings settings, Client client) {
super(settings, client);
}
public static class Source extends RestHeadAction {
@Inject
public Source(Settings settings, RestController controller, Client client) {
super(settings, client);
controller.registerHandler(HEAD, "/{index}/{type}/{id}/_source", this);
}
@Override
protected void handleRequest(RestRequest request, RestChannel channel, Client client) throws Exception {
getHandler(request, channel, client, false);
}
}
public static class Document extends RestHeadAction {
@Inject
public Document(Settings settings, RestController controller, Client client) {
super(settings, client);
controller.registerHandler(HEAD, "/{index}/{type}/{id}", this);
}
@Override
protected void handleRequest(RestRequest request, RestChannel channel, Client client) throws Exception {
getHandler(request, channel, client, false);
}
}
private static void getHandler(RestRequest request, RestChannel channel, Client client, final boolean source) {
final GetRequest getRequest = new GetRequest(request.param("index"), request.param("type"), request.param("id"));
getRequest.operationThreaded(true);
getRequest.refresh(request.paramAsBoolean("refresh", getRequest.refresh()));
getRequest.routing(request.param("routing")); // order is important, set it after routing, so it will set the routing
getRequest.parent(request.param("parent"));
getRequest.preference(request.param("preference"));
getRequest.realtime(request.paramAsBoolean("realtime", getRequest.realtime()));
// don't get any fields back...
getRequest.fields(Strings.EMPTY_ARRAY);
// TODO we can also just return the document size as Content-Length
client.get(getRequest, new RestResponseListener<GetResponse>(channel) {
@Override
public RestResponse buildResponse(GetResponse response) {
if (!response.isExists()) {
return new BytesRestResponse(NOT_FOUND, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY);
} else if (source && response.isSourceEmpty()) { // doc exists, but source might not
return new BytesRestResponse(NOT_FOUND, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY);
} else {
return new BytesRestResponse(OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY);
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment