Skip to content

BodyDeferringAsyncHandler.getResponse() can not be used to get status or headers when completes without any bodypart #113

Closed
@charlesk40

Description

@charlesk40

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
    // ...
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions