|
34 | 34 | import org.apache.http.message.BasicStatusLine;
|
35 | 35 | import org.apache.http.nio.entity.NByteArrayEntity;
|
36 | 36 | import org.apache.http.nio.entity.NStringEntity;
|
| 37 | +import org.apache.lucene.util.BytesRef; |
37 | 38 | import org.elasticsearch.Build;
|
38 | 39 | import org.elasticsearch.ElasticsearchException;
|
39 | 40 | import org.elasticsearch.Version;
|
|
89 | 90 | import org.hamcrest.Matchers;
|
90 | 91 | import org.junit.Before;
|
91 | 92 |
|
| 93 | +import java.io.ByteArrayOutputStream; |
92 | 94 | import java.io.IOException;
|
93 | 95 | import java.lang.reflect.Method;
|
94 | 96 | import java.lang.reflect.Modifier;
|
95 | 97 | import java.net.SocketTimeoutException;
|
| 98 | +import java.nio.charset.StandardCharsets; |
96 | 99 | import java.util.ArrayList;
|
97 | 100 | import java.util.Arrays;
|
98 | 101 | import java.util.Collections;
|
|
106 | 109 | import java.util.concurrent.atomic.AtomicReference;
|
107 | 110 | import java.util.stream.Collectors;
|
108 | 111 | import java.util.stream.Stream;
|
| 112 | +import java.util.zip.GZIPOutputStream; |
109 | 113 |
|
110 | 114 | import static org.elasticsearch.common.xcontent.XContentHelper.toXContent;
|
111 | 115 | import static org.hamcrest.CoreMatchers.endsWith;
|
@@ -273,6 +277,59 @@ public void testParseEntity() throws IOException {
|
273 | 277 | }
|
274 | 278 | }
|
275 | 279 |
|
| 280 | + public void testParseCompressedEntity() throws IOException { |
| 281 | + CheckedFunction<XContentParser, String, IOException> entityParser = parser -> { |
| 282 | + assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken()); |
| 283 | + assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken()); |
| 284 | + assertTrue(parser.nextToken().isValue()); |
| 285 | + String value = parser.text(); |
| 286 | + assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken()); |
| 287 | + return value; |
| 288 | + }; |
| 289 | + |
| 290 | + HttpEntity jsonEntity = createGzipEncodedEntity("{\"field\":\"value\"}", ContentType.APPLICATION_JSON); |
| 291 | + assertEquals("value", restHighLevelClient.parseEntity(jsonEntity, entityParser)); |
| 292 | + HttpEntity yamlEntity = createGzipEncodedEntity("---\nfield: value\n", ContentType.create("application/yaml")); |
| 293 | + assertEquals("value", restHighLevelClient.parseEntity(yamlEntity, entityParser)); |
| 294 | + HttpEntity smileEntity = createGzipEncodedEntity(SmileXContent.contentBuilder(), ContentType.create("application/smile")); |
| 295 | + assertEquals("value", restHighLevelClient.parseEntity(smileEntity, entityParser)); |
| 296 | + HttpEntity cborEntity = createGzipEncodedEntity(CborXContent.contentBuilder(), ContentType.create("application/cbor")); |
| 297 | + assertEquals("value", restHighLevelClient.parseEntity(cborEntity, entityParser)); |
| 298 | + } |
| 299 | + |
| 300 | + private HttpEntity createGzipEncodedEntity(String content, ContentType contentType) throws IOException { |
| 301 | + byte[] gzipEncodedContent = compressContentWithGzip(content.getBytes(StandardCharsets.UTF_8)); |
| 302 | + NByteArrayEntity httpEntity = new NByteArrayEntity(gzipEncodedContent, contentType); |
| 303 | + httpEntity.setContentEncoding("gzip"); |
| 304 | + |
| 305 | + return httpEntity; |
| 306 | + } |
| 307 | + |
| 308 | + private HttpEntity createGzipEncodedEntity(XContentBuilder xContentBuilder, ContentType contentType) throws IOException { |
| 309 | + try (XContentBuilder builder = xContentBuilder) { |
| 310 | + builder.startObject(); |
| 311 | + builder.field("field", "value"); |
| 312 | + builder.endObject(); |
| 313 | + |
| 314 | + BytesRef bytesRef = BytesReference.bytes(xContentBuilder).toBytesRef(); |
| 315 | + byte[] gzipEncodedContent = compressContentWithGzip(bytesRef.bytes); |
| 316 | + NByteArrayEntity httpEntity = new NByteArrayEntity(gzipEncodedContent, contentType); |
| 317 | + httpEntity.setContentEncoding("gzip"); |
| 318 | + |
| 319 | + return httpEntity; |
| 320 | + } |
| 321 | + } |
| 322 | + |
| 323 | + private static byte[] compressContentWithGzip(byte[] content) throws IOException { |
| 324 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(content.length); |
| 325 | + GZIPOutputStream gzip = new GZIPOutputStream(bos); |
| 326 | + gzip.write(content); |
| 327 | + gzip.close(); |
| 328 | + bos.close(); |
| 329 | + |
| 330 | + return bos.toByteArray(); |
| 331 | + } |
| 332 | + |
276 | 333 | private static HttpEntity createBinaryEntity(XContentBuilder xContentBuilder, ContentType contentType) throws IOException {
|
277 | 334 | try (XContentBuilder builder = xContentBuilder) {
|
278 | 335 | builder.startObject();
|
@@ -763,12 +820,12 @@ public void testApiNamingConventions() throws Exception {
|
763 | 820 | Collectors.mapping(Tuple::v2, Collectors.toSet())));
|
764 | 821 |
|
765 | 822 | // TODO remove in 8.0 - we will undeprecate indices.get_template because the current getIndexTemplate
|
766 |
| - // impl will replace the existing getTemplate method. |
| 823 | + // impl will replace the existing getTemplate method. |
767 | 824 | // The above general-purpose code ignores all deprecated methods which in this case leaves `getTemplate`
|
768 |
| - // looking like it doesn't have a valid implementatation when it does. |
| 825 | + // looking like it doesn't have a valid implementatation when it does. |
769 | 826 | apiUnsupported.remove("indices.get_template");
|
770 |
| - |
771 |
| - |
| 827 | + |
| 828 | + |
772 | 829 |
|
773 | 830 | for (Map.Entry<String, Set<Method>> entry : methods.entrySet()) {
|
774 | 831 | String apiName = entry.getKey();
|
@@ -803,7 +860,7 @@ public void testApiNamingConventions() throws Exception {
|
803 | 860 | apiName.startsWith("index_lifecycle.") == false &&
|
804 | 861 | apiName.startsWith("ccr.") == false &&
|
805 | 862 | apiName.endsWith("freeze") == false &&
|
806 |
| - // IndicesClientIT.getIndexTemplate should be renamed "getTemplate" in version 8.0 when we |
| 863 | + // IndicesClientIT.getIndexTemplate should be renamed "getTemplate" in version 8.0 when we |
807 | 864 | // can get rid of 7.0's deprecated "getTemplate"
|
808 | 865 | apiName.equals("indices.get_index_template") == false) {
|
809 | 866 | apiNotFound.add(apiName);
|
|
0 commit comments