Class AbstractResponseListener

java.lang.Object
org.eclipse.jetty.client.AbstractResponseListener
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:
BufferingResponseListener, RetainingResponseListener

public abstract class AbstractResponseListener extends Object implements Response.Listener

Instances of this class are not reusable, so one must be allocated for each request.

The content may be retrieved from Response.Listener.onSuccess(Response) or Response.Listener.onComplete(Result) via one of the getContent*() or takeContent*() methods.

IMPORTANT: The content MUST be consumed by calling one of the getContent*() or takeContent*() methods otherwise the backing buffers may be leaked. If either of takeContentAsInputStream() or takeContentAsContentSource() is called, the content MUST be read until the end (or closed/failed appropriately) otherwise the backing buffers of the unread content may also be leaked.

  • Constructor Details

  • Method Details

    • getMaxLength

      public long getMaxLength()
    • getEncoding

      public String getEncoding()
    • getMediaType

      public String getMediaType()
    • 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, Content.Chunk chunk, Runnable demander) throws Exception
      Description copied from interface: Response.AsyncContentListener
      Callback method invoked when the response content has been received, parsed and there is demand. The chunk must be consumed, copied, or retained before returning from this method as it is then automatically released. The demander must be run before this method may be invoked again.
      Specified by:
      onContent in interface Response.AsyncContentListener
      Specified by:
      onContent in interface Response.ContentListener
      Parameters:
      response - the response containing the response line data and the headers
      chunk - the chunk received
      demander - the runnable to be run to demand the next chunk
      Throws:
      Exception - an uncaught exception will abort the response, release the chunk and fail the content source from which the chunk was read from
    • 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
    • onFailure

      public void onFailure(Response response, Throwable failure)
      Description copied from interface: Response.FailureListener
      Callback method invoked when the response has failed in the process of being received
      Specified by:
      onFailure in interface Response.FailureListener
      Specified by:
      onFailure in interface Response.Listener
      Parameters:
      response - the response containing data up to the point the failure happened
      failure - the failure happened
    • getContent

      public byte[] getContent()
      Returns:
      the content as a byte array.
      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 charset)
      Parameters:
      charset - the charset of the content bytes
      Returns:
      the content as a string, with the specified charset
      See Also:
    • getContentAsInputStream

      public InputStream getContentAsInputStream()
      Return the content as InputStream. A copy of the content is kept in memory to allow the following getContent*() and takeContent*() calls to read the content again.
      Returns:
      the content as InputStream
    • takeContentAsInputStream

      public InputStream takeContentAsInputStream()
      Take the content and return it as InputStream. Following getContent*() and takeContent*() calls will see an empty content.
      Returns:
      the content as InputStream
    • takeContentAsContentSource

      public Content.Source takeContentAsContentSource()
      Take the content and return it as Content.Source. Following getContent*() and takeContent*() calls will see an empty content.
      Returns:
      the content as Content.Source