More common AcknowledgedRestResponseActionListener

Introduced use of AcknowledgedRestResponseActionListener in put/delete index template
This commit is contained in:
Luca Cavanna 2013-12-13 19:39:46 +01:00
parent d684a2b8da
commit 9b121baafc
4 changed files with 12 additions and 94 deletions

View File

@ -19,7 +19,7 @@
package org.elasticsearch.action.admin.indices.template.delete; 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.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
@ -28,30 +28,24 @@ import java.io.IOException;
/** /**
* A response for a delete index template. * A response for a delete index template.
*/ */
public class DeleteIndexTemplateResponse extends ActionResponse { public class DeleteIndexTemplateResponse extends AcknowledgedResponse {
private boolean acknowledged;
DeleteIndexTemplateResponse() { DeleteIndexTemplateResponse() {
} }
DeleteIndexTemplateResponse(boolean acknowledged) { DeleteIndexTemplateResponse(boolean acknowledged) {
this.acknowledged = acknowledged; super(acknowledged);
}
public boolean isAcknowledged() {
return acknowledged;
} }
@Override @Override
public void readFrom(StreamInput in) throws IOException { public void readFrom(StreamInput in) throws IOException {
super.readFrom(in); super.readFrom(in);
acknowledged = in.readBoolean(); readAcknowledged(in);
} }
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out); super.writeTo(out);
out.writeBoolean(acknowledged); writeAcknowledged(out);
} }
} }

View File

@ -19,7 +19,7 @@
package org.elasticsearch.action.admin.indices.template.put; 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.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
@ -28,30 +28,24 @@ import java.io.IOException;
/** /**
* A response for a put index template action. * A response for a put index template action.
*/ */
public class PutIndexTemplateResponse extends ActionResponse { public class PutIndexTemplateResponse extends AcknowledgedResponse {
private boolean acknowledged;
PutIndexTemplateResponse() { PutIndexTemplateResponse() {
} }
PutIndexTemplateResponse(boolean acknowledged) { PutIndexTemplateResponse(boolean acknowledged) {
this.acknowledged = acknowledged; super(acknowledged);
}
public boolean isAcknowledged() {
return acknowledged;
} }
@Override @Override
public void readFrom(StreamInput in) throws IOException { public void readFrom(StreamInput in) throws IOException {
super.readFrom(in); super.readFrom(in);
acknowledged = in.readBoolean(); readAcknowledged(in);
} }
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out); super.writeTo(out);
out.writeBoolean(acknowledged); writeAcknowledged(out);
} }
} }

View File

@ -19,20 +19,12 @@
package org.elasticsearch.rest.action.admin.indices.template.delete; 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.DeleteIndexTemplateRequest;
import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse; import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; 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.*;
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 deleteIndexTemplateRequest = new DeleteIndexTemplateRequest(request.param("name"));
deleteIndexTemplateRequest.listenerThreaded(false); deleteIndexTemplateRequest.listenerThreaded(false);
deleteIndexTemplateRequest.masterNodeTimeout(request.paramAsTime("master_timeout", deleteIndexTemplateRequest.masterNodeTimeout())); deleteIndexTemplateRequest.masterNodeTimeout(request.paramAsTime("master_timeout", deleteIndexTemplateRequest.masterNodeTimeout()));
client.admin().indices().deleteTemplate(deleteIndexTemplateRequest, new ActionListener<DeleteIndexTemplateResponse>() { client.admin().indices().deleteTemplate(deleteIndexTemplateRequest, new AcknowledgedRestResponseActionListener<DeleteIndexTemplateResponse>(request, channel, logger));
@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");
} }
} }

View File

@ -19,21 +19,15 @@
package org.elasticsearch.rest.action.admin.indices.template.put; 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.PutIndexTemplateRequest;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse; import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; 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.*;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import java.io.IOException; import java.io.IOException;
import static org.elasticsearch.rest.RestStatus.OK;
/** /**
* *
*/ */
@ -76,34 +70,6 @@ public class RestPutIndexTemplateAction extends BaseRestHandler {
return; return;
} }
client.admin().indices().putTemplate(putRequest, new ActionListener<PutIndexTemplateResponse>() { client.admin().indices().putTemplate(putRequest, new AcknowledgedRestResponseActionListener<PutIndexTemplateResponse>(request, channel, logger));
@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");
} }
} }