private StringBuilder requestString = new StringBuilder();
public void onRequest(final IHttpExchange exchange) throws IOException {
IHttpRequest req = exchange.getRequest();
requestString.append(req.getRequestHeader().toString());
IHttpResponseHandler respHdl = new IHttpResponseHandler() {
public void onResponse(IHttpResponse response) throws IOException {
// does request contain a body?
if (response.hasBody()) {
// get the body
NonBlockingBodyDataSource orgDataSource = response.getNonBlockingBody();
// ... and replace it
final BodyDataSink inBodyChannel = exchange.send(response.getResponseHeader());
//... by a body forward handler
BodyForwarder bodyForwardHandler = new BodyForwarder(orgDataSource, inBodyChannel) {
@Override
public void onData(NonBlockingBodyDataSource bodyDataSource, BodyDataSink bodyDataSink) throws BufferUnderflowException, IOException {
ByteBuffer[] bufs = bodyDataSource.readByteBufferByLength(bodyDataSource.available());
for (ByteBuffer byteBuffer : bufs) {
// WARNING DataConverter call could fail!
requestString.append(DataConverter.toString(byteBuffer.duplicate()));
}
bodyDataSink.write(bufs);
bodyDataSink.flush();
}
};
orgDataSource.setDataHandler(bodyForwardHandler);
} else {
exchange.send(response);
}
}
public void onException(IOException ioe) {
}
};
// does request contain a body?
if (req.hasBody()) {
// get the body
NonBlockingBodyDataSource orgDataSource = req.getNonBlockingBody();
// ... and replace it
final BodyDataSink inBodyChannel = exchange.forward(req.getRequestHeader(), respHdl);
//... by a body forward handler
BodyForwarder bodyForwardHandler = new BodyForwarder(orgDataSource, inBodyChannel) {
@Override