Class BasicHttpEntity

java.lang.Object
org.apache.juneau.http.entity.BasicHttpEntity
All Implemented Interfaces:
HttpEntity
Direct Known Subclasses:
ByteArrayEntity, FileEntity, ReaderEntity, SerializedEntity, StreamEntity, StringEntity

@BeanIgnore public class BasicHttpEntity extends Object implements HttpEntity
A basic HttpEntity implementation with additional features. Provides the following features:
  • Caching.
  • Fluent setters.
  • Fluent assertions.
  • Externally-supplied/dynamic content.
See Also:
  • Field Details

  • Constructor Details

  • Method Details

    • asBytes

      public byte[] asBytes() throws IOException
      Converts the contents of this entity as a byte array.

      Note that this may exhaust the content on non-repeatable, non-cached entities.

      Returns:
      The contents of this entity as a byte array.
      Throws:
      IOException - If a problem occurred while trying to read the content.
    • assertBytes

      Returns an assertion on the contents of this entity.

      Note that this may exhaust the content on non-repeatable, non-cached entities.

      Returns:
      A new fluent assertion.
      Throws:
      IOException - If a problem occurred while trying to read the byte array.
    • assertString

      Returns an assertion on the contents of this entity.

      Note that this may exhaust the content on non-repeatable, non-cached entities.

      Returns:
      A new fluent assertion.
      Throws:
      IOException - If a problem occurred while trying to read the byte array.
    • asString

      public String asString() throws IOException
      Converts the contents of this entity as a string.

      Note that this may exhaust the content on non-repeatable, non-cached entities.

      Returns:
      The contents of this entity as a string.
      Throws:
      IOException - If a problem occurred while trying to read the content.
    • consumeContent

      public void consumeContent() throws IOException
      Specified by:
      consumeContent in interface HttpEntity
      Throws:
      IOException
    • copy

      Creates a builder for this class initialized with the contents of this bean.

      Allows you to create a modifiable copy of this bean.

      Returns:
      A new builder bean.
    • getCharset

      public Charset getCharset()
      Returns the charset to use when converting to and from stream-based resources.
      Returns:
      The charset to use when converting to and from stream-based resources.
    • getContent

      Specified by:
      getContent in interface HttpEntity
      Throws:
      IOException
      UnsupportedOperationException
    • getContentEncoding

      Specified by:
      getContentEncoding in interface HttpEntity
    • getContentLength

      public long getContentLength()
      Specified by:
      getContentLength in interface HttpEntity
    • getContentType

      Specified by:
      getContentType in interface HttpEntity
    • getMaxLength

      public int getMaxLength()
      Returns the maximum number of bytes to read or write to and from stream-based resources.
      Returns:
      The maximum number of bytes to read or write to and from stream-based resources.
    • isCached

      public boolean isCached()
      Returns true if this entity is cached in-memory.
      Returns:
      true if this entity is cached in-memory.
    • isChunked

      public boolean isChunked()
      Specified by:
      isChunked in interface HttpEntity
    • isRepeatable

      public boolean isRepeatable()
      Specified by:
      isRepeatable in interface HttpEntity
    • isStreaming

      public boolean isStreaming()
      Specified by:
      isStreaming in interface HttpEntity
    • isUnmodifiable

      public boolean isUnmodifiable()
      Returns true if this bean is unmodifiable.
      Returns:
      true if this bean is unmodifiable.
    • setCached

      Specifies that the contents of this resource should be cached into an internal byte array so that it can be read multiple times.
      Returns:
      This object.
      Throws:
      IOException - If entity could not be read into memory.
    • setCharset

      Specifies the charset to use when converting to and from stream-based resources.
      Parameters:
      value - The new value. If null, UTF-8 is assumed.
      Returns:
      This object.
    • setChunked

      Sets the 'chunked' flag value to true.
      Notes:
      Returns:
      This object.
    • setChunked

      public BasicHttpEntity setChunked(boolean value)
      Sets the 'chunked' flag value.
      Notes:
      Parameters:
      value - The new value for this flag.
      Returns:
      This object.
    • setContent

      Sets the content on this entity bean.
      Parameters:
      value - The entity content, can be null.
      Returns:
      This object.
    • setContent

      public BasicHttpEntity setContent(Supplier<?> value)
      Sets the content on this entity bean from a supplier.

      Repeatable entities such as StringEntity use this to allow the entity content to be resolved at serialization time.

      Parameters:
      value - The entity content, can be null.
      Returns:
      This object.
    • setContentEncoding

      Sets the content encoding header on this entity bean.
      Parameters:
      value - The new Content-Encoding header, or null to unset.
      Returns:
      This object.
    • setContentEncoding

      Sets the content encoding header on this entity bean.
      Parameters:
      value - The new Content-Encoding header, or null to unset.
      Returns:
      This object.
    • setContentLength

      public BasicHttpEntity setContentLength(long value)
      Sets the content length on this entity bean.
      Parameters:
      value - The new Content-Length header value, or -1 to unset.
      Returns:
      This object.
    • setContentType

      Sets the content type on this entity bean.
      Parameters:
      value - The new Content-Type header, or null to unset.
      Returns:
      This object.
    • setContentType

      Sets the content type on this entity bean.
      Parameters:
      value - The new Content-Type header, or null to unset.
      Returns:
      This object.
    • setMaxLength

      public BasicHttpEntity setMaxLength(int value)
      Specifies the maximum number of bytes to read or write to and from stream-based resources.

      Implementation is not universal.

      Parameters:
      value - The new value. The default is -1 which means read everything.
      Returns:
      This object.
    • setUnmodifiable

      Specifies whether this bean should be unmodifiable.

      When enabled, attempting to set any properties on this bean will cause an UnsupportedOperationException.

      Returns:
      This object.
    • writeTo

      public void writeTo(OutputStream outStream) throws IOException
      Specified by:
      writeTo in interface HttpEntity
      Throws:
      IOException
    • asSafeBytes

      protected byte[] asSafeBytes()
      Same as asBytes() but wraps IOExceptions inside a RuntimeException.
      Returns:
      The contents of this entity as a byte array.
    • assertModifiable

      protected final void assertModifiable()
      Throws an UnsupportedOperationException if the unmodifiable flag is set on this bean.
    • contentOrElse

      protected <T> T contentOrElse(T def)
      Returns the content of this entity.
      Type Parameters:
      T - The value type.
      Parameters:
      def - The default value if null.
      Returns:
      The content object.
    • isSupplied

      protected boolean isSupplied()
      Returns true if the contents of this entity are provided through an external supplier.

      Externally supplied content generally means the content length cannot be reliably determined based on the content.

      Returns:
      true if the contents of this entity are provided through an external supplier.