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.)
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");
链接地址: http://www.djcxy.com/p/78454.html