Class InputStreamResponseListener
- All Implemented Interfaces:
EventListener
,Response.AsyncContentListener
,Response.BeginListener
,Response.CompleteListener
,Response.ContentListener
,Response.ContentSourceListener
,Response.FailureListener
,Response.HeaderListener
,Response.HeadersListener
,Response.Listener
,Response.ResponseListener
,Response.SuccessListener
Response.Listener
that produces an InputStream
that allows applications to read the response content.
Typical usage is:
InputStreamResponseListener listener = new InputStreamResponseListener(); client.newRequest(...).send(listener); // Wait for the response headers to arrive Response response = listener.get(5, TimeUnit.SECONDS); if (response.getStatus() == 200) { // Obtain the input stream on the response content try (InputStream input = listener.getInputStream()) { // Read the response content } }
The HttpClient
implementation (the producer) will feed the input stream
asynchronously while the application (the consumer) is reading from it.
If the consumer is faster than the producer, then the consumer will block
with the typical InputStream.read()
semantic.
If the consumer is slower than the producer, then the producer will block
until the client consumes.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionWaits for the given timeout for the whole request/response cycle to be finished, then returns the corresponding result.Waits for the given timeout for the response to be available, then returns it.Returns anInputStream
providing the response content bytes.void
onComplete
(Result result) Callback method invoked when the request and the response have been processed, either successfully or not.void
onContent
(Response response, Content.Chunk chunk, Runnable demander) Callback method invoked when the response content has been received, parsed and there is demand.void
Callback method invoked when the response has failed in the process of being receivedvoid
Callback method invoked when all the response headers have been received and parsed.void
Callback method invoked when the whole response has been successfully received.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.eclipse.jetty.client.Response.AsyncContentListener
onContentSource
Methods inherited from interface org.eclipse.jetty.client.Response.Listener
onBegin, onContent, onHeader
-
Constructor Details
-
InputStreamResponseListener
public InputStreamResponseListener()
-
-
Method Details
-
onHeaders
Description copied from interface:Response.HeadersListener
Callback method invoked when all the response headers have been received and parsed.- Specified by:
onHeaders
in interfaceResponse.HeadersListener
- Specified by:
onHeaders
in interfaceResponse.Listener
- Parameters:
response
- the response containing the response line data and the headers
-
onContent
Description copied from interface:Response.AsyncContentListener
Callback method invoked when the response content has been received, parsed and there is demand. Thechunk
must be consumed, copied, or retained before returning from this method as it is then automatically released. Thedemander
must be run before this method may be invoked again.- Specified by:
onContent
in interfaceResponse.AsyncContentListener
- Specified by:
onContent
in interfaceResponse.ContentListener
- Parameters:
response
- the response containing the response line data and the headerschunk
- the chunk receiveddemander
- the runnable to be run to demand the next chunk
-
onSuccess
Description copied from interface:Response.SuccessListener
Callback method invoked when the whole response has been successfully received.- Specified by:
onSuccess
in interfaceResponse.Listener
- Specified by:
onSuccess
in interfaceResponse.SuccessListener
- Parameters:
response
- the response containing the response line data and the headers
-
onFailure
Description copied from interface:Response.FailureListener
Callback method invoked when the response has failed in the process of being received- Specified by:
onFailure
in interfaceResponse.FailureListener
- Specified by:
onFailure
in interfaceResponse.Listener
- Parameters:
response
- the response containing data up to the point the failure happenedfailure
- the failure happened
-
onComplete
Description copied from interface:Response.CompleteListener
Callback method invoked when the request and the response have been processed, either successfully or not.The
result
parameter contains the request, the response, and eventual failures.Requests may complete after response, for example in case of big uploads that are discarded or read asynchronously by the server. This method is always invoked after
Response.SuccessListener.onSuccess(Response)
orResponse.FailureListener.onFailure(Response, Throwable)
, and only when request indicates that it is completed.- Specified by:
onComplete
in interfaceResponse.CompleteListener
- Specified by:
onComplete
in interfaceResponse.Listener
- Parameters:
result
- the result of the request / response exchange
-
get
public Response get(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException, ExecutionException Waits for the given timeout for the response to be available, then returns it.The wait ends as soon as all the HTTP headers have been received, without waiting for the content. To wait for the whole content, see
await(long, TimeUnit)
.- Parameters:
timeout
- the time to waitunit
- the timeout unit- Returns:
- the response
- Throws:
InterruptedException
- if the thread is interruptedTimeoutException
- if the timeout expiresExecutionException
- if a failure happened
-
await
Waits for the given timeout for the whole request/response cycle to be finished, then returns the corresponding result.- Parameters:
timeout
- the time to waitunit
- the timeout unit- Returns:
- the result
- Throws:
InterruptedException
- if the thread is interruptedTimeoutException
- if the timeout expires- See Also:
-
getInputStream
Returns anInputStream
providing the response content bytes.The method may be invoked only once; subsequent invocations will return a closed
InputStream
.- Returns:
- an input stream providing the response content
-