|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.client; |
| 21 | + |
| 22 | +import com.sun.net.httpserver.HttpExchange; |
| 23 | +import com.sun.net.httpserver.HttpHandler; |
| 24 | +import com.sun.net.httpserver.HttpServer; |
| 25 | +import org.apache.http.HttpEntity; |
| 26 | +import org.apache.http.HttpHost; |
| 27 | +import org.apache.http.entity.ContentType; |
| 28 | +import org.apache.http.entity.StringEntity; |
| 29 | +import org.elasticsearch.mocksocket.MockHttpServer; |
| 30 | +import org.junit.AfterClass; |
| 31 | +import org.junit.Assert; |
| 32 | +import org.junit.BeforeClass; |
| 33 | + |
| 34 | +import java.io.ByteArrayOutputStream; |
| 35 | +import java.io.IOException; |
| 36 | +import java.io.InputStream; |
| 37 | +import java.io.OutputStream; |
| 38 | +import java.net.InetAddress; |
| 39 | +import java.net.InetSocketAddress; |
| 40 | +import java.nio.charset.StandardCharsets; |
| 41 | +import java.util.concurrent.CompletableFuture; |
| 42 | +import java.util.zip.GZIPInputStream; |
| 43 | +import java.util.zip.GZIPOutputStream; |
| 44 | + |
| 45 | +public class RestClientGzipCompressionTests extends RestClientTestCase { |
| 46 | + |
| 47 | + private static HttpServer httpServer; |
| 48 | + |
| 49 | + @BeforeClass |
| 50 | + public static void startHttpServer() throws Exception { |
| 51 | + httpServer = MockHttpServer.createHttp(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0); |
| 52 | + httpServer.createContext("/", new GzipResponseHandler()); |
| 53 | + httpServer.start(); |
| 54 | + } |
| 55 | + |
| 56 | + @AfterClass |
| 57 | + public static void stopHttpServers() throws IOException { |
| 58 | + httpServer.stop(0); |
| 59 | + httpServer = null; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * A response handler that accepts gzip-encoded data and replies request and response encoding values |
| 64 | + * followed by the request body. The response is compressed if "Accept-Encoding" is "gzip". |
| 65 | + */ |
| 66 | + private static class GzipResponseHandler implements HttpHandler { |
| 67 | + @Override |
| 68 | + public void handle(HttpExchange exchange) throws IOException { |
| 69 | + |
| 70 | + // Decode body (if any) |
| 71 | + String contentEncoding = exchange.getRequestHeaders().getFirst("Content-Encoding"); |
| 72 | + InputStream body = exchange.getRequestBody(); |
| 73 | + if ("gzip".equals(contentEncoding)) { |
| 74 | + body = new GZIPInputStream(body); |
| 75 | + } |
| 76 | + byte[] bytes = readAll(body); |
| 77 | + |
| 78 | + boolean compress = "gzip".equals(exchange.getRequestHeaders().getFirst("Accept-Encoding")); |
| 79 | + if (compress) { |
| 80 | + exchange.getResponseHeaders().add("Content-Encoding", "gzip"); |
| 81 | + } |
| 82 | + |
| 83 | + exchange.sendResponseHeaders(200, 0); |
| 84 | + |
| 85 | + // Encode response if needed |
| 86 | + OutputStream out = exchange.getResponseBody(); |
| 87 | + if (compress) { |
| 88 | + out = new GZIPOutputStream(out); |
| 89 | + } |
| 90 | + |
| 91 | + // Outputs <request-encoding|null>#<response-encoding|null>#<request-body> |
| 92 | + out.write(String.valueOf(contentEncoding).getBytes(StandardCharsets.UTF_8)); |
| 93 | + out.write('#'); |
| 94 | + out.write((compress ? "gzip" : "null").getBytes(StandardCharsets.UTF_8)); |
| 95 | + out.write('#'); |
| 96 | + out.write(bytes); |
| 97 | + out.close(); |
| 98 | + |
| 99 | + exchange.close(); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** Read all bytes of an input stream and close it. */ |
| 104 | + private static byte[] readAll(InputStream in) throws IOException { |
| 105 | + byte[] buffer = new byte[1024]; |
| 106 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 107 | + int len = 0; |
| 108 | + while ((len = in.read(buffer)) > 0) { |
| 109 | + bos.write(buffer, 0, len); |
| 110 | + } |
| 111 | + in.close(); |
| 112 | + return bos.toByteArray(); |
| 113 | + } |
| 114 | + |
| 115 | + private RestClient createClient(boolean enableCompression) { |
| 116 | + InetSocketAddress address = httpServer.getAddress(); |
| 117 | + return RestClient.builder(new HttpHost(address.getHostString(), address.getPort(), "http")) |
| 118 | + .setCompressionEnabled(enableCompression) |
| 119 | + .build(); |
| 120 | + } |
| 121 | + |
| 122 | + public void testGzipHeaderSync() throws Exception { |
| 123 | + RestClient restClient = createClient(false); |
| 124 | + |
| 125 | + // Send non-compressed request, expect compressed response |
| 126 | + Request request = new Request("POST", "/"); |
| 127 | + request.setEntity(new StringEntity("plain request, gzip response", ContentType.TEXT_PLAIN)); |
| 128 | + request.setOptions(RequestOptions.DEFAULT.toBuilder().addHeader("Accept-Encoding", "gzip").build()); |
| 129 | + |
| 130 | + Response response = restClient.performRequest(request); |
| 131 | + |
| 132 | + HttpEntity entity = response.getEntity(); |
| 133 | + String content = new String(readAll(entity.getContent()), StandardCharsets.UTF_8); |
| 134 | + Assert.assertEquals("null#gzip#plain request, gzip response", content); |
| 135 | + |
| 136 | + restClient.close(); |
| 137 | + } |
| 138 | + |
| 139 | + public void testGzipHeaderAsync() throws Exception { |
| 140 | + RestClient restClient = createClient(false); |
| 141 | + |
| 142 | + // Send non-compressed request, expect compressed response |
| 143 | + Request request = new Request("POST", "/"); |
| 144 | + request.setEntity(new StringEntity("plain request, gzip response", ContentType.TEXT_PLAIN)); |
| 145 | + request.setOptions(RequestOptions.DEFAULT.toBuilder().addHeader("Accept-Encoding", "gzip").build()); |
| 146 | + |
| 147 | + FutureResponse futureResponse = new FutureResponse(); |
| 148 | + restClient.performRequestAsync(request, futureResponse); |
| 149 | + Response response = futureResponse.get(); |
| 150 | + |
| 151 | + HttpEntity entity = response.getEntity(); |
| 152 | + String content = new String(readAll(entity.getContent()), StandardCharsets.UTF_8); |
| 153 | + Assert.assertEquals("null#gzip#plain request, gzip response", content); |
| 154 | + |
| 155 | + restClient.close(); |
| 156 | + } |
| 157 | + |
| 158 | + public void testCompressingClientSync() throws Exception { |
| 159 | + RestClient restClient = createClient(true); |
| 160 | + |
| 161 | + Request request = new Request("POST", "/"); |
| 162 | + request.setEntity(new StringEntity("compressing client", ContentType.TEXT_PLAIN)); |
| 163 | + |
| 164 | + Response response = restClient.performRequest(request); |
| 165 | + |
| 166 | + HttpEntity entity = response.getEntity(); |
| 167 | + String content = new String(readAll(entity.getContent()), StandardCharsets.UTF_8); |
| 168 | + Assert.assertEquals("gzip#gzip#compressing client", content); |
| 169 | + |
| 170 | + restClient.close(); |
| 171 | + } |
| 172 | + |
| 173 | + public void testCompressingClientAsync() throws Exception { |
| 174 | + InetSocketAddress address = httpServer.getAddress(); |
| 175 | + RestClient restClient = RestClient.builder(new HttpHost(address.getHostString(), address.getPort(), "http")) |
| 176 | + .setCompressionEnabled(true) |
| 177 | + .build(); |
| 178 | + |
| 179 | + Request request = new Request("POST", "/"); |
| 180 | + request.setEntity(new StringEntity("compressing client", ContentType.TEXT_PLAIN)); |
| 181 | + |
| 182 | + FutureResponse futureResponse = new FutureResponse(); |
| 183 | + restClient.performRequestAsync(request, futureResponse); |
| 184 | + Response response = futureResponse.get(); |
| 185 | + |
| 186 | + // Server should report it had a compressed request and sent back a compressed response |
| 187 | + HttpEntity entity = response.getEntity(); |
| 188 | + String content = new String(readAll(entity.getContent()), StandardCharsets.UTF_8); |
| 189 | + Assert.assertEquals("gzip#gzip#compressing client", content); |
| 190 | + |
| 191 | + restClient.close(); |
| 192 | + } |
| 193 | + |
| 194 | + public static class FutureResponse extends CompletableFuture<Response> implements ResponseListener { |
| 195 | + @Override |
| 196 | + public void onSuccess(Response response) { |
| 197 | + this.complete(response); |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + public void onFailure(Exception exception) { |
| 202 | + this.completeExceptionally(exception); |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments