Skip to content

Commit 0beeebb

Browse files
committed
custom generic response, checking in transport base
1 parent 435d2ef commit 0beeebb

File tree

5 files changed

+104
-65
lines changed

5 files changed

+104
-65
lines changed

java-client/src/main/java/co/elastic/clients/transport/DefaultTransportOptions.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,24 @@ public class DefaultTransportOptions implements TransportOptions {
3737
private final HeaderMap headers;
3838
private final Map<String, String> parameters;
3939
private final Function<List<String>, Boolean> onWarnings;
40+
private boolean keepResponseBodyOnException;
4041

4142
public static final DefaultTransportOptions EMPTY = new DefaultTransportOptions();
4243

4344
public DefaultTransportOptions() {
4445
this(new HeaderMap(), Collections.emptyMap(), null);
4546
}
4647

48+
public DefaultTransportOptions(
49+
@Nullable HeaderMap headers,
50+
@Nullable Map<String, String> parameters,
51+
@Nullable Function<List<String>, Boolean> onWarnings,
52+
boolean keepResponseBodyOnException
53+
) {
54+
this(headers,parameters,onWarnings);
55+
this.keepResponseBodyOnException = keepResponseBodyOnException;
56+
}
57+
4758
public DefaultTransportOptions(
4859
@Nullable HeaderMap headers,
4960
@Nullable Map<String, String> parameters,
@@ -53,10 +64,11 @@ public DefaultTransportOptions(
5364
this.parameters = (parameters == null || parameters.isEmpty()) ?
5465
Collections.emptyMap() : Collections.unmodifiableMap(parameters);
5566
this.onWarnings = onWarnings;
67+
this.keepResponseBodyOnException = false;
5668
}
5769

5870
protected DefaultTransportOptions(AbstractBuilder<?> builder) {
59-
this(builder.headers, builder.parameters, builder.onWarnings);
71+
this(builder.headers, builder.parameters, builder.onWarnings, builder.keepResponseBodyOnException);
6072
}
6173

6274
public static DefaultTransportOptions of(@Nullable TransportOptions options) {
@@ -90,7 +102,7 @@ public Function<List<String>, Boolean> onWarnings() {
90102

91103
@Override
92104
public boolean keepResponseBodyOnException() {
93-
return false;
105+
return keepResponseBodyOnException;
94106
}
95107

96108
@Override
@@ -125,13 +137,13 @@ public AbstractBuilder(DefaultTransportOptions options) {
125137
this.headers = new HeaderMap(options.headers);
126138
this.parameters = copyOrNull(options.parameters);
127139
this.onWarnings = options.onWarnings;
128-
this.keepResponseBodyOnException = options.keepResponseBodyOnException();
140+
this.keepResponseBodyOnException = options.keepResponseBodyOnException;
129141
}
130142

131143
protected abstract BuilderT self();
132144

133145
@Override
134-
public BuilderT keepResponseBodyOnException(boolean value){
146+
public BuilderT keepResponseBodyOnException(boolean value) {
135147
this.keepResponseBodyOnException = value;
136148
return self();
137149
}

java-client/src/main/java/co/elastic/clients/transport/ElasticsearchTransportBase.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import co.elastic.clients.transport.endpoints.BooleanEndpoint;
3030
import co.elastic.clients.transport.endpoints.BooleanResponse;
3131
import co.elastic.clients.transport.http.HeaderMap;
32+
import co.elastic.clients.transport.http.RepeatableBodyResponse;
3233
import co.elastic.clients.transport.http.TransportHttpClient;
3334
import co.elastic.clients.transport.instrumentation.Instrumentation;
3435
import co.elastic.clients.transport.instrumentation.NoopInstrumentation;
@@ -306,6 +307,9 @@ private <ResponseT, ErrorT> ResponseT getApiResponse(
306307

307308
int statusCode = clientResp.statusCode();
308309

310+
if(options().keepResponseBodyOnException()){
311+
clientResp = RepeatableBodyResponse.of(clientResp);
312+
}
309313
try {
310314
if (statusCode == 200) {
311315
checkProductHeader(clientResp, endpoint);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package co.elastic.clients.transport.http;
2+
3+
import co.elastic.clients.util.BinaryData;
4+
import co.elastic.clients.util.ByteArrayBinaryData;
5+
6+
import javax.annotation.Nullable;
7+
import java.io.IOException;
8+
import java.util.List;
9+
10+
public class RepeatableBodyResponse implements TransportHttpClient.Response {
11+
12+
private final TransportHttpClient.Response response;
13+
private final BinaryData body;
14+
15+
public static TransportHttpClient.Response of(TransportHttpClient.Response response) throws IOException {
16+
BinaryData body = response.body();
17+
if (body == null || body.isRepeatable()) {
18+
return response;
19+
}
20+
return new RepeatableBodyResponse(response);
21+
}
22+
23+
public RepeatableBodyResponse(TransportHttpClient.Response response) throws IOException {
24+
this.response = response;
25+
this.body = new ByteArrayBinaryData(response.body());
26+
}
27+
28+
@Override
29+
public TransportHttpClient.Node node() {
30+
return response.node();
31+
}
32+
33+
@Override
34+
public int statusCode() {
35+
return response.statusCode();
36+
}
37+
38+
@Nullable
39+
@Override
40+
public String header(String name) {
41+
return response.header(name);
42+
}
43+
44+
@Override
45+
public List<String> headers(String name) {
46+
return response.headers(name);
47+
}
48+
49+
@Nullable
50+
@Override
51+
public BinaryData body() throws IOException {
52+
return this.body;
53+
}
54+
55+
@Nullable
56+
@Override
57+
public Object originalResponse() {
58+
return response.originalResponse();
59+
}
60+
61+
@Override
62+
public void close() throws IOException {
63+
response.close();
64+
}
65+
}

java-client/src/main/java/co/elastic/clients/transport/rest_client/RestClientHttpClient.java

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import co.elastic.clients.transport.http.HeaderMap;
2424
import co.elastic.clients.transport.http.TransportHttpClient;
2525
import co.elastic.clients.util.BinaryData;
26-
import co.elastic.clients.util.ByteArrayBinaryData;
2726
import co.elastic.clients.util.NoCopyByteArrayOutputStream;
2827
import org.apache.http.Header;
2928
import org.apache.http.HeaderElement;
@@ -35,10 +34,8 @@
3534
import org.elasticsearch.client.RestClient;
3635

3736
import javax.annotation.Nullable;
38-
import java.io.BufferedReader;
3937
import java.io.IOException;
4038
import java.io.InputStream;
41-
import java.io.InputStreamReader;
4239
import java.io.OutputStream;
4340
import java.nio.ByteBuffer;
4441
import java.util.AbstractList;
@@ -93,9 +90,6 @@ public Response performRequest(String endpointId, @Nullable Node node, Request r
9390
RestClientOptions rcOptions = RestClientOptions.of(options);
9491
org.elasticsearch.client.Request restRequest = createRestRequest(request, rcOptions);
9592
org.elasticsearch.client.Response restResponse = restClient.performRequest(restRequest);
96-
if (options.keepResponseBodyOnException()) {
97-
return new RepeatableBodyResponse(restResponse);
98-
}
9993
return new RestResponse(restResponse);
10094
}
10195

@@ -119,9 +113,6 @@ public CompletableFuture<Response> performRequestAsync(
119113
future.cancellable = restClient.performRequestAsync(restRequest, new ResponseListener() {
120114
@Override
121115
public void onSuccess(org.elasticsearch.client.Response response) {
122-
if (options.keepResponseBodyOnException()) {
123-
future.complete(new RepeatableBodyResponse(response));
124-
}
125116
future.complete(new RestResponse(response));
126117
}
127118

@@ -251,51 +242,6 @@ public void close() throws IOException {
251242
}
252243
}
253244

254-
public class RepeatableBodyResponse extends RestResponse {
255-
256-
BinaryData repeatableBody;
257-
258-
RepeatableBodyResponse(org.elasticsearch.client.Response restResponse) {
259-
super(restResponse);
260-
}
261-
262-
@Nullable
263-
@Override
264-
public BinaryData body() throws IOException {
265-
if(repeatableBody != null) {
266-
return repeatableBody;
267-
}
268-
BinaryData body = super.body();
269-
if (body != null) {
270-
if(body.isRepeatable()){
271-
repeatableBody = body;
272-
}
273-
else{
274-
repeatableBody = new ByteArrayBinaryData(body);
275-
}
276-
}
277-
return repeatableBody;
278-
}
279-
280-
public String getOriginalBodyAsString() throws IOException {
281-
BinaryData body = body();
282-
283-
if (body != null) {
284-
StringBuilder sb = new StringBuilder();
285-
BufferedReader br = new BufferedReader(new InputStreamReader(body.asInputStream()));
286-
String read;
287-
288-
while ((read = br.readLine()) != null) {
289-
sb.append(read);
290-
}
291-
br.close();
292-
return sb.toString();
293-
}
294-
return null;
295-
}
296-
297-
}
298-
299245
private static class HttpEntityBinaryData implements BinaryData {
300246
private final HttpEntity entity;
301247

java-client/src/test/java/co/elastic/clients/transport/TransportTest.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@
2121

2222
import co.elastic.clients.elasticsearch.ElasticsearchClient;
2323
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
24-
import co.elastic.clients.transport.rest_client.RestClientHttpClient;
24+
import co.elastic.clients.transport.http.RepeatableBodyResponse;
2525
import co.elastic.clients.transport.rest_client.RestClientOptions;
2626
import co.elastic.clients.transport.rest_client.RestClientTransport;
27+
import co.elastic.clients.util.BinaryData;
2728
import com.sun.net.httpserver.HttpServer;
2829
import org.apache.http.HttpHost;
2930
import org.elasticsearch.client.RequestOptions;
@@ -32,6 +33,8 @@
3233
import org.junit.jupiter.api.Assertions;
3334
import org.junit.jupiter.api.Test;
3435

36+
import java.io.BufferedReader;
37+
import java.io.InputStreamReader;
3538
import java.io.OutputStream;
3639
import java.net.InetAddress;
3740
import java.net.InetSocketAddress;
@@ -106,7 +109,7 @@ public void testOriginalJsonBodyRetrievalException() throws Exception {
106109
.builder(new HttpHost(address.getHostString(), address.getPort(), "http"))
107110
.build();
108111

109-
// no transport options, should throw TransportException, but original body cannot be retrieved
112+
// no transport options, response is not RepeatableBodyResponse, original body cannot be retrieved
110113
ElasticsearchClient esClient = new ElasticsearchClient(new RestClientTransport(restClient,
111114
new JacksonJsonpMapper()));
112115

@@ -116,7 +119,7 @@ public void testOriginalJsonBodyRetrievalException() throws Exception {
116119
);
117120

118121
assertEquals(200, ex.statusCode());
119-
assertNotEquals(RestClientHttpClient.RepeatableBodyResponse.class, ex.response().getClass());
122+
assertNotEquals(RepeatableBodyResponse.class, ex.response().getClass());
120123

121124
// setting transport option
122125
RestClientOptions options = new RestClientOptions(RequestOptions.DEFAULT, true);
@@ -134,10 +137,19 @@ public void testOriginalJsonBodyRetrievalException() throws Exception {
134137
httpServer.stop(0);
135138

136139
assertEquals(200, ex.statusCode());
137-
assertEquals(RestClientHttpClient.RepeatableBodyResponse.class, ex.response().getClass());
138-
139-
try (RestClientHttpClient.RepeatableBodyResponse repeatableResponse = (RestClientHttpClient.RepeatableBodyResponse) ex.response()){
140-
assertEquals("definitely not json",repeatableResponse.getOriginalBodyAsString());
140+
assertEquals(RepeatableBodyResponse.class, ex.response().getClass());
141+
142+
try (RepeatableBodyResponse repeatableResponse = (RepeatableBodyResponse) ex.response()){
143+
BinaryData body = repeatableResponse.body();
144+
StringBuilder sb = new StringBuilder();
145+
BufferedReader br = new BufferedReader(new InputStreamReader(body.asInputStream()));
146+
String read;
147+
148+
while ((read = br.readLine()) != null) {
149+
sb.append(read);
150+
}
151+
br.close();
152+
assertEquals("definitely not json",sb.toString());
141153
}
142154
}
143155
}

0 commit comments

Comments
 (0)