@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 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);
                }
              }
            });
  }
예제 #3
0
  @Override
  public void handleRequest(final RestRequest request, final RestChannel channel) {
    ClusterStateRequest clusterStateRequest = new ClusterStateRequest();
    clusterStateRequest.listenerThreaded(false);
    clusterStateRequest.masterNodeTimeout(TimeValue.timeValueMillis(0));
    clusterStateRequest.local(true);
    clusterStateRequest.filterAll().filterBlocks(false);
    client
        .admin()
        .cluster()
        .state(
            clusterStateRequest,
            new ActionListener<ClusterStateResponse>() {
              @Override
              public void onResponse(ClusterStateResponse response) {
                RestStatus status = RestStatus.OK;
                if (response.getState().blocks().hasGlobalBlock(RestStatus.SERVICE_UNAVAILABLE)) {
                  status = RestStatus.SERVICE_UNAVAILABLE;
                }
                if (request.method() == RestRequest.Method.HEAD) {
                  channel.sendResponse(new StringRestResponse(status));
                  return;
                }

                try {
                  XContentBuilder builder =
                      RestXContentBuilder.restContentBuilder(request).prettyPrint();
                  builder.startObject();
                  builder.field("ok", true);
                  builder.field("status", status.getStatus());
                  if (settings.get("name") != null) {
                    builder.field("name", settings.get("name"));
                  }
                  builder
                      .startObject("version")
                      .field("number", version.number())
                      .field("build_hash", Build.CURRENT.hash())
                      .field("build_timestamp", Build.CURRENT.timestamp())
                      .field("build_snapshot", version.snapshot)
                      // We use the lucene version from lucene constants since
                      // this includes bugfix release version as well and is already in
                      // the right format. We can also be sure that the format is maitained
                      // since this is also recorded in lucene segments and has BW compat
                      .field("lucene_version", Constants.LUCENE_MAIN_VERSION)
                      .endObject();
                  builder.field("tagline", "You Know, for Search");
                  builder.endObject();
                  channel.sendResponse(new XContentRestResponse(request, status, builder));
                } catch (Throwable e) {
                  onFailure(e);
                }
              }

              @Override
              public void onFailure(Throwable e) {
                try {
                  if (request.method() == HEAD) {
                    channel.sendResponse(new StringRestResponse(ExceptionsHelper.status(e)));
                  } else {
                    channel.sendResponse(new XContentThrowableRestResponse(request, e));
                  }
                } catch (Exception e1) {
                  logger.warn("Failed to send response", e);
                }
              }
            });
  }
  @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);
                }
              }
            });
  }