Closed
Description
Per this example, I should be able to get the status code. but looking through the code, Response instance is only created when first byte of bodypart is arrived in onBodyPartReceived. This results in NPE which doesn't seems like there are ways to get at the status code or headers unless I blocked on the the complete response which seems to defeat the purpose of BodyDeferringAsyncHandler. Am I missing something here? Thanks.
public STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
// body arrived, flush headers
if (!responseSet) {
response = responseBuilder.build();
responseSet = true;
headersArrived.countDown();
}
bodyPart.writeTo(output);
return STATE.CONTINUE;
}
PipedOutputStream pout = new PipedOutputStream();
BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(pout);
// client executes async
Future<Response> fr = client.prepareGet("http://foo.com/aresource").execute(bdah);
// main thread will block here until headers are available
Response response = bdah.getResponse();
if (response.getStatusCode() == 200) {
InputStream pin = new BodyDeferringInputStream(fr,new PipedInputStream(pout));
// consume InputStream
// ...
} else {
// handle unexpected response status code
// ...
}