Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

elasticsearch: convert StreamOutput to String

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

like image 242
lf215 Avatar asked Mar 21 '17 18:03

lf215


1 Answers

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");
like image 72
Val Avatar answered Oct 10 '22 17:10

Val