@Override public void handleRequest( final RestRequest request, final RestChannel channel, final NodeClient client) { GetIndexTemplatesRequest getIndexTemplatesRequest = new GetIndexTemplatesRequest(request.param("name")); getIndexTemplatesRequest.local( request.paramAsBoolean("local", getIndexTemplatesRequest.local())); getIndexTemplatesRequest.masterNodeTimeout( request.paramAsTime("master_timeout", getIndexTemplatesRequest.masterNodeTimeout())); client .admin() .indices() .getTemplates( getIndexTemplatesRequest, new RestResponseListener<GetIndexTemplatesResponse>(channel) { @Override public RestResponse buildResponse( GetIndexTemplatesResponse getIndexTemplatesResponse) { boolean templateExists = getIndexTemplatesResponse.getIndexTemplates().size() > 0; if (templateExists) { return new BytesRestResponse( OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY); } else { return new BytesRestResponse( NOT_FOUND, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY); } } }); }
@Override public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { final String[] names = Strings.splitStringByCommaToArray(request.param("name")); GetIndexTemplatesRequest getIndexTemplatesRequest = new GetIndexTemplatesRequest(names); getIndexTemplatesRequest.local( request.paramAsBoolean("local", getIndexTemplatesRequest.local())); getIndexTemplatesRequest.masterNodeTimeout( request.paramAsTime("master_timeout", getIndexTemplatesRequest.masterNodeTimeout())); final boolean implicitAll = getIndexTemplatesRequest.names().length == 0; return channel -> client .admin() .indices() .getTemplates( getIndexTemplatesRequest, new RestToXContentListener<GetIndexTemplatesResponse>(channel) { @Override protected RestStatus getStatus(GetIndexTemplatesResponse response) { boolean templateExists = false == response.getIndexTemplates().isEmpty(); return (templateExists || implicitAll) ? OK : NOT_FOUND; } }); }