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

public abstract class BufferingResponseListener extends Object implements Response.Listener

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 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

      public void onHeaders(Response response)
      Description copied from interface: Response.HeadersListener
      Callback method invoked when all the response headers have been received and parsed.
      Specified by:
      onHeaders in interface Response.HeadersListener
      Specified by:
      onHeaders in interface Response.Listener
      Parameters:
      response - the response containing the response line data and the headers
    • onContent

      public void onContent(Response response, ByteBuffer content)
      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 the content 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 interface Response.ContentListener
      Specified by:
      onContent in interface Response.Listener
      Parameters:
      response - the response containing the response line data and the headers
      content - the content bytes received
    • onComplete

      public abstract void onComplete(Result result)
      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) or Response.FailureListener.onFailure(Response, Throwable), and only when request indicates that it is completed.

      Specified by:
      onComplete in interface Response.CompleteListener
      Specified by:
      onComplete in interface Response.Listener
      Parameters:
      result - the result of the request / response exchange
    • getMediaType

      public String getMediaType()
    • getEncoding

      public String getEncoding()
    • getContent

      public byte[] getContent()
      Returns:
      the content as bytes
      See Also:
    • getContentAsString

      public String 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

      public String getContentAsString(String encoding)
      Parameters:
      encoding - the encoding of the content bytes
      Returns:
      the content as a string, with the specified encoding
      See Also:
    • getContentAsString

      public String getContentAsString(Charset encoding)
      Parameters:
      encoding - the encoding of the content bytes
      Returns:
      the content as a string, with the specified encoding
      See Also:
    • getContentAsInputStream

      public InputStream getContentAsInputStream()
      Returns:
      Content as InputStream