java.lang.Object |
↳ |
com.ning.http.client.BodyDeferringAsyncHandler |
Class Overview
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
...
}
Summary
Nested Classes |
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. |
[Expand]
Inherited Methods |
From class
java.lang.Object
Object
|
clone()
|
boolean
|
equals(Object arg0)
|
void
|
finalize()
|
final
Class<?>
|
getClass()
|
int
|
hashCode()
|
final
void
|
notify()
|
final
void
|
notifyAll()
|
String
|
toString()
|
final
void
|
wait()
|
final
void
|
wait(long arg0)
|
final
void
|
wait(long arg0, int arg1)
|
|
From interface
com.ning.http.client.AsyncHandler
|
Public Constructors
public
BodyDeferringAsyncHandler
(OutputStream os)
Public Methods
public
Response
getResponse
()
This method -- unlike Future.get() -- will block only as long,
as headers arrive. This is useful for large transfers, to examine headers
ASAP, and defer body streaming to it's fine destination and prevent
unneeded bandwidth consumption. The response here will contain the very
1st response from server, so status code and headers, but it might be
incomplete in case of broken servers sending trailing headers. In that
case, the "usual" Future.get() method will return complete
headers, but multiple invocations of getResponse() will always return the
1st cached, probably incomplete one. Note: the response returned by this
method will contain everything except the response body itself,
so invoking any method like Response.getResponseBodyXXX() will result in
error! Also, please not that this method might return null
in case of some errors.
Throws
InterruptedException
| |
IOException
| |
Invoked as soon as some response body part are received. Could be invoked many times.
Parameters
bodyPart
| response's body part. |
public
Response
onCompleted
()
Invoked once the HTTP response processing is finished.
Gets always invoked as last callback method.
Returns
- T Value that will be returned by the associated java.util.concurrent.Future
Invoked as soon as the HTTP headers has been received. Can potentially be invoked more than once if a broken server
sent trailing headers.
Parameters
headers
| the HTTP headers. |
Invoked as soon as the HTTP status line has been received
Parameters
responseStatus
| the status code and test of the response |
public
void
onThrowable
(Throwable t)
Invoked when an unexpected exception occurs during the processing of the response. The exception may have been
produced by implementation of onXXXReceived method invocation.
Protected Methods
protected
void
closeOut
()