Skip to content

Commit

Permalink
Added index templates REST support for HEAD and proper 404
Browse files Browse the repository at this point in the history
* Added HEAD support for index templates to find out of they exist
* Returning a 404 instead of a 200 if a GET hits on a non-existing index template

Closes elastic#3434
  • Loading branch information
spinscale committed Aug 5, 2013
1 parent c78f7bb commit 757f97a
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 7 deletions.
Expand Up @@ -62,6 +62,7 @@
import org.elasticsearch.rest.action.admin.indices.status.RestIndicesStatusAction;
import org.elasticsearch.rest.action.admin.indices.template.delete.RestDeleteIndexTemplateAction;
import org.elasticsearch.rest.action.admin.indices.template.get.RestGetIndexTemplateAction;
import org.elasticsearch.rest.action.admin.indices.template.head.RestHeadIndexTemplateAction;
import org.elasticsearch.rest.action.admin.indices.template.put.RestPutIndexTemplateAction;
import org.elasticsearch.rest.action.admin.indices.validate.query.RestValidateQueryAction;
import org.elasticsearch.rest.action.admin.indices.warmer.delete.RestDeleteWarmerAction;
Expand Down Expand Up @@ -142,6 +143,7 @@ protected void configure() {
bind(RestGetIndexTemplateAction.class).asEagerSingleton();
bind(RestPutIndexTemplateAction.class).asEagerSingleton();
bind(RestDeleteIndexTemplateAction.class).asEagerSingleton();
bind(RestHeadIndexTemplateAction.class).asEagerSingleton();

bind(RestPutWarmerAction.class).asEagerSingleton();
bind(RestDeleteWarmerAction.class).asEagerSingleton();
Expand Down
Expand Up @@ -39,6 +39,7 @@
import java.util.Map;

import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestStatus.NOT_FOUND;
import static org.elasticsearch.rest.RestStatus.OK;

/**
Expand Down Expand Up @@ -76,16 +77,21 @@ public void onResponse(ClusterStateResponse response) {

try {
MetaData metaData = response.getState().metaData();
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject();

for (IndexTemplateMetaData indexMetaData : metaData.templates().values()) {
IndexTemplateMetaData.Builder.toXContent(indexMetaData, builder, params);
}
if (metaData.templates().values().size() == 0) {
channel.sendResponse(new StringRestResponse(NOT_FOUND));
} else {
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject();

for (IndexTemplateMetaData indexMetaData : metaData.templates().values()) {
IndexTemplateMetaData.Builder.toXContent(indexMetaData, builder, params);
}

builder.endObject();
builder.endObject();

channel.sendResponse(new XContentRestResponse(request, OK, builder));
channel.sendResponse(new XContentRestResponse(request, OK, builder));
}
} catch (Throwable e) {
onFailure(e);
}
Expand Down
@@ -0,0 +1,83 @@
/*
* Licensed to ElasticSearch and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.rest.action.admin.indices.template.head;

import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest;
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.*;

import static org.elasticsearch.rest.RestRequest.Method.HEAD;
import static org.elasticsearch.rest.RestStatus.NOT_FOUND;
import static org.elasticsearch.rest.RestStatus.OK;

/**
*
*/
public class RestHeadIndexTemplateAction extends BaseRestHandler {

@Inject
public RestHeadIndexTemplateAction(Settings settings, Client client, RestController controller) {
super(settings, client);

controller.registerHandler(HEAD, "/_template/{name}", this);
}

@Override
public void handleRequest(final RestRequest request, final RestChannel channel) {
ClusterStateRequest clusterStateRequest = Requests.clusterStateRequest()
.filterRoutingTable(true)
.filterNodes(true)
.filteredIndexTemplates(request.param("name"))
.filterOutIndices();

clusterStateRequest.listenerThreaded(false);

client.admin().cluster().state(clusterStateRequest, new ActionListener<ClusterStateResponse>() {
@Override
public void onResponse(ClusterStateResponse response) {
try {
boolean templateExists = response.getState().metaData().templates().size() > 0;

if (templateExists) {
channel.sendResponse(new StringRestResponse(OK));
} else {
channel.sendResponse(new StringRestResponse(NOT_FOUND));
}
} catch (Throwable e) {
onFailure(e);
}
}

@Override
public void onFailure(Throwable e) {
try {
channel.sendResponse(new StringRestResponse(ExceptionsHelper.status(e)));
} catch (Exception e1) {
logger.error("Failed to send failure response", e1);
}
}
});
}
}

0 comments on commit 757f97a

Please sign in to comment.