Skip to content

Commit 28d3a7a

Browse files
committed
Update apache client 5.6 + correct gzip handling (#1143)
* support new gzip * get entity details from response * fix gzip test * unused import
1 parent 7d5e036 commit 28d3a7a

File tree

5 files changed

+48
-20
lines changed

5 files changed

+48
-20
lines changed

java-client/build.gradle.kts

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

187187
// Apache 2.0
188188
// https://hc.apache.org/httpcomponents-client-ga/
189-
api("org.apache.httpcomponents.client5","httpclient5","5.4.4")
189+
api("org.apache.httpcomponents.client5","httpclient5","5.6")
190190

191191
// Apache 2.0
192192
// https://search.maven.org/artifact/com.google.code.findbugs/jsr305

java-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

java-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-
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
BufferedByteConsumer(int bufferLimit) {
3845
super();
@@ -44,8 +51,11 @@ class BufferedByteConsumer extends AbstractBinAsyncEntityConsumer<ByteArrayEntit
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

java-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;
@@ -335,13 +334,7 @@ private ResponseOrResponseException convertResponse(InternalRequest request, Nod
335334

336335
HttpEntity entity = httpResponse.getEntity();
337336
if (entity != null) {
338-
Header encoding = null;
339-
try {
340-
encoding = httpResponse.getHeader(CONTENT_ENCODING);
341-
} catch (ProtocolException e) {
342-
throw new IOException("Couldn't retrieve content encoding: " + e);
343-
}
344-
if (encoding != null && "gzip".equals(encoding.getValue())) {
337+
if ("gzip".equals(entity.getContentEncoding())) {
345338
// Decompress and cleanup response headers
346339
httpResponse.setEntity(new GzipDecompressingEntity(entity));
347340
httpResponse.removeHeaders(CONTENT_ENCODING);

java-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)