Class AbstractBody

java.lang.Object
com.predic8.membrane.core.http.AbstractBody
Direct Known Subclasses:
Body, ChunkedBody, EmptyBody

public abstract class AbstractBody extends Object
A HTTP message body (request or response), as it is received or constructed internally by Membrane.

(Sending a body is handled by one of the AbstractBodyTransferrers.)

To read a body, use the concrete implementation ChunkedBody (iff "Transfer-Encoding: chunked" is used) or Body (iff not). To construct a body within Membrane, Body is used by some helper method like Response.ResponseBuilder.body(String).

This class supports "streaming" the body: If a HTTP message is directly forwarded by Membrane (without any component reading or changing the message's body), the incoming network stream's buffer is directly written to the output stream. This allows Membrane to perform very well in this situation.

  • Field Details

  • Constructor Details

    • AbstractBody

      public AbstractBody()
  • Method Details

    • read

      public void read() throws IOException
      Throws:
      IOException
    • discard

      public void discard() throws IOException
      Throws:
      IOException
    • markAsRead

      protected void markAsRead()
    • readLocal

      protected abstract void readLocal() throws IOException
      Throws:
      IOException
    • getContent

      public byte[] getContent() throws IOException
      Returns the body's content as a byte[] representation.

      For example, getContent() might return a byte representation of

       Wikipedia in
      
       chunks.
       
      The return value does not differ whether "Transfer-Encoding: chunked" is used or not (see Chunked Transfer Encoding ), the example above is taken from there.

      Please note that a new array is allocated when calling getContent(). If you do not need the body as one single byte[], you should therefore use getContentAsStream() instead.

      Throws:
      IOException
    • getContentAsStream

      public InputStream getContentAsStream() throws IOException
      Throws:
      IOException
    • write

      public void write(AbstractBodyTransferrer out, boolean retainCopy) throws IOException
      Throws:
      IOException
    • writeAlreadyRead

      protected abstract void writeAlreadyRead(AbstractBodyTransferrer out) throws IOException
      Throws:
      IOException
    • writeNotRead

      protected abstract void writeNotRead(AbstractBodyTransferrer out) throws IOException
      Throws:
      IOException
    • writeStreamed

      protected abstract void writeStreamed(AbstractBodyTransferrer out) throws IOException
      Is called when there are no observers that need to read the body. Streams the body without reading it
      Throws:
      IOException
    • getLength

      public int getLength() throws IOException
      Warning: Calling this method will trigger reading the body from the client, disabling "streaming". Use isRead() to determine wether the body already has been read, if necessary.
      Returns:
      the length of the return value of getContent()
      Throws:
      IOException
    • getRaw

      public byte[] getRaw() throws IOException
      Returns a reconstruction of the over-the-wire byte sequence received.

      When "Transfer-Encoding: chunked" is used (see Chunked Transfer Encoding ), the return value might be (to follow the example from Wikipedia) a byte representation of

       4
       Wiki
       5
       pedia
       E
        in
      
       chunks.
       0
       
      Throws:
      IOException
    • getRawLocal

      protected abstract byte[] getRawLocal() throws IOException
      Throws:
      IOException
    • hasRelevantObservers

      protected boolean hasRelevantObservers()
    • toString

      public String toString()
      Supposes UTF-8 encoding. Should therefore not be used for primary functionality.
      Overrides:
      toString in class Object
    • isRead

      public boolean isRead()
    • getObservers

      public List<MessageObserver> getObservers()
    • wasStreamed

      public boolean wasStreamed()
    • getTrailer

      public Header getTrailer()
    • setTrailer

      public boolean setTrailer(Header trailer)
      Returns:
      true, when the body supports trailers and the trailer was therefore set.
    • hasTrailer

      public boolean hasTrailer()