@Override
  public void handleRequest(final RestRequest request, final RestChannel channel) {
    ClusterStateRequest clusterStateRequest =
        Requests.clusterStateRequest()
            .filterRoutingTable(true)
            .filterNodes(true)
            .filteredIndexTemplates(request.param("name"))
            .filterOutIndices();

    clusterStateRequest.listenerThreaded(false);

    client
        .admin()
        .cluster()
        .state(
            clusterStateRequest,
            new ActionListener<ClusterStateResponse>() {
              @Override
              public void onResponse(ClusterStateResponse response) {
                Map<String, String> paramsMap = Maps.newHashMap();
                paramsMap.put("reduce_mappings", "true");
                ToXContent.Params params = new ToXContent.DelegatingMapParams(paramsMap, request);

                try {
                  MetaData metaData = response.getState().metaData();
                  XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
                  builder.startObject();

                  for (IndexTemplateMetaData indexMetaData : metaData.templates().values()) {
                    IndexTemplateMetaData.Builder.toXContent(indexMetaData, builder, params);
                  }

                  builder.endObject();

                  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);
                }
              }
            });
  }
 @Override
 public Set<String> queryForAlias(String indexName) {
   ClusterStateRequest clusterStateRequest =
       Requests.clusterStateRequest().routingTable(true).nodes(true).indices(indexName);
   Iterator<String> iterator =
       client
           .admin()
           .cluster()
           .state(clusterStateRequest)
           .actionGet()
           .getState()
           .getMetaData()
           .aliases()
           .keysIt();
   return newHashSet(iterator);
 }
  @Test
  public void testCopyHeadersClusterAdminRequest() {
    Map<String, String> transportHeaders = randomHeaders(randomIntBetween(0, 10));
    Map<String, String> restHeaders = randomHeaders(randomIntBetween(0, 10));
    Map<String, String> copiedHeaders = randomHeadersFrom(restHeaders);
    Set<String> usefulRestHeaders = new HashSet<>(copiedHeaders.keySet());
    usefulRestHeaders.addAll(randomMap(randomIntBetween(0, 10), "useful-").keySet());
    Map<String, String> restContext = randomContext(randomIntBetween(0, 10));
    Map<String, String> transportContext =
        Maps.difference(randomContext(randomIntBetween(0, 10)), restContext).entriesOnlyOnLeft();

    HashMap<String, String> expectedHeaders = new HashMap<>();
    expectedHeaders.putAll(transportHeaders);
    expectedHeaders.putAll(copiedHeaders);

    Map<String, String> expectedContext = new HashMap<>();
    expectedContext.putAll(transportContext);
    expectedContext.putAll(restContext);

    Client client =
        client(
            new NoOpClient(), new FakeRestRequest(restHeaders, expectedContext), usefulRestHeaders);

    ClusterHealthRequest clusterHealthRequest = Requests.clusterHealthRequest();
    putHeaders(clusterHealthRequest, transportHeaders);
    putContext(clusterHealthRequest, transportContext);
    assertHeaders(clusterHealthRequest, transportHeaders);
    client.admin().cluster().health(clusterHealthRequest);
    assertHeaders(clusterHealthRequest, expectedHeaders);
    assertContext(clusterHealthRequest, expectedContext);

    ClusterStateRequest clusterStateRequest = Requests.clusterStateRequest();
    putHeaders(clusterStateRequest, transportHeaders);
    putContext(clusterStateRequest, transportContext);
    assertHeaders(clusterStateRequest, transportHeaders);
    client.admin().cluster().state(clusterStateRequest);
    assertHeaders(clusterStateRequest, expectedHeaders);
    assertContext(clusterStateRequest, expectedContext);

    ClusterStatsRequest clusterStatsRequest = Requests.clusterStatsRequest();
    putHeaders(clusterStatsRequest, transportHeaders);
    putContext(clusterStatsRequest, transportContext);
    assertHeaders(clusterStatsRequest, transportHeaders);
    client.admin().cluster().clusterStats(clusterStatsRequest);
    assertHeaders(clusterStatsRequest, expectedHeaders);
    assertContext(clusterStatsRequest, expectedContext);
  }
  @Override
  public void handleRequest(final RestRequest request, final RestChannel channel) {
    ClusterStateRequest clusterStateRequest =
        Requests.clusterStateRequest().listenerThreaded(false).routingTable(false).nodes(false);
    clusterStateRequest.local(request.paramAsBoolean("local", clusterStateRequest.local()));
    client
        .admin()
        .cluster()
        .state(
            clusterStateRequest,
            new ActionListener<ClusterStateResponse>() {
              @Override
              public void onResponse(ClusterStateResponse response) {
                try {
                  XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
                  builder.startObject();

                  builder.startObject("persistent");
                  response.getState().metaData().persistentSettings().toXContent(builder, request);
                  builder.endObject();

                  builder.startObject("transient");
                  response.getState().metaData().transientSettings().toXContent(builder, request);
                  builder.endObject();

                  builder.endObject();

                  channel.sendResponse(new XContentRestResponse(request, RestStatus.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);
                }
              }
            });
  }
  @Override
  public void handleRequest(final RestRequest request, final RestChannel channel) {
    ClusterStateRequest clusterStateRequest =
        Requests.clusterStateRequest()
            .filterRoutingTable(true)
            .filterNodes(true)
            .filteredIndexTemplates(request.param("name"))
            .filteredIndices("_na");

    clusterStateRequest.listenerThreaded(false);

    client
        .admin()
        .cluster()
        .state(
            clusterStateRequest,
            new ActionListener<ClusterStateResponse>() {
              @Override
              public void onResponse(ClusterStateResponse response) {
                try {
                  MetaData metaData = response.state().metaData();
                  XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
                  builder.startObject();

                  for (IndexTemplateMetaData indexMetaData : metaData.templates().values()) {
                    builder.startObject(
                        indexMetaData.name(), XContentBuilder.FieldCaseConversion.NONE);

                    builder.field("template", indexMetaData.template());
                    builder.field("order", indexMetaData.order());

                    builder.startObject("settings");
                    Settings settings = settingsFilter.filterSettings(indexMetaData.settings());
                    for (Map.Entry<String, String> entry : settings.getAsMap().entrySet()) {
                      builder.field(entry.getKey(), entry.getValue());
                    }
                    builder.endObject();

                    builder.startObject("mappings");
                    for (Map.Entry<String, CompressedString> entry :
                        indexMetaData.mappings().entrySet()) {
                      byte[] mappingSource = entry.getValue().uncompressed();
                      XContentParser parser =
                          XContentFactory.xContent(mappingSource).createParser(mappingSource);
                      Map<String, Object> mapping = parser.map();
                      if (mapping.size() == 1 && mapping.containsKey(entry.getKey())) {
                        // the type name is the root value, reduce it
                        mapping = (Map<String, Object>) mapping.get(entry.getKey());
                      }
                      builder.field(entry.getKey());
                      builder.map(mapping);
                    }
                    builder.endObject();

                    builder.endObject();
                  }

                  builder.endObject();

                  channel.sendResponse(new XContentRestResponse(request, OK, builder));
                } catch (Exception 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);
                }
              }
            });
  }
  @Override
  public void handleRequest(final RestRequest request, final RestChannel channel) {
    final String[] indices = splitIndices(request.param("index"));

    ClusterStateRequest clusterStateRequest =
        Requests.clusterStateRequest()
            .filterRoutingTable(true)
            .filterNodes(true)
            .filteredIndices(indices);
    clusterStateRequest.listenerThreaded(false);

    client
        .admin()
        .cluster()
        .state(
            clusterStateRequest,
            new ActionListener<ClusterStateResponse>() {
              @Override
              public void onResponse(ClusterStateResponse response) {
                try {
                  MetaData metaData = response.state().metaData();

                  if (metaData.indices().isEmpty()) {
                    channel.sendResponse(
                        new XContentThrowableRestResponse(
                            request, new IndexMissingException(new Index(indices[0]))));
                    return;
                  }

                  boolean foundAny = false;
                  XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
                  builder.startObject();

                  for (IndexMetaData indexMetaData : metaData) {
                    builder.startObject(
                        indexMetaData.index(), XContentBuilder.FieldCaseConversion.NONE);
                    foundAny = true;
                    builder.startObject("settings");
                    Settings settings = settingsFilter.filterSettings(indexMetaData.settings());
                    for (Map.Entry<String, String> entry : settings.getAsMap().entrySet()) {
                      builder.field(entry.getKey(), entry.getValue());
                    }
                    builder.endObject();

                    builder.endObject();
                  }

                  builder.endObject();

                  channel.sendResponse(
                      new XContentRestResponse(request, foundAny ? OK : NOT_FOUND, builder));
                } catch (Exception 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);
                }
              }
            });
  }