@Override
  protected void masterOperation(
      final PutIndexTemplateRequest request,
      final ClusterState state,
      final ActionListener<PutIndexTemplateResponse> listener) {
    String cause = request.cause();
    if (cause.length() == 0) {
      cause = "api";
    }

    indexTemplateService.putTemplate(
        new MetaDataIndexTemplateService.PutRequest(cause, request.name())
            .template(request.template())
            .order(request.order())
            .settings(request.settings())
            .mappings(request.mappings())
            .aliases(request.aliases())
            .customs(request.customs())
            .create(request.create())
            .masterTimeout(request.masterNodeTimeout()),
        new MetaDataIndexTemplateService.PutListener() {
          @Override
          public void onResponse(MetaDataIndexTemplateService.PutResponse response) {
            listener.onResponse(new PutIndexTemplateResponse(response.acknowledged()));
          }

          @Override
          public void onFailure(Throwable t) {
            logger.debug("failed to put template [{}]", t, request.name());
            listener.onFailure(t);
          }
        });
  }
  @SuppressWarnings({"unchecked"})
  @Override
  public void handleRequest(final RestRequest request, final RestChannel channel) {
    PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest(request.param("name"));

    try {
      putRequest.create(request.paramAsBoolean("create", false));
      putRequest.cause(request.param("cause", ""));
      putRequest.timeout(request.paramAsTime("timeout", timeValueSeconds(10)));
      putRequest.source(
          request.contentByteArray(), request.contentByteArrayOffset(), request.contentLength());
    } catch (Exception e) {
      try {
        channel.sendResponse(new XContentThrowableRestResponse(request, e));
      } catch (IOException e1) {
        logger.warn("Failed to send response", e1);
      }
      return;
    }

    putRequest.template(request.param("template", putRequest.template()));
    putRequest.order(request.paramAsInt("order", putRequest.order()));

    client
        .admin()
        .indices()
        .putTemplate(
            putRequest,
            new ActionListener<PutIndexTemplateResponse>() {
              @Override
              public void onResponse(PutIndexTemplateResponse response) {
                try {
                  XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
                  builder
                      .startObject()
                      .field(Fields.OK, true)
                      .field(Fields.ACKNOWLEDGED, response.acknowledged())
                      .endObject();
                  channel.sendResponse(new XContentRestResponse(request, OK, builder));
                } catch (IOException 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);
                }
              }
            });
  }