public class BodyDeferringAsyncHandler extends Object implements AsyncHandler<Response>
 This class introduces new call: getResponse(), that blocks caller thread as
 long as headers are received, and return Response as soon as possible, but
 still pouring response body into supplied output stream. This handler is
 meant for situations when the "recommended" way (using
 client.prepareGet("http://foo.com/aResource").execute().get()
 would not work for you, since a potentially large response body is about to
 be GETted, but you need headers first, or you don't know yet (depending on
 some logic, maybe coming from headers) where to save the body, or you just
 want to leave body stream to some other component to consume it.
 
All these above means that this AsyncHandler needs a bit of different handling than "recommended" way. Some examples:
     FileOutputStream fos = ...
     BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(fos);
     // 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();
     // you can continue examine headers while actual body download happens
     // in separate thread
     // ...
     // finally "join" the download
     fr.get();
 
 
 
     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
      ...
     }
 | Modifier and Type | Class and Description | 
|---|---|
static class  | 
BodyDeferringAsyncHandler.BodyDeferringInputStream
A simple helper class that is used to perform automatic "join" for async
 download and the error checking of the Future of the request. 
 | 
AsyncHandler.STATE| Constructor and Description | 
|---|
BodyDeferringAsyncHandler(OutputStream os)  | 
| Modifier and Type | Method and Description | 
|---|---|
protected void | 
closeOut()  | 
Response | 
getResponse()
This method -- unlike Future 
 | 
AsyncHandler.STATE | 
onBodyPartReceived(HttpResponseBodyPart bodyPart)
Invoked as soon as some response body part are received. 
 | 
Response | 
onCompleted()
Invoked once the HTTP response processing is finished. 
 | 
AsyncHandler.STATE | 
onHeadersReceived(HttpResponseHeaders headers)
Invoked as soon as the HTTP headers has been received. 
 | 
AsyncHandler.STATE | 
onStatusReceived(HttpResponseStatus responseStatus)
Invoked as soon as the HTTP status line has been received 
 | 
void | 
onThrowable(Throwable t)
Invoked when an unexpected exception occurs during the processing of the response. 
 | 
public BodyDeferringAsyncHandler(OutputStream os)
public void onThrowable(Throwable t)
AsyncHandleronThrowable in interface AsyncHandler<Response>t - a Throwablepublic AsyncHandler.STATE onStatusReceived(HttpResponseStatus responseStatus) throws Exception
AsyncHandleronStatusReceived in interface AsyncHandler<Response>responseStatus - the status code and test of the responseAsyncHandler.STATE telling to CONTINUE or ABORT the current processing.Exception - if something wrong happenspublic AsyncHandler.STATE onHeadersReceived(HttpResponseHeaders headers) throws Exception
AsyncHandleronHeadersReceived in interface AsyncHandler<Response>headers - the HTTP headers.AsyncHandler.STATE telling to CONTINUE or ABORT the current processing.Exception - if something wrong happenspublic AsyncHandler.STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception
AsyncHandleronBodyPartReceived in interface AsyncHandler<Response>bodyPart - response's body part.AsyncHandler.STATE telling to CONTINUE or ABORT the current processing.Exception - if something wrong happensprotected void closeOut()
                 throws IOException
IOExceptionpublic Response onCompleted() throws IOException
AsyncHandleronCompleted in interface AsyncHandler<Response>FutureIOExceptionpublic Response getResponse() throws InterruptedException, IOException
null
 in case of some errors.ResponseInterruptedExceptionIOExceptionCopyright © 2015. All rights reserved.