/*
 * Licensed to ElasticSearch and Shay Banon under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. ElasticSearch licenses this
 * file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.rest.action.get;

import org.elasticsearch.action.ActionListener;
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.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import org.elasticsearch.search.fetch.source.FetchSourceContext;

import java.io.IOException;

import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestStatus.NOT_FOUND;
import static org.elasticsearch.rest.RestStatus.OK;
import static org.elasticsearch.rest.action.support.RestXContentBuilder.restContentBuilder;

/**
 *
 */
public class RestGetSourceAction extends BaseRestHandler {

    @Inject
    public RestGetSourceAction(Settings settings, Client client, RestController controller) {
        super(settings, client);
        controller.registerHandler(GET, "/{index}/{type}/{id}/_source", this);
    }

    @Override
    public void handleRequest(final RestRequest request, final RestChannel channel) {
        final GetRequest getRequest = new GetRequest(request.param("index"), request.param("type"), request.param("id"));
        getRequest.listenerThreaded(false);
        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.paramAsBooleanOptional("realtime", null));

        String[] includes = null, excludes = null;
        String sIncludes = request.param("include");
        sIncludes = request.param("includes", sIncludes);
        if (sIncludes != null) {
            includes = Strings.splitStringByCommaToArray(sIncludes);
        }

        String sExcludes = request.param("exclude");
        sExcludes = request.param("excludes", sExcludes);
        if (sExcludes != null) {
            excludes = Strings.splitStringByCommaToArray(sExcludes);
        }

        if (includes != null || excludes != null) {
            getRequest.fetchSourceContext(new FetchSourceContext(includes, excludes));
        }

        client.get(getRequest, new ActionListener<GetResponse>() {
            @Override
            public void onResponse(GetResponse response) {

                try {
                    XContentBuilder builder = restContentBuilder(request, response.getSourceInternal());
                    if (!response.isExists()) {
                        channel.sendResponse(new XContentRestResponse(request, NOT_FOUND, builder));
                    } else {
                        RestXContentBuilder.directSource(response.getSourceInternal(), builder, request);
                        channel.sendResponse(new XContentRestResponse(request, OK, builder));
                    }
                } catch (Throwable e) {
                    onFailure(e);
                }
            }

            @Override
            public void onFailure(Throwable e) {
                try {
                    channel.sendResponse(new XContentThrowableRestResponse(request, e));
                } catch (IOException e1) {
                    logger.error("Failed to send failure response", e1);
                }
            }
        });
    }
}

@@ -888,24 +888,90 @@
 onListener;%0A
+import org.elasticsearch.action.ActionRequestValidationException;%0A
 import org.e
@@ -1099,49 +1099,8 @@
 nt;%0A
-import org.elasticsearch.common.Strings;%0A
 impo
@@ -2732,112 +2732,98 @@
     
-String%5B%5D includes = null, excludes = null;%0A        String sIncludes = request.param(%22include%22
+getRequest.fetchSourceContext(FetchSourceContext.parseFromRestRequest(request)
 );%0A
+%0A
         
 sInc
@@ -2822,88 +2822,120 @@
     
-sIncludes = request.param(%22includes%22, sIncludes);%0A        if (sIncludes != null)
+if (getRequest.fetchSourceContext() != null && !getRequest.fetchSourceContext().fetchSource()) %7B%0A            try
  %7B%0A 
@@ -2949,62 +2949,100 @@
     
+ 
-includes = Strings.splitStringByCommaToArray(sIncludes
+   ActionRequestValidationException validationError = new ActionRequestValidationException(
 );%0A 
@@ -3048,27 +3048,24 @@
         
-%7D%0A%0A
         
 String s
@@ -3060,210 +3060,179 @@
     
-String sExcludes = request.param(%22exclude%22);%0A        sExcludes = request.param(%22excludes%22, sExcludes);%0A        if (sExcludes != null) %7B%0A            excludes = Strings.splitStringByCommaToArray(sExcludes
+validationError.addValidationError(%22fetching source can not be disabled%22);%0A                channel.sendResponse(new XContentThrowableRestResponse(request, validationError)
 );%0A 
@@ -3242,59 +3242,34 @@
     
-%7D%0A%0A
     
-    if (includes != null %7C%7C excludes != null
+%7D catch (IOException e
 ) %7B%0A
@@ -3284,82 +3284,77 @@
     
+   
-getRequest.fetchSourceContext(new FetchSourceContext(includes, excludes));
+ logger.error(%22Failed to send failure response%22, e);%0A            %7D
 %0A   

Make the get source REST API source fetching query parameters consistent with other API