diff --git a/src/main/java/org/elasticsearch/action/admin/indices/alias/get/TransportGetAliasesAction.java b/src/main/java/org/elasticsearch/action/admin/indices/alias/get/TransportGetAliasesAction.java index dce8a6e5f23..b316e362358 100644 --- a/src/main/java/org/elasticsearch/action/admin/indices/alias/get/TransportGetAliasesAction.java +++ b/src/main/java/org/elasticsearch/action/admin/indices/alias/get/TransportGetAliasesAction.java @@ -26,7 +26,6 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.AliasMetaData; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.indices.AliasMissingException; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; @@ -68,10 +67,8 @@ public class TransportGetAliasesAction extends TransportMasterNodeOperationActio String[] concreteIndices = state.metaData().concreteIndices(request.indices(), request.ignoreIndices(), true); request.indices(concreteIndices); + @SuppressWarnings("unchecked") // ImmutableList to List results incompatible type Map> result = (Map) state.metaData().findAliases(request.aliases(), request.indices()); - if (result.isEmpty()) { - throw new AliasMissingException(request.aliases()); - } listener.onResponse(new GetAliasesResponse(result)); } diff --git a/src/main/java/org/elasticsearch/indices/AliasMissingException.java b/src/main/java/org/elasticsearch/indices/AliasMissingException.java deleted file mode 100644 index 3af5fbabce8..00000000000 --- a/src/main/java/org/elasticsearch/indices/AliasMissingException.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * 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.indices; - -import java.util.Locale; - -import org.elasticsearch.ElasticSearchException; -import org.elasticsearch.rest.RestStatus; - -/** - */ -public class AliasMissingException extends ElasticSearchException { - - private final String[] names; - - public AliasMissingException(String... names) { - super(String.format(Locale.ROOT, "alias [%s] missing", toNamesString(names))); - this.names = names; - } - - public String[] getName() { - return names; - } - - @Override - public RestStatus status() { - return RestStatus.NOT_FOUND; - } - - private static String toNamesString(String... names) { - if (names == null || names.length == 0) { - return ""; - } else if (names.length == 1) { - return names[0]; - } else { - StringBuilder builder = new StringBuilder(names[0]); - for (int i = 1; i < names.length; i++) { - builder.append(',').append(names[i]); - } - return builder.toString(); - } - } -} diff --git a/src/main/java/org/elasticsearch/rest/action/admin/indices/alias/get/RestGetAliasesAction.java b/src/main/java/org/elasticsearch/rest/action/admin/indices/alias/get/RestGetAliasesAction.java index f3e71d8ce57..97798387bb7 100644 --- a/src/main/java/org/elasticsearch/rest/action/admin/indices/alias/get/RestGetAliasesAction.java +++ b/src/main/java/org/elasticsearch/rest/action/admin/indices/alias/get/RestGetAliasesAction.java @@ -37,6 +37,7 @@ import org.elasticsearch.rest.action.support.RestXContentBuilder; import java.io.IOException; import java.util.List; +import java.util.Locale; import java.util.Map; import static org.elasticsearch.rest.RestRequest.Method.GET; @@ -57,7 +58,7 @@ public class RestGetAliasesAction extends BaseRestHandler { public void handleRequest(final RestRequest request, final RestChannel channel) { String[] aliases = request.paramAsStringArray("name", Strings.EMPTY_ARRAY); final String[] indices = RestActions.splitIndices(request.param("index")); - GetAliasesRequest getAliasesRequest = new GetAliasesRequest(aliases); + final GetAliasesRequest getAliasesRequest = new GetAliasesRequest(aliases); getAliasesRequest.indices(indices); if (request.hasParam("ignore_indices")) { @@ -70,6 +71,16 @@ public class RestGetAliasesAction extends BaseRestHandler { public void onResponse(GetAliasesResponse response) { try { XContentBuilder builder = RestXContentBuilder.restContentBuilder(request); + if (response.getAliases().isEmpty()) { + String message = String.format(Locale.ROOT, "alias [%s] missing", toNamesString(getAliasesRequest.aliases())); + builder.startObject() + .field("error", message) + .field("status", RestStatus.NOT_FOUND.getStatus()) + .endObject(); + channel.sendResponse(new XContentRestResponse(request, RestStatus.NOT_FOUND, builder)); + return; + } + builder.startObject(); for (Map.Entry> entry : response.getAliases().entrySet()) { builder.startObject(entry.getKey(), XContentBuilder.FieldCaseConversion.NONE); @@ -98,6 +109,20 @@ public class RestGetAliasesAction extends BaseRestHandler { }); } + private static String toNamesString(String... names) { + if (names == null || names.length == 0) { + return ""; + } else if (names.length == 1) { + return names[0]; + } else { + StringBuilder builder = new StringBuilder(names[0]); + for (int i = 1; i < names.length; i++) { + builder.append(',').append(names[i]); + } + return builder.toString(); + } + } + static class Fields { static final XContentBuilderString ALIASES = new XContentBuilderString("aliases"); diff --git a/src/test/java/org/elasticsearch/benchmark/aliases/AliasesBenchmark.java b/src/test/java/org/elasticsearch/benchmark/aliases/AliasesBenchmark.java index dadcb8fbf8e..558835905dd 100644 --- a/src/test/java/org/elasticsearch/benchmark/aliases/AliasesBenchmark.java +++ b/src/test/java/org/elasticsearch/benchmark/aliases/AliasesBenchmark.java @@ -24,7 +24,6 @@ import org.elasticsearch.action.admin.indices.alias.get.GetAliasesResponse; import org.elasticsearch.client.Client; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.indices.AliasMissingException; import org.elasticsearch.indices.IndexAlreadyExistsException; import org.elasticsearch.node.Node; import org.elasticsearch.node.NodeBuilder; @@ -120,13 +119,13 @@ public class AliasesBenchmark { } private static int countAliases(Client client) { - try { - GetAliasesResponse response = client.admin().indices().prepareGetAliases("a*") - .addIndices(INDEX_NAME) - .execute().actionGet(); - return response.getAliases().get(INDEX_NAME).size(); - } catch (AliasMissingException e) { + GetAliasesResponse response = client.admin().indices().prepareGetAliases("a*") + .addIndices(INDEX_NAME) + .execute().actionGet(); + if (response.getAliases().isEmpty()) { return 0; + } else { + return response.getAliases().get(INDEX_NAME).size(); } } diff --git a/src/test/java/org/elasticsearch/test/integration/aliases/IndexAliasesTests.java b/src/test/java/org/elasticsearch/test/integration/aliases/IndexAliasesTests.java index 77855cb6898..f39bc1f7052 100644 --- a/src/test/java/org/elasticsearch/test/integration/aliases/IndexAliasesTests.java +++ b/src/test/java/org/elasticsearch/test/integration/aliases/IndexAliasesTests.java @@ -37,14 +37,12 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.index.query.FilterBuilder; import org.elasticsearch.index.query.QueryBuilders; -import org.elasticsearch.indices.AliasMissingException; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.facet.FacetBuilders; import org.elasticsearch.search.facet.terms.TermsFacet; import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.test.integration.AbstractSharedClusterTest; -import org.junit.Assert; import org.junit.Test; import java.util.Set; @@ -777,12 +775,8 @@ public class IndexAliasesTests extends AbstractSharedClusterTest { .execute().actionGet(); assertThat(indicesAliasesResponse.isAcknowledged(), equalTo(true)); - try { - client().admin().indices().prepareGetAliases("foo").addIndices("foobar").execute().actionGet(); - Assert.fail("Exception should have been thrown"); - } catch (AliasMissingException e) { - assertThat(e.getMessage(), equalTo("alias [foo] missing")); - } + getResponse = client().admin().indices().prepareGetAliases("foo").addIndices("foobar").execute().actionGet(); + assertThat(getResponse.getAliases().isEmpty(), equalTo(true)); existsResponse = client().admin().indices().prepareAliasesExist("foo") .addIndices("foobar").execute().actionGet(); assertThat(existsResponse.exists(), equalTo(false));