More common AcknowledgedRestResponseActionListener

Introduced use of AcknowledgedRestResponseActionListener in put/delete alias & delete index
This commit is contained in:
Luca Cavanna 2013-12-11 21:28:15 +01:00
parent 037bf6a85f
commit b1fa147968
4 changed files with 8 additions and 128 deletions

View File

@ -19,7 +19,7 @@
package org.elasticsearch.action.admin.indices.delete; package org.elasticsearch.action.admin.indices.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,34 +28,24 @@ import java.io.IOException;
/** /**
* A response for a delete index action. * A response for a delete index action.
*/ */
public class DeleteIndexResponse extends ActionResponse { public class DeleteIndexResponse extends AcknowledgedResponse {
private boolean acknowledged;
DeleteIndexResponse() { DeleteIndexResponse() {
} }
DeleteIndexResponse(boolean acknowledged) { DeleteIndexResponse(boolean acknowledged) {
this.acknowledged = acknowledged; super(acknowledged);
}
/**
* Has the index deletion been acknowledged by all current cluster nodes within the
* provided {@link DeleteIndexRequest#timeout(org.elasticsearch.common.unit.TimeValue)}.
*/
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

@ -18,21 +18,14 @@
package org.elasticsearch.rest.action.admin.indices.alias.delete; package org.elasticsearch.rest.action.admin.indices.alias.delete;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesResponse; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesResponse;
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.RestRequest.Method.DELETE; import static org.elasticsearch.rest.RestRequest.Method.DELETE;
import static org.elasticsearch.rest.RestStatus.OK;
/** /**
*/ */
@ -53,37 +46,6 @@ public class RestIndexDeleteAliasesAction extends BaseRestHandler {
indicesAliasesRequest.removeAlias(index, alias); indicesAliasesRequest.removeAlias(index, alias);
indicesAliasesRequest.masterNodeTimeout(request.paramAsTime("master_timeout", indicesAliasesRequest.masterNodeTimeout())); indicesAliasesRequest.masterNodeTimeout(request.paramAsTime("master_timeout", indicesAliasesRequest.masterNodeTimeout()));
client.admin().indices().aliases(indicesAliasesRequest, new ActionListener<IndicesAliasesResponse>() { client.admin().indices().aliases(indicesAliasesRequest, new AcknowledgedRestResponseActionListener<IndicesAliasesResponse>(request, channel, logger));
@Override
public void onResponse(IndicesAliasesResponse response) {
try {
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject();
builder.field(Fields.OK, true);
builder.field(Fields.ACK, response.isAcknowledged());
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);
}
}
});
}
static class Fields {
static final XContentBuilderString OK = new XContentBuilderString("ok");
static final XContentBuilderString ACK = new XContentBuilderString("acknowledged");
} }
} }

View File

@ -19,25 +19,20 @@
package org.elasticsearch.rest.action.admin.indices.alias.put; package org.elasticsearch.rest.action.admin.indices.alias.put;
import org.elasticsearch.ElasticSearchIllegalArgumentException; import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesResponse; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesResponse;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.metadata.AliasAction; import org.elasticsearch.cluster.metadata.AliasAction;
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.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.rest.*; import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
import static org.elasticsearch.rest.RestRequest.Method.PUT; import static org.elasticsearch.rest.RestRequest.Method.PUT;
import static org.elasticsearch.rest.RestStatus.OK;
/** /**
*/ */
@ -123,37 +118,6 @@ public class RestIndexPutAliasAction extends BaseRestHandler {
if (filter != null) { if (filter != null) {
aliasAction.filter(filter); aliasAction.filter(filter);
} }
client.admin().indices().aliases(indicesAliasesRequest, new ActionListener<IndicesAliasesResponse>() { client.admin().indices().aliases(indicesAliasesRequest, new AcknowledgedRestResponseActionListener<IndicesAliasesResponse>(request, channel, logger));
@Override
public void onResponse(IndicesAliasesResponse response) {
try {
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject();
builder.field(Fields.OK, true);
builder.field(Fields.ACK, response.isAcknowledged());
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);
}
}
});
}
static class Fields {
static final XContentBuilderString OK = new XContentBuilderString("ok");
static final XContentBuilderString ACK = new XContentBuilderString("acknowledged");
} }
} }

View File

@ -19,21 +19,13 @@
package org.elasticsearch.rest.action.admin.indices.delete; package org.elasticsearch.rest.action.admin.indices.delete;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse; import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
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;
/** /**
* *
@ -53,34 +45,6 @@ public class RestDeleteIndexAction extends BaseRestHandler {
deleteIndexRequest.listenerThreaded(false); deleteIndexRequest.listenerThreaded(false);
deleteIndexRequest.timeout(request.paramAsTime("timeout", deleteIndexRequest.timeout())); deleteIndexRequest.timeout(request.paramAsTime("timeout", deleteIndexRequest.timeout()));
deleteIndexRequest.masterNodeTimeout(request.paramAsTime("master_timeout", deleteIndexRequest.masterNodeTimeout())); deleteIndexRequest.masterNodeTimeout(request.paramAsTime("master_timeout", deleteIndexRequest.masterNodeTimeout()));
client.admin().indices().delete(deleteIndexRequest, new ActionListener<DeleteIndexResponse>() { client.admin().indices().delete(deleteIndexRequest, new AcknowledgedRestResponseActionListener<DeleteIndexResponse>(request, channel, logger));
@Override
public void onResponse(DeleteIndexResponse 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");
} }
} }