From bdd43fa69f062c5084e52d7b6c72eea2ccbcd077 Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Thu, 3 May 2018 07:22:59 -0500 Subject: [PATCH] Change signature of Get Repositories Response (#30333) The Get Repositories response object held a list of RepositoryMetaData entries. This object does not have the from/toXContent methods that are needed to expose this to the high level REST client. The RepositoriesMetaData, however, does, and it also contains a list of RepositoryMetaData objects within it. So rather than duplicate this logic or move it (RepositoriesMetaData is a fragment object used by cluster state), the object holding state in the Response was changed to use the RepositoriesMetaData instead. This also cleans up the read/write methods in the response, as they can now use the same read/write in RepositoriesMetaData, which also were not present in the singular class. --- .../get/GetRepositoriesResponse.java | 47 ++++++------------- .../get/TransportGetRepositoriesAction.java | 6 +-- .../cluster/RestGetRepositoriesAction.java | 22 ++------- 3 files changed, 21 insertions(+), 54 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/repositories/get/GetRepositoriesResponse.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/repositories/get/GetRepositoriesResponse.java index 6d4cb839345..a71f9fd8fd7 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/repositories/get/GetRepositoriesResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/repositories/get/GetRepositoriesResponse.java @@ -20,13 +20,14 @@ package org.elasticsearch.action.admin.cluster.repositories.get; import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.cluster.metadata.RepositoriesMetaData; import org.elasticsearch.cluster.metadata.RepositoryMetaData; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.ToXContentObject; +import org.elasticsearch.common.xcontent.XContentBuilder; import java.io.IOException; -import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -34,15 +35,15 @@ import java.util.List; /** * Get repositories response */ -public class GetRepositoriesResponse extends ActionResponse implements Iterable { - - private List repositories = Collections.emptyList(); +public class GetRepositoriesResponse extends ActionResponse implements ToXContentObject { + private RepositoriesMetaData repositories; GetRepositoriesResponse() { + repositories = new RepositoriesMetaData(Collections.emptyList()); } - GetRepositoriesResponse(List repositories) { + GetRepositoriesResponse(RepositoriesMetaData repositories) { this.repositories = repositories; } @@ -52,43 +53,25 @@ public class GetRepositoriesResponse extends ActionResponse implements Iterable< * @return list or repositories */ public List repositories() { - return repositories; + return repositories.repositories(); } @Override public void readFrom(StreamInput in) throws IOException { - super.readFrom(in); - int size = in.readVInt(); - List repositoryListBuilder = new ArrayList<>(size); - for (int j = 0; j < size; j++) { - repositoryListBuilder.add(new RepositoryMetaData( - in.readString(), - in.readString(), - Settings.readSettingsFromStream(in)) - ); - } - repositories = Collections.unmodifiableList(repositoryListBuilder); + repositories = new RepositoriesMetaData(in); } @Override public void writeTo(StreamOutput out) throws IOException { - super.writeTo(out); - out.writeVInt(repositories.size()); - for (RepositoryMetaData repository : repositories) { - out.writeString(repository.name()); - out.writeString(repository.type()); - Settings.writeSettingsToStream(repository.settings(), out); - } + repositories.writeTo(out); } - /** - * Iterator over the repositories data - * - * @return iterator over the repositories data - */ @Override - public Iterator iterator() { - return repositories.iterator(); + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.startObject(); + repositories.toXContent(builder, params); + builder.endObject(); + return builder; } } diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/repositories/get/TransportGetRepositoriesAction.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/repositories/get/TransportGetRepositoriesAction.java index 6e7a0ca5cf4..c7474fc28cc 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/repositories/get/TransportGetRepositoriesAction.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/repositories/get/TransportGetRepositoriesAction.java @@ -75,9 +75,9 @@ public class TransportGetRepositoriesAction extends TransportMasterNodeReadActio RepositoriesMetaData repositories = metaData.custom(RepositoriesMetaData.TYPE); if (request.repositories().length == 0 || (request.repositories().length == 1 && "_all".equals(request.repositories()[0]))) { if (repositories != null) { - listener.onResponse(new GetRepositoriesResponse(repositories.repositories())); + listener.onResponse(new GetRepositoriesResponse(repositories)); } else { - listener.onResponse(new GetRepositoriesResponse(Collections.emptyList())); + listener.onResponse(new GetRepositoriesResponse(new RepositoriesMetaData(Collections.emptyList()))); } } else { if (repositories != null) { @@ -102,7 +102,7 @@ public class TransportGetRepositoriesAction extends TransportMasterNodeReadActio } repositoryListBuilder.add(repositoryMetaData); } - listener.onResponse(new GetRepositoriesResponse(Collections.unmodifiableList(repositoryListBuilder))); + listener.onResponse(new GetRepositoriesResponse(new RepositoriesMetaData(repositoryListBuilder))); } else { listener.onFailure(new RepositoryMissingException(request.repositories()[0])); } diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestGetRepositoriesAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestGetRepositoriesAction.java index ed6fe2f95f4..4e06497c7fe 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestGetRepositoriesAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestGetRepositoriesAction.java @@ -22,25 +22,19 @@ package org.elasticsearch.rest.action.admin.cluster; import org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesRequest; import org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesResponse; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.cluster.metadata.RepositoriesMetaData; -import org.elasticsearch.cluster.metadata.RepositoryMetaData; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsFilter; -import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.rest.BaseRestHandler; -import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.RestResponse; -import org.elasticsearch.rest.action.RestBuilderListener; +import org.elasticsearch.rest.action.RestToXContentListener; import java.io.IOException; import java.util.Set; import static org.elasticsearch.client.Requests.getRepositoryRequest; import static org.elasticsearch.rest.RestRequest.Method.GET; -import static org.elasticsearch.rest.RestStatus.OK; /** * Returns repository information @@ -69,18 +63,8 @@ public class RestGetRepositoriesAction extends BaseRestHandler { getRepositoriesRequest.local(request.paramAsBoolean("local", getRepositoriesRequest.local())); settingsFilter.addFilterSettingParams(request); return channel -> - client.admin().cluster().getRepositories(getRepositoriesRequest, new RestBuilderListener(channel) { - @Override - public RestResponse buildResponse(GetRepositoriesResponse response, XContentBuilder builder) throws Exception { - builder.startObject(); - for (RepositoryMetaData repositoryMetaData : response.repositories()) { - RepositoriesMetaData.toXContent(repositoryMetaData, builder, request); - } - builder.endObject(); - - return new BytesRestResponse(OK, builder); - } - }); + client.admin().cluster().getRepositories(getRepositoriesRequest, + new RestToXContentListener<>(channel)); } @Override