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