Class HttpResourceBean

java.lang.Object
org.apache.juneau.http.resource.HttpResourceBean
All Implemented Interfaces:
HttpBody, HttpResource

public final class HttpResourceBean extends Object implements HttpResource
Default immutable implementation of HttpResource: an HttpBody bundled with a set of associated headers (such as Content-Type, Content-Disposition, or Content-Language).

Useful for static-file delivery, multipart form parts, and other scenarios where headers must travel with the body.

Create instances via the static factory methods:

// Body with content type header HttpResource resource = HttpResourceBean.of(FileBody.of(myFile)) .withHeader(ContentType.of("application/pdf")) .withHeader(ContentDisposition.of("attachment; filename=\"report.pdf\""));

See Also:
Since:
9.2.1
  • Method Details

    • of

      public static HttpResourceBean of(HttpBody body)
      Creates an HttpResourceBean wrapping the given body with no additional headers.
      Parameters:
      body - The body. Must not be null.
      Returns:
      A new instance. Never null.
    • of

      public static HttpResourceBean of(HttpBody body, List<HttpHeader> headers)
      Creates an HttpResourceBean wrapping the given body and headers.
      Parameters:
      body - The body. Must not be null.
      headers - The associated headers. Must not be null.
      Returns:
      A new instance. Never null.
    • withHeader

      Returns a new instance with the given header added.
      Parameters:
      header - The header to add. Must not be null.
      Returns:
      A new instance. Never null.
    • withHeader

      public HttpResourceBean withHeader(String name, String value)
      Returns a new instance with the given header name and value added.
      Parameters:
      name - The header name. Must not be null.
      value - The header value. May be null.
      Returns:
      A new instance. Never null.
    • withHeaders

      Returns a new instance with the given headers added.
      Parameters:
      toAdd - The headers to add. null entries are ignored.
      Returns:
      A new instance. Never null.
    • withHeaders

      Returns a new instance with the given headers added.
      Parameters:
      toAdd - The headers to add. null entries are ignored.
      Returns:
      A new instance. Never null.
    • getBody

      public HttpBody getBody()
      Returns the underlying body.
      Returns:
      The body. Never null.
    • getHeaders

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

      The returned list may include body-specific headers (Content-Type, Content-Length) or arbitrary application headers (e.g. Cache-Control, Content-Disposition).

      Specified by:
      getHeaders in interface HttpResource
      Returns:
      The headers. Never null.
    • getFirstHeader

      Returns the first header with the given name (case-insensitive), or null if absent.
      Parameters:
      name - The header name. Must not be null.
      Returns:
      The first matching header, or null if absent.
    • getContentType

      Description copied from interface: HttpBody
      Returns the MIME content type of this body, e.g. "application/json; charset=UTF-8".
      Specified by:
      getContentType in interface HttpBody
      Returns:
      The content type, or null if unknown.
    • getContentLength

      public long getContentLength()
      Description copied from interface: HttpBody
      Returns the content length in bytes, or -1 if unknown.

      Implementations that buffer content may return the exact length; streaming implementations should return -1 to cause the transport to use chunked transfer encoding.

      Specified by:
      getContentLength in interface HttpBody
      Returns:
      The content length, or -1 if unknown.
    • isRepeatable

      public boolean isRepeatable()
      Description copied from interface: HttpBody
      Returns true if this body can be written more than once.

      Streaming bodies backed by a one-shot InputStream should return false. Buffered bodies (byte arrays, strings, files) should return true.

      Specified by:
      isRepeatable in interface HttpBody
      Returns:
      true if repeatable.
    • writeTo

      public void writeTo(OutputStream out) throws IOException
      Description copied from interface: HttpBody
      Writes the body content to the given output stream.

      Implementations must not close out.

      Specified by:
      writeTo in interface HttpBody
      Parameters:
      out - The stream to write to. Never null.
      Throws:
      IOException - If an I/O error occurs.
    • toString

      public String toString()
      Overrides:
      toString in class Object