@InvokeOn(InvokeOn.HEADER_RECEIVED)
public void onResponse(IHttpResponse response) throws IOException {
// is response cacheable?
if (HttpCache.isCacheable(response, isSharedCache())) {
final IHttpResponseHeader responseHeader = response.getResponseHeader();
if (response.hasBody()) {
final NonBlockingBodyDataSource dataSource = response.getNonBlockingBody();
BodyDataSink dataSink = exchange.send(responseHeader);
BodyForwarder bodyForwarder = new BodyForwarder(dataSource, dataSink) {
private final List<ByteBuffer> responseBodyCopy = new ArrayList<ByteBuffer>();
@Override
public void onData(NonBlockingBodyDataSource bodyDataSource, BodyDataSink bodyDataSink) throws BufferUnderflowException, IOException {
ByteBuffer[] data = bodyDataSource.readByteBufferByLength(bodyDataSource.available());
for (ByteBuffer buf : data) {
responseBodyCopy.add(buf.duplicate());
}
bodyDataSink.write(data);
}
@Override
public void onComplete() {
try {
IHttpResponse responseCopy = new HttpResponse(responseHeader.copy(), responseBodyCopy);
interaction.setResponse(responseCopy);
} catch (IOException ioe) {
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("error occured by creating/registering cachedResponse " + ioe.toString());