Class HttpReceiver

java.lang.Object
org.eclipse.jetty.client.transport.HttpReceiver
All Implemented Interfaces:
org.eclipse.jetty.util.thread.Invocable

public abstract class HttpReceiver extends Object implements org.eclipse.jetty.util.thread.Invocable
HttpReceiver provides the abstract code to implement the various steps of the receive of HTTP responses.

HttpReceiver maintains a state machine that is updated when the steps of receiving a response are executed.

Subclasses must handle the transport-specific details, for example how to read from the raw socket and how to parse the bytes read from the socket. Then they have to call the methods defined in this class in the following order:

  1. responseBegin(HttpExchange), when the HTTP response data containing the HTTP status code is available
  2. responseHeader(HttpExchange, HttpField), when an HTTP field is available
  3. responseHeaders(HttpExchange), when all HTTP headers are available
  4. responseSuccess(Runnable), when the response is successful
At any time, subclasses may invoke responseFailure(Throwable, Promise) to indicate that the response has failed (for example, because of I/O exceptions). At any time, user threads may abort the response which will cause responseFailure(Throwable, Promise) to be invoked.

The state machine maintained by this class ensures that the response steps are not executed by an I/O thread if the response has already been failed.

See Also:
  • Nested Class Summary

    Nested classes/interfaces inherited from interface org.eclipse.jetty.util.thread.Invocable

    org.eclipse.jetty.util.thread.Invocable.Callable, org.eclipse.jetty.util.thread.Invocable.InvocationType, org.eclipse.jetty.util.thread.Invocable.ReadyTask, org.eclipse.jetty.util.thread.Invocable.Task
  • Field Summary

    Fields inherited from interface org.eclipse.jetty.util.thread.Invocable

    __nonBlocking, NOOP
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    abort(HttpExchange exchange, Throwable failure, org.eclipse.jetty.util.Promise<Boolean> promise)
     
    protected void
    Disposes the state of this HttpReceiver.
    protected abstract void
    Fails the receiver and closes the underlying stream.
    protected HttpChannel
     
    protected HttpDestination
     
    protected HttpExchange
     
    org.eclipse.jetty.util.thread.Invocable.InvocationType
     
    protected boolean
     
    boolean
     
    protected abstract void
    Prepare for the next step after an interim response was read.
    protected abstract org.eclipse.jetty.io.Content.Chunk
    read(boolean fillInterestIfNeeded)
    Reads a chunk of data.
    protected void
    Resets the state of this HttpReceiver.
    protected void
    Method to be invoked when the response status code is available.
    protected void
    Method to be invoked when response content is available to be read.
    protected void
    responseFailure(Throwable failure, org.eclipse.jetty.util.Promise<Boolean> promise)
    Method to be invoked when the response is failed.
    protected void
    responseHeader(HttpExchange exchange, org.eclipse.jetty.http.HttpField field)
    Method to be invoked when a response HTTP header is available.
    protected void
    Method to be invoked after all response HTTP headers are available.
    protected void
    responseSuccess(Runnable afterSuccessTask)
    Method to be invoked when the response is successful.
    protected void
    storeCookie(URI uri, org.eclipse.jetty.http.HttpField field)
     
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • HttpReceiver

      protected HttpReceiver(HttpChannel channel)
  • Method Details

    • read

      protected abstract org.eclipse.jetty.io.Content.Chunk read(boolean fillInterestIfNeeded)
      Reads a chunk of data.

      If no data was read, null is returned and if fillInterestIfNeeded is true then fill interest is registered.

      The returned chunk of data may be the last one or an error exactly like Content.Source.read() specifies.

      Parameters:
      fillInterestIfNeeded - true to register for fill interest when no data was read.
      Returns:
      the chunk of data that was read, or null if nothing was read.
    • onInterim

      protected abstract void onInterim()
      Prepare for the next step after an interim response was read.
    • failAndClose

      protected abstract void failAndClose(Throwable failure)
      Fails the receiver and closes the underlying stream.
      Parameters:
      failure - the failure.
    • getHttpChannel

      protected HttpChannel getHttpChannel()
    • getHttpExchange

      protected HttpExchange getHttpExchange()
    • getHttpDestination

      protected HttpDestination getHttpDestination()
    • isFailed

      public boolean isFailed()
    • hasContent

      protected boolean hasContent()
    • responseBegin

      protected void responseBegin(HttpExchange exchange)
      Method to be invoked when the response status code is available.

      Subclasses must have set the response status code on the Response object of the HttpExchange prior invoking this method.

      This method takes case of notifying Response.BeginListeners.

      Parameters:
      exchange - the HTTP exchange
    • responseHeader

      protected void responseHeader(HttpExchange exchange, org.eclipse.jetty.http.HttpField field)
      Method to be invoked when a response HTTP header is available.

      Subclasses must not have added the header to the Response object of the HttpExchange prior invoking this method.

      This method takes case of notifying Response.HeaderListeners and storing cookies.

      Parameters:
      exchange - the HTTP exchange
      field - the response HTTP field
    • storeCookie

      protected void storeCookie(URI uri, org.eclipse.jetty.http.HttpField field)
    • responseHeaders

      protected void responseHeaders(HttpExchange exchange)
      Method to be invoked after all response HTTP headers are available.

      This method takes care of notifying Response.HeadersListeners.

      Parameters:
      exchange - the HTTP exchange
    • responseContentAvailable

      protected void responseContentAvailable(HttpExchange exchange)
      Method to be invoked when response content is available to be read.

      This method takes care of ensuring the Content.Source passed to Response.ContentSourceListener.onContentSource(Response, Content.Source) calls the demand callback.

    • responseSuccess

      protected void responseSuccess(Runnable afterSuccessTask)
      Method to be invoked when the response is successful.

      This method takes care of notifying Response.SuccessListeners and possibly Response.CompleteListeners (if the exchange is completed).

      Parameters:
      afterSuccessTask - an optional task to invoke afterwards
    • responseFailure

      protected void responseFailure(Throwable failure, org.eclipse.jetty.util.Promise<Boolean> promise)
      Method to be invoked when the response is failed.

      This method takes care of notifying Response.FailureListeners.

      Parameters:
      failure - the response failure
    • getInvocationType

      public org.eclipse.jetty.util.thread.Invocable.InvocationType getInvocationType()
      Specified by:
      getInvocationType in interface org.eclipse.jetty.util.thread.Invocable
    • reset

      protected void reset()
      Resets the state of this HttpReceiver.

      Subclasses should override (but remember to call super) to reset their own state.

      Either this method or dispose() is called.

    • dispose

      protected void dispose()
      Disposes the state of this HttpReceiver.

      Subclasses should override (but remember to call super) to dispose their own state.

      Either this method or reset() is called.

    • abort

      public void abort(HttpExchange exchange, Throwable failure, org.eclipse.jetty.util.Promise<Boolean> promise)
    • toString

      public String toString()
      Overrides:
      toString in class Object