Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private void handleIncomingPublishRequest(
ActionListener<PublishWithJoinResponse> publishResponseListener
) throws IOException {
assert ThreadPool.assertCurrentThreadPool(GENERIC);
final Compressor compressor = CompressorFactory.compressor(request.bytes());
final Compressor compressor = CompressorFactory.compressorForUnknownXContentType(request.bytes());
StreamInput in = request.bytes().streamInput();
try {
if (compressor != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public CompressedXContent(ToXContent xcontent, ToXContent.Params params) throws
* that may already be compressed.
*/
public CompressedXContent(BytesReference data) throws IOException {
Compressor compressor = CompressorFactory.compressor(data);
Compressor compressor = CompressorFactory.compressorForUnknownXContentType(data);
if (compressor != null) {
// already compressed...
this.bytes = BytesReference.toBytes(data);
Expand All @@ -148,7 +148,7 @@ public CompressedXContent(BytesReference data) throws IOException {
}

private void assertConsistent() {
assert CompressorFactory.compressor(new BytesArray(bytes)) != null;
assert CompressorFactory.compressorForUnknownXContentType(new BytesArray(bytes)) != null;
assert this.sha256.equals(sha256(uncompressed()));
assert this.sha256.equals(sha256FromCompressed(bytes));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class CompressorFactory {
public static final Compressor COMPRESSOR = new DeflateCompressor();

public static boolean isCompressed(BytesReference bytes) {
return compressor(bytes) != null;
return compressorForUnknownXContentType(bytes) != null;
}

@Nullable
Expand All @@ -34,6 +34,15 @@ public static Compressor compressor(BytesReference bytes) {
assert XContentHelper.xContentType(bytes) == null;
return COMPRESSOR;
}
return null;
}

@Nullable
public static Compressor compressorForUnknownXContentType(BytesReference bytes) {
Compressor compressor = compressor(bytes);
if (compressor != null) {
return compressor;
}

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

/** Decompress the provided {@link BytesReference}. */
public static BytesReference uncompress(BytesReference bytes) throws IOException {
Compressor compressor = compressor(bytes);
Compressor compressor = compressorForUnknownXContentType(bytes);
if (compressor == null) {
throw new NotCompressedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static XContentParser createParser(NamedXContentRegistry registry, Deprec
*/
@Deprecated
public static XContentParser createParser(XContentParserConfiguration config, BytesReference bytes) throws IOException {
Compressor compressor = CompressorFactory.compressor(bytes);
Compressor compressor = CompressorFactory.compressorForUnknownXContentType(bytes);
if (compressor != null) {
InputStream compressedInput = compressor.threadLocalInputStream(bytes.streamInput());
if (compressedInput.markSupported() == false) {
Expand Down Expand Up @@ -568,7 +568,7 @@ public interface CustomMerge {
@Deprecated
public static void writeRawField(String field, BytesReference source, XContentBuilder builder, ToXContent.Params params)
throws IOException {
Compressor compressor = CompressorFactory.compressor(source);
Compressor compressor = CompressorFactory.compressorForUnknownXContentType(source);
if (compressor != null) {
try (InputStream compressedStreamInput = compressor.threadLocalInputStream(source.streamInput())) {
builder.rawField(field, compressedStreamInput);
Expand All @@ -592,7 +592,7 @@ public static void writeRawField(
ToXContent.Params params
) throws IOException {
Objects.requireNonNull(xContentType);
Compressor compressor = CompressorFactory.compressor(source);
Compressor compressor = CompressorFactory.compressorForUnknownXContentType(source);
if (compressor != null) {
try (InputStream compressedStreamInput = compressor.threadLocalInputStream(source.streamInput())) {
builder.rawField(field, compressedStreamInput, xContentType);
Expand Down Expand Up @@ -677,7 +677,7 @@ public static BytesReference toXContent(ChunkedToXContent toXContent, XContentTy
*/
@Deprecated
public static XContentType xContentTypeMayCompressed(BytesReference bytes) {
Compressor compressor = CompressorFactory.compressor(bytes);
Compressor compressor = CompressorFactory.compressorForUnknownXContentType(bytes);
if (compressor != null) {
try {
InputStream compressedStreamInput = compressor.threadLocalInputStream(bytes.streamInput());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private static boolean isDiff(BytesTransportRequest request, TransportVersion ve
StreamInput in = null;
try {
in = request.bytes().streamInput();
final Compressor compressor = CompressorFactory.compressor(request.bytes());
final Compressor compressor = CompressorFactory.compressorForUnknownXContentType(request.bytes());
if (compressor != null) {
in = compressor.threadLocalStreamInput(in);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.compress.NotXContentException;
import org.elasticsearch.common.util.Maps;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xcontent.ToXContent;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentParseException;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentParserConfiguration;
import org.elasticsearch.xcontent.XContentType;
Expand Down Expand Up @@ -421,4 +423,19 @@ public void testParseToType() throws IOException {

assertThat(names, equalTo(Set.of("a", "c")));
}

public void testGetParserWithInvalidInput() throws IOException {
assertThrows(
"Should detect bad JSON",
NotXContentException.class,
() -> XContentHelper.createParser(XContentParserConfiguration.EMPTY, new BytesArray("not actually XContent"))
);
XContentParser parser = XContentHelper.createParser(
XContentParserConfiguration.EMPTY,
new BytesArray("not actually XContent"),
XContentType.JSON
);
assertNotNull("Should not detect bad JSON", parser); // This is more like assertNotThrows
assertThrows("Should detect bad JSON at parse time", XContentParseException.class, parser::numberValue);
}
}