From 9b121baafcfa1964f1c79a81bd47e60b4f1de6e1 Mon Sep 17 00:00:00 2001 From: Luca Cavanna Date: Fri, 13 Dec 2013 19:39:46 +0100 Subject: [PATCH] More common AcknowledgedRestResponseActionListener Introduced use of AcknowledgedRestResponseActionListener in put/delete index template --- .../delete/DeleteIndexTemplateResponse.java | 16 +++----- .../put/PutIndexTemplateResponse.java | 16 +++----- .../delete/RestDeleteIndexTemplateAction.java | 38 +------------------ .../put/RestPutIndexTemplateAction.java | 36 +----------------- 4 files changed, 12 insertions(+), 94 deletions(-) diff --git a/src/main/java/org/elasticsearch/action/admin/indices/template/delete/DeleteIndexTemplateResponse.java b/src/main/java/org/elasticsearch/action/admin/indices/template/delete/DeleteIndexTemplateResponse.java index ad9f757a0d6..ae11260340e 100644 --- a/src/main/java/org/elasticsearch/action/admin/indices/template/delete/DeleteIndexTemplateResponse.java +++ b/src/main/java/org/elasticsearch/action/admin/indices/template/delete/DeleteIndexTemplateResponse.java @@ -19,7 +19,7 @@ package org.elasticsearch.action.admin.indices.template.delete; -import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; @@ -28,30 +28,24 @@ import java.io.IOException; /** * A response for a delete index template. */ -public class DeleteIndexTemplateResponse extends ActionResponse { - - private boolean acknowledged; +public class DeleteIndexTemplateResponse extends AcknowledgedResponse { DeleteIndexTemplateResponse() { } DeleteIndexTemplateResponse(boolean acknowledged) { - this.acknowledged = acknowledged; - } - - public boolean isAcknowledged() { - return acknowledged; + super(acknowledged); } @Override public void readFrom(StreamInput in) throws IOException { super.readFrom(in); - acknowledged = in.readBoolean(); + readAcknowledged(in); } @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); - out.writeBoolean(acknowledged); + writeAcknowledged(out); } } diff --git a/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateResponse.java b/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateResponse.java index 0584c1bb9ce..eb81beac396 100644 --- a/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateResponse.java +++ b/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateResponse.java @@ -19,7 +19,7 @@ package org.elasticsearch.action.admin.indices.template.put; -import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; @@ -28,30 +28,24 @@ import java.io.IOException; /** * A response for a put index template action. */ -public class PutIndexTemplateResponse extends ActionResponse { - - private boolean acknowledged; +public class PutIndexTemplateResponse extends AcknowledgedResponse { PutIndexTemplateResponse() { } PutIndexTemplateResponse(boolean acknowledged) { - this.acknowledged = acknowledged; - } - - public boolean isAcknowledged() { - return acknowledged; + super(acknowledged); } @Override public void readFrom(StreamInput in) throws IOException { super.readFrom(in); - acknowledged = in.readBoolean(); + readAcknowledged(in); } @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); - out.writeBoolean(acknowledged); + writeAcknowledged(out); } } diff --git a/src/main/java/org/elasticsearch/rest/action/admin/indices/template/delete/RestDeleteIndexTemplateAction.java b/src/main/java/org/elasticsearch/rest/action/admin/indices/template/delete/RestDeleteIndexTemplateAction.java index 21d855c936a..d0ab34e7b92 100644 --- a/src/main/java/org/elasticsearch/rest/action/admin/indices/template/delete/RestDeleteIndexTemplateAction.java +++ b/src/main/java/org/elasticsearch/rest/action/admin/indices/template/delete/RestDeleteIndexTemplateAction.java @@ -19,20 +19,12 @@ package org.elasticsearch.rest.action.admin.indices.template.delete; -import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest; import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse; import org.elasticsearch.client.Client; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentBuilderString; import org.elasticsearch.rest.*; -import org.elasticsearch.rest.action.support.RestXContentBuilder; - -import java.io.IOException; - -import static org.elasticsearch.rest.RestStatus.OK; /** * @@ -50,34 +42,6 @@ public class RestDeleteIndexTemplateAction extends BaseRestHandler { DeleteIndexTemplateRequest deleteIndexTemplateRequest = new DeleteIndexTemplateRequest(request.param("name")); deleteIndexTemplateRequest.listenerThreaded(false); deleteIndexTemplateRequest.masterNodeTimeout(request.paramAsTime("master_timeout", deleteIndexTemplateRequest.masterNodeTimeout())); - client.admin().indices().deleteTemplate(deleteIndexTemplateRequest, new ActionListener() { - @Override - public void onResponse(DeleteIndexTemplateResponse response) { - try { - XContentBuilder builder = RestXContentBuilder.restContentBuilder(request); - builder.startObject() - .field(Fields.OK, true) - .field(Fields.ACKNOWLEDGED, response.isAcknowledged()) - .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); - } - } - }); - } - - static final class Fields { - static final XContentBuilderString OK = new XContentBuilderString("ok"); - static final XContentBuilderString ACKNOWLEDGED = new XContentBuilderString("acknowledged"); + client.admin().indices().deleteTemplate(deleteIndexTemplateRequest, new AcknowledgedRestResponseActionListener(request, channel, logger)); } } diff --git a/src/main/java/org/elasticsearch/rest/action/admin/indices/template/put/RestPutIndexTemplateAction.java b/src/main/java/org/elasticsearch/rest/action/admin/indices/template/put/RestPutIndexTemplateAction.java index a0e6bfa5e0e..d663dd10364 100644 --- a/src/main/java/org/elasticsearch/rest/action/admin/indices/template/put/RestPutIndexTemplateAction.java +++ b/src/main/java/org/elasticsearch/rest/action/admin/indices/template/put/RestPutIndexTemplateAction.java @@ -19,21 +19,15 @@ package org.elasticsearch.rest.action.admin.indices.template.put; -import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest; import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse; import org.elasticsearch.client.Client; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentBuilderString; import org.elasticsearch.rest.*; -import org.elasticsearch.rest.action.support.RestXContentBuilder; import java.io.IOException; -import static org.elasticsearch.rest.RestStatus.OK; - /** * */ @@ -76,34 +70,6 @@ public class RestPutIndexTemplateAction extends BaseRestHandler { return; } - client.admin().indices().putTemplate(putRequest, new ActionListener() { - @Override - public void onResponse(PutIndexTemplateResponse response) { - try { - XContentBuilder builder = RestXContentBuilder.restContentBuilder(request); - builder.startObject() - .field(Fields.OK, true) - .field(Fields.ACKNOWLEDGED, response.isAcknowledged()) - .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); - } - } - }); - } - - static final class Fields { - static final XContentBuilderString OK = new XContentBuilderString("ok"); - static final XContentBuilderString ACKNOWLEDGED = new XContentBuilderString("acknowledged"); + client.admin().indices().putTemplate(putRequest, new AcknowledgedRestResponseActionListener(request, channel, logger)); } }