Skip to content

Commit 89ad24f

Browse files
authored
CompressorFactory.compressor (#132448)
This reinstates #131655.
1 parent fc8fc5a commit 89ad24f

File tree

6 files changed

+37
-11
lines changed

6 files changed

+37
-11
lines changed

server/src/main/java/org/elasticsearch/cluster/coordination/PublicationTransportHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ private void handleIncomingPublishRequest(
130130
ActionListener<PublishWithJoinResponse> publishResponseListener
131131
) throws IOException {
132132
assert ThreadPool.assertCurrentThreadPool(GENERIC);
133-
final Compressor compressor = CompressorFactory.compressor(request.bytes());
133+
final Compressor compressor = CompressorFactory.compressorForUnknownXContentType(request.bytes());
134134
StreamInput in = request.bytes().streamInput();
135135
try {
136136
if (compressor != null) {

server/src/main/java/org/elasticsearch/common/compress/CompressedXContent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public CompressedXContent(ToXContent xcontent, ToXContent.Params params) throws
135135
* that may already be compressed.
136136
*/
137137
public CompressedXContent(BytesReference data) throws IOException {
138-
Compressor compressor = CompressorFactory.compressor(data);
138+
Compressor compressor = CompressorFactory.compressorForUnknownXContentType(data);
139139
if (compressor != null) {
140140
// already compressed...
141141
this.bytes = BytesReference.toBytes(data);
@@ -148,7 +148,7 @@ public CompressedXContent(BytesReference data) throws IOException {
148148
}
149149

150150
private void assertConsistent() {
151-
assert CompressorFactory.compressor(new BytesArray(bytes)) != null;
151+
assert CompressorFactory.compressorForUnknownXContentType(new BytesArray(bytes)) != null;
152152
assert this.sha256.equals(sha256(uncompressed()));
153153
assert this.sha256.equals(sha256FromCompressed(bytes));
154154
}

server/src/main/java/org/elasticsearch/common/compress/CompressorFactory.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class CompressorFactory {
2222
public static final Compressor COMPRESSOR = new DeflateCompressor();
2323

2424
public static boolean isCompressed(BytesReference bytes) {
25-
return compressor(bytes) != null;
25+
return compressorForUnknownXContentType(bytes) != null;
2626
}
2727

2828
@Nullable
@@ -34,6 +34,15 @@ public static Compressor compressor(BytesReference bytes) {
3434
assert XContentHelper.xContentType(bytes) == null;
3535
return COMPRESSOR;
3636
}
37+
return null;
38+
}
39+
40+
@Nullable
41+
public static Compressor compressorForUnknownXContentType(BytesReference bytes) {
42+
Compressor compressor = compressor(bytes);
43+
if (compressor != null) {
44+
return compressor;
45+
}
3746

3847
XContentType contentType = XContentHelper.xContentType(bytes);
3948
if (contentType == null) {
@@ -56,13 +65,13 @@ private static boolean isAncient(BytesReference bytes) {
5665
* @throws NullPointerException a NullPointerException will be thrown when bytes is null
5766
*/
5867
public static BytesReference uncompressIfNeeded(BytesReference bytes) throws IOException {
59-
Compressor compressor = compressor(Objects.requireNonNull(bytes, "the BytesReference must not be null"));
68+
Compressor compressor = compressorForUnknownXContentType(Objects.requireNonNull(bytes, "the BytesReference must not be null"));
6069
return compressor == null ? bytes : compressor.uncompress(bytes);
6170
}
6271

6372
/** Decompress the provided {@link BytesReference}. */
6473
public static BytesReference uncompress(BytesReference bytes) throws IOException {
65-
Compressor compressor = compressor(bytes);
74+
Compressor compressor = compressorForUnknownXContentType(bytes);
6675
if (compressor == null) {
6776
throw new NotCompressedException();
6877
}

server/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static XContentParser createParser(NamedXContentRegistry registry, Deprec
6767
*/
6868
@Deprecated
6969
public static XContentParser createParser(XContentParserConfiguration config, BytesReference bytes) throws IOException {
70-
Compressor compressor = CompressorFactory.compressor(bytes);
70+
Compressor compressor = CompressorFactory.compressorForUnknownXContentType(bytes);
7171
if (compressor != null) {
7272
InputStream compressedInput = compressor.threadLocalInputStream(bytes.streamInput());
7373
if (compressedInput.markSupported() == false) {
@@ -568,7 +568,7 @@ public interface CustomMerge {
568568
@Deprecated
569569
public static void writeRawField(String field, BytesReference source, XContentBuilder builder, ToXContent.Params params)
570570
throws IOException {
571-
Compressor compressor = CompressorFactory.compressor(source);
571+
Compressor compressor = CompressorFactory.compressorForUnknownXContentType(source);
572572
if (compressor != null) {
573573
try (InputStream compressedStreamInput = compressor.threadLocalInputStream(source.streamInput())) {
574574
builder.rawField(field, compressedStreamInput);
@@ -592,7 +592,7 @@ public static void writeRawField(
592592
ToXContent.Params params
593593
) throws IOException {
594594
Objects.requireNonNull(xContentType);
595-
Compressor compressor = CompressorFactory.compressor(source);
595+
Compressor compressor = CompressorFactory.compressorForUnknownXContentType(source);
596596
if (compressor != null) {
597597
try (InputStream compressedStreamInput = compressor.threadLocalInputStream(source.streamInput())) {
598598
builder.rawField(field, compressedStreamInput, xContentType);
@@ -677,7 +677,7 @@ public static BytesReference toXContent(ChunkedToXContent toXContent, XContentTy
677677
*/
678678
@Deprecated
679679
public static XContentType xContentTypeMayCompressed(BytesReference bytes) {
680-
Compressor compressor = CompressorFactory.compressor(bytes);
680+
Compressor compressor = CompressorFactory.compressorForUnknownXContentType(bytes);
681681
if (compressor != null) {
682682
try {
683683
InputStream compressedStreamInput = compressor.threadLocalInputStream(bytes.streamInput());

server/src/test/java/org/elasticsearch/cluster/coordination/PublicationTransportHandlerTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private static boolean isDiff(BytesTransportRequest request, TransportVersion ve
145145
StreamInput in = null;
146146
try {
147147
in = request.bytes().streamInput();
148-
final Compressor compressor = CompressorFactory.compressor(request.bytes());
148+
final Compressor compressor = CompressorFactory.compressorForUnknownXContentType(request.bytes());
149149
if (compressor != null) {
150150
in = compressor.threadLocalStreamInput(in);
151151
}

server/src/test/java/org/elasticsearch/common/xcontent/support/XContentHelperTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
import org.elasticsearch.common.bytes.BytesArray;
1313
import org.elasticsearch.common.bytes.BytesReference;
1414
import org.elasticsearch.common.compress.CompressedXContent;
15+
import org.elasticsearch.common.compress.NotXContentException;
1516
import org.elasticsearch.common.util.Maps;
1617
import org.elasticsearch.common.xcontent.XContentHelper;
1718
import org.elasticsearch.test.ESTestCase;
1819
import org.elasticsearch.xcontent.ToXContent;
1920
import org.elasticsearch.xcontent.ToXContentObject;
2021
import org.elasticsearch.xcontent.XContentBuilder;
22+
import org.elasticsearch.xcontent.XContentParseException;
2123
import org.elasticsearch.xcontent.XContentParser;
2224
import org.elasticsearch.xcontent.XContentParserConfiguration;
2325
import org.elasticsearch.xcontent.XContentType;
@@ -421,4 +423,19 @@ public void testParseToType() throws IOException {
421423

422424
assertThat(names, equalTo(Set.of("a", "c")));
423425
}
426+
427+
public void testGetParserWithInvalidInput() throws IOException {
428+
assertThrows(
429+
"Should detect bad JSON",
430+
NotXContentException.class,
431+
() -> XContentHelper.createParser(XContentParserConfiguration.EMPTY, new BytesArray("not actually XContent"))
432+
);
433+
XContentParser parser = XContentHelper.createParser(
434+
XContentParserConfiguration.EMPTY,
435+
new BytesArray("not actually XContent"),
436+
XContentType.JSON
437+
);
438+
assertNotNull("Should not detect bad JSON", parser); // This is more like assertNotThrows
439+
assertThrows("Should detect bad JSON at parse time", XContentParseException.class, parser::numberValue);
440+
}
424441
}

0 commit comments

Comments
 (0)