// Path: chapter_17/rest_plugin/src/main/java/org/elasticsearch/action/simple/SimpleAction.java
// public class SimpleAction extends Action<SimpleRequest, SimpleResponse, SimpleRequestBuilder> {
// 
//     public static final SimpleAction INSTANCE = new SimpleAction();
//     public static final String NAME = "custom:indices/simple";
// 
//     private SimpleAction() {
//         super(NAME);
//     }
// 
//     @Override
//     public SimpleResponse newResponse() {
//         return new SimpleResponse();
//     }
// 
//     @Override
//     public SimpleRequestBuilder newRequestBuilder(ElasticsearchClient elasticsearchClient) {
//         return new SimpleRequestBuilder(elasticsearchClient, this);
//     }
// 
// }
// 
// Path: chapter_17/rest_plugin/src/main/java/org/elasticsearch/action/simple/TransportSimpleAction.java
// public class TransportSimpleAction
//         extends TransportBroadcastByNodeAction<SimpleRequest, SimpleResponse, ShardSimpleResponse> {
// 
//     private final IndicesService indicesService;
// 
//     @Inject
//     public TransportSimpleAction(Settings settings, ThreadPool threadPool, ClusterService clusterService,
//                                  TransportService transportService, IndicesService indicesService,
//                                  ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver) {
//         super(settings, SimpleAction.NAME, threadPool, clusterService, transportService, actionFilters,
//                 indexNameExpressionResolver, SimpleRequest::new, ThreadPool.Names.SEARCH);
//         this.indicesService = indicesService;
//     }
// 
// 
//     @Override
//     protected SimpleResponse newResponse(SimpleRequest request, int totalShards, int successfulShards, int failedShards, List<ShardSimpleResponse> shardSimpleResponses, List<ShardOperationFailedException> shardFailures, ClusterState clusterState) {
//         Set<String> simple = new HashSet<String>();
//         for (ShardSimpleResponse shardSimpleResponse : shardSimpleResponses) {
//             simple.addAll(shardSimpleResponse.getTermList());
//         }
// 
//         return new SimpleResponse(totalShards, successfulShards, failedShards, shardFailures, simple);
//     }
// 
//     @Override
//     protected ShardSimpleResponse shardOperation(SimpleRequest request, ShardRouting shardRouting) throws IOException {
//         IndexService indexService = indicesService.indexServiceSafe(shardRouting.shardId().getIndex());
//         IndexShard indexShard = indexService.getShard(shardRouting.shardId().id());
//         indexShard.store().directory();
//         Set<String> set = new HashSet<String>();
//         set.add(request.getField() + "_" + shardRouting.shardId());
//         return new ShardSimpleResponse(shardRouting, set);
//     }
// 
// 
//     @Override
//     protected ShardSimpleResponse readShardResult(StreamInput in) throws IOException {
//         return ShardSimpleResponse.readShardResult(in);
//     }
// 
//     @Override
//     protected SimpleRequest readRequestFrom(StreamInput in) throws IOException {
//         SimpleRequest request = new SimpleRequest();
//         request.readFrom(in);
//         return request;
//     }
// 
//     @Override
//     protected ShardsIterator shards(ClusterState clusterState, SimpleRequest request, String[] concreteIndices) {
//         return clusterState.routingTable().allShards(concreteIndices);
//     }
// 
//     @Override
//     protected ClusterBlockException checkGlobalBlock(ClusterState state, SimpleRequest request) {
//         return state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_READ);
//     }
// 
//     @Override
//     protected ClusterBlockException checkRequestBlock(ClusterState state, SimpleRequest request, String[] concreteIndices) {
//         return state.blocks().indicesBlockedException(ClusterBlockLevel.METADATA_READ, concreteIndices);
//     }
// }
// 
// Path: chapter_17/rest_plugin/src/main/java/org/elasticsearch/rest/RestSimpleAction.java
// public class RestSimpleAction extends BaseRestHandler {
//     @Inject
//     public RestSimpleAction(Settings settings, Client client, RestController controller) {
//         super(settings);
//         controller.registerHandler(POST, "/_simple", this);
//         controller.registerHandler(POST, "/{index}/_simple", this);
//         controller.registerHandler(POST, "/_simple/{field}", this);
//         controller.registerHandler(GET, "/_simple", this);
//         controller.registerHandler(GET, "/{index}/_simple", this);
//         controller.registerHandler(GET, "/_simple/{field}", this);
//     }
// 
//     @Override
//     protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
//         final SimpleRequest simpleRequest = new SimpleRequest(Strings.splitStringByCommaToArray(request.param("index")));
//         simpleRequest.setField(request.param("field"));
//         return channel -> client.execute(SimpleAction.INSTANCE, simpleRequest, new RestBuilderListener<SimpleResponse>(channel){
//             @Override
//             public RestResponse buildResponse(SimpleResponse simpleResponse, XContentBuilder builder) throws Exception {
//                 try {
//                     builder.startObject();
//                     builder.field("ok", true);
//                     builder.array("terms", simpleResponse.getSimple().toArray());
//                     builder.endObject();
// 
//                 } catch (Exception e) {
//                     onFailure(e);
//                 }
//                 return new BytesRestResponse(OK, builder);
//             }
//         });
//     }
// }


import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.action.simple.SimpleAction;
import org.elasticsearch.action.simple.TransportSimpleAction;
import org.elasticsearch.plugins.ActionPlugin;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.rest.RestHandler;
import org.elasticsearch.rest.RestSimpleAction;
import java.util.List;
import static java.util.Collections.singletonList;
package org.elasticsearch.plugin.simple;




public class RestPlugin extends Plugin implements ActionPlugin {

    @Override
    public List<Class<? extends RestHandler>> getRestHandlers() {
// Path: chapter_17/rest_plugin/src/main/java/org/elasticsearch/action/simple/SimpleAction.java
// public class SimpleAction extends Action<SimpleRequest, SimpleResponse, SimpleRequestBuilder> {
// 
//     public static final SimpleAction INSTANCE = new SimpleAction();
//     public static final String NAME = "custom:indices/simple";
// 
//     private SimpleAction() {
//         super(NAME);
//     }
// 
//     @Override
//     public SimpleResponse newResponse() {
//         return new SimpleResponse();
//     }
// 
//     @Override
//     public SimpleRequestBuilder newRequestBuilder(ElasticsearchClient elasticsearchClient) {
//         return new SimpleRequestBuilder(elasticsearchClient, this);
//     }
// 
// }
// 
// Path: chapter_17/rest_plugin/src/main/java/org/elasticsearch/action/simple/TransportSimpleAction.java
// public class TransportSimpleAction
//         extends TransportBroadcastByNodeAction<SimpleRequest, SimpleResponse, ShardSimpleResponse> {
// 
//     private final IndicesService indicesService;
// 
//     @Inject
//     public TransportSimpleAction(Settings settings, ThreadPool threadPool, ClusterService clusterService,
//                                  TransportService transportService, IndicesService indicesService,
//                                  ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver) {
//         super(settings, SimpleAction.NAME, threadPool, clusterService, transportService, actionFilters,
//                 indexNameExpressionResolver, SimpleRequest::new, ThreadPool.Names.SEARCH);
//         this.indicesService = indicesService;
//     }
// 
// 
//     @Override
//     protected SimpleResponse newResponse(SimpleRequest request, int totalShards, int successfulShards, int failedShards, List<ShardSimpleResponse> shardSimpleResponses, List<ShardOperationFailedException> shardFailures, ClusterState clusterState) {
//         Set<String> simple = new HashSet<String>();
//         for (ShardSimpleResponse shardSimpleResponse : shardSimpleResponses) {
//             simple.addAll(shardSimpleResponse.getTermList());
//         }
// 
//         return new SimpleResponse(totalShards, successfulShards, failedShards, shardFailures, simple);
//     }
// 
//     @Override
//     protected ShardSimpleResponse shardOperation(SimpleRequest request, ShardRouting shardRouting) throws IOException {
//         IndexService indexService = indicesService.indexServiceSafe(shardRouting.shardId().getIndex());
//         IndexShard indexShard = indexService.getShard(shardRouting.shardId().id());
//         indexShard.store().directory();
//         Set<String> set = new HashSet<String>();
//         set.add(request.getField() + "_" + shardRouting.shardId());
//         return new ShardSimpleResponse(shardRouting, set);
//     }
// 
// 
//     @Override
//     protected ShardSimpleResponse readShardResult(StreamInput in) throws IOException {
//         return ShardSimpleResponse.readShardResult(in);
//     }
// 
//     @Override
//     protected SimpleRequest readRequestFrom(StreamInput in) throws IOException {
//         SimpleRequest request = new SimpleRequest();
//         request.readFrom(in);
//         return request;
//     }
// 
//     @Override
//     protected ShardsIterator shards(ClusterState clusterState, SimpleRequest request, String[] concreteIndices) {
//         return clusterState.routingTable().allShards(concreteIndices);
//     }
// 
//     @Override
//     protected ClusterBlockException checkGlobalBlock(ClusterState state, SimpleRequest request) {
//         return state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_READ);
//     }
// 
//     @Override
//     protected ClusterBlockException checkRequestBlock(ClusterState state, SimpleRequest request, String[] concreteIndices) {
//         return state.blocks().indicesBlockedException(ClusterBlockLevel.METADATA_READ, concreteIndices);
//     }
// }
// 
// Path: chapter_17/rest_plugin/src/main/java/org/elasticsearch/rest/RestSimpleAction.java
// public class RestSimpleAction extends BaseRestHandler {
//     @Inject
//     public RestSimpleAction(Settings settings, Client client, RestController controller) {
//         super(settings);
//         controller.registerHandler(POST, "/_simple", this);
//         controller.registerHandler(POST, "/{index}/_simple", this);
//         controller.registerHandler(POST, "/_simple/{field}", this);
//         controller.registerHandler(GET, "/_simple", this);
//         controller.registerHandler(GET, "/{index}/_simple", this);
//         controller.registerHandler(GET, "/_simple/{field}", this);
//     }
// 
//     @Override
//     protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
//         final SimpleRequest simpleRequest = new SimpleRequest(Strings.splitStringByCommaToArray(request.param("index")));
//         simpleRequest.setField(request.param("field"));
//         return channel -> client.execute(SimpleAction.INSTANCE, simpleRequest, new RestBuilderListener<SimpleResponse>(channel){
//             @Override
//             public RestResponse buildResponse(SimpleResponse simpleResponse, XContentBuilder builder) throws Exception {
//                 try {
//                     builder.startObject();
//                     builder.field("ok", true);
//                     builder.array("terms", simpleResponse.getSimple().toArray());
//                     builder.endObject();
// 
//                 } catch (Exception e) {
//                     onFailure(e);
//                 }
//                 return new BytesRestResponse(OK, builder);
//             }
//         });
//     }
// }


// Path: chapter_17/rest_plugin/src/main/java/org/elasticsearch/plugin/simple/RestPlugin.java
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.action.simple.SimpleAction;
import org.elasticsearch.action.simple.TransportSimpleAction;
import org.elasticsearch.plugins.ActionPlugin;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.rest.RestHandler;
import org.elasticsearch.rest.RestSimpleAction;
import java.util.List;
import static java.util.Collections.singletonList;

package org.elasticsearch.plugin.simple;




public class RestPlugin extends Plugin implements ActionPlugin {

    @Override
    public List<Class<? extends RestHandler>> getRestHandlers() {
        return singletonList(RestSimpleAction.class);