Package org.eclipse.jetty.client
Class BufferingResponseListener
java.lang.Object
org.eclipse.jetty.client.BufferingResponseListener
- 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
- Direct Known Subclasses:
CompletableResponseListener
,ContinueProtocolHandler.ContinueListener
,FutureResponseListener
Implementation of Response.Listener
that buffers the content up to a maximum length
specified to the constructors.
The content may be retrieved from Response.Listener.onSuccess(Response)
or onComplete(Result)
via getContent()
or getContentAsString()
.
Instances of this class are not reusable, so one must be allocated for each request.
-
Constructor Summary
ConstructorDescriptionCreates an instance with a default maximum length of 2 MiB.BufferingResponseListener
(int maxLength) Creates an instance with the given maximum length -
Method Summary
Modifier and TypeMethodDescriptionbyte[]
getContentAsString
(String encoding) getContentAsString
(Charset encoding) abstract void
onComplete
(Result result) Callback method invoked when the request and the response have been processed, either successfully or not.void
onContent
(Response response, ByteBuffer content) Callback method invoked when the response content has been received, parsed and there is demand.void
Callback method invoked when all the response headers have been received and parsed.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.ContentListener
onContent
Methods inherited from interface org.eclipse.jetty.client.Response.Listener
onBegin, onFailure, onHeader, onSuccess
-
Constructor Details
-
BufferingResponseListener
public BufferingResponseListener()Creates an instance with a default maximum length of 2 MiB. -
BufferingResponseListener
public BufferingResponseListener(int maxLength) Creates an instance with the given maximum length- Parameters:
maxLength
- the maximum length of the content
-
-
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.ContentListener
Callback method invoked when the response content has been received, parsed and there is demand. This method may be invoked multiple times, and thecontent
buffer must be consumed (or copied) before returning from this method. This method is also always invoked when content arrives as demand is automatically registered on return.- Specified by:
onContent
in interfaceResponse.ContentListener
- Specified by:
onContent
in interfaceResponse.Listener
- Parameters:
response
- the response containing the response line data and the headerscontent
- the content bytes received
-
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
-
getMediaType
-
getEncoding
-
getContent
public byte[] getContent()- Returns:
- the content as bytes
- See Also:
-
getContentAsString
- Returns:
- the content as a string, using the "Content-Type" header to detect the encoding or defaulting to UTF-8 if the encoding could not be detected.
- See Also:
-
getContentAsString
- Parameters:
encoding
- the encoding of the content bytes- Returns:
- the content as a string, with the specified encoding
- See Also:
-
getContentAsString
- Parameters:
encoding
- the encoding of the content bytes- Returns:
- the content as a string, with the specified encoding
- See Also:
-
getContentAsInputStream
- Returns:
- Content as InputStream
-