Skip to content

Commit 5024169

Browse files
authored
Remove unnecessary RestHandler#supportsBulkContent (#135900)
This method only exists to add some additional `Content-type` validation, but we can do all the validation needed using `RestHandler#mediaTypesValid` instead.
1 parent f3b28fc commit 5024169

File tree

18 files changed

+97
-71
lines changed

18 files changed

+97
-71
lines changed

libs/x-content/impl/src/main/java/org/elasticsearch/xcontent/provider/cbor/CborXContentImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public XContentType type() {
6363
return XContentType.CBOR;
6464
}
6565

66+
@Override
67+
public boolean hasBulkSeparator() {
68+
return false;
69+
}
70+
6671
@Override
6772
public byte bulkSeparator() {
6873
throw new XContentParseException("cbor does not support bulk parsing...");

libs/x-content/impl/src/main/java/org/elasticsearch/xcontent/provider/json/JsonXContentImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ public XContentType type() {
6868
return XContentType.JSON;
6969
}
7070

71+
@Override
72+
public boolean hasBulkSeparator() {
73+
return true;
74+
}
75+
7176
@Override
7277
public byte bulkSeparator() {
7378
return '\n';

libs/x-content/impl/src/main/java/org/elasticsearch/xcontent/provider/smile/SmileXContentImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ public byte bulkSeparator() {
7070
return (byte) 0xFF;
7171
}
7272

73+
@Override
74+
public boolean hasBulkSeparator() {
75+
return true;
76+
}
77+
7378
@Override
7479
public boolean detectContent(byte[] bytes, int offset, int length) {
7580
return length > 2

libs/x-content/impl/src/main/java/org/elasticsearch/xcontent/provider/yaml/YamlXContentImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public XContentType type() {
6161
return XContentType.YAML;
6262
}
6363

64+
@Override
65+
public boolean hasBulkSeparator() {
66+
return false;
67+
}
68+
6469
@Override
6570
public byte bulkSeparator() {
6671
throw new UnsupportedOperationException("yaml does not support bulk parsing...");

libs/x-content/src/main/java/org/elasticsearch/xcontent/XContent.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ public interface XContent {
2525
*/
2626
XContentType type();
2727

28+
/**
29+
* @return {@code true} if this {@link XContent} can be sent in bulk, delimited by the byte returned by {@link #bulkSeparator()}, or
30+
* {@code false} if this {@link XContent} does not support a delimited bulk format (in which case {@link #bulkSeparator()} throws an
31+
* exception.
32+
* <p>
33+
* In practice, this is {@code true} for content with canonical type {@link XContentType#JSON} or {@link XContentType#SMILE} and
34+
* {@code false} for content with canonical type {@link XContentType#CBOR} or {@link XContentType#YAML}.
35+
*/
36+
boolean hasBulkSeparator();
37+
38+
/**
39+
* @return a {@link byte} that separates items in a bulk request that uses this {@link XContent}.
40+
* @throws RuntimeException if this {@link XContent} does not support a delimited bulk format. See {@link #hasBulkSeparator()}.
41+
*/
2842
byte bulkSeparator();
2943

3044
@Deprecated

libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentType.java

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

1010
package org.elasticsearch.xcontent;
1111

12+
import org.elasticsearch.core.Nullable;
1213
import org.elasticsearch.xcontent.cbor.CborXContent;
1314
import org.elasticsearch.xcontent.json.JsonXContent;
1415
import org.elasticsearch.xcontent.smile.SmileXContent;
@@ -287,4 +288,15 @@ public XContentType canonical() {
287288
public static XContentType ofOrdinal(int ordinal) {
288289
return values[ordinal];
289290
}
291+
292+
/**
293+
* Indicates if the {@link XContentType} (from the {@code Content-Type} header of a REST request) is compatible with delimited bulk
294+
* content. A bulk request contains multiple objects each terminated with {@link XContent#bulkSeparator()}.
295+
* <p>
296+
* In practice, this returns {@code true} if the argument has canonical type {@link XContentType#JSON} or {@link XContentType#SMILE}
297+
* and {@code false} if the argument is {@code null} or has canonical type {@link XContentType#CBOR} or {@link XContentType#YAML}.
298+
*/
299+
public static boolean supportsDelimitedBulkRequests(@Nullable XContentType xContentType) {
300+
return xContentType != null && xContentType.xContent.hasBulkSeparator();
301+
}
290302
}

modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/RestMultiSearchTemplateAction.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.elasticsearch.rest.action.RestToXContentListener;
1919
import org.elasticsearch.rest.action.search.RestMultiSearchAction;
2020
import org.elasticsearch.rest.action.search.RestSearchAction;
21+
import org.elasticsearch.xcontent.XContentType;
2122

2223
import java.io.IOException;
2324
import java.util.List;
@@ -95,8 +96,8 @@ public MultiSearchTemplateRequest parseRequest(RestRequest restRequest, boolean
9596
}
9697

9798
@Override
98-
public boolean supportsBulkContent() {
99-
return true;
99+
public boolean mediaTypesValid(RestRequest request) {
100+
return super.mediaTypesValid(request) && XContentType.supportsDelimitedBulkRequests(request.getXContentType());
100101
}
101102

102103
@Override

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@ public boolean canTripCircuitBreaker() {
4343
return delegate.canTripCircuitBreaker();
4444
}
4545

46-
@Override
47-
public boolean supportsBulkContent() {
48-
return delegate.supportsBulkContent();
49-
}
50-
5146
@Override
5247
public boolean mediaTypesValid(RestRequest request) {
5348
return delegate.mediaTypesValid(request);

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -421,20 +421,6 @@ private void dispatchRequest(
421421
sendContentTypeErrorMessage(request.getAllHeaderValues("Content-Type"), channel);
422422
return;
423423
}
424-
final XContentType xContentType = request.getXContentType();
425-
// TODO consider refactoring to handler.supportsContentStream(xContentType). It is only used with JSON and SMILE
426-
if (handler.supportsBulkContent()
427-
&& XContentType.JSON != xContentType.canonical()
428-
&& XContentType.SMILE != xContentType.canonical()) {
429-
channel.sendResponse(
430-
RestResponse.createSimpleErrorResponse(
431-
channel,
432-
RestStatus.NOT_ACCEPTABLE,
433-
"Content-Type [" + xContentType + "] does not support stream parsing. Use JSON or SMILE instead"
434-
)
435-
);
436-
return;
437-
}
438424
}
439425
RestChannel responseChannel = channel;
440426
if (apiProtections.isEnabled()) {

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.elasticsearch.core.Nullable;
1616
import org.elasticsearch.core.RestApiVersion;
1717
import org.elasticsearch.rest.RestRequest.Method;
18-
import org.elasticsearch.xcontent.XContent;
1918

2019
import java.util.Collections;
2120
import java.util.List;
@@ -44,15 +43,6 @@ default boolean supportsContentStream() {
4443
return false;
4544
}
4645

47-
/**
48-
* Indicates if the RestHandler supports bulk content. A bulk request contains multiple objects
49-
* delineated by {@link XContent#bulkSeparator()}. If a handler returns true this will affect
50-
* the types of content that can be sent to this endpoint.
51-
*/
52-
default boolean supportsBulkContent() {
53-
return false;
54-
}
55-
5646
/**
5747
* Returns the concrete RestHandler for this RestHandler. That is, if this is a delegating RestHandler it returns the delegate.
5848
* Otherwise it returns itself.

0 commit comments

Comments
 (0)