7

I'm overriding FilterClient so I can see incoming requests. I'd like some way to get a String representation of the ActionRequest that's passed in. ActionRequest let's you write to a StreamOuput, which is an Elasticsearch type that is a subclass of OutputStream. This SO post shows how to convert OutputStream to a String, but I'm forced to use StreamOuput due to the FilterClient API.

How do I get a String representation of ActionRequest or at least a readable version that will show me useful information about the request? (Calling ActionRequest.toString calls Object.toString, which is not good enough for me.)

2
  • StreamOuput extends OutputStream so can't you just use it as an OutputStream? Commented Mar 23, 2017 at 18:27
  • I don't understand your suggestion. The SO post I linked to suggests using ByteArrayOutputStream. But you can't use ByteArrayOutputStream where StreamOutput is expected. Perhaps you can include a bit of code in a solution so I can understand. Commented Mar 24, 2017 at 1:46

1 Answer 1

5
+50

StreamOutput is an abstract class which has a subclass called OutputStreamStreamOutput. The latter is basically a wrapper around an OutputStream, so you'd create an instance that wraps a ByteArrayOutputStream and then use it in the ActionRequest.writeTo() call.

// in your override doExecute method, add this:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStreamStreamOutput osso = new OutputStreamStreamOutput(baos);

request.writeTo(osso);
String requestAsString = baos.toString("UTF-8");
Sign up to request clarification or add additional context in comments.

7 Comments

This looks close to what I need, but I'm getting a bunch of �s in the output. I think it's outputting the correct request field names but the values are all gibberish. Any idea?
Maybe the content you're getting is binary and not textual... or simply not encoded in UTF-8
Is there a specific flag/configuration where the encoding of Elasticsearch requests is set?
Can you tell which request you're intercepting?
The request could be a bunch of different request types, including SearchRequest, BulkRequest, MultiGetRequest. Is this what you mean?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.