Class BasicResource

java.lang.Object
org.apache.juneau.http.resource.BasicResource
All Implemented Interfaces:
HttpEntity, HttpResource
Direct Known Subclasses:
ByteArrayResource, FileResource, ReaderResource, StreamResource, StringResource

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

  • Method Details

    • copy

      public BasicResource 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.
    • copyFrom

      public BasicResource copyFrom(HttpResponse response) throws IOException
      Copies the contents of the specified HTTP response to this builder.
      Parameters:
      response - The response to copy from. Must not be null.
      Returns:
      This object.
      Throws:
      IOException - If content could not be retrieved.
    • 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.
    • isUnmodifiable

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

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

      Returns access to the underlying builder for the HTTP entity.
      Returns:
      The underlying builder for the HTTP entity.
    • setContent

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

      public BasicResource 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.
    • 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.
    • setContentLength

      public BasicResource 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.
    • 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.
    • setChunked

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

      public BasicResource setChunked(boolean value)
      Sets the 'chunked' flag value.
      Notes:
      Parameters:
      value - The new value for this flag.
      Returns:
      This object.
    • 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.
    • setHeader

      public BasicResource setHeader(String name, String value)
      Sets the specified header in this builder.

      This is a no-op if either the name or value is null.

      Parameters:
      name - The header name.
      value - The header value.
      Returns:
      This object.
    • addHeader

      public BasicResource addHeader(String name, String value)
      Appends the specified header to the end of the headers in this builder.

      This is a no-op if either the name or value is null.

      Parameters:
      name - The header name.
      value - The header value.
      Returns:
      This object.
    • setHeaders

      Sets the specified headers in this builder.
      Parameters:
      value - The new value.
      Returns:
      This object.
    • setHeaders

      public BasicResource setHeaders(Header... values)
      Sets the specified headers in this builder.
      Parameters:
      values - The headers to add. null values are ignored.
      Returns:
      This object.
    • addHeaders

      public BasicResource addHeaders(Header... values)
      Appends the specified headers to the end of the headers in this builder.
      Parameters:
      values - The headers to set. null headers and headers with null names or values are ignored.
      Returns:
      This object.
    • asString

      public String asString() throws IOException
      Converts the contents of the entity of this resource 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.
    • asBytes

      public byte[] asBytes() throws IOException
      Converts the contents of the entity of this resource 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.
    • assertString

      public org.apache.juneau.assertions.FluentStringAssertion<BasicResource> assertString() throws IOException
      Returns an assertion on the contents of the entity of this resource.

      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.
    • assertBytes

      public org.apache.juneau.assertions.FluentByteArrayAssertion<BasicResource> assertBytes() throws IOException
      Returns an assertion on the contents of the entity of this resource.

      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.
    • getContentLength

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

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

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

      Specified by:
      getContentType in interface HttpEntity
    • getContentEncoding

      Specified by:
      getContentEncoding in interface HttpEntity
    • isStreaming

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

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

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

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

      Description copied from interface: HttpResource
      Returns the list of headers associated with this resource.

      Note that this typically does NOT include headers associated with HttpEntity (e.g. Content-Type, Content-Encoding, and Content-Length).

      Specified by:
      getHeaders in interface HttpResource
      Returns:
      The list of headers associated with this resource.