NonBlockingBodyDataSource orgDataSource = response.getNonBlockingBody();
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) {
bodyData.add(byteBuffer.duplicate());
}
bodyDataSink.write(bufs);
bodyDataSink.flush();
}
@Override
public void onComplete() {
System.out.println(header.toString());
try {
System.out.println(header.toString() + DataConverter.toString(bodyData, header.getCharacterEncoding()));
} catch (Exception e) {
System.out.println("<body not printable>");
}
}
};
orgDataSource.setDataHandler(bodyForwardHandler);
} else {
try {
System.out.println(response.getResponseHeader());
} catch (Exception ignore) { }
exchange.send(response);
}
}
public void onException(IOException ioe) {
exchange.sendError(500);
}
};
// does request contain a body?
if (req.hasBody()) {
final IHttpRequestHeader header = req.getRequestHeader();
final List<ByteBuffer> bodyData = new ArrayList<ByteBuffer>();
// 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
public void onData(NonBlockingBodyDataSource bodyDataSource, BodyDataSink bodyDataSink) throws BufferUnderflowException, IOException {
ByteBuffer[] bufs = bodyDataSource.readByteBufferByLength(bodyDataSource.available());