Skip to content

Commit 237fd28

Browse files
Simplify RestResponse Implementations (#87504)
This sets up chunked encoding rest responses. Make it so we only have a single REST response implementation (+the security filtering override) throughout the codebase. The test implementations were all redundant, removing them made the abstract base class redundant. A follow-up to this will change the signature of the `content()` method to an abstraction that allows for chunked encoding and simplify the very complicated resource handling associated with REST responses in `org.elasticsearch.http.DefaultRestChannel#sendResponse` based on the simplification in here.
1 parent 4cd2fe0 commit 237fd28

File tree

112 files changed

+377
-579
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+377
-579
lines changed

client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/action/bulk/RestNoopBulkAction.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.elasticsearch.client.internal.node.NodeClient;
1919
import org.elasticsearch.index.shard.ShardId;
2020
import org.elasticsearch.rest.BaseRestHandler;
21-
import org.elasticsearch.rest.BytesRestResponse;
2221
import org.elasticsearch.rest.RestChannel;
2322
import org.elasticsearch.rest.RestRequest;
2423
import org.elasticsearch.rest.RestResponse;
@@ -107,7 +106,7 @@ public RestResponse buildResponse(BulkRequest bulkRequest, XContentBuilder build
107106
}
108107
builder.endArray();
109108
builder.endObject();
110-
return new BytesRestResponse(OK, builder);
109+
return new RestResponse(OK, builder);
111110
}
112111
}
113112

client/rest-high-level/src/main/java/org/elasticsearch/client/RestHighLevelClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
import org.elasticsearch.index.reindex.ReindexRequest;
6161
import org.elasticsearch.index.reindex.UpdateByQueryRequest;
6262
import org.elasticsearch.plugins.spi.NamedXContentProvider;
63-
import org.elasticsearch.rest.BytesRestResponse;
63+
import org.elasticsearch.rest.RestResponse;
6464
import org.elasticsearch.rest.RestStatus;
6565
import org.elasticsearch.search.aggregations.Aggregation;
6666
import org.elasticsearch.search.aggregations.bucket.adjacency.AdjacencyMatrixAggregationBuilder;
@@ -1143,7 +1143,7 @@ protected final ElasticsearchStatusException parseResponseException(ResponseExce
11431143
elasticsearchException = new ElasticsearchStatusException(responseException.getMessage(), restStatus, responseException);
11441144
} else {
11451145
try {
1146-
elasticsearchException = parseEntity(entity, BytesRestResponse::errorFromXContent);
1146+
elasticsearchException = parseEntity(entity, RestResponse::errorFromXContent);
11471147
elasticsearchException.addSuppressed(responseException);
11481148
} catch (Exception e) {
11491149
elasticsearchException = new ElasticsearchStatusException("Unable to parse response body", restStatus, responseException);

modules/reindex/src/main/java/org/elasticsearch/reindex/AbstractBaseReindexRestHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import org.elasticsearch.index.reindex.BulkByScrollResponse;
1818
import org.elasticsearch.index.reindex.BulkByScrollTask;
1919
import org.elasticsearch.rest.BaseRestHandler;
20-
import org.elasticsearch.rest.BytesRestResponse;
2120
import org.elasticsearch.rest.RestRequest;
21+
import org.elasticsearch.rest.RestResponse;
2222
import org.elasticsearch.rest.RestStatus;
2323
import org.elasticsearch.tasks.LoggingTaskListener;
2424
import org.elasticsearch.tasks.Task;
@@ -109,7 +109,7 @@ private RestChannelConsumer sendTask(String localNodeId, Task task) {
109109
builder.startObject();
110110
builder.field("task", localNodeId + ":" + task.getId());
111111
builder.endObject();
112-
channel.sendResponse(new BytesRestResponse(RestStatus.OK, builder));
112+
channel.sendResponse(new RestResponse(RestStatus.OK, builder));
113113
}
114114
};
115115
}

modules/reindex/src/main/java/org/elasticsearch/reindex/BulkIndexByScrollResponseContentListener.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.elasticsearch.action.bulk.BulkItemResponse.Failure;
1212
import org.elasticsearch.index.reindex.BulkByScrollResponse;
1313
import org.elasticsearch.index.reindex.ScrollableHitSource.SearchFailure;
14-
import org.elasticsearch.rest.BytesRestResponse;
1514
import org.elasticsearch.rest.RestChannel;
1615
import org.elasticsearch.rest.RestResponse;
1716
import org.elasticsearch.rest.RestStatus;
@@ -38,7 +37,7 @@ public RestResponse buildResponse(BulkByScrollResponse response, XContentBuilder
3837
builder.startObject();
3938
response.toXContent(builder, new ToXContent.DelegatingMapParams(params, channel.request()));
4039
builder.endObject();
41-
return new BytesRestResponse(getStatus(response), builder);
40+
return new RestResponse(getStatus(response), builder);
4241
}
4342

4443
private RestStatus getStatus(BulkByScrollResponse response) {

modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4BadRequestTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
import org.elasticsearch.common.util.concurrent.ThreadContext;
2222
import org.elasticsearch.http.HttpServerTransport;
2323
import org.elasticsearch.http.HttpTransportSettings;
24-
import org.elasticsearch.rest.BytesRestResponse;
2524
import org.elasticsearch.rest.RestChannel;
2625
import org.elasticsearch.rest.RestRequest;
26+
import org.elasticsearch.rest.RestResponse;
2727
import org.elasticsearch.rest.RestStatus;
2828
import org.elasticsearch.test.ESTestCase;
2929
import org.elasticsearch.threadpool.TestThreadPool;
@@ -70,7 +70,7 @@ public void dispatchRequest(RestRequest request, RestChannel channel, ThreadCont
7070
public void dispatchBadRequest(RestChannel channel, ThreadContext threadContext, Throwable cause) {
7171
try {
7272
final Exception e = cause instanceof Exception ? (Exception) cause : new ElasticsearchException(cause);
73-
channel.sendResponse(new BytesRestResponse(channel, RestStatus.BAD_REQUEST, e));
73+
channel.sendResponse(new RestResponse(channel, RestStatus.BAD_REQUEST, e));
7474
} catch (final IOException e) {
7575
throw new UncheckedIOException(e);
7676
}

modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4HttpServerTransportTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@
5757
import org.elasticsearch.http.HttpServerTransport;
5858
import org.elasticsearch.http.HttpTransportSettings;
5959
import org.elasticsearch.http.NullDispatcher;
60-
import org.elasticsearch.rest.BytesRestResponse;
6160
import org.elasticsearch.rest.RestChannel;
6261
import org.elasticsearch.rest.RestRequest;
62+
import org.elasticsearch.rest.RestResponse;
6363
import org.elasticsearch.test.rest.FakeRestRequest;
6464
import org.elasticsearch.threadpool.TestThreadPool;
6565
import org.elasticsearch.threadpool.ThreadPool;
@@ -157,7 +157,7 @@ private void runExpectHeaderTest(
157157
final HttpServerTransport.Dispatcher dispatcher = new HttpServerTransport.Dispatcher() {
158158
@Override
159159
public void dispatchRequest(RestRequest request, RestChannel channel, ThreadContext threadContext) {
160-
channel.sendResponse(new BytesRestResponse(OK, BytesRestResponse.TEXT_CONTENT_TYPE, new BytesArray("done")));
160+
channel.sendResponse(new RestResponse(OK, RestResponse.TEXT_CONTENT_TYPE, new BytesArray("done")));
161161
}
162162

163163
@Override
@@ -263,7 +263,7 @@ public void dispatchBadRequest(final RestChannel channel, final ThreadContext th
263263
causeReference.set(cause);
264264
try {
265265
final ElasticsearchException e = new ElasticsearchException("you sent a bad request and you should feel bad");
266-
channel.sendResponse(new BytesRestResponse(channel, BAD_REQUEST, e));
266+
channel.sendResponse(new RestResponse(channel, BAD_REQUEST, e));
267267
} catch (final IOException e) {
268268
throw new AssertionError(e);
269269
}
@@ -333,7 +333,7 @@ private void testLargeResponse(boolean compressed) throws InterruptedException {
333333
@Override
334334
public void dispatchRequest(final RestRequest request, final RestChannel channel, final ThreadContext threadContext) {
335335
if (url.equals(request.uri())) {
336-
channel.sendResponse(new BytesRestResponse(OK, responseString));
336+
channel.sendResponse(new RestResponse(OK, responseString));
337337
} else {
338338
logger.error("--> Unexpected successful uri [{}]", request.uri());
339339
throw new AssertionError();

qa/smoke-test-http/src/javaRestTest/java/org/elasticsearch/http/TestResponseHeaderRestAction.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import org.elasticsearch.client.internal.node.NodeClient;
1111
import org.elasticsearch.rest.BaseRestHandler;
12-
import org.elasticsearch.rest.BytesRestResponse;
1312
import org.elasticsearch.rest.RestRequest;
1413
import org.elasticsearch.rest.RestResponse;
1514
import org.elasticsearch.rest.RestStatus;
@@ -33,11 +32,11 @@ public String getName() {
3332
@Override
3433
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) {
3534
if ("password".equals(request.header("Secret"))) {
36-
RestResponse response = new BytesRestResponse(RestStatus.OK, "Access granted");
35+
RestResponse response = new RestResponse(RestStatus.OK, "Access granted");
3736
response.addHeader("Secret", "granted");
3837
return channel -> channel.sendResponse(response);
3938
} else {
40-
RestResponse response = new BytesRestResponse(RestStatus.UNAUTHORIZED, "Access denied");
39+
RestResponse response = new RestResponse(RestStatus.UNAUTHORIZED, "Access denied");
4140
response.addHeader("Secret", "required");
4241
return channel -> channel.sendResponse(response);
4342
}

server/src/main/java/org/elasticsearch/rest/BytesRestResponse.java

Lines changed: 0 additions & 192 deletions
This file was deleted.

0 commit comments

Comments
 (0)