From f580501bc98cbc78d67c2223f8020a271a631c8f Mon Sep 17 00:00:00 2001 From: Alan Woodward Date: Mon, 2 Oct 2023 13:57:29 +0100 Subject: [PATCH] Remove StatusToXContent interface (#99824) The StatusToXContent interface extends ToXContent with a REST status method. It is used by RestStatusToXContentListener, which uses the extra method to set the status of the RestResponse. However, given the generics parametrization of RestToXContentListener, we can already get the status of a response object without having these extra interfaces, which mix serialization and status concerns. This commit removes StatusToXContent and RestStatusToXContentListener, instead adding some extra optional parameters to RestToXContentListener to handle retrieving status and location information from the passed in Response object. --- .../mustache/RestSearchTemplateAction.java | 8 ++- .../mustache/SearchTemplateResponse.java | 5 +- .../elasticsearch/http/SystemIndexRestIT.java | 5 +- .../action/DocWriteResponse.java | 5 +- .../cluster/health/ClusterHealthResponse.java | 5 +- .../GetScriptContextResponse.java | 10 +--- .../GetScriptLanguageResponse.java | 10 +--- .../GetStoredScriptResponse.java | 5 +- .../list/ListDanglingIndicesResponse.java | 5 +- .../action/bulk/BulkItemResponse.java | 5 +- .../action/explain/ExplainResponse.java | 11 ++-- .../action/ingest/GetPipelineResponse.java | 7 +-- .../action/search/ClearScrollResponse.java | 5 +- .../search/RestClosePointInTimeAction.java | 8 ++- .../synonyms/SynonymUpdateResponse.java | 5 +- .../xcontent/StatusToXContentObject.java | 23 -------- .../action/RestStatusToXContentListener.java | 55 ------------------- .../rest/action/RestToXContentListener.java | 37 +++++++++++-- .../cluster/RestClusterHealthAction.java | 5 +- .../cluster/RestGetStoredScriptAction.java | 7 ++- .../cluster/RestResetFeatureStateAction.java | 25 ++++----- .../RestDeleteDanglingIndexAction.java | 9 +-- .../RestImportDanglingIndexAction.java | 9 +-- .../RestGetComponentTemplateAction.java | 12 ++-- .../RestGetComposableIndexTemplateAction.java | 12 ++-- .../indices/RestGetIndexTemplateAction.java | 13 ++--- .../admin/indices/RestRefreshAction.java | 12 ++-- .../action/document/RestDeleteAction.java | 5 +- .../rest/action/document/RestGetAction.java | 9 +-- .../rest/action/document/RestIndexAction.java | 5 +- .../action/document/RestUpdateAction.java | 5 +- .../action/ingest/RestGetPipelineAction.java | 5 +- .../action/search/RestClearScrollAction.java | 5 +- .../rest/action/search/RestExplainAction.java | 5 +- .../synonyms/RestPutSynonymRuleAction.java | 12 ++-- .../synonyms/RestPutSynonymsAction.java | 12 ++-- .../search/RestGetAsyncStatusAction.java | 4 +- .../license/PostStartBasicResponse.java | 5 +- .../license/RestPostStartBasicLicense.java | 8 ++- .../action/AbstractGetResourcesResponse.java | 12 +--- .../core/ml/action/GetCalendarsAction.java | 3 +- .../core/ml/action/GetFiltersAction.java | 4 +- .../xpack/core/ml/action/PostDataAction.java | 10 +--- .../ml/action/RevertModelSnapshotAction.java | 11 +--- .../ml/action/UpdateModelSnapshotAction.java | 9 +-- .../core/ml/action/UpdateProcessAction.java | 18 ++---- .../search/action/AsyncStatusResponse.java | 11 +--- .../action/FindStructureAction.java | 10 +--- .../action/PostAnalyticsEventAction.java | 9 +-- .../action/PutAnalyticsCollectionAction.java | 9 +-- .../action/RestPostAnalyticsEventAction.java | 9 ++- .../RestPutAnalyticsCollectionAction.java | 5 +- .../rules/action/ListQueryRulesetsAction.java | 9 +-- .../rules/action/PutQueryRulesetAction.java | 4 +- .../action/RestPutQueryRulesetAction.java | 12 ++-- .../action/ListSearchApplicationAction.java | 9 +-- .../action/PutSearchApplicationAction.java | 4 +- .../RestPutSearchApplicationAction.java | 12 ++-- .../plugin/RestEqlGetAsyncStatusAction.java | 4 +- .../logstash/rest/RestGetPipelineAction.java | 17 ++---- .../ml/integration/RevertModelSnapshotIT.java | 6 +- .../rest/calendar/RestGetCalendarsAction.java | 4 +- .../ml/rest/filter/RestGetFiltersAction.java | 4 +- .../inference/RestGetTrainedModelsAction.java | 2 +- .../xpack/ml/rest/job/RestPostDataAction.java | 5 +- .../RestRevertModelSnapshotAction.java | 4 +- .../RestUpdateModelSnapshotAction.java | 8 +-- .../xpack/profiling/GetStatusAction.java | 5 +- .../xpack/profiling/RestGetStatusAction.java | 8 ++- .../xpack/ql/async/QlStatusResponse.java | 11 +--- .../rest/RestDeleteRollupJobAction.java | 17 ++---- .../RestDeleteServiceAccountTokenAction.java | 8 +-- .../testkit/RepositoryAnalysisFailureIT.java | 4 +- .../testkit/RepositoryAnalysisSuccessIT.java | 5 +- .../testkit/RepositoryAnalyzeAction.java | 10 +--- .../testkit/RestRepositoryAnalyzeAction.java | 4 +- .../sql/action/SqlClearCursorResponse.java | 5 +- .../plugin/RestSqlAsyncGetStatusAction.java | 4 +- 78 files changed, 244 insertions(+), 459 deletions(-) delete mode 100644 server/src/main/java/org/elasticsearch/common/xcontent/StatusToXContentObject.java delete mode 100644 server/src/main/java/org/elasticsearch/rest/action/RestStatusToXContentListener.java diff --git a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestSearchTemplateAction.java b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestSearchTemplateAction.java index 131db77f4e7c..7d6c02096e43 100644 --- a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestSearchTemplateAction.java +++ b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestSearchTemplateAction.java @@ -15,7 +15,7 @@ import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.Scope; import org.elasticsearch.rest.ServerlessScope; -import org.elasticsearch.rest.action.RestStatusToXContentListener; +import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.rest.action.search.RestSearchAction; import org.elasticsearch.xcontent.XContentParser; @@ -76,7 +76,11 @@ public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client if (searchRequest.source().explain() != null) { searchTemplateRequest.setExplain(searchRequest.source().explain()); } - return channel -> client.execute(SearchTemplateAction.INSTANCE, searchTemplateRequest, new RestStatusToXContentListener<>(channel)); + return channel -> client.execute( + SearchTemplateAction.INSTANCE, + searchTemplateRequest, + new RestToXContentListener<>(channel, SearchTemplateResponse::status) + ); } @Override diff --git a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/SearchTemplateResponse.java b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/SearchTemplateResponse.java index cb17ed039460..b4b804bf22e9 100644 --- a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/SearchTemplateResponse.java +++ b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/SearchTemplateResponse.java @@ -14,9 +14,9 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.ChunkedToXContent; -import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; import org.elasticsearch.xcontent.XContentParser; @@ -26,7 +26,7 @@ import java.io.InputStream; import java.util.Map; -public class SearchTemplateResponse extends ActionResponse implements StatusToXContentObject { +public class SearchTemplateResponse extends ActionResponse implements ToXContentObject { public static ParseField TEMPLATE_OUTPUT_FIELD = new ParseField("template_output"); /** Contains the source of the rendered template **/ @@ -113,7 +113,6 @@ void innerToXContent(XContentBuilder builder, Params params) throws IOException } } - @Override public RestStatus status() { if (hasResponse()) { return response.status(); diff --git a/qa/smoke-test-http/src/javaRestTest/java/org/elasticsearch/http/SystemIndexRestIT.java b/qa/smoke-test-http/src/javaRestTest/java/org/elasticsearch/http/SystemIndexRestIT.java index 7d0095dd81f6..7eb0a38ad809 100644 --- a/qa/smoke-test-http/src/javaRestTest/java/org/elasticsearch/http/SystemIndexRestIT.java +++ b/qa/smoke-test-http/src/javaRestTest/java/org/elasticsearch/http/SystemIndexRestIT.java @@ -9,6 +9,7 @@ package org.elasticsearch.http; import org.elasticsearch.Version; +import org.elasticsearch.action.DocWriteResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.client.Request; @@ -32,7 +33,7 @@ import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestHandler; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.action.RestStatusToXContentListener; +import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; @@ -234,7 +235,7 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli indexRequest.source(Map.of("some_field", "some_value")); return channel -> client.index( indexRequest, - new RestStatusToXContentListener<>(channel, r -> r.getLocation(indexRequest.routing())) + new RestToXContentListener<>(channel, DocWriteResponse::status, r -> r.getLocation(indexRequest.routing())) ); } } diff --git a/server/src/main/java/org/elasticsearch/action/DocWriteResponse.java b/server/src/main/java/org/elasticsearch/action/DocWriteResponse.java index 9ea073a69d4d..b6e5a51c117b 100644 --- a/server/src/main/java/org/elasticsearch/action/DocWriteResponse.java +++ b/server/src/main/java/org/elasticsearch/action/DocWriteResponse.java @@ -16,7 +16,6 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.core.Nullable; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.index.Index; @@ -25,6 +24,7 @@ import org.elasticsearch.index.seqno.SequenceNumbers; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -41,7 +41,7 @@ /** * A base class for the response of a write operation that involves a single doc */ -public abstract class DocWriteResponse extends ReplicationResponse implements WriteResponse, StatusToXContentObject { +public abstract class DocWriteResponse extends ReplicationResponse implements WriteResponse, ToXContentObject { private static final String _SHARDS = "_shards"; private static final String _INDEX = "_index"; @@ -216,7 +216,6 @@ public void setForcedRefresh(boolean forcedRefresh) { } /** returns the rest status for this response (based on {@link ShardInfo#status()} */ - @Override public RestStatus status() { return getShardInfo().status(); } diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/health/ClusterHealthResponse.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/health/ClusterHealthResponse.java index abe51b35f89e..19e84e7443ee 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/health/ClusterHealthResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/health/ClusterHealthResponse.java @@ -18,12 +18,12 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.util.Maps; -import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.core.TimeValue; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -37,7 +37,7 @@ import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg; -public class ClusterHealthResponse extends ActionResponse implements StatusToXContentObject { +public class ClusterHealthResponse extends ActionResponse implements ToXContentObject { private static final String CLUSTER_NAME = "cluster_name"; private static final String STATUS = "status"; private static final String TIMED_OUT = "timed_out"; @@ -333,7 +333,6 @@ public String toString() { return Strings.toString(this); } - @Override public RestStatus status() { return isTimedOut() ? RestStatus.REQUEST_TIMEOUT : RestStatus.OK; } diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/GetScriptContextResponse.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/GetScriptContextResponse.java index 781947b9db81..e7568a0c66a3 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/GetScriptContextResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/GetScriptContextResponse.java @@ -12,11 +12,10 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.util.Maps; -import org.elasticsearch.common.xcontent.StatusToXContentObject; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.script.ScriptContextInfo; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -30,7 +29,7 @@ import java.util.function.Function; import java.util.stream.Collectors; -public class GetScriptContextResponse extends ActionResponse implements StatusToXContentObject { +public class GetScriptContextResponse extends ActionResponse implements ToXContentObject { private static final ParseField CONTEXTS = new ParseField("contexts"); final Map contexts; @@ -87,11 +86,6 @@ public void writeTo(StreamOutput out) throws IOException { } } - @Override - public RestStatus status() { - return RestStatus.OK; - } - @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject().startArray(CONTEXTS.getPreferredName()); diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/GetScriptLanguageResponse.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/GetScriptLanguageResponse.java index 4bd2b63c435b..36fe688b396d 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/GetScriptLanguageResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/GetScriptLanguageResponse.java @@ -12,16 +12,15 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.common.xcontent.StatusToXContentObject; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.script.ScriptLanguagesInfo; +import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; import java.util.Objects; -public class GetScriptLanguageResponse extends ActionResponse implements StatusToXContentObject, Writeable { +public class GetScriptLanguageResponse extends ActionResponse implements ToXContentObject, Writeable { public final ScriptLanguagesInfo info; GetScriptLanguageResponse(ScriptLanguagesInfo info) { @@ -38,11 +37,6 @@ public void writeTo(StreamOutput out) throws IOException { info.writeTo(out); } - @Override - public RestStatus status() { - return RestStatus.OK; - } - public static GetScriptLanguageResponse fromXContent(XContentParser parser) throws IOException { return new GetScriptLanguageResponse(ScriptLanguagesInfo.fromXContent(parser)); } diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/GetStoredScriptResponse.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/GetStoredScriptResponse.java index 9331ded7a2de..0202a0355abb 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/GetStoredScriptResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/GetStoredScriptResponse.java @@ -11,12 +11,12 @@ import org.elasticsearch.action.ActionResponse; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.script.StoredScriptSource; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -26,7 +26,7 @@ import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg; -public class GetStoredScriptResponse extends ActionResponse implements StatusToXContentObject { +public class GetStoredScriptResponse extends ActionResponse implements ToXContentObject { public static final ParseField _ID_PARSE_FIELD = new ParseField("_id"); public static final ParseField FOUND_PARSE_FIELD = new ParseField("found"); @@ -84,7 +84,6 @@ public StoredScriptSource getSource() { return source; } - @Override public RestStatus status() { return source != null ? RestStatus.OK : RestStatus.NOT_FOUND; } diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/dangling/list/ListDanglingIndicesResponse.java b/server/src/main/java/org/elasticsearch/action/admin/indices/dangling/list/ListDanglingIndicesResponse.java index b61b6318a20b..dadd493c780e 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/dangling/list/ListDanglingIndicesResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/dangling/list/ListDanglingIndicesResponse.java @@ -14,8 +14,8 @@ import org.elasticsearch.cluster.ClusterName; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContent; import org.elasticsearch.xcontent.XContentBuilder; @@ -34,7 +34,7 @@ * information for each dangling index is presented under the "dangling_indices" key. If any nodes * in the cluster failed to answer, the details are presented under the "_nodes.failures" key. */ -public class ListDanglingIndicesResponse extends BaseNodesResponse implements StatusToXContentObject { +public class ListDanglingIndicesResponse extends BaseNodesResponse implements ToXContentObject { public ListDanglingIndicesResponse(StreamInput in) throws IOException { super(in); @@ -48,7 +48,6 @@ public ListDanglingIndicesResponse( super(clusterName, nodes, failures); } - @Override public RestStatus status() { return this.hasFailures() ? RestStatus.INTERNAL_SERVER_ERROR : RestStatus.OK; } diff --git a/server/src/main/java/org/elasticsearch/action/bulk/BulkItemResponse.java b/server/src/main/java/org/elasticsearch/action/bulk/BulkItemResponse.java index e0b15558b860..76259d899c90 100644 --- a/server/src/main/java/org/elasticsearch/action/bulk/BulkItemResponse.java +++ b/server/src/main/java/org/elasticsearch/action/bulk/BulkItemResponse.java @@ -20,7 +20,6 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.core.CheckedConsumer; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.index.mapper.MapperService; @@ -30,6 +29,7 @@ import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentFragment; +import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -44,14 +44,13 @@ * Represents a single item response for an action executed as part of the bulk API. Holds the index/type/id * of the relevant action, and if it has failed or not (with the failure message in case it failed). */ -public class BulkItemResponse implements Writeable, StatusToXContentObject { +public class BulkItemResponse implements Writeable, ToXContentObject { private static final String _INDEX = "_index"; private static final String _ID = "_id"; private static final String STATUS = "status"; private static final String ERROR = "error"; - @Override public RestStatus status() { return failure == null ? response.status() : failure.getStatus(); } diff --git a/server/src/main/java/org/elasticsearch/action/explain/ExplainResponse.java b/server/src/main/java/org/elasticsearch/action/explain/ExplainResponse.java index 51e750986379..58c50df47c3c 100644 --- a/server/src/main/java/org/elasticsearch/action/explain/ExplainResponse.java +++ b/server/src/main/java/org/elasticsearch/action/explain/ExplainResponse.java @@ -13,13 +13,13 @@ import org.elasticsearch.action.ActionResponse; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.index.get.GetResult; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -33,7 +33,7 @@ /** * Response containing the score explanation. */ -public class ExplainResponse extends ActionResponse implements StatusToXContentObject { +public class ExplainResponse extends ActionResponse implements ToXContentObject { private static final ParseField _INDEX = new ParseField("_index"); private static final ParseField _ID = new ParseField("_id"); @@ -44,9 +44,9 @@ public class ExplainResponse extends ActionResponse implements StatusToXContentO private static final ParseField DETAILS = new ParseField("details"); private static final ParseField GET = new ParseField("get"); - private String index; - private String id; - private boolean exists; + private final String index; + private final String id; + private final boolean exists; private Explanation explanation; private GetResult getResult; @@ -110,7 +110,6 @@ public GetResult getGetResult() { return getResult; } - @Override public RestStatus status() { return exists ? RestStatus.OK : RestStatus.NOT_FOUND; } diff --git a/server/src/main/java/org/elasticsearch/action/ingest/GetPipelineResponse.java b/server/src/main/java/org/elasticsearch/action/ingest/GetPipelineResponse.java index 10aff29b8e70..bc9c88a706f3 100644 --- a/server/src/main/java/org/elasticsearch/action/ingest/GetPipelineResponse.java +++ b/server/src/main/java/org/elasticsearch/action/ingest/GetPipelineResponse.java @@ -13,9 +13,9 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.ingest.PipelineConfiguration; import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xcontent.XContentParser.Token; @@ -29,9 +29,9 @@ import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; -public class GetPipelineResponse extends ActionResponse implements StatusToXContentObject { +public class GetPipelineResponse extends ActionResponse implements ToXContentObject { - private List pipelines; + private final List pipelines; private final boolean summary; public GetPipelineResponse(StreamInput in) throws IOException { @@ -76,7 +76,6 @@ public boolean isSummary() { return summary; } - @Override public RestStatus status() { return isFound() ? RestStatus.OK : RestStatus.NOT_FOUND; } diff --git a/server/src/main/java/org/elasticsearch/action/search/ClearScrollResponse.java b/server/src/main/java/org/elasticsearch/action/search/ClearScrollResponse.java index 466973af37d8..0a7b53ea8b9c 100644 --- a/server/src/main/java/org/elasticsearch/action/search/ClearScrollResponse.java +++ b/server/src/main/java/org/elasticsearch/action/search/ClearScrollResponse.java @@ -11,11 +11,11 @@ import org.elasticsearch.action.ActionResponse; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -25,7 +25,7 @@ import static org.elasticsearch.rest.RestStatus.OK; import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; -public class ClearScrollResponse extends ActionResponse implements StatusToXContentObject { +public class ClearScrollResponse extends ActionResponse implements ToXContentObject { private static final ParseField SUCCEEDED = new ParseField("succeeded"); private static final ParseField NUMFREED = new ParseField("num_freed"); @@ -69,7 +69,6 @@ public int getNumFreed() { return numFreed; } - @Override public RestStatus status() { return numFreed == 0 ? NOT_FOUND : OK; } diff --git a/server/src/main/java/org/elasticsearch/action/search/RestClosePointInTimeAction.java b/server/src/main/java/org/elasticsearch/action/search/RestClosePointInTimeAction.java index 320d2b81ca41..a9da16bd6202 100644 --- a/server/src/main/java/org/elasticsearch/action/search/RestClosePointInTimeAction.java +++ b/server/src/main/java/org/elasticsearch/action/search/RestClosePointInTimeAction.java @@ -13,7 +13,7 @@ import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.Scope; import org.elasticsearch.rest.ServerlessScope; -import org.elasticsearch.rest.action.RestStatusToXContentListener; +import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.xcontent.XContentParser; import java.io.IOException; @@ -40,6 +40,10 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC try (XContentParser parser = request.contentOrSourceParamParser()) { clearRequest = ClosePointInTimeRequest.fromXContent(parser); } - return channel -> client.execute(ClosePointInTimeAction.INSTANCE, clearRequest, new RestStatusToXContentListener<>(channel)); + return channel -> client.execute( + ClosePointInTimeAction.INSTANCE, + clearRequest, + new RestToXContentListener<>(channel, ClosePointInTimeResponse::status) + ); } } diff --git a/server/src/main/java/org/elasticsearch/action/synonyms/SynonymUpdateResponse.java b/server/src/main/java/org/elasticsearch/action/synonyms/SynonymUpdateResponse.java index fd464d787db8..7bbddfca7907 100644 --- a/server/src/main/java/org/elasticsearch/action/synonyms/SynonymUpdateResponse.java +++ b/server/src/main/java/org/elasticsearch/action/synonyms/SynonymUpdateResponse.java @@ -12,17 +12,17 @@ import org.elasticsearch.action.admin.indices.analyze.ReloadAnalyzersResponse; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.synonyms.SynonymsManagementAPIService.SynonymsReloadResult; import org.elasticsearch.synonyms.SynonymsManagementAPIService.UpdateSynonymsResultStatus; +import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.Locale; import java.util.Objects; -public class SynonymUpdateResponse extends ActionResponse implements StatusToXContentObject { +public class SynonymUpdateResponse extends ActionResponse implements ToXContentObject { private final UpdateSynonymsResultStatus updateStatus; private final ReloadAnalyzersResponse reloadAnalyzersResponse; @@ -63,7 +63,6 @@ public void writeTo(StreamOutput out) throws IOException { reloadAnalyzersResponse.writeTo(out); } - @Override public RestStatus status() { return switch (updateStatus) { case CREATED -> RestStatus.CREATED; diff --git a/server/src/main/java/org/elasticsearch/common/xcontent/StatusToXContentObject.java b/server/src/main/java/org/elasticsearch/common/xcontent/StatusToXContentObject.java deleted file mode 100644 index e0af9777fddf..000000000000 --- a/server/src/main/java/org/elasticsearch/common/xcontent/StatusToXContentObject.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ -package org.elasticsearch.common.xcontent; - -import org.elasticsearch.rest.RestStatus; -import org.elasticsearch.xcontent.ToXContentObject; - -/** - * Objects that can both render themselves in as json/yaml/etc and can provide a {@link RestStatus} for their response. Usually should be - * implemented by top level responses sent back to users from REST endpoints. - */ -public interface StatusToXContentObject extends ToXContentObject { - - /** - * Returns the REST status to make sure it is returned correctly - */ - RestStatus status(); -} diff --git a/server/src/main/java/org/elasticsearch/rest/action/RestStatusToXContentListener.java b/server/src/main/java/org/elasticsearch/rest/action/RestStatusToXContentListener.java deleted file mode 100644 index 178146c49480..000000000000 --- a/server/src/main/java/org/elasticsearch/rest/action/RestStatusToXContentListener.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ -package org.elasticsearch.rest.action; - -import org.elasticsearch.common.xcontent.StatusToXContentObject; -import org.elasticsearch.rest.RestChannel; -import org.elasticsearch.rest.RestResponse; -import org.elasticsearch.rest.RestStatus; -import org.elasticsearch.xcontent.XContentBuilder; - -import java.util.function.Function; - -/** - * Content listener that extracts that {@link RestStatus} from the response. - */ -public class RestStatusToXContentListener extends RestToXContentListener { - private final Function extractLocation; - - /** - * Build an instance that doesn't support responses with the status {@code 201 CREATED}. - */ - public RestStatusToXContentListener(RestChannel channel) { - this(channel, r -> { - assert false : "Returned a 201 CREATED but not set up to support a Location header"; - return null; - }); - } - - /** - * Build an instance that does support responses with the status {@code 201 CREATED}. - */ - public RestStatusToXContentListener(RestChannel channel, Function extractLocation) { - super(channel); - this.extractLocation = extractLocation; - } - - @Override - public RestResponse buildResponse(Response response, XContentBuilder builder) throws Exception { - assert response.isFragment() == false; // would be nice if we could make default methods final - response.toXContent(builder, channel.request()); - RestResponse restResponse = new RestResponse(response.status(), builder); - if (RestStatus.CREATED == restResponse.status()) { - final String location = extractLocation.apply(response); - if (location != null) { - restResponse.addHeader("Location", location); - } - } - return restResponse; - } -} diff --git a/server/src/main/java/org/elasticsearch/rest/action/RestToXContentListener.java b/server/src/main/java/org/elasticsearch/rest/action/RestToXContentListener.java index 4685eb51c436..cc759985f7d1 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/RestToXContentListener.java +++ b/server/src/main/java/org/elasticsearch/rest/action/RestToXContentListener.java @@ -14,23 +14,50 @@ import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; +import java.util.function.Function; + /** * A REST based action listener that requires the response to implement {@link ToXContentObject} and automatically * builds an XContent based response. */ +// TODO make this final public class RestToXContentListener extends RestBuilderListener { + protected final Function statusFunction; + private final Function locationFunction; + public RestToXContentListener(RestChannel channel) { + this(channel, r -> RestStatus.OK); + } + + public RestToXContentListener(RestChannel channel, Function statusFunction) { + this(channel, statusFunction, r -> { + assert false : "Returned a 201 CREATED but not set up to support a Location header from " + r.getClass(); + return null; + }); + } + + public RestToXContentListener( + RestChannel channel, + Function statusFunction, + Function locationFunction + ) { super(channel); + this.statusFunction = statusFunction; + this.locationFunction = locationFunction; } public RestResponse buildResponse(Response response, XContentBuilder builder) throws Exception { assert response.isFragment() == false; // would be nice if we could make default methods final response.toXContent(builder, channel.request()); - return new RestResponse(getStatus(response), builder); - } - - protected RestStatus getStatus(Response response) { - return RestStatus.OK; + RestStatus restStatus = statusFunction.apply(response); + RestResponse r = new RestResponse(restStatus, builder); + if (RestStatus.CREATED == restStatus) { + final String location = locationFunction.apply(response); + if (location != null) { + r.addHeader("Location", location); + } + } + return r; } } diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestClusterHealthAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestClusterHealthAction.java index 88093ea5c7ea..6518ccc6e0c9 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestClusterHealthAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestClusterHealthAction.java @@ -10,6 +10,7 @@ import org.elasticsearch.action.ClusterStatsLevel; import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest; +import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; import org.elasticsearch.action.support.ActiveShardCount; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.client.internal.node.NodeClient; @@ -21,7 +22,7 @@ import org.elasticsearch.rest.Scope; import org.elasticsearch.rest.ServerlessScope; import org.elasticsearch.rest.action.RestCancellableNodeClient; -import org.elasticsearch.rest.action.RestStatusToXContentListener; +import org.elasticsearch.rest.action.RestToXContentListener; import java.io.IOException; import java.util.Collections; @@ -54,7 +55,7 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC final ClusterHealthRequest clusterHealthRequest = fromRequest(request); return channel -> new RestCancellableNodeClient(client, request.getHttpChannel()).admin() .cluster() - .health(clusterHealthRequest, new RestStatusToXContentListener<>(channel)); + .health(clusterHealthRequest, new RestToXContentListener<>(channel, ClusterHealthResponse::status)); } public static ClusterHealthRequest fromRequest(final RestRequest request) { diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestGetStoredScriptAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestGetStoredScriptAction.java index 2bf2afd7ecb2..b0d5bce981f2 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestGetStoredScriptAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestGetStoredScriptAction.java @@ -8,12 +8,13 @@ package org.elasticsearch.rest.action.admin.cluster; import org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptRequest; +import org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptResponse; import org.elasticsearch.client.internal.node.NodeClient; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.Scope; import org.elasticsearch.rest.ServerlessScope; -import org.elasticsearch.rest.action.RestStatusToXContentListener; +import org.elasticsearch.rest.action.RestToXContentListener; import java.io.IOException; import java.util.List; @@ -38,6 +39,8 @@ public RestChannelConsumer prepareRequest(final RestRequest request, NodeClient String id = request.param("id"); GetStoredScriptRequest getRequest = new GetStoredScriptRequest(id); getRequest.masterNodeTimeout(request.paramAsTime("master_timeout", getRequest.masterNodeTimeout())); - return channel -> client.admin().cluster().getStoredScript(getRequest, new RestStatusToXContentListener<>(channel)); + return channel -> client.admin() + .cluster() + .getStoredScript(getRequest, new RestToXContentListener<>(channel, GetStoredScriptResponse::status)); } } diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestResetFeatureStateAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestResetFeatureStateAction.java index a11cc65d1e92..dcf6a1d165e7 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestResetFeatureStateAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestResetFeatureStateAction.java @@ -45,20 +45,17 @@ public String getName() { protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { final ResetFeatureStateRequest req = new ResetFeatureStateRequest(); - return restChannel -> client.execute(ResetFeatureStateAction.INSTANCE, req, new RestToXContentListener<>(restChannel) { - @Override - protected RestStatus getStatus(ResetFeatureStateResponse response) { - long failures = response.getFeatureStateResetStatuses() - .stream() - .filter(status -> status.getStatus() == ResetFeatureStateResponse.ResetFeatureStateStatus.Status.FAILURE) - .count(); - if (failures == 0) { - return RestStatus.OK; - } else if (failures == response.getFeatureStateResetStatuses().size()) { - return RestStatus.INTERNAL_SERVER_ERROR; - } - return RestStatus.MULTI_STATUS; + return restChannel -> client.execute(ResetFeatureStateAction.INSTANCE, req, new RestToXContentListener<>(restChannel, r -> { + long failures = r.getFeatureStateResetStatuses() + .stream() + .filter(status -> status.getStatus() == ResetFeatureStateResponse.ResetFeatureStateStatus.Status.FAILURE) + .count(); + if (failures == 0) { + return RestStatus.OK; + } else if (failures == r.getFeatureStateResetStatuses().size()) { + return RestStatus.INTERNAL_SERVER_ERROR; } - }); + return RestStatus.MULTI_STATUS; + })); } } diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/dangling/RestDeleteDanglingIndexAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/dangling/RestDeleteDanglingIndexAction.java index 56d475ecfab7..4213f42549cd 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/dangling/RestDeleteDanglingIndexAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/dangling/RestDeleteDanglingIndexAction.java @@ -9,11 +9,9 @@ package org.elasticsearch.rest.action.admin.cluster.dangling; import org.elasticsearch.action.admin.indices.dangling.delete.DeleteDanglingIndexRequest; -import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.internal.node.NodeClient; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestToXContentListener; import java.io.IOException; @@ -44,11 +42,6 @@ public RestChannelConsumer prepareRequest(final RestRequest request, NodeClient deleteRequest.timeout(request.paramAsTime("timeout", deleteRequest.timeout())); deleteRequest.masterNodeTimeout(request.paramAsTime("master_timeout", deleteRequest.masterNodeTimeout())); - return channel -> client.admin().cluster().deleteDanglingIndex(deleteRequest, new RestToXContentListener<>(channel) { - @Override - protected RestStatus getStatus(AcknowledgedResponse acknowledgedResponse) { - return ACCEPTED; - } - }); + return channel -> client.admin().cluster().deleteDanglingIndex(deleteRequest, new RestToXContentListener<>(channel, r -> ACCEPTED)); } } diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/dangling/RestImportDanglingIndexAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/dangling/RestImportDanglingIndexAction.java index 4eebc06d6490..7f481c16118b 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/dangling/RestImportDanglingIndexAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/dangling/RestImportDanglingIndexAction.java @@ -9,11 +9,9 @@ package org.elasticsearch.rest.action.admin.cluster.dangling; import org.elasticsearch.action.admin.indices.dangling.import_index.ImportDanglingIndexRequest; -import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.internal.node.NodeClient; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestToXContentListener; import java.io.IOException; @@ -43,11 +41,6 @@ public RestChannelConsumer prepareRequest(final RestRequest request, NodeClient importRequest.timeout(request.paramAsTime("timeout", importRequest.timeout())); importRequest.masterNodeTimeout(request.paramAsTime("master_timeout", importRequest.masterNodeTimeout())); - return channel -> client.admin().cluster().importDanglingIndex(importRequest, new RestToXContentListener<>(channel) { - @Override - protected RestStatus getStatus(AcknowledgedResponse acknowledgedResponse) { - return ACCEPTED; - } - }); + return channel -> client.admin().cluster().importDanglingIndex(importRequest, new RestToXContentListener<>(channel, r -> ACCEPTED)); } } diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetComponentTemplateAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetComponentTemplateAction.java index 5827e1519fb4..84e7865d9f69 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetComponentTemplateAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetComponentTemplateAction.java @@ -13,7 +13,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.Scope; import org.elasticsearch.rest.ServerlessScope; import org.elasticsearch.rest.action.RestToXContentListener; @@ -54,13 +53,10 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC final boolean implicitAll = getRequest.name() == null; - return channel -> client.execute(GetComponentTemplateAction.INSTANCE, getRequest, new RestToXContentListener<>(channel) { - @Override - protected RestStatus getStatus(final GetComponentTemplateAction.Response response) { - final boolean templateExists = response.getComponentTemplates().isEmpty() == false; - return (templateExists || implicitAll) ? OK : NOT_FOUND; - } - }); + return channel -> client.execute(GetComponentTemplateAction.INSTANCE, getRequest, new RestToXContentListener<>(channel, r -> { + final boolean templateExists = r.getComponentTemplates().isEmpty() == false; + return (templateExists || implicitAll) ? OK : NOT_FOUND; + })); } @Override diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetComposableIndexTemplateAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetComposableIndexTemplateAction.java index 456616568788..0981d5820131 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetComposableIndexTemplateAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetComposableIndexTemplateAction.java @@ -13,7 +13,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.Scope; import org.elasticsearch.rest.ServerlessScope; import org.elasticsearch.rest.action.RestToXContentListener; @@ -53,13 +52,10 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC getRequest.includeDefaults(request.paramAsBoolean("include_defaults", false)); final boolean implicitAll = getRequest.name() == null; - return channel -> client.execute(GetComposableIndexTemplateAction.INSTANCE, getRequest, new RestToXContentListener<>(channel) { - @Override - protected RestStatus getStatus(final GetComposableIndexTemplateAction.Response response) { - final boolean templateExists = response.indexTemplates().isEmpty() == false; - return (templateExists || implicitAll) ? OK : NOT_FOUND; - } - }); + return channel -> client.execute(GetComposableIndexTemplateAction.INSTANCE, getRequest, new RestToXContentListener<>(channel, r -> { + final boolean templateExists = r.indexTemplates().isEmpty() == false; + return (templateExists || implicitAll) ? OK : NOT_FOUND; + })); } @Override diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetIndexTemplateAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetIndexTemplateAction.java index 363702c88e64..2efcfe3bef11 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetIndexTemplateAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetIndexTemplateAction.java @@ -9,7 +9,6 @@ package org.elasticsearch.rest.action.admin.indices; import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest; -import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse; import org.elasticsearch.client.internal.node.NodeClient; import org.elasticsearch.common.Strings; import org.elasticsearch.common.logging.DeprecationLogger; @@ -17,7 +16,6 @@ import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestToXContentListener; import java.io.IOException; @@ -65,13 +63,10 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC final boolean implicitAll = getIndexTemplatesRequest.names().length == 0; - return channel -> client.admin().indices().getTemplates(getIndexTemplatesRequest, new RestToXContentListener<>(channel) { - @Override - protected RestStatus getStatus(final GetIndexTemplatesResponse response) { - final boolean templateExists = response.getIndexTemplates().isEmpty() == false; - return (templateExists || implicitAll) ? OK : NOT_FOUND; - } - }); + return channel -> client.admin().indices().getTemplates(getIndexTemplatesRequest, new RestToXContentListener<>(channel, r -> { + final boolean templateExists = r.getIndexTemplates().isEmpty() == false; + return (templateExists || implicitAll) ? OK : NOT_FOUND; + })); } @Override diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestRefreshAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestRefreshAction.java index 59040f45344d..0882cd076dfc 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestRefreshAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestRefreshAction.java @@ -9,13 +9,12 @@ package org.elasticsearch.rest.action.admin.indices; import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; -import org.elasticsearch.action.admin.indices.refresh.RefreshResponse; import org.elasticsearch.action.support.IndicesOptions; +import org.elasticsearch.action.support.broadcast.BaseBroadcastResponse; import org.elasticsearch.client.internal.node.NodeClient; import org.elasticsearch.common.Strings; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.Scope; import org.elasticsearch.rest.ServerlessScope; import org.elasticsearch.rest.action.RestToXContentListener; @@ -48,11 +47,8 @@ public String getName() { public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { RefreshRequest refreshRequest = new RefreshRequest(Strings.splitStringByCommaToArray(request.param("index"))); refreshRequest.indicesOptions(IndicesOptions.fromRequest(request, refreshRequest.indicesOptions())); - return channel -> client.admin().indices().refresh(refreshRequest, new RestToXContentListener(channel) { - @Override - protected RestStatus getStatus(RefreshResponse response) { - return response.getStatus(); - } - }); + return channel -> client.admin() + .indices() + .refresh(refreshRequest, new RestToXContentListener<>(channel, BaseBroadcastResponse::getStatus)); } } diff --git a/server/src/main/java/org/elasticsearch/rest/action/document/RestDeleteAction.java b/server/src/main/java/org/elasticsearch/rest/action/document/RestDeleteAction.java index 5d5ad7559cbe..52ffd4251653 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/document/RestDeleteAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/document/RestDeleteAction.java @@ -9,6 +9,7 @@ package org.elasticsearch.rest.action.document; import org.elasticsearch.action.delete.DeleteRequest; +import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.support.ActiveShardCount; import org.elasticsearch.client.internal.node.NodeClient; import org.elasticsearch.core.RestApiVersion; @@ -18,7 +19,7 @@ import org.elasticsearch.rest.Scope; import org.elasticsearch.rest.ServerlessScope; import org.elasticsearch.rest.action.RestActions; -import org.elasticsearch.rest.action.RestStatusToXContentListener; +import org.elasticsearch.rest.action.RestToXContentListener; import java.io.IOException; import java.util.List; @@ -62,6 +63,6 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC deleteRequest.waitForActiveShards(ActiveShardCount.parseString(waitForActiveShards)); } - return channel -> client.delete(deleteRequest, new RestStatusToXContentListener<>(channel)); + return channel -> client.delete(deleteRequest, new RestToXContentListener<>(channel, DeleteResponse::status)); } } diff --git a/server/src/main/java/org/elasticsearch/rest/action/document/RestGetAction.java b/server/src/main/java/org/elasticsearch/rest/action/document/RestGetAction.java index 21d0258c1e98..13424cfc8278 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/document/RestGetAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/document/RestGetAction.java @@ -9,14 +9,12 @@ package org.elasticsearch.rest.action.document; import org.elasticsearch.action.get.GetRequest; -import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.client.internal.node.NodeClient; import org.elasticsearch.common.Strings; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.index.VersionType; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.Scope; import org.elasticsearch.rest.ServerlessScope; import org.elasticsearch.rest.action.RestActions; @@ -85,12 +83,7 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC getRequest.setForceSyntheticSource(true); } - return channel -> client.get(getRequest, new RestToXContentListener(channel) { - @Override - protected RestStatus getStatus(final GetResponse response) { - return response.isExists() ? OK : NOT_FOUND; - } - }); + return channel -> client.get(getRequest, new RestToXContentListener<>(channel, r -> r.isExists() ? OK : NOT_FOUND)); } } diff --git a/server/src/main/java/org/elasticsearch/rest/action/document/RestIndexAction.java b/server/src/main/java/org/elasticsearch/rest/action/document/RestIndexAction.java index 9a943fc1e6cf..e5c70fa4fe18 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/document/RestIndexAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/document/RestIndexAction.java @@ -10,6 +10,7 @@ import org.elasticsearch.Version; import org.elasticsearch.action.DocWriteRequest; +import org.elasticsearch.action.DocWriteResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.support.ActiveShardCount; import org.elasticsearch.client.internal.node.NodeClient; @@ -21,7 +22,7 @@ import org.elasticsearch.rest.Scope; import org.elasticsearch.rest.ServerlessScope; import org.elasticsearch.rest.action.RestActions; -import org.elasticsearch.rest.action.RestStatusToXContentListener; +import org.elasticsearch.rest.action.RestToXContentListener; import java.io.IOException; import java.util.List; @@ -146,7 +147,7 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC return channel -> client.index( indexRequest, - new RestStatusToXContentListener<>(channel, r -> r.getLocation(indexRequest.routing())) + new RestToXContentListener<>(channel, DocWriteResponse::status, r -> r.getLocation(indexRequest.routing())) ); } diff --git a/server/src/main/java/org/elasticsearch/rest/action/document/RestUpdateAction.java b/server/src/main/java/org/elasticsearch/rest/action/document/RestUpdateAction.java index 3428c9a54a9a..aff71cba2d85 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/document/RestUpdateAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/document/RestUpdateAction.java @@ -13,6 +13,7 @@ import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.support.ActiveShardCount; import org.elasticsearch.action.update.UpdateRequest; +import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.internal.node.NodeClient; import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.index.VersionType; @@ -21,7 +22,7 @@ import org.elasticsearch.rest.Scope; import org.elasticsearch.rest.ServerlessScope; import org.elasticsearch.rest.action.RestActions; -import org.elasticsearch.rest.action.RestStatusToXContentListener; +import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import java.io.IOException; @@ -98,7 +99,7 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC return channel -> client.update( updateRequest, - new RestStatusToXContentListener<>(channel, r -> r.getLocation(updateRequest.routing())) + new RestToXContentListener<>(channel, UpdateResponse::status, r -> r.getLocation(updateRequest.routing())) ); } diff --git a/server/src/main/java/org/elasticsearch/rest/action/ingest/RestGetPipelineAction.java b/server/src/main/java/org/elasticsearch/rest/action/ingest/RestGetPipelineAction.java index b095dde11943..e87a78c6b658 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/ingest/RestGetPipelineAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/ingest/RestGetPipelineAction.java @@ -9,13 +9,14 @@ package org.elasticsearch.rest.action.ingest; import org.elasticsearch.action.ingest.GetPipelineRequest; +import org.elasticsearch.action.ingest.GetPipelineResponse; import org.elasticsearch.client.internal.node.NodeClient; import org.elasticsearch.common.Strings; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.Scope; import org.elasticsearch.rest.ServerlessScope; -import org.elasticsearch.rest.action.RestStatusToXContentListener; +import org.elasticsearch.rest.action.RestToXContentListener; import java.io.IOException; import java.util.List; @@ -42,6 +43,6 @@ public RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient cl Strings.splitStringByCommaToArray(restRequest.param("id")) ); request.masterNodeTimeout(restRequest.paramAsTime("master_timeout", request.masterNodeTimeout())); - return channel -> client.admin().cluster().getPipeline(request, new RestStatusToXContentListener<>(channel)); + return channel -> client.admin().cluster().getPipeline(request, new RestToXContentListener<>(channel, GetPipelineResponse::status)); } } diff --git a/server/src/main/java/org/elasticsearch/rest/action/search/RestClearScrollAction.java b/server/src/main/java/org/elasticsearch/rest/action/search/RestClearScrollAction.java index 179e545ff62e..cadae92277d3 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/search/RestClearScrollAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/search/RestClearScrollAction.java @@ -9,13 +9,14 @@ package org.elasticsearch.rest.action.search; import org.elasticsearch.action.search.ClearScrollRequest; +import org.elasticsearch.action.search.ClearScrollResponse; import org.elasticsearch.client.internal.node.NodeClient; import org.elasticsearch.common.Strings; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.Scope; import org.elasticsearch.rest.ServerlessScope; -import org.elasticsearch.rest.action.RestStatusToXContentListener; +import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.xcontent.XContentParseException; import java.io.IOException; @@ -53,7 +54,7 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC } })); - return channel -> client.clearScroll(clearRequest, new RestStatusToXContentListener<>(channel)); + return channel -> client.clearScroll(clearRequest, new RestToXContentListener<>(channel, ClearScrollResponse::status)); } } diff --git a/server/src/main/java/org/elasticsearch/rest/action/search/RestExplainAction.java b/server/src/main/java/org/elasticsearch/rest/action/search/RestExplainAction.java index 73d09ddaad48..037ca41a8e19 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/search/RestExplainAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/search/RestExplainAction.java @@ -9,6 +9,7 @@ package org.elasticsearch.rest.action.search; import org.elasticsearch.action.explain.ExplainRequest; +import org.elasticsearch.action.explain.ExplainResponse; import org.elasticsearch.client.internal.node.NodeClient; import org.elasticsearch.common.Strings; import org.elasticsearch.core.RestApiVersion; @@ -18,7 +19,7 @@ import org.elasticsearch.rest.Scope; import org.elasticsearch.rest.ServerlessScope; import org.elasticsearch.rest.action.RestActions; -import org.elasticsearch.rest.action.RestStatusToXContentListener; +import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import java.io.IOException; @@ -84,6 +85,6 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC explainRequest.fetchSourceContext(FetchSourceContext.parseFromRestRequest(request)); - return channel -> client.explain(explainRequest, new RestStatusToXContentListener<>(channel)); + return channel -> client.explain(explainRequest, new RestToXContentListener<>(channel, ExplainResponse::status)); } } diff --git a/server/src/main/java/org/elasticsearch/rest/action/synonyms/RestPutSynonymRuleAction.java b/server/src/main/java/org/elasticsearch/rest/action/synonyms/RestPutSynonymRuleAction.java index b03165cea6a5..790e62729f11 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/synonyms/RestPutSynonymRuleAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/synonyms/RestPutSynonymRuleAction.java @@ -13,7 +13,6 @@ import org.elasticsearch.client.internal.node.NodeClient; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.Scope; import org.elasticsearch.rest.ServerlessScope; import org.elasticsearch.rest.action.RestToXContentListener; @@ -44,11 +43,10 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient restRequest.content(), restRequest.getXContentType() ); - return channel -> client.execute(PutSynonymRuleAction.INSTANCE, request, new RestToXContentListener<>(channel) { - @Override - protected RestStatus getStatus(SynonymUpdateResponse response) { - return response.status(); - } - }); + return channel -> client.execute( + PutSynonymRuleAction.INSTANCE, + request, + new RestToXContentListener<>(channel, SynonymUpdateResponse::status, r -> null) + ); } } diff --git a/server/src/main/java/org/elasticsearch/rest/action/synonyms/RestPutSynonymsAction.java b/server/src/main/java/org/elasticsearch/rest/action/synonyms/RestPutSynonymsAction.java index 2b59a2603bf8..e16db62a9ff9 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/synonyms/RestPutSynonymsAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/synonyms/RestPutSynonymsAction.java @@ -13,7 +13,6 @@ import org.elasticsearch.client.internal.node.NodeClient; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.Scope; import org.elasticsearch.rest.ServerlessScope; import org.elasticsearch.rest.action.RestToXContentListener; @@ -43,11 +42,10 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient restRequest.content(), restRequest.getXContentType() ); - return channel -> client.execute(PutSynonymsAction.INSTANCE, request, new RestToXContentListener<>(channel) { - @Override - protected RestStatus getStatus(SynonymUpdateResponse response) { - return response.status(); - } - }); + return channel -> client.execute( + PutSynonymsAction.INSTANCE, + request, + new RestToXContentListener<>(channel, SynonymUpdateResponse::status, r -> null) + ); } } diff --git a/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/RestGetAsyncStatusAction.java b/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/RestGetAsyncStatusAction.java index eef791c1bf9d..31a1af22ef8d 100644 --- a/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/RestGetAsyncStatusAction.java +++ b/x-pack/plugin/async-search/src/main/java/org/elasticsearch/xpack/search/RestGetAsyncStatusAction.java @@ -11,7 +11,7 @@ import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.Scope; import org.elasticsearch.rest.ServerlessScope; -import org.elasticsearch.rest.action.RestStatusToXContentListener; +import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.xpack.core.async.GetAsyncStatusRequest; import org.elasticsearch.xpack.core.search.action.GetAsyncStatusAction; @@ -34,6 +34,6 @@ public String getName() { @Override protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) { GetAsyncStatusRequest statusRequest = new GetAsyncStatusRequest(request.param("id")); - return channel -> client.execute(GetAsyncStatusAction.INSTANCE, statusRequest, new RestStatusToXContentListener<>(channel)); + return channel -> client.execute(GetAsyncStatusAction.INSTANCE, statusRequest, new RestToXContentListener<>(channel)); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/PostStartBasicResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/PostStartBasicResponse.java index cc22d7394bab..9ef14ac72528 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/PostStartBasicResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/PostStartBasicResponse.java @@ -10,10 +10,10 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.util.Maps; -import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.protocol.xpack.common.ProtocolUtils; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; @@ -21,7 +21,7 @@ import java.util.Map; import java.util.Objects; -public class PostStartBasicResponse extends AcknowledgedResponse implements StatusToXContentObject { +public class PostStartBasicResponse extends AcknowledgedResponse implements ToXContentObject { private static final ParseField BASIC_WAS_STARTED_FIELD = new ParseField("basic_was_started"); private static final ParseField ERROR_MESSAGE_FIELD = new ParseField("error_message"); @@ -116,7 +116,6 @@ protected void addCustomFields(XContentBuilder builder, Params params) throws IO } } - @Override public RestStatus status() { return status.restStatus; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestPostStartBasicLicense.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestPostStartBasicLicense.java index 815ea0ffa391..38a7ea7f7da2 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestPostStartBasicLicense.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestPostStartBasicLicense.java @@ -11,7 +11,7 @@ import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.action.RestStatusToXContentListener; +import org.elasticsearch.rest.action.RestToXContentListener; import java.io.IOException; import java.util.List; @@ -35,7 +35,11 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli startBasicRequest.acknowledge(request.paramAsBoolean("acknowledge", false)); startBasicRequest.timeout(request.paramAsTime("timeout", startBasicRequest.timeout())); startBasicRequest.masterNodeTimeout(request.paramAsTime("master_timeout", startBasicRequest.masterNodeTimeout())); - return channel -> client.execute(PostStartBasicAction.INSTANCE, startBasicRequest, new RestStatusToXContentListener<>(channel)); + return channel -> client.execute( + PostStartBasicAction.INSTANCE, + startBasicRequest, + new RestToXContentListener<>(channel, PostStartBasicResponse::status) + ); } @Override diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/AbstractGetResourcesResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/AbstractGetResourcesResponse.java index 70000cb64d2a..3324b37df01e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/AbstractGetResourcesResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/AbstractGetResourcesResponse.java @@ -11,18 +11,15 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.common.xcontent.StatusToXContentObject; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.action.util.QueryPage; import java.io.IOException; import java.util.Objects; -public abstract class AbstractGetResourcesResponse extends ActionResponse - implements - StatusToXContentObject { +public abstract class AbstractGetResourcesResponse extends ActionResponse implements ToXContentObject { private QueryPage resources; @@ -47,11 +44,6 @@ public void writeTo(StreamOutput out) throws IOException { resources.writeTo(out); } - @Override - public RestStatus status() { - return RestStatus.OK; - } - @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetCalendarsAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetCalendarsAction.java index 217ab505f7eb..09e59096283c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetCalendarsAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetCalendarsAction.java @@ -11,7 +11,6 @@ import org.elasticsearch.action.ActionType; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; @@ -138,7 +137,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } } - public static class Response extends AbstractGetResourcesResponse implements StatusToXContentObject { + public static class Response extends AbstractGetResourcesResponse implements ToXContentObject { public Response(QueryPage calendars) { super(calendars); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetFiltersAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetFiltersAction.java index e213703b5689..f37264cc6bc9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetFiltersAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetFiltersAction.java @@ -8,7 +8,7 @@ import org.elasticsearch.action.ActionType; import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.xcontent.StatusToXContentObject; +import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xpack.core.action.AbstractGetResourcesRequest; import org.elasticsearch.xpack.core.action.AbstractGetResourcesResponse; import org.elasticsearch.xpack.core.action.util.QueryPage; @@ -55,7 +55,7 @@ public String getResourceIdField() { } } - public static class Response extends AbstractGetResourcesResponse implements StatusToXContentObject { + public static class Response extends AbstractGetResourcesResponse implements ToXContentObject { public Response(QueryPage filters) { super(filters); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PostDataAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PostDataAction.java index f245b6a9754b..c7b2bcee8a69 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PostDataAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PostDataAction.java @@ -12,10 +12,9 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.core.ml.job.config.DataDescription; @@ -33,7 +32,7 @@ private PostDataAction() { super(NAME, PostDataAction.Response::new); } - public static class Response extends BaseTasksResponse implements StatusToXContentObject, Writeable { + public static class Response extends BaseTasksResponse implements ToXContentObject, Writeable { private final DataCounts dataCounts; @@ -62,11 +61,6 @@ public DataCounts getDataCounts() { return dataCounts; } - @Override - public RestStatus status() { - return RestStatus.ACCEPTED; - } - @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/RevertModelSnapshotAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/RevertModelSnapshotAction.java index f0801a9ab1a8..b8bff389061b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/RevertModelSnapshotAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/RevertModelSnapshotAction.java @@ -13,8 +13,6 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.xcontent.StatusToXContentObject; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; @@ -151,10 +149,10 @@ public boolean equals(Object obj) { } } - public static class Response extends ActionResponse implements StatusToXContentObject { + public static class Response extends ActionResponse implements ToXContentObject { private static final ParseField MODEL = new ParseField("model"); - private ModelSnapshot model; + private final ModelSnapshot model; public Response(StreamInput in) throws IOException { super(in); @@ -174,11 +172,6 @@ public void writeTo(StreamOutput out) throws IOException { model.writeTo(out); } - @Override - public RestStatus status() { - return RestStatus.OK; - } - @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateModelSnapshotAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateModelSnapshotAction.java index b253e4144ba2..c83b7d7578bf 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateModelSnapshotAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateModelSnapshotAction.java @@ -13,8 +13,6 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.xcontent.StatusToXContentObject; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xcontent.ObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; @@ -153,7 +151,7 @@ public boolean equals(Object obj) { } } - public static class Response extends ActionResponse implements StatusToXContentObject { + public static class Response extends ActionResponse implements ToXContentObject { private static final ParseField ACKNOWLEDGED = new ParseField("acknowledged"); private static final ParseField MODEL = new ParseField("model"); @@ -178,11 +176,6 @@ public void writeTo(StreamOutput out) throws IOException { model.writeTo(out); } - @Override - public RestStatus status() { - return RestStatus.OK; - } - @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateProcessAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateProcessAction.java index 1c0f79a68639..9641f9afbb00 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateProcessAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/UpdateProcessAction.java @@ -11,8 +11,7 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.common.xcontent.StatusToXContentObject; -import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.job.config.JobUpdate; import org.elasticsearch.xpack.core.ml.job.config.MlFilter; @@ -32,7 +31,7 @@ private UpdateProcessAction() { super(NAME, UpdateProcessAction.Response::new); } - public static class Response extends BaseTasksResponse implements StatusToXContentObject, Writeable { + public static class Response extends BaseTasksResponse implements ToXContentObject, Writeable { private final boolean isUpdated; @@ -56,11 +55,6 @@ public boolean isUpdated() { return isUpdated; } - @Override - public RestStatus status() { - return RestStatus.ACCEPTED; - } - @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); @@ -90,11 +84,11 @@ public boolean equals(Object obj) { public static class Request extends JobTaskRequest { - private ModelPlotConfig modelPlotConfig; - private PerPartitionCategorizationConfig perPartitionCategorizationConfig; + private final ModelPlotConfig modelPlotConfig; + private final PerPartitionCategorizationConfig perPartitionCategorizationConfig; private List detectorUpdates; - private MlFilter filter; - private boolean updateScheduledEvents = false; + private final MlFilter filter; + private final boolean updateScheduledEvents; public Request(StreamInput in) throws IOException { super(in); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/search/action/AsyncStatusResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/search/action/AsyncStatusResponse.java index 7399a8e9c9e3..9a5f59b84b34 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/search/action/AsyncStatusResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/search/action/AsyncStatusResponse.java @@ -12,21 +12,19 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.core.Nullable; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestActions; +import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.Objects; -import static org.elasticsearch.rest.RestStatus.OK; - /** * A response of an async search status request. */ -public class AsyncStatusResponse extends ActionResponse implements SearchStatusResponse, StatusToXContentObject { +public class AsyncStatusResponse extends ActionResponse implements SearchStatusResponse, ToXContentObject { private final String id; private final boolean isRunning; private final boolean isPartial; @@ -171,11 +169,6 @@ public void writeTo(StreamOutput out) throws IOException { } } - @Override - public RestStatus status() { - return OK; - } - @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/textstructure/action/FindStructureAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/textstructure/action/FindStructureAction.java index 8387511fa50f..e760471a6f1c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/textstructure/action/FindStructureAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/textstructure/action/FindStructureAction.java @@ -15,11 +15,10 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.core.TimeValue; import org.elasticsearch.grok.GrokBuiltinPatterns; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xcontent.ParseField; +import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.textstructure.structurefinder.TextStructure; @@ -42,7 +41,7 @@ private FindStructureAction() { super(NAME, Response::new); } - public static class Response extends ActionResponse implements StatusToXContentObject, Writeable { + public static class Response extends ActionResponse implements ToXContentObject, Writeable { private final TextStructure textStructure; @@ -60,11 +59,6 @@ public void writeTo(StreamOutput out) throws IOException { textStructure.writeTo(out); } - @Override - public RestStatus status() { - return RestStatus.OK; - } - @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { textStructure.toXContent(builder, params); diff --git a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/analytics/action/PostAnalyticsEventAction.java b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/analytics/action/PostAnalyticsEventAction.java index b5e810e30d12..67599e565b81 100644 --- a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/analytics/action/PostAnalyticsEventAction.java +++ b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/analytics/action/PostAnalyticsEventAction.java @@ -16,10 +16,8 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.network.NetworkAddress; -import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.Nullable; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; @@ -315,7 +313,7 @@ public RequestBuilder clientAddress(String clientAddress) { } } - public static class Response extends ActionResponse implements StatusToXContentObject { + public static class Response extends ActionResponse implements ToXContentObject { public static Response ACCEPTED = new Response(true); public static Response readFromStreamInput(StreamInput in) throws IOException { @@ -379,11 +377,6 @@ protected void addFieldsToXContent(XContentBuilder builder, Params params) throw } - @Override - public RestStatus status() { - return RestStatus.ACCEPTED; - } - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "post_analytics_event_response", false, diff --git a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/analytics/action/PutAnalyticsCollectionAction.java b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/analytics/action/PutAnalyticsCollectionAction.java index 06296eddcd8f..6b051a410487 100644 --- a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/analytics/action/PutAnalyticsCollectionAction.java +++ b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/analytics/action/PutAnalyticsCollectionAction.java @@ -13,8 +13,6 @@ import org.elasticsearch.action.support.master.MasterNodeRequest; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.xcontent.StatusToXContentObject; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; @@ -106,7 +104,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } } - public static class Response extends AcknowledgedResponse implements StatusToXContentObject { + public static class Response extends AcknowledgedResponse implements ToXContentObject { public static final ParseField COLLECTION_NAME_FIELD = new ParseField("name"); @@ -122,11 +120,6 @@ public Response(boolean acknowledged, String name) { this.name = name; } - @Override - public RestStatus status() { - return RestStatus.CREATED; - } - public String getName() { return name; } diff --git a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/analytics/action/RestPostAnalyticsEventAction.java b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/analytics/action/RestPostAnalyticsEventAction.java index 4f2b25bee4d0..0e3bb7150c45 100644 --- a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/analytics/action/RestPostAnalyticsEventAction.java +++ b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/analytics/action/RestPostAnalyticsEventAction.java @@ -13,9 +13,10 @@ import org.elasticsearch.core.Tuple; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.Scope; import org.elasticsearch.rest.ServerlessScope; -import org.elasticsearch.rest.action.RestStatusToXContentListener; +import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.xcontent.XContentType; import org.elasticsearch.xpack.application.EnterpriseSearch; import org.elasticsearch.xpack.application.EnterpriseSearchBaseRestHandler; @@ -48,7 +49,11 @@ public List routes() { @Override protected RestChannelConsumer innerPrepareRequest(RestRequest restRequest, NodeClient client) { PostAnalyticsEventAction.Request request = buidRequest(restRequest); - return channel -> client.execute(PostAnalyticsEventAction.INSTANCE, request, new RestStatusToXContentListener<>(channel)); + return channel -> client.execute( + PostAnalyticsEventAction.INSTANCE, + request, + new RestToXContentListener<>(channel, r -> RestStatus.ACCEPTED) + ); } private InetAddress getClientAddress(RestRequest restRequest, Map> headers) { diff --git a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/analytics/action/RestPutAnalyticsCollectionAction.java b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/analytics/action/RestPutAnalyticsCollectionAction.java index fee4cef9a494..7fbdcc116e61 100644 --- a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/analytics/action/RestPutAnalyticsCollectionAction.java +++ b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/analytics/action/RestPutAnalyticsCollectionAction.java @@ -11,9 +11,10 @@ import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.RestHandler; import org.elasticsearch.rest.RestRequest; +import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.Scope; import org.elasticsearch.rest.ServerlessScope; -import org.elasticsearch.rest.action.RestStatusToXContentListener; +import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.xpack.application.EnterpriseSearch; import org.elasticsearch.xpack.application.EnterpriseSearchBaseRestHandler; import org.elasticsearch.xpack.application.utils.LicenseUtils; @@ -45,7 +46,7 @@ protected RestChannelConsumer innerPrepareRequest(RestRequest restRequest, NodeC return channel -> client.execute( PutAnalyticsCollectionAction.INSTANCE, request, - new RestStatusToXContentListener<>(channel, _r -> location) + new RestToXContentListener<>(channel, r -> RestStatus.CREATED, _r -> location) ); } } diff --git a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/action/ListQueryRulesetsAction.java b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/action/ListQueryRulesetsAction.java index 2017fc2be4e3..f352fe4d7347 100644 --- a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/action/ListQueryRulesetsAction.java +++ b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/action/ListQueryRulesetsAction.java @@ -13,8 +13,6 @@ import org.elasticsearch.action.ActionType; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.xcontent.StatusToXContentObject; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; @@ -104,7 +102,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } } - public static class Response extends ActionResponse implements StatusToXContentObject { + public static class Response extends ActionResponse implements ToXContentObject { public static final ParseField RESULT_FIELD = new ParseField("results"); @@ -129,11 +127,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return queryPage.toXContent(builder, params); } - @Override - public RestStatus status() { - return RestStatus.OK; - } - public QueryPage queryPage() { return queryPage; } diff --git a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/action/PutQueryRulesetAction.java b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/action/PutQueryRulesetAction.java index af87e6efbdb0..d29df284f866 100644 --- a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/action/PutQueryRulesetAction.java +++ b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/action/PutQueryRulesetAction.java @@ -16,7 +16,6 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ParseField; @@ -125,7 +124,7 @@ public String toString() { } - public static class Response extends ActionResponse implements StatusToXContentObject { + public static class Response extends ActionResponse implements ToXContentObject { final DocWriteResponse.Result result; @@ -151,7 +150,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return builder; } - @Override public RestStatus status() { return switch (result) { case CREATED -> RestStatus.CREATED; diff --git a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/action/RestPutQueryRulesetAction.java b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/action/RestPutQueryRulesetAction.java index 2866dd5bb635..a43ac70327e7 100644 --- a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/action/RestPutQueryRulesetAction.java +++ b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/action/RestPutQueryRulesetAction.java @@ -10,7 +10,6 @@ import org.elasticsearch.client.internal.node.NodeClient; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.Scope; import org.elasticsearch.rest.ServerlessScope; import org.elasticsearch.rest.action.RestToXContentListener; @@ -46,11 +45,10 @@ protected RestChannelConsumer innerPrepareRequest(RestRequest restRequest, NodeC restRequest.content(), restRequest.getXContentType() ); - return channel -> client.execute(PutQueryRulesetAction.INSTANCE, request, new RestToXContentListener<>(channel) { - @Override - protected RestStatus getStatus(PutQueryRulesetAction.Response response) { - return response.status(); - } - }); + return channel -> client.execute( + PutQueryRulesetAction.INSTANCE, + request, + new RestToXContentListener<>(channel, PutQueryRulesetAction.Response::status, r -> null) + ); } } diff --git a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/search/action/ListSearchApplicationAction.java b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/search/action/ListSearchApplicationAction.java index 6b7588730e05..f44fa2a7b67e 100644 --- a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/search/action/ListSearchApplicationAction.java +++ b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/search/action/ListSearchApplicationAction.java @@ -15,9 +15,7 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.core.Nullable; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; @@ -124,7 +122,7 @@ public static Request parse(XContentParser parser) { } } - public static class Response extends ActionResponse implements StatusToXContentObject { + public static class Response extends ActionResponse implements ToXContentObject { public static final ParseField RESULT_FIELD = new ParseField("results"); @@ -149,11 +147,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return queryPage.toXContent(builder, params); } - @Override - public RestStatus status() { - return RestStatus.OK; - } - public QueryPage queryPage() { return queryPage; } diff --git a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/search/action/PutSearchApplicationAction.java b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/search/action/PutSearchApplicationAction.java index 439b7c663313..78ada2753489 100644 --- a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/search/action/PutSearchApplicationAction.java +++ b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/search/action/PutSearchApplicationAction.java @@ -15,7 +15,6 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xcontent.ConstructingObjectParser; import org.elasticsearch.xcontent.ParseField; @@ -133,7 +132,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } } - public static class Response extends ActionResponse implements StatusToXContentObject { + public static class Response extends ActionResponse implements ToXContentObject { final DocWriteResponse.Result result; @@ -159,7 +158,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return builder; } - @Override public RestStatus status() { return switch (result) { case CREATED -> RestStatus.CREATED; diff --git a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/search/action/RestPutSearchApplicationAction.java b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/search/action/RestPutSearchApplicationAction.java index f88f24272ef1..cd84c85bc14e 100644 --- a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/search/action/RestPutSearchApplicationAction.java +++ b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/search/action/RestPutSearchApplicationAction.java @@ -10,7 +10,6 @@ import org.elasticsearch.client.internal.node.NodeClient; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.Scope; import org.elasticsearch.rest.ServerlessScope; import org.elasticsearch.rest.action.RestToXContentListener; @@ -47,11 +46,10 @@ protected RestChannelConsumer innerPrepareRequest(RestRequest restRequest, NodeC restRequest.content(), restRequest.getXContentType() ); - return channel -> client.execute(PutSearchApplicationAction.INSTANCE, request, new RestToXContentListener<>(channel) { - @Override - protected RestStatus getStatus(PutSearchApplicationAction.Response response) { - return response.status(); - } - }); + return channel -> client.execute( + PutSearchApplicationAction.INSTANCE, + request, + new RestToXContentListener<>(channel, PutSearchApplicationAction.Response::status, r -> null) + ); } } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plugin/RestEqlGetAsyncStatusAction.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plugin/RestEqlGetAsyncStatusAction.java index 2059bf16e77e..ee0129f025a0 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plugin/RestEqlGetAsyncStatusAction.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plugin/RestEqlGetAsyncStatusAction.java @@ -11,7 +11,7 @@ import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.Scope; import org.elasticsearch.rest.ServerlessScope; -import org.elasticsearch.rest.action.RestStatusToXContentListener; +import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.xpack.core.async.GetAsyncStatusRequest; import java.util.List; @@ -33,6 +33,6 @@ public String getName() { @Override protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) { GetAsyncStatusRequest statusRequest = new GetAsyncStatusRequest(request.param("id")); - return channel -> client.execute(EqlAsyncGetStatusAction.INSTANCE, statusRequest, new RestStatusToXContentListener<>(channel)); + return channel -> client.execute(EqlAsyncGetStatusAction.INSTANCE, statusRequest, new RestToXContentListener<>(channel)); } } diff --git a/x-pack/plugin/logstash/src/main/java/org/elasticsearch/xpack/logstash/rest/RestGetPipelineAction.java b/x-pack/plugin/logstash/src/main/java/org/elasticsearch/xpack/logstash/rest/RestGetPipelineAction.java index d45d9173349f..d1013999732c 100644 --- a/x-pack/plugin/logstash/src/main/java/org/elasticsearch/xpack/logstash/rest/RestGetPipelineAction.java +++ b/x-pack/plugin/logstash/src/main/java/org/elasticsearch/xpack/logstash/rest/RestGetPipelineAction.java @@ -17,9 +17,7 @@ import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.xpack.logstash.action.GetPipelineAction; import org.elasticsearch.xpack.logstash.action.GetPipelineRequest; -import org.elasticsearch.xpack.logstash.action.GetPipelineResponse; -import java.io.IOException; import java.util.List; import static org.elasticsearch.rest.RestRequest.Method.GET; @@ -38,20 +36,17 @@ public List routes() { } @Override - protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { + protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) { final List ids = List.of(request.paramAsStringArray("id", Strings.EMPTY_ARRAY)); return restChannel -> client.execute( GetPipelineAction.INSTANCE, new GetPipelineRequest(ids), - new RestToXContentListener<>(restChannel) { - @Override - protected RestStatus getStatus(GetPipelineResponse response) { - if (response.pipelines().isEmpty() && ids.isEmpty() == false) { - return RestStatus.NOT_FOUND; - } - return RestStatus.OK; + new RestToXContentListener<>(restChannel, r -> { + if (r.pipelines().isEmpty() && ids.isEmpty() == false) { + return RestStatus.NOT_FOUND; } - } + return RestStatus.OK; + }) ); } } diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/RevertModelSnapshotIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/RevertModelSnapshotIT.java index ceafc9c8f349..4e8272053245 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/RevertModelSnapshotIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/RevertModelSnapshotIT.java @@ -11,7 +11,6 @@ import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.query.QueryBuilders; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.SearchHits; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; @@ -200,10 +199,7 @@ private void testRunJobInTwoPartsAndRevertSnapshotAndRunToCompletion(String jobI GetJobsStatsAction.Response.JobStats statsBeforeRevert = getJobStats(jobId).get(0); Instant timeBeforeRevert = Instant.now(); - assertThat( - revertModelSnapshot(job.getId(), revertSnapshot.getSnapshotId(), deleteInterveningResults).status(), - equalTo(RestStatus.OK) - ); + revertModelSnapshot(job.getId(), revertSnapshot.getSnapshotId(), deleteInterveningResults); GetJobsStatsAction.Response.JobStats statsAfterRevert = getJobStats(job.getId()).get(0); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestGetCalendarsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestGetCalendarsAction.java index 310f7d22f113..1e662e28cf56 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestGetCalendarsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/calendar/RestGetCalendarsAction.java @@ -13,7 +13,7 @@ import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.Scope; import org.elasticsearch.rest.ServerlessScope; -import org.elasticsearch.rest.action.RestStatusToXContentListener; +import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.action.util.PageParams; import org.elasticsearch.xpack.core.ml.action.GetCalendarsAction; @@ -74,6 +74,6 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient } } - return channel -> client.execute(GetCalendarsAction.INSTANCE, request, new RestStatusToXContentListener<>(channel)); + return channel -> client.execute(GetCalendarsAction.INSTANCE, request, new RestToXContentListener<>(channel)); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/filter/RestGetFiltersAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/filter/RestGetFiltersAction.java index e2b20566a498..b4c76c3fa447 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/filter/RestGetFiltersAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/filter/RestGetFiltersAction.java @@ -14,7 +14,7 @@ import org.elasticsearch.rest.Scope; import org.elasticsearch.rest.ServerlessScope; import org.elasticsearch.rest.action.RestCancellableNodeClient; -import org.elasticsearch.rest.action.RestStatusToXContentListener; +import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.xpack.core.action.util.PageParams; import org.elasticsearch.xpack.core.ml.action.GetFiltersAction; import org.elasticsearch.xpack.core.ml.job.config.MlFilter; @@ -62,7 +62,7 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient return channel -> new RestCancellableNodeClient(client, restRequest.getHttpChannel()).execute( GetFiltersAction.INSTANCE, request, - new RestStatusToXContentListener<>(channel) + new RestToXContentListener<>(channel) ); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestGetTrainedModelsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestGetTrainedModelsAction.java index 1475b64e381e..ae7b26ebad0e 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestGetTrainedModelsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestGetTrainedModelsAction.java @@ -138,7 +138,7 @@ public RestResponse buildResponse(T response, XContentBuilder builder) throws Ex includes.forEach(include -> params.put(include, "true")); params.put(ToXContentParams.FOR_INTERNAL_STORAGE, "false"); response.toXContent(builder, new ToXContent.MapParams(params)); - return new RestResponse(getStatus(response), builder); + return new RestResponse(statusFunction.apply(response), builder); } } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestPostDataAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestPostDataAction.java index 8ddfe39188fa..fa9a15b246f8 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestPostDataAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestPostDataAction.java @@ -10,7 +10,8 @@ import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.action.RestStatusToXContentListener; +import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.xpack.core.ml.action.PostDataAction; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -48,7 +49,7 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient request.setResetEnd(restRequest.param(PostDataAction.Request.RESET_END.getPreferredName(), DEFAULT_RESET_END)); request.setContent(restRequest.content(), restRequest.getXContentType()); - return channel -> client.execute(PostDataAction.INSTANCE, request, new RestStatusToXContentListener<>(channel)); + return channel -> client.execute(PostDataAction.INSTANCE, request, new RestToXContentListener<>(channel, r -> RestStatus.ACCEPTED)); } @Override diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/modelsnapshots/RestRevertModelSnapshotAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/modelsnapshots/RestRevertModelSnapshotAction.java index e33bdf2d100e..f1411db1e99e 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/modelsnapshots/RestRevertModelSnapshotAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/modelsnapshots/RestRevertModelSnapshotAction.java @@ -12,7 +12,7 @@ import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.Scope; import org.elasticsearch.rest.ServerlessScope; -import org.elasticsearch.rest.action.RestStatusToXContentListener; +import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.RevertModelSnapshotAction; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -68,6 +68,6 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient request.timeout(restRequest.paramAsTime("timeout", request.timeout())); request.masterNodeTimeout(restRequest.paramAsTime("master_timeout", request.masterNodeTimeout())); - return channel -> client.execute(RevertModelSnapshotAction.INSTANCE, request, new RestStatusToXContentListener<>(channel)); + return channel -> client.execute(RevertModelSnapshotAction.INSTANCE, request, new RestToXContentListener<>(channel)); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/modelsnapshots/RestUpdateModelSnapshotAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/modelsnapshots/RestUpdateModelSnapshotAction.java index 45d5536a9f1c..911305aca474 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/modelsnapshots/RestUpdateModelSnapshotAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/modelsnapshots/RestUpdateModelSnapshotAction.java @@ -12,7 +12,7 @@ import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.Scope; import org.elasticsearch.rest.ServerlessScope; -import org.elasticsearch.rest.action.RestStatusToXContentListener; +import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.core.ml.action.UpdateModelSnapshotAction; import org.elasticsearch.xpack.core.ml.job.config.Job; @@ -55,10 +55,6 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient parser ); - return channel -> client.execute( - UpdateModelSnapshotAction.INSTANCE, - updateModelSnapshot, - new RestStatusToXContentListener<>(channel) - ); + return channel -> client.execute(UpdateModelSnapshotAction.INSTANCE, updateModelSnapshot, new RestToXContentListener<>(channel)); } } diff --git a/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/GetStatusAction.java b/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/GetStatusAction.java index 8566978decaa..ec8c85d39015 100644 --- a/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/GetStatusAction.java +++ b/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/GetStatusAction.java @@ -14,8 +14,8 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; @@ -29,7 +29,7 @@ protected GetStatusAction() { super(NAME, GetStatusAction.Response::new); } - public static class Response extends ActionResponse implements StatusToXContentObject { + public static class Response extends ActionResponse implements ToXContentObject { private boolean profilingEnabled; private boolean resourceManagementEnabled; @@ -102,7 +102,6 @@ public String toString() { return Strings.toString(this, true, true); } - @Override public RestStatus status() { return timedOut ? RestStatus.REQUEST_TIMEOUT : RestStatus.OK; } diff --git a/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/RestGetStatusAction.java b/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/RestGetStatusAction.java index 714181f3dc0b..e20d7a595d86 100644 --- a/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/RestGetStatusAction.java +++ b/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/RestGetStatusAction.java @@ -10,7 +10,7 @@ import org.elasticsearch.client.internal.node.NodeClient; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.action.RestStatusToXContentListener; +import org.elasticsearch.rest.action.RestToXContentListener; import java.util.List; @@ -34,6 +34,10 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient request.timeout(restRequest.paramAsTime("timeout", request.timeout())); request.masterNodeTimeout(restRequest.paramAsTime("master_timeout", request.masterNodeTimeout())); request.waitForResourcesCreated(restRequest.paramAsBoolean("wait_for_resources_created", false)); - return channel -> client.execute(GetStatusAction.INSTANCE, request, new RestStatusToXContentListener<>(channel)); + return channel -> client.execute( + GetStatusAction.INSTANCE, + request, + new RestToXContentListener<>(channel, GetStatusAction.Response::status) + ); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/async/QlStatusResponse.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/async/QlStatusResponse.java index 30e43e71c82b..3943ddd3e207 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/async/QlStatusResponse.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/async/QlStatusResponse.java @@ -11,8 +11,8 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.async.StoredAsyncResponse; import org.elasticsearch.xpack.core.search.action.SearchStatusResponse; @@ -20,12 +20,10 @@ import java.io.IOException; import java.util.Objects; -import static org.elasticsearch.rest.RestStatus.OK; - /** * A response for *QL search status request */ -public class QlStatusResponse extends ActionResponse implements SearchStatusResponse, StatusToXContentObject { +public class QlStatusResponse extends ActionResponse implements SearchStatusResponse, ToXContentObject { private final String id; private final boolean isRunning; private final boolean isPartial; @@ -115,11 +113,6 @@ public void writeTo(StreamOutput out) throws IOException { } } - @Override - public RestStatus status() { - return OK; - } - @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); diff --git a/x-pack/plugin/rollup/src/main/java/org/elasticsearch/xpack/rollup/rest/RestDeleteRollupJobAction.java b/x-pack/plugin/rollup/src/main/java/org/elasticsearch/xpack/rollup/rest/RestDeleteRollupJobAction.java index 33e489ae2037..3662fc94e4c7 100644 --- a/x-pack/plugin/rollup/src/main/java/org/elasticsearch/xpack/rollup/rest/RestDeleteRollupJobAction.java +++ b/x-pack/plugin/rollup/src/main/java/org/elasticsearch/xpack/rollup/rest/RestDeleteRollupJobAction.java @@ -34,19 +34,12 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient String id = restRequest.param(ID.getPreferredName()); DeleteRollupJobAction.Request request = new DeleteRollupJobAction.Request(id); - return channel -> client.execute( - DeleteRollupJobAction.INSTANCE, - request, - new RestToXContentListener(channel) { - @Override - protected RestStatus getStatus(DeleteRollupJobAction.Response response) { - if (response.getNodeFailures().size() > 0 || response.getTaskFailures().size() > 0) { - return RestStatus.INTERNAL_SERVER_ERROR; - } - return RestStatus.OK; - } + return channel -> client.execute(DeleteRollupJobAction.INSTANCE, request, new RestToXContentListener<>(channel, r -> { + if (r.getNodeFailures().size() > 0 || r.getTaskFailures().size() > 0) { + return RestStatus.INTERNAL_SERVER_ERROR; } - ); + return RestStatus.OK; + })); } @Override diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/service/RestDeleteServiceAccountTokenAction.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/service/RestDeleteServiceAccountTokenAction.java index 3343bb63d10d..b12f1b8b3c8d 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/service/RestDeleteServiceAccountTokenAction.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/service/RestDeleteServiceAccountTokenAction.java @@ -16,7 +16,6 @@ import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.xpack.core.security.action.service.DeleteServiceAccountTokenAction; import org.elasticsearch.xpack.core.security.action.service.DeleteServiceAccountTokenRequest; -import org.elasticsearch.xpack.core.security.action.service.DeleteServiceAccountTokenResponse; import org.elasticsearch.xpack.security.rest.action.SecurityBaseRestHandler; import java.io.IOException; @@ -54,12 +53,7 @@ protected RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClien return channel -> client.execute( DeleteServiceAccountTokenAction.INSTANCE, deleteServiceAccountTokenRequest, - new RestToXContentListener<>(channel) { - @Override - protected RestStatus getStatus(DeleteServiceAccountTokenResponse response) { - return response.found() ? RestStatus.OK : RestStatus.NOT_FOUND; - } - } + new RestToXContentListener<>(channel, r -> r.found() ? RestStatus.OK : RestStatus.NOT_FOUND) ); } } diff --git a/x-pack/plugin/snapshot-repo-test-kit/src/internalClusterTest/java/org/elasticsearch/repositories/blobstore/testkit/RepositoryAnalysisFailureIT.java b/x-pack/plugin/snapshot-repo-test-kit/src/internalClusterTest/java/org/elasticsearch/repositories/blobstore/testkit/RepositoryAnalysisFailureIT.java index a2c1f5964d83..ba132938b238 100644 --- a/x-pack/plugin/snapshot-repo-test-kit/src/internalClusterTest/java/org/elasticsearch/repositories/blobstore/testkit/RepositoryAnalysisFailureIT.java +++ b/x-pack/plugin/snapshot-repo-test-kit/src/internalClusterTest/java/org/elasticsearch/repositories/blobstore/testkit/RepositoryAnalysisFailureIT.java @@ -36,7 +36,6 @@ import org.elasticsearch.repositories.RepositoryMissingException; import org.elasticsearch.repositories.RepositoryVerificationException; import org.elasticsearch.repositories.blobstore.BlobStoreRepository; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.snapshots.AbstractSnapshotIntegTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.LocalStateCompositeXPackPlugin; @@ -98,8 +97,7 @@ public void testSuccess() { request.blobCount(1); request.maxBlobSize(ByteSizeValue.ofBytes(10L)); - final RepositoryAnalyzeAction.Response response = analyseRepository(request); - assertThat(response.status(), equalTo(RestStatus.OK)); + analyseRepository(request); } public void testFailsOnReadError() { diff --git a/x-pack/plugin/snapshot-repo-test-kit/src/internalClusterTest/java/org/elasticsearch/repositories/blobstore/testkit/RepositoryAnalysisSuccessIT.java b/x-pack/plugin/snapshot-repo-test-kit/src/internalClusterTest/java/org/elasticsearch/repositories/blobstore/testkit/RepositoryAnalysisSuccessIT.java index db36713dedec..45b1bdc75678 100644 --- a/x-pack/plugin/snapshot-repo-test-kit/src/internalClusterTest/java/org/elasticsearch/repositories/blobstore/testkit/RepositoryAnalysisSuccessIT.java +++ b/x-pack/plugin/snapshot-repo-test-kit/src/internalClusterTest/java/org/elasticsearch/repositories/blobstore/testkit/RepositoryAnalysisSuccessIT.java @@ -35,7 +35,6 @@ import org.elasticsearch.repositories.Repository; import org.elasticsearch.repositories.RepositoryMissingException; import org.elasticsearch.repositories.blobstore.BlobStoreRepository; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.snapshots.AbstractSnapshotIntegTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xpack.core.LocalStateCompositeXPackPlugin; @@ -125,10 +124,8 @@ public void testRepositoryAnalysis() { request.timeout(TimeValue.timeValueSeconds(20)); - final RepositoryAnalyzeAction.Response response = client().execute(RepositoryAnalyzeAction.INSTANCE, request) - .actionGet(30L, TimeUnit.SECONDS); + client().execute(RepositoryAnalyzeAction.INSTANCE, request).actionGet(30L, TimeUnit.SECONDS); - assertThat(response.status(), equalTo(RestStatus.OK)); assertThat(blobStore.currentPath, nullValue()); } diff --git a/x-pack/plugin/snapshot-repo-test-kit/src/main/java/org/elasticsearch/repositories/blobstore/testkit/RepositoryAnalyzeAction.java b/x-pack/plugin/snapshot-repo-test-kit/src/main/java/org/elasticsearch/repositories/blobstore/testkit/RepositoryAnalyzeAction.java index e2bb9343bb61..d8e3c8243370 100644 --- a/x-pack/plugin/snapshot-repo-test-kit/src/main/java/org/elasticsearch/repositories/blobstore/testkit/RepositoryAnalyzeAction.java +++ b/x-pack/plugin/snapshot-repo-test-kit/src/main/java/org/elasticsearch/repositories/blobstore/testkit/RepositoryAnalyzeAction.java @@ -40,7 +40,6 @@ import org.elasticsearch.common.util.concurrent.ConcurrentCollections; import org.elasticsearch.common.util.concurrent.EsExecutors; import org.elasticsearch.common.util.concurrent.ThrottledIterator; -import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.core.Releasable; import org.elasticsearch.core.Releasables; import org.elasticsearch.core.TimeValue; @@ -48,7 +47,6 @@ import org.elasticsearch.repositories.Repository; import org.elasticsearch.repositories.RepositoryVerificationException; import org.elasticsearch.repositories.blobstore.BlobStoreRepository; -import org.elasticsearch.rest.RestStatus; import org.elasticsearch.tasks.CancellableTask; import org.elasticsearch.tasks.Task; import org.elasticsearch.tasks.TaskCancelledException; @@ -58,6 +56,7 @@ import org.elasticsearch.transport.TransportRequestOptions; import org.elasticsearch.transport.TransportResponseHandler; import org.elasticsearch.transport.TransportService; +import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; @@ -1016,7 +1015,7 @@ public void reseed(long newSeed) { } - public static class Response extends ActionResponse implements StatusToXContentObject { + public static class Response extends ActionResponse implements ToXContentObject { private final String coordinatingNodeId; private final String coordinatingNodeName; @@ -1111,11 +1110,6 @@ public void writeTo(StreamOutput out) throws IOException { out.writeVLong(deleteTimeNanos); } - @Override - public RestStatus status() { - return RestStatus.OK; - } - @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); diff --git a/x-pack/plugin/snapshot-repo-test-kit/src/main/java/org/elasticsearch/repositories/blobstore/testkit/RestRepositoryAnalyzeAction.java b/x-pack/plugin/snapshot-repo-test-kit/src/main/java/org/elasticsearch/repositories/blobstore/testkit/RestRepositoryAnalyzeAction.java index b1e3e36a8d92..8dce1d460027 100644 --- a/x-pack/plugin/snapshot-repo-test-kit/src/main/java/org/elasticsearch/repositories/blobstore/testkit/RestRepositoryAnalyzeAction.java +++ b/x-pack/plugin/snapshot-repo-test-kit/src/main/java/org/elasticsearch/repositories/blobstore/testkit/RestRepositoryAnalyzeAction.java @@ -14,7 +14,7 @@ import org.elasticsearch.rest.Scope; import org.elasticsearch.rest.ServerlessScope; import org.elasticsearch.rest.action.RestCancellableNodeClient; -import org.elasticsearch.rest.action.RestStatusToXContentListener; +import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.xcontent.XContentBuilder; import java.util.List; @@ -62,7 +62,7 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC return channel -> cancelClient.execute( RepositoryAnalyzeAction.INSTANCE, analyzeRepositoryRequest, - new RestStatusToXContentListener<>(channel) { + new RestToXContentListener<>(channel) { @Override public RestResponse buildResponse(RepositoryAnalyzeAction.Response response, XContentBuilder builder) throws Exception { builder.humanReadable(request.paramAsBoolean("human", true)); diff --git a/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlClearCursorResponse.java b/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlClearCursorResponse.java index bd5abb5a993e..10db7228807d 100644 --- a/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlClearCursorResponse.java +++ b/x-pack/plugin/sql/sql-action/src/main/java/org/elasticsearch/xpack/sql/action/SqlClearCursorResponse.java @@ -9,8 +9,8 @@ import org.elasticsearch.action.ActionResponse; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; @@ -22,7 +22,7 @@ /** * Response to the request to clean all SQL resources associated with the cursor */ -public class SqlClearCursorResponse extends ActionResponse implements StatusToXContentObject { +public class SqlClearCursorResponse extends ActionResponse implements ToXContentObject { private boolean succeeded; @@ -47,7 +47,6 @@ public SqlClearCursorResponse setSucceeded(boolean succeeded) { return this; } - @Override public RestStatus status() { return succeeded ? NOT_FOUND : OK; } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/RestSqlAsyncGetStatusAction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/RestSqlAsyncGetStatusAction.java index 5be7336d2cfa..4ce9655872cb 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/RestSqlAsyncGetStatusAction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/RestSqlAsyncGetStatusAction.java @@ -11,7 +11,7 @@ import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.Scope; import org.elasticsearch.rest.ServerlessScope; -import org.elasticsearch.rest.action.RestStatusToXContentListener; +import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.xpack.core.async.GetAsyncStatusRequest; import java.util.List; @@ -35,6 +35,6 @@ public String getName() { @Override protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) { GetAsyncStatusRequest statusRequest = new GetAsyncStatusRequest(request.param(ID_NAME)); - return channel -> client.execute(SqlAsyncGetStatusAction.INSTANCE, statusRequest, new RestStatusToXContentListener<>(channel)); + return channel -> client.execute(SqlAsyncGetStatusAction.INSTANCE, statusRequest, new RestToXContentListener<>(channel)); } }