Calling `GET _template` gave a 500 error back.

Also:
We always return a valid json back
If there are no templates define we return 200 OK ( as opposed to a request for a specific template id which returns 404)

Closes #3812
This commit is contained in:
Boaz Leskes 2013-10-01 14:18:37 +02:00
parent ba40aa374e
commit b5ae1fb51b
2 changed files with 18 additions and 18 deletions

View File

@ -36,7 +36,8 @@ public class GetIndexTemplatesRequest extends MasterNodeOperationRequest<GetInde
private String[] names;
public GetIndexTemplatesRequest() {}
public GetIndexTemplatesRequest() {
}
public GetIndexTemplatesRequest(String... names) {
this.names = names;
@ -45,7 +46,7 @@ public class GetIndexTemplatesRequest extends MasterNodeOperationRequest<GetInde
@Override
public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = null;
if (names == null || names.length == 0) {
if (names == null) {
validationException = addValidationError("names is null or empty", validationException);
} else {
for (String name : names) {

View File

@ -20,7 +20,6 @@
package org.elasticsearch.rest.action.admin.indices.template.get;
import com.google.common.collect.Maps;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest;
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse;
@ -60,28 +59,28 @@ public class RestGetIndexTemplateAction extends BaseRestHandler {
GetIndexTemplatesRequest getIndexTemplatesRequest = new GetIndexTemplatesRequest(names);
getIndexTemplatesRequest.listenerThreaded(false);
final boolean implicitAll = getIndexTemplatesRequest.names().length == 0;
client.admin().indices().getTemplates(getIndexTemplatesRequest, new ActionListener<GetIndexTemplatesResponse>() {
@Override
public void onResponse(GetIndexTemplatesResponse getIndexTemplatesResponse) {
try {
boolean templateExists = getIndexTemplatesResponse.getIndexTemplates().size() > 0;
if (!templateExists) {
channel.sendResponse(new StringRestResponse(NOT_FOUND));
} else {
Map<String, String> paramsMap = Maps.newHashMap();
paramsMap.put("reduce_mappings", "true");
ToXContent.Params params = new ToXContent.DelegatingMapParams(paramsMap, request);
Map<String, String> paramsMap = Maps.newHashMap();
paramsMap.put("reduce_mappings", "true");
ToXContent.Params params = new ToXContent.DelegatingMapParams(paramsMap, request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject();
for (IndexTemplateMetaData indexTemplateMetaData : getIndexTemplatesResponse.getIndexTemplates()) {
IndexTemplateMetaData.Builder.toXContent(indexTemplateMetaData, builder, params);
}
builder.endObject();
channel.sendResponse(new XContentRestResponse(request, OK, builder));
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject();
for (IndexTemplateMetaData indexTemplateMetaData : getIndexTemplatesResponse.getIndexTemplates()) {
IndexTemplateMetaData.Builder.toXContent(indexTemplateMetaData, builder, params);
}
builder.endObject();
RestStatus restStatus = (templateExists || implicitAll) ? OK : NOT_FOUND;
channel.sendResponse(new XContentRestResponse(request, restStatus, builder));
} catch (Throwable e) {
onFailure(e);
}
@ -90,7 +89,7 @@ public class RestGetIndexTemplateAction extends BaseRestHandler {
@Override
public void onFailure(Throwable e) {
try {
channel.sendResponse(new StringRestResponse(ExceptionsHelper.status(e)));
channel.sendResponse(new XContentThrowableRestResponse(request, e));
} catch (Exception e1) {
logger.error("Failed to send failure response", e1);
}