// Path: src/main/java/crate/elasticsearch/action/import_/ImportRequest.java
// public class ImportRequest extends NodesOperationRequest<ImportRequest> {
// 
//     private BytesReference source;
// 
//     private String type;
//     private String index;
// 
//     /**
//      * Constructs a new import request against the provided nodes. No nodes provided
//      * means it will run against all nodes.
//      */
//     public ImportRequest(String... nodes) {
//         super(nodes);
//     }
// 
//     /**
//      * The query source to execute.
//      * @return
//      */
//     public BytesReference source() {
//         return source;
//     }
// 
//     @Required
//     public ImportRequest source(String source) {
//         return this.source(new BytesArray(source), false);
//     }
// 
//     @Required
//     public ImportRequest source(BytesReference source, boolean unsafe) {
//         this.source = source;
//         return this;
//     }
// 
//     public String type() {
//         return this.type;
//     }
// 
//     public void type(String type) {
//         this.type = type;
//     }
// 
//     public String index() {
//         return this.index ;
//     }
// 
//     public void index(String index) {
//         this.index = index;
//     }
// 
//     @Override
//     public void readFrom(StreamInput in) throws IOException {
//         super.readFrom(in);
//         index = in.readOptionalString();
//         type = in.readOptionalString();
//         source = in.readBytesReference();
//     }
// 
//     @Override
//     public void writeTo(StreamOutput out) throws IOException {
//         super.writeTo(out);
//         out.writeOptionalString(index);
//         out.writeOptionalString(type);
//         out.writeBytesReference(source);
//     }
// 
// }
// 
// Path: src/main/java/crate/elasticsearch/action/import_/ImportResponse.java
// public class ImportResponse extends NodesOperationResponse<NodeImportResponse> implements ToXContent {
// 
//     private List<NodeImportResponse> responses;
//     private List<FailedNodeException> nodeFailures;
// 
//     public ImportResponse() {
//     }
// 
//     public ImportResponse(List<NodeImportResponse> responses, int total,
//             int successfulNodes, int failedNodes, List<FailedNodeException> nodeFailures) {
//         this.responses = responses;
//         this.nodeFailures = nodeFailures;
//     }
// 
//     public List<NodeImportResponse> getResponses() {
//         return responses;
//     }
// 
//     @Override
//     public XContentBuilder toXContent(XContentBuilder builder, Params params)
//             throws IOException {
//         builder.startObject();
//         builder.startArray("imports");
//         for (NodeImportResponse r : this.responses) {
//             r.toXContent(builder, params);
//         }
//         builder.endArray();
//         if (nodeFailures != null && nodeFailures.size() > 0) {
//             builder.startArray("failures");
//             for (FailedNodeException failure : nodeFailures) {
//                 builder.startObject();
//                 builder.field("node_id", failure.nodeId());
//                 builder.field("reason", failure.getDetailedMessage());
//                 builder.endObject();
//             }
//             builder.endArray();
//         }
//         builder.endObject();
//         return builder;
//     }
// 
//     @Override
//     public void readFrom(StreamInput in) throws IOException {
//         super.readFrom(in);
//         int responsesCount = in.readInt();
//         this.responses = new ArrayList<NodeImportResponse>(responsesCount);
//         for (int i = 0; i < responsesCount; i++) {
//             responses.add(NodeImportResponse.readNew(in));
//         }
//         int failuresCount = in.readInt();
//         this.nodeFailures = new ArrayList<FailedNodeException>(failuresCount);
//         for (int i = 0; i < failuresCount; i++) {
//             String nodeId = in.readString();
//             String msg = in.readOptionalString();
//             FailedNodeException e = new FailedNodeException(nodeId, msg, null);
//             nodeFailures.add(e);
//         }
//     }
// 
//     @Override
//     public void writeTo(StreamOutput out) throws IOException {
//         super.writeTo(out);
//         out.writeInt(responses.size());
//         for (NodeImportResponse response : responses) {
//             response.writeTo(out);
//         }
//         out.writeInt(nodeFailures.size());
//         for (FailedNodeException e : nodeFailures) {
//             out.writeString(e.nodeId());
//             out.writeOptionalString(e.getMessage());
//         }
//     }
// }


import crate.elasticsearch.action.import_.ImportAction;
import crate.elasticsearch.action.import_.ImportRequest;
import crate.elasticsearch.action.import_.ImportResponse;
import org.elasticsearch.action.Action;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.bytes.BytesReference;
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.RestActions;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestStatus.BAD_REQUEST;
import static org.elasticsearch.rest.RestStatus.OK;
    public void handleRequest(final RestRequest request, final RestChannel channel) {
        ImportRequest importRequest = new ImportRequest();
        importRequest.listenerThreaded(false);
        try {
            if (request.hasContent()) {
                importRequest.source(request.content(), request.contentUnsafe());
            } else {
                String source = request.param("source");
                if (source != null) {
                    importRequest.source(source);
                } else {
                    BytesReference querySource = RestActions.parseQuerySource(request);
                    if (querySource != null) {
                        importRequest.source(querySource, false);
                    }
                }
            }
            importRequest.index(request.param("index"));
            importRequest.type(request.param("type"));
        } catch (Exception e) {
            try {
                XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
                channel.sendResponse(new XContentRestResponse(request, BAD_REQUEST, builder.startObject().field("error", e.getMessage()).endObject()));
            } catch (IOException e1) {
                logger.error("Failed to send failure response", e1);
            }
            return;
        }


// Path: src/main/java/crate/elasticsearch/action/import_/ImportRequest.java
// public class ImportRequest extends NodesOperationRequest<ImportRequest> {
// 
//     private BytesReference source;
// 
//     private String type;
//     private String index;
// 
//     /**
//      * Constructs a new import request against the provided nodes. No nodes provided
//      * means it will run against all nodes.
//      */
//     public ImportRequest(String... nodes) {
//         super(nodes);
//     }
// 
//     /**
//      * The query source to execute.
//      * @return
//      */
//     public BytesReference source() {
//         return source;
//     }
// 
//     @Required
//     public ImportRequest source(String source) {
//         return this.source(new BytesArray(source), false);
//     }
// 
//     @Required
//     public ImportRequest source(BytesReference source, boolean unsafe) {
//         this.source = source;
//         return this;
//     }
// 
//     public String type() {
//         return this.type;
//     }
// 
//     public void type(String type) {
//         this.type = type;
//     }
// 
//     public String index() {
//         return this.index ;
//     }
// 
//     public void index(String index) {
//         this.index = index;
//     }
// 
//     @Override
//     public void readFrom(StreamInput in) throws IOException {
//         super.readFrom(in);
//         index = in.readOptionalString();
//         type = in.readOptionalString();
//         source = in.readBytesReference();
//     }
// 
//     @Override
//     public void writeTo(StreamOutput out) throws IOException {
//         super.writeTo(out);
//         out.writeOptionalString(index);
//         out.writeOptionalString(type);
//         out.writeBytesReference(source);
//     }
// 
// }
// 
// Path: src/main/java/crate/elasticsearch/action/import_/ImportResponse.java
// public class ImportResponse extends NodesOperationResponse<NodeImportResponse> implements ToXContent {
// 
//     private List<NodeImportResponse> responses;
//     private List<FailedNodeException> nodeFailures;
// 
//     public ImportResponse() {
//     }
// 
//     public ImportResponse(List<NodeImportResponse> responses, int total,
//             int successfulNodes, int failedNodes, List<FailedNodeException> nodeFailures) {
//         this.responses = responses;
//         this.nodeFailures = nodeFailures;
//     }
// 
//     public List<NodeImportResponse> getResponses() {
//         return responses;
//     }
// 
//     @Override
//     public XContentBuilder toXContent(XContentBuilder builder, Params params)
//             throws IOException {
//         builder.startObject();
//         builder.startArray("imports");
//         for (NodeImportResponse r : this.responses) {
//             r.toXContent(builder, params);
//         }
//         builder.endArray();
//         if (nodeFailures != null && nodeFailures.size() > 0) {
//             builder.startArray("failures");
//             for (FailedNodeException failure : nodeFailures) {
//                 builder.startObject();
//                 builder.field("node_id", failure.nodeId());
//                 builder.field("reason", failure.getDetailedMessage());
//                 builder.endObject();
//             }
//             builder.endArray();
//         }
//         builder.endObject();
//         return builder;
//     }
// 
//     @Override
//     public void readFrom(StreamInput in) throws IOException {
//         super.readFrom(in);
//         int responsesCount = in.readInt();
//         this.responses = new ArrayList<NodeImportResponse>(responsesCount);
//         for (int i = 0; i < responsesCount; i++) {
//             responses.add(NodeImportResponse.readNew(in));
//         }
//         int failuresCount = in.readInt();
//         this.nodeFailures = new ArrayList<FailedNodeException>(failuresCount);
//         for (int i = 0; i < failuresCount; i++) {
//             String nodeId = in.readString();
//             String msg = in.readOptionalString();
//             FailedNodeException e = new FailedNodeException(nodeId, msg, null);
//             nodeFailures.add(e);
//         }
//     }
// 
//     @Override
//     public void writeTo(StreamOutput out) throws IOException {
//         super.writeTo(out);
//         out.writeInt(responses.size());
//         for (NodeImportResponse response : responses) {
//             response.writeTo(out);
//         }
//         out.writeInt(nodeFailures.size());
//         for (FailedNodeException e : nodeFailures) {
//             out.writeString(e.nodeId());
//             out.writeOptionalString(e.getMessage());
//         }
//     }
// }


// Path: src/main/java/crate/elasticsearch/rest/action/admin/import_/RestImportAction.java
import crate.elasticsearch.action.import_.ImportAction;
import crate.elasticsearch.action.import_.ImportRequest;
import crate.elasticsearch.action.import_.ImportResponse;
import org.elasticsearch.action.Action;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.bytes.BytesReference;
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.RestActions;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestStatus.BAD_REQUEST;
import static org.elasticsearch.rest.RestStatus.OK;

    public void handleRequest(final RestRequest request, final RestChannel channel) {
        ImportRequest importRequest = new ImportRequest();
        importRequest.listenerThreaded(false);
        try {
            if (request.hasContent()) {
                importRequest.source(request.content(), request.contentUnsafe());
            } else {
                String source = request.param("source");
                if (source != null) {
                    importRequest.source(source);
                } else {
                    BytesReference querySource = RestActions.parseQuerySource(request);
                    if (querySource != null) {
                        importRequest.source(querySource, false);
                    }
                }
            }
            importRequest.index(request.param("index"));
            importRequest.type(request.param("type"));
        } catch (Exception e) {
            try {
                XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
                channel.sendResponse(new XContentRestResponse(request, BAD_REQUEST, builder.startObject().field("error", e.getMessage()).endObject()));
            } catch (IOException e1) {
                logger.error("Failed to send failure response", e1);
            }
            return;
        }


        client.execute(action(), importRequest, new ActionListener<ImportResponse>() {