|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.ning.http.client.BodyDeferringAsyncHandler
public class BodyDeferringAsyncHandler
An AsyncHandler that returns Response (without body, so status code and headers only) as fast as possible for inspection, but leaves you the option to defer body consumption.
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 ... }
Nested Class Summary | |
---|---|
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. |
Nested classes/interfaces inherited from interface com.ning.http.client.AsyncHandler |
---|
AsyncHandler.STATE |
Constructor Summary | |
---|---|
BodyDeferringAsyncHandler(OutputStream os)
|
Method Summary | |
---|---|
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. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public BodyDeferringAsyncHandler(OutputStream os)
Method Detail |
---|
public void onThrowable(Throwable t)
AsyncHandler
onThrowable
in interface AsyncHandler<Response>
t
- a Throwable
public AsyncHandler.STATE onStatusReceived(HttpResponseStatus responseStatus) throws Exception
AsyncHandler
onStatusReceived
in interface AsyncHandler<Response>
responseStatus
- the status code and test of the response
AsyncHandler.STATE
telling to CONTINUE or ABORT the current processing.
Exception
- if something wrong happenspublic AsyncHandler.STATE onHeadersReceived(HttpResponseHeaders headers) throws Exception
AsyncHandler
onHeadersReceived
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
AsyncHandler
onBodyPartReceived
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
IOException
public Response onCompleted() throws IOException
AsyncHandler
onCompleted
in interface AsyncHandler<Response>
Future
IOException
public Response getResponse() throws InterruptedException, IOException
null
in case of some errors.
Response
InterruptedException
IOException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |