|
33 | 33 | import org.apache.http.message.BasicStatusLine; |
34 | 34 | import org.apache.http.nio.entity.NByteArrayEntity; |
35 | 35 | import org.apache.http.nio.entity.NStringEntity; |
| 36 | +import org.apache.lucene.util.BytesRef; |
36 | 37 | import org.elasticsearch.ElasticsearchException; |
37 | 38 | import org.elasticsearch.action.ActionListener; |
38 | 39 | import org.elasticsearch.action.ActionRequest; |
|
117 | 118 | import org.hamcrest.Matchers; |
118 | 119 | import org.junit.Before; |
119 | 120 |
|
| 121 | +import java.io.ByteArrayOutputStream; |
120 | 122 | import java.io.IOException; |
121 | 123 | import java.lang.reflect.Method; |
122 | 124 | import java.lang.reflect.Modifier; |
123 | 125 | import java.net.SocketTimeoutException; |
| 126 | +import java.nio.charset.StandardCharsets; |
124 | 127 | import java.util.ArrayList; |
125 | 128 | import java.util.Arrays; |
126 | 129 | import java.util.Collections; |
|
134 | 137 | import java.util.concurrent.atomic.AtomicReference; |
135 | 138 | import java.util.stream.Collectors; |
136 | 139 | import java.util.stream.Stream; |
| 140 | +import java.util.zip.GZIPOutputStream; |
137 | 141 |
|
138 | 142 | import static org.elasticsearch.client.ml.dataframe.evaluation.MlEvaluationNamedXContentProvider.registeredMetricName; |
139 | 143 | import static org.elasticsearch.common.xcontent.XContentHelper.toXContent; |
@@ -322,6 +326,59 @@ public void testParseEntity() throws IOException { |
322 | 326 | } |
323 | 327 | } |
324 | 328 |
|
| 329 | + public void testParseCompressedEntity() throws IOException { |
| 330 | + CheckedFunction<XContentParser, String, IOException> entityParser = parser -> { |
| 331 | + assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken()); |
| 332 | + assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken()); |
| 333 | + assertTrue(parser.nextToken().isValue()); |
| 334 | + String value = parser.text(); |
| 335 | + assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken()); |
| 336 | + return value; |
| 337 | + }; |
| 338 | + |
| 339 | + HttpEntity jsonEntity = createGzipEncodedEntity("{\"field\":\"value\"}", ContentType.APPLICATION_JSON); |
| 340 | + assertEquals("value", restHighLevelClient.parseEntity(jsonEntity, entityParser)); |
| 341 | + HttpEntity yamlEntity = createGzipEncodedEntity("---\nfield: value\n", ContentType.create("application/yaml")); |
| 342 | + assertEquals("value", restHighLevelClient.parseEntity(yamlEntity, entityParser)); |
| 343 | + HttpEntity smileEntity = createGzipEncodedEntity(SmileXContent.contentBuilder(), ContentType.create("application/smile")); |
| 344 | + assertEquals("value", restHighLevelClient.parseEntity(smileEntity, entityParser)); |
| 345 | + HttpEntity cborEntity = createGzipEncodedEntity(CborXContent.contentBuilder(), ContentType.create("application/cbor")); |
| 346 | + assertEquals("value", restHighLevelClient.parseEntity(cborEntity, entityParser)); |
| 347 | + } |
| 348 | + |
| 349 | + private HttpEntity createGzipEncodedEntity(String content, ContentType contentType) throws IOException { |
| 350 | + byte[] gzipEncodedContent = compressContentWithGzip(content.getBytes(StandardCharsets.UTF_8)); |
| 351 | + NByteArrayEntity httpEntity = new NByteArrayEntity(gzipEncodedContent, contentType); |
| 352 | + httpEntity.setContentEncoding("gzip"); |
| 353 | + |
| 354 | + return httpEntity; |
| 355 | + } |
| 356 | + |
| 357 | + private HttpEntity createGzipEncodedEntity(XContentBuilder xContentBuilder, ContentType contentType) throws IOException { |
| 358 | + try (XContentBuilder builder = xContentBuilder) { |
| 359 | + builder.startObject(); |
| 360 | + builder.field("field", "value"); |
| 361 | + builder.endObject(); |
| 362 | + |
| 363 | + BytesRef bytesRef = BytesReference.bytes(xContentBuilder).toBytesRef(); |
| 364 | + byte[] gzipEncodedContent = compressContentWithGzip(bytesRef.bytes); |
| 365 | + NByteArrayEntity httpEntity = new NByteArrayEntity(gzipEncodedContent, contentType); |
| 366 | + httpEntity.setContentEncoding("gzip"); |
| 367 | + |
| 368 | + return httpEntity; |
| 369 | + } |
| 370 | + } |
| 371 | + |
| 372 | + private static byte[] compressContentWithGzip(byte[] content) throws IOException { |
| 373 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(content.length); |
| 374 | + GZIPOutputStream gzip = new GZIPOutputStream(bos); |
| 375 | + gzip.write(content); |
| 376 | + gzip.close(); |
| 377 | + bos.close(); |
| 378 | + |
| 379 | + return bos.toByteArray(); |
| 380 | + } |
| 381 | + |
325 | 382 | private static HttpEntity createBinaryEntity(XContentBuilder xContentBuilder, ContentType contentType) throws IOException { |
326 | 383 | try (XContentBuilder builder = xContentBuilder) { |
327 | 384 | builder.startObject(); |
|
0 commit comments