Class BasicResource<SELF extends BasicResource<SELF>>

java.lang.Object
org.apache.juneau.http.classic.resource.BasicResource<SELF>
Type Parameters:
SELF - The self type for fluent setters.
All Implemented Interfaces:
HttpEntity, HttpResource
Direct Known Subclasses:
ByteArrayResource, FileResource, ReaderResource, StreamResource, StringResource

@BeanIgnore public abstract class BasicResource<SELF extends BasicResource<SELF>> extends Object implements HttpResource
A basic HttpResource implementation with additional features. Provides the following features:
  • Caching.
  • Fluent setters.
  • Fluent assertions.
  • Externally-supplied/dynamic content.

This is a self-typed (CRTP) root: a leaf declares class StringResource extends BasicResource<StringResource> and inherits leaf-typed fluent setters with no covariant overrides.

See Also:
  • Constructor Details

  • Method Details

    • addHeader

      public SELF 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.
    • addHeaders

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

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

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

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

      public abstract SELF 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 SELF 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.
    • 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
    • getEntity

      Returns access to the underlying builder for the HTTP entity.
      Returns:
      The underlying builder for the HTTP entity.
    • 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.
    • 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

      public SELF setCached() throws IOException
      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.
    • setChunked

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

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

      public SELF setContent(Object value)
      Sets the content on this entity bean.
      Parameters:
      value - The entity content. Can be null (the effect depends on the concrete leaf type: e.g. treated as empty for StringEntity/ByteArrayEntity, or a NullPointerException on read for StreamEntity/ReaderEntity/FileEntity).
      Returns:
      This object.
    • setContent

      public SELF 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 (treated as a supplier that always returns 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

      public SELF setContentEncoding(String value)
      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 SELF 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

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

      public SELF 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.
    • setHeaders

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

      public SELF setHeaders(HeaderList value)
      Sets the specified headers in this builder.
      Parameters:
      value - The new value. Must not be null.
      Returns:
      This object.
    • unmodifiable

      public abstract SELF unmodifiable()
      Returns an unmodifiable snapshot of this bean.

      Idempotency: if this bean is already an unmodifiable snapshot, it is returned as-is.

      Returns:
      An unmodifiable snapshot of this bean, or this bean if it is already unmodifiable.
    • writeTo

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

      protected final SELF self()
      Returns this object cast to the self type.
      Returns:
      This object.
    • modify

      protected SELF modify(Runnable mutation)
      Single mutation funnel for direct-field state changes on this bean.

      Each leaf's nested Unmodifiable snapshot overrides this method to throw. Mutators that delegate to the entity or header sub-beans are frozen by the freeze() deep-freeze of those sub-beans; this funnel additionally covers the direct-field setHeaders(HeaderList) reassignment.

      Parameters:
      mutation - The state change to apply.
      Returns:
      This object.
    • freeze

      protected void freeze()
      Deep-freeze hook invoked from a leaf's nested Unmodifiable constructor after the snapshot copy completes.

      The mutable sub-beans are frozen here by direct field assignment (never via a setter, which would route through the throwing modify(Runnable) / frozen sub-beans and blow up during construction).