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

Community
  • 1
  • 1
lf215
  • 1,185
  • 7
  • 41
  • 83
  • `StreamOuput` extends `OutputStream` so can't you just use it as an `OutputStream`? – Amir Raminfar Mar 23 '17 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. – lf215 Mar 24 '17 at 01:46

1 Answers1

5

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");
Val
  • 207,596
  • 13
  • 358
  • 360
  • 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? – lf215 Mar 24 '17 at 13:13
  • Maybe the content you're getting is binary and not textual... or simply not encoded in UTF-8 – Val Mar 24 '17 at 13:13
  • Is there a specific flag/configuration where the encoding of Elasticsearch requests is set? – lf215 Mar 24 '17 at 13:21
  • Can you tell which request you're intercepting? – Val Mar 24 '17 at 13:46
  • The request could be a bunch of different request types, including `SearchRequest`, `BulkRequest`, `MultiGetRequest`. Is this what you mean? – lf215 Mar 24 '17 at 18:33
  • @lf215 no he means that how the content that is sent may not only be simple text - Looking at the implementation of BulkRequest(writeTo Method) they write a single bytes representing the Type of request in the stream and may do the same for the fields - so if you assume the whole request is just a UTF8 String, then the resulting string may be offset from the correct output – Japu_D_Cret Mar 28 '17 at 12:31
  • Yes, that's right, the request classes have their own protocol in `writeTo` and `readFrom` and that's not necessarily a string, but integers, boolean, etc – Val Mar 28 '17 at 12:39