Skip to content

Commit f3deefd

Browse files
Update apache client 5.6 + correct gzip handling (#1143) (#1144)
* support new gzip * get entity details from response * fix gzip test * unused import Co-authored-by: Laura Trotta <153528055+l-trotta@users.noreply.github.com>
1 parent 64404cb commit f3deefd

6 files changed

Lines changed: 48 additions & 24 deletions

File tree

java-client/build.gradle.kts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,6 @@ dependencies {
187187
compileOnly("org.elasticsearch.client", "elasticsearch-rest-client", elasticsearchVersion)
188188
testImplementation("org.elasticsearch.client", "elasticsearch-rest-client", elasticsearchVersion)
189189

190-
// Apache 2.0
191-
// https://hc.apache.org/httpcomponents-client-ga/
192-
api("org.apache.httpcomponents.client5","httpclient5","5.4.4")
193-
194190
// Apache 2.0
195191
// https://search.maven.org/artifact/com.google.code.findbugs/jsr305
196192
api("com.google.code.findbugs:jsr305:3.0.2")

rest5-client/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ dependencies {
142142

143143
// Apache 2.0
144144
// https://hc.apache.org/httpcomponents-client-ga/
145-
api("org.apache.httpcomponents.client5","httpclient5","5.4.4")
145+
api("org.apache.httpcomponents.client5","httpclient5","5.6")
146146

147147
// Apache 2.0
148148
// http://commons.apache.org/logging/

rest5-client/src/main/java/co/elastic/clients/transport/rest5_client/low_level/BasicAsyncResponseConsumer.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@
1919

2020
package co.elastic.clients.transport.rest5_client.low_level;
2121

22-
2322
import org.apache.hc.core5.http.ClassicHttpResponse;
2423
import org.apache.hc.core5.http.ContentType;
2524
import org.apache.hc.core5.http.HttpResponse;
2625
import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
2726
import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
28-
import org.apache.hc.core5.http.nio.entity.AbstractBinAsyncEntityConsumer;
2927
import org.apache.hc.core5.http.nio.support.AbstractAsyncResponseConsumer;
3028
import org.apache.hc.core5.http.protocol.HttpContext;
3129

@@ -39,7 +37,7 @@ class BasicAsyncResponseConsumer extends AbstractAsyncResponseConsumer<ClassicHt
3937
/**
4038
* Creates a new instance of this consumer with the provided buffer limit
4139
*/
42-
BasicAsyncResponseConsumer(AbstractBinAsyncEntityConsumer consumer) {
40+
BasicAsyncResponseConsumer(BufferedByteConsumer consumer) {
4341
super(consumer);
4442
}
4543

rest5-client/src/main/java/co/elastic/clients/transport/rest5_client/low_level/BufferedByteConsumer.java

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,28 @@
1818
*/
1919
package co.elastic.clients.transport.rest5_client.low_level;
2020

21+
import org.apache.hc.core5.concurrent.FutureCallback;
2122
import org.apache.hc.core5.http.ContentTooLongException;
2223
import org.apache.hc.core5.http.ContentType;
24+
import org.apache.hc.core5.http.EntityDetails;
2325
import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
24-
import org.apache.hc.core5.http.nio.entity.AbstractBinAsyncEntityConsumer;
26+
import org.apache.hc.core5.http.nio.AsyncEntityConsumer;
27+
import org.apache.hc.core5.http.nio.entity.AbstractBinDataConsumer;
2528
import org.apache.hc.core5.util.ByteArrayBuffer;
2629

30+
import java.io.IOException;
2731
import java.nio.ByteBuffer;
2832

2933
import static co.elastic.clients.transport.rest5_client.low_level.Constants.DEFAULT_BUFFER_INITIAL_CAPACITY;
3034

31-
public class BufferedByteConsumer extends AbstractBinAsyncEntityConsumer<ByteArrayEntity> {
35+
public class BufferedByteConsumer extends AbstractBinDataConsumer implements AsyncEntityConsumer<ByteArrayEntity> {
3236

33-
private volatile ByteArrayBuffer buffer;
3437
private final int limit;
35-
private ContentType contentType;
38+
private volatile ByteArrayBuffer buffer;
39+
private volatile FutureCallback<ByteArrayEntity> resultCallback;
40+
private volatile ContentType contentType;
41+
private volatile String contentEncoding;
42+
private volatile ByteArrayEntity result;
3643

3744
public BufferedByteConsumer(int bufferLimit) {
3845
super();
@@ -44,8 +51,11 @@ public BufferedByteConsumer(int bufferLimit) {
4451
}
4552

4653
@Override
47-
protected void streamStart(final ContentType contentType) {
48-
this.contentType = contentType;
54+
public void streamStart(final EntityDetails entityDetails,
55+
final FutureCallback<ByteArrayEntity> resultCallback) {
56+
this.contentType = entityDetails != null ? ContentType.parse(entityDetails.getContentType()) : null;
57+
this.contentEncoding = entityDetails != null ? entityDetails.getContentEncoding() : null;
58+
this.resultCallback = resultCallback;
4959
}
5060

5161
@Override
@@ -64,8 +74,25 @@ protected void data(final ByteBuffer src, final boolean endOfStream) throws Cont
6474
}
6575

6676
@Override
67-
protected ByteArrayEntity generateContent() {
68-
return new ByteArrayEntity(buffer.toByteArray(), contentType);
77+
protected final void completed() throws IOException {
78+
result = new ByteArrayEntity(buffer.toByteArray(), contentType, contentEncoding);
79+
if (resultCallback != null) {
80+
resultCallback.completed(result);
81+
}
82+
releaseResources();
83+
}
84+
85+
@Override
86+
public ByteArrayEntity getContent() {
87+
return result;
88+
}
89+
90+
@Override
91+
public final void failed(final Exception cause) {
92+
if (resultCallback != null) {
93+
resultCallback.failed(cause);
94+
}
95+
releaseResources();
6996
}
7097

7198
@Override

rest5-client/src/main/java/co/elastic/clients/transport/rest5_client/low_level/Rest5Client.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import org.apache.hc.core5.http.HttpEntity;
4444
import org.apache.hc.core5.http.HttpHost;
4545
import org.apache.hc.core5.http.HttpRequest;
46-
import org.apache.hc.core5.http.ProtocolException;
4746
import org.apache.hc.core5.http.message.RequestLine;
4847
import org.apache.hc.core5.http.nio.AsyncEntityProducer;
4948
import org.apache.hc.core5.http.nio.AsyncRequestProducer;
@@ -339,13 +338,7 @@ private ResponseOrResponseException convertResponse(InternalRequest request, Nod
339338

340339
HttpEntity entity = httpResponse.getEntity();
341340
if (entity != null) {
342-
Header encoding = null;
343-
try {
344-
encoding = httpResponse.getHeader(CONTENT_ENCODING);
345-
} catch (ProtocolException e) {
346-
throw new IOException("Couldn't retrieve content encoding: " + e);
347-
}
348-
if (encoding != null && "gzip".equals(encoding.getValue())) {
341+
if ("gzip".equals(entity.getContentEncoding())) {
349342
// Decompress and cleanup response headers
350343
httpResponse.setEntity(new GzipDecompressingEntity(entity));
351344
httpResponse.removeHeaders(CONTENT_ENCODING);

rest5-client/src/test/java/co/elastic/clients/transport/rest5_client/low_level/RestClientGzipCompressionTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.sun.net.httpserver.HttpExchange;
2323
import com.sun.net.httpserver.HttpHandler;
2424
import com.sun.net.httpserver.HttpServer;
25+
import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder;
2526
import org.apache.hc.core5.http.ContentType;
2627
import org.apache.hc.core5.http.HttpEntity;
2728
import org.apache.hc.core5.http.HttpHost;
@@ -122,10 +123,16 @@ private static byte[] readAll(InputStream in) throws IOException {
122123
return bos.toByteArray();
123124
}
124125

126+
// From org.apache.httpcomponents.client5.httpclient5.5.6, the client decompresses responses
127+
// by default, so to test the java client's decompression logic, a custom http client with decompression
128+
// disabled must be used.
125129
private Rest5Client createClient(boolean enableCompression) {
126130
InetSocketAddress address = httpServer.getAddress();
127131
return Rest5Client.builder(new HttpHost("http", address.getHostString(), address.getPort()))
128132
.setCompressionEnabled(enableCompression)
133+
.setHttpClient(HttpAsyncClientBuilder.create()
134+
.disableContentCompression()
135+
.build())
129136
.build();
130137
}
131138

@@ -205,6 +212,9 @@ public void testCompressingClientAsync() throws Exception {
205212
Rest5Client restClient = Rest5Client.builder(new HttpHost("http", address.getHostString(),
206213
address.getPort()))
207214
.setCompressionEnabled(true)
215+
.setHttpClient(HttpAsyncClientBuilder.create()
216+
.disableContentCompression()
217+
.build())
208218
.build();
209219

210220
Request request = new Request("POST", "/");

0 commit comments

Comments
 (0)