|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.rest.action.admin.indices; |
| 21 | + |
| 22 | +import org.elasticsearch.action.admin.indices.get.GetIndexRequest; |
| 23 | +import org.elasticsearch.action.admin.indices.get.GetIndexRequest.Feature; |
| 24 | +import org.elasticsearch.action.admin.indices.get.GetIndexResponse; |
| 25 | +import org.elasticsearch.action.support.IndicesOptions; |
| 26 | +import org.elasticsearch.client.node.NodeClient; |
| 27 | +import org.elasticsearch.cluster.metadata.AliasMetaData; |
| 28 | +import org.elasticsearch.common.Strings; |
| 29 | +import org.elasticsearch.common.settings.Settings; |
| 30 | +import org.elasticsearch.common.xcontent.ToXContent.Params; |
| 31 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 32 | +import org.elasticsearch.rest.BaseRestHandler; |
| 33 | +import org.elasticsearch.rest.BytesRestResponse; |
| 34 | +import org.elasticsearch.rest.RestController; |
| 35 | +import org.elasticsearch.rest.RestRequest; |
| 36 | +import org.elasticsearch.rest.RestResponse; |
| 37 | +import org.elasticsearch.rest.action.RestBuilderListener; |
| 38 | + |
| 39 | +import java.io.IOException; |
| 40 | +import java.util.List; |
| 41 | + |
| 42 | +import static org.elasticsearch.rest.RestRequest.Method.GET; |
| 43 | +import static org.elasticsearch.rest.RestStatus.OK; |
| 44 | + |
| 45 | +/** |
| 46 | + * The REST handler for retrieving all aliases |
| 47 | + */ |
| 48 | +public class RestGetAllAliasesAction extends BaseRestHandler { |
| 49 | + |
| 50 | + public RestGetAllAliasesAction(final Settings settings, final RestController controller) { |
| 51 | + super(settings); |
| 52 | + controller.registerHandler(GET, "/_alias", this); |
| 53 | + controller.registerHandler(GET, "/_aliases", this); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public String getName() { |
| 58 | + return "get_all_aliases_action"; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { |
| 63 | + final GetIndexRequest getIndexRequest = new GetIndexRequest(); |
| 64 | + getIndexRequest.indices(Strings.EMPTY_ARRAY); |
| 65 | + getIndexRequest.features(Feature.ALIASES); |
| 66 | + getIndexRequest.indicesOptions(IndicesOptions.fromRequest(request, getIndexRequest.indicesOptions())); |
| 67 | + getIndexRequest.local(request.paramAsBoolean("local", getIndexRequest.local())); |
| 68 | + getIndexRequest.humanReadable(request.paramAsBoolean("human", false)); |
| 69 | + return channel -> client.admin().indices().getIndex(getIndexRequest, new RestBuilderListener<GetIndexResponse>(channel) { |
| 70 | + |
| 71 | + @Override |
| 72 | + public RestResponse buildResponse(final GetIndexResponse response, final XContentBuilder builder) throws Exception { |
| 73 | + builder.startObject(); |
| 74 | + { |
| 75 | + for (final String index : response.indices()) { |
| 76 | + builder.startObject(index); |
| 77 | + { |
| 78 | + writeAliases(response.aliases().get(index), builder, request); |
| 79 | + } |
| 80 | + builder.endObject(); |
| 81 | + } |
| 82 | + } |
| 83 | + builder.endObject(); |
| 84 | + |
| 85 | + return new BytesRestResponse(OK, builder); |
| 86 | + } |
| 87 | + |
| 88 | + private void writeAliases(final List<AliasMetaData> aliases, final XContentBuilder builder, |
| 89 | + final Params params) throws IOException { |
| 90 | + builder.startObject("aliases"); |
| 91 | + { |
| 92 | + if (aliases != null) { |
| 93 | + for (final AliasMetaData alias : aliases) { |
| 94 | + AliasMetaData.Builder.toXContent(alias, builder, params); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + builder.endObject(); |
| 99 | + } |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments