Search in sources :

Example 6 with NOT_FOUND

use of org.elasticsearch.rest.RestStatus.NOT_FOUND in project elasticsearch by elastic.

the class RestGetSourceAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    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"));
    getRequest.parent(request.param("parent"));
    getRequest.preference(request.param("preference"));
    getRequest.realtime(request.paramAsBoolean("realtime", getRequest.realtime()));
    getRequest.fetchSourceContext(FetchSourceContext.parseFromRestRequest(request));
    return channel -> {
        if (getRequest.fetchSourceContext() != null && !getRequest.fetchSourceContext().fetchSource()) {
            final ActionRequestValidationException validationError = new ActionRequestValidationException();
            validationError.addValidationError("fetching source can not be disabled");
            channel.sendResponse(new BytesRestResponse(channel, validationError));
        } else {
            client.get(getRequest, new RestResponseListener<GetResponse>(channel) {

                @Override
                public RestResponse buildResponse(final GetResponse response) throws Exception {
                    final XContentBuilder builder = channel.newBuilder(request.getXContentType(), false);
                    if (response.isSourceEmpty()) {
                        return new BytesRestResponse(NOT_FOUND, builder);
                    } else {
                        builder.rawValue(response.getSourceInternal());
                        return new BytesRestResponse(OK, builder);
                    }
                }
            });
        }
    };
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) GetResponse(org.elasticsearch.action.get.GetResponse) GetRequest(org.elasticsearch.action.get.GetRequest) GET(org.elasticsearch.rest.RestRequest.Method.GET) RestResponse(org.elasticsearch.rest.RestResponse) IOException(java.io.IOException) NOT_FOUND(org.elasticsearch.rest.RestStatus.NOT_FOUND) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) Settings(org.elasticsearch.common.settings.Settings) ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException) RestRequest(org.elasticsearch.rest.RestRequest) OK(org.elasticsearch.rest.RestStatus.OK) NodeClient(org.elasticsearch.client.node.NodeClient) HEAD(org.elasticsearch.rest.RestRequest.Method.HEAD) RestResponseListener(org.elasticsearch.rest.action.RestResponseListener) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException) RestResponseListener(org.elasticsearch.rest.action.RestResponseListener) GetRequest(org.elasticsearch.action.get.GetRequest) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) GetResponse(org.elasticsearch.action.get.GetResponse) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

IOException (java.io.IOException)6 NodeClient (org.elasticsearch.client.node.NodeClient)6 Settings (org.elasticsearch.common.settings.Settings)6 BaseRestHandler (org.elasticsearch.rest.BaseRestHandler)6 RestController (org.elasticsearch.rest.RestController)6 RestRequest (org.elasticsearch.rest.RestRequest)6 NOT_FOUND (org.elasticsearch.rest.RestStatus.NOT_FOUND)6 OK (org.elasticsearch.rest.RestStatus.OK)6 Strings (org.elasticsearch.common.Strings)5 GET (org.elasticsearch.rest.RestRequest.Method.GET)5 BytesRestResponse (org.elasticsearch.rest.BytesRestResponse)4 HEAD (org.elasticsearch.rest.RestRequest.Method.HEAD)4 RestResponse (org.elasticsearch.rest.RestResponse)4 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)3 RestStatus (org.elasticsearch.rest.RestStatus)3 FetchSourceContext (org.elasticsearch.search.fetch.subphase.FetchSourceContext)3 GetRequest (org.elasticsearch.action.get.GetRequest)2 GetResponse (org.elasticsearch.action.get.GetResponse)2 IndicesOptions (org.elasticsearch.action.support.IndicesOptions)2 RestActions (org.elasticsearch.rest.action.RestActions)2