-
Notifications
You must be signed in to change notification settings - Fork 243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: option to retrieve original json body if parse exception occurred #886
Changes from 10 commits
51976d1
35c07e9
9835bc4
0e93afd
d003cc6
5bcf666
9832695
890a76b
cdeb192
e7eee8a
30ee626
435d2ef
0beeebb
d93cd29
d416490
a7b422e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
import co.elastic.clients.transport.http.HeaderMap; | ||
import co.elastic.clients.transport.http.TransportHttpClient; | ||
import co.elastic.clients.util.BinaryData; | ||
import co.elastic.clients.util.ByteArrayBinaryData; | ||
import co.elastic.clients.util.NoCopyByteArrayOutputStream; | ||
import org.apache.http.Header; | ||
import org.apache.http.HeaderElement; | ||
|
@@ -34,8 +35,10 @@ | |
import org.elasticsearch.client.RestClient; | ||
|
||
import javax.annotation.Nullable; | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.OutputStream; | ||
import java.nio.ByteBuffer; | ||
import java.util.AbstractList; | ||
|
@@ -85,10 +88,14 @@ public RestClientOptions createOptions(@Nullable TransportOptions options) { | |
} | ||
|
||
@Override | ||
public Response performRequest(String endpointId, @Nullable Node node, Request request, TransportOptions options) throws IOException { | ||
public Response performRequest(String endpointId, @Nullable Node node, Request request, | ||
TransportOptions options) throws IOException { | ||
RestClientOptions rcOptions = RestClientOptions.of(options); | ||
org.elasticsearch.client.Request restRequest = createRestRequest(request, rcOptions); | ||
org.elasticsearch.client.Response restResponse = restClient.performRequest(restRequest); | ||
if (options.keepResponseBodyOnException()) { | ||
return new RepeatableBodyResponse(restResponse); | ||
} | ||
return new RestResponse(restResponse); | ||
} | ||
|
||
|
@@ -103,7 +110,7 @@ public CompletableFuture<Response> performRequestAsync( | |
try { | ||
RestClientOptions rcOptions = RestClientOptions.of(options); | ||
restRequest = createRestRequest(request, rcOptions); | ||
} catch(Throwable thr) { | ||
} catch (Throwable thr) { | ||
// Terminate early | ||
future.completeExceptionally(thr); | ||
return future; | ||
|
@@ -112,6 +119,9 @@ public CompletableFuture<Response> performRequestAsync( | |
future.cancellable = restClient.performRequestAsync(restRequest, new ResponseListener() { | ||
@Override | ||
public void onSuccess(org.elasticsearch.client.Response response) { | ||
if (options.keepResponseBodyOnException()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be moved to |
||
future.complete(new RepeatableBodyResponse(response)); | ||
} | ||
future.complete(new RestResponse(response)); | ||
} | ||
|
||
|
@@ -166,7 +176,7 @@ private org.elasticsearch.client.Request createRestRequest(Request request, Rest | |
if (body != null) { | ||
ContentType ct = null; | ||
String ctStr; | ||
if (( ctStr = requestHeaders.get(HeaderMap.CONTENT_TYPE)) != null) { | ||
if ((ctStr = requestHeaders.get(HeaderMap.CONTENT_TYPE)) != null) { | ||
ct = ContentTypeCache.computeIfAbsent(ctStr, ContentType::parse); | ||
} | ||
clientReq.setEntity(new MultiBufferEntity(body, ct)); | ||
|
@@ -241,6 +251,51 @@ public void close() throws IOException { | |
} | ||
} | ||
|
||
public class RepeatableBodyResponse extends RestResponse { | ||
|
||
BinaryData repeatableBody; | ||
|
||
RepeatableBodyResponse(org.elasticsearch.client.Response restResponse) { | ||
super(restResponse); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public BinaryData body() throws IOException { | ||
if(repeatableBody != null) { | ||
return repeatableBody; | ||
} | ||
BinaryData body = super.body(); | ||
if (body != null) { | ||
if(body.isRepeatable()){ | ||
repeatableBody = body; | ||
} | ||
else{ | ||
repeatableBody = new ByteArrayBinaryData(body); | ||
} | ||
} | ||
return repeatableBody; | ||
} | ||
|
||
public String getOriginalBodyAsString() throws IOException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this is used only in tests. Let's not add in the public API some things that aren't required to use the library. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually wanted to provide this as a utility method to let users get the string directly |
||
BinaryData body = body(); | ||
|
||
if (body != null) { | ||
StringBuilder sb = new StringBuilder(); | ||
BufferedReader br = new BufferedReader(new InputStreamReader(body.asInputStream())); | ||
String read; | ||
|
||
while ((read = br.readLine()) != null) { | ||
sb.append(read); | ||
} | ||
br.close(); | ||
return sb.toString(); | ||
} | ||
return null; | ||
} | ||
|
||
} | ||
|
||
private static class HttpEntityBinaryData implements BinaryData { | ||
private final HttpEntity entity; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,8 @@ public class RestClientOptions implements TransportOptions { | |
|
||
private final RequestOptions options; | ||
|
||
boolean keepResponseBodyOnException; | ||
|
||
@VisibleForTesting | ||
static final String CLIENT_META_VALUE = getClientMeta(); | ||
@VisibleForTesting | ||
|
@@ -63,7 +65,8 @@ static RestClientOptions of(@Nullable TransportOptions options) { | |
return builder.build(); | ||
} | ||
|
||
public RestClientOptions(RequestOptions options) { | ||
public RestClientOptions(RequestOptions options, boolean keepResponseBodyOnException) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should keep the existing constructor without the additional parameter to avoid a breaking change (and default the additional parameter to Also, to be future proof if we add more options in the features, what about changing this constructor to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the future proof constructor will make more sense when we'll have control over RequestOptions in the low level client! |
||
this.keepResponseBodyOnException = keepResponseBodyOnException; | ||
this.options = addBuiltinHeaders(options.toBuilder()).build(); | ||
} | ||
|
||
|
@@ -99,6 +102,11 @@ public Function<List<String>, Boolean> onWarnings() { | |
return warnings -> options.getWarningsHandler().warningsShouldFailRequest(warnings); | ||
} | ||
|
||
@Override | ||
public boolean keepResponseBodyOnException() { | ||
return this.keepResponseBodyOnException; | ||
} | ||
|
||
@Override | ||
public Builder toBuilder() { | ||
return new Builder(options.toBuilder()); | ||
|
@@ -108,6 +116,8 @@ public static class Builder implements TransportOptions.Builder { | |
|
||
private RequestOptions.Builder builder; | ||
|
||
private boolean keepResponseBodyOnException; | ||
|
||
public Builder(RequestOptions.Builder builder) { | ||
this.builder = builder; | ||
} | ||
|
@@ -181,14 +191,20 @@ public TransportOptions.Builder onWarnings(Function<List<String>, Boolean> liste | |
return this; | ||
} | ||
|
||
@Override | ||
public TransportOptions.Builder keepResponseBodyOnException(boolean value) { | ||
this.keepResponseBodyOnException = value; | ||
return this; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is it empty? It should set a boolean flag in the builder, and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it cannot set a boolean flag in the builder, because the builder is in the RestClient code |
||
} | ||
|
||
@Override | ||
public RestClientOptions build() { | ||
return new RestClientOptions(addBuiltinHeaders(builder).build()); | ||
return new RestClientOptions(addBuiltinHeaders(builder).build(), keepResponseBodyOnException); | ||
} | ||
} | ||
|
||
static RestClientOptions initialOptions() { | ||
return new RestClientOptions(SafeResponseConsumer.DEFAULT_REQUEST_OPTIONS); | ||
return new RestClientOptions(SafeResponseConsumer.DEFAULT_REQUEST_OPTIONS, false); | ||
} | ||
|
||
private static RequestOptions.Builder addBuiltinHeaders(RequestOptions.Builder builder) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add a new field here, that is initialized from the builder's
keepResponseBodyOnException
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the default one, shouldn't it be false by default?