Class HttpResourceBean
- All Implemented Interfaces:
HttpBody,HttpResource
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:
See Also:
- Since:
- 9.2.1
-
Method Summary
Modifier and TypeMethodDescriptiongetBody()Returns the underlying body.longReturns the content length in bytes, or-1if unknown.Returns the MIME content type of this body, e.g.getFirstHeader(String name) Returns the first header with the given name (case-insensitive), ornull if absent.Returns the headers associated with this resource.booleanReturnstrueif this body can be written more than once.static HttpResourceBeanCreates anHttpResourceBeanwrapping the given body with no additional headers.static HttpResourceBeanof(HttpBody body, List<HttpHeader> headers) Creates anHttpResourceBeanwrapping the given body and headers.toString()withHeader(String name, String value) Returns a new instance with the given header name and value added.withHeader(HttpHeader header) Returns a new instance with the given header added.withHeaders(List<HttpHeader> toAdd) Returns a new instance with the given headers added.withHeaders(HttpHeader... toAdd) Returns a new instance with the given headers added.voidwriteTo(OutputStream out) Writes the body content to the given output stream.
-
Method Details
-
of
Creates anHttpResourceBeanwrapping the given body with no additional headers.- Parameters:
body- The body. Must not benull .- Returns:
- A new instance. Never
null .
-
of
Creates anHttpResourceBeanwrapping the given body and headers.- Parameters:
body- The body. Must not benull .headers- The associated headers. Must not benull .- Returns:
- A new instance. Never
null .
-
withHeader
Returns a new instance with the given header added.- Parameters:
header- The header to add. Must not benull .- Returns:
- A new instance. Never
null .
-
withHeader
Returns a new instance with the given header name and value added.- Parameters:
name- The header name. Must not benull .value- The header value. May benull .- Returns:
- A new instance. Never
null .
-
withHeaders
Returns a new instance with the given headers added.- Parameters:
toAdd- The headers to add.nullentries are ignored.- Returns:
- A new instance. Never
null .
-
withHeaders
Returns a new instance with the given headers added.- Parameters:
toAdd- The headers to add.nullentries are ignored.- Returns:
- A new instance. Never
null .
-
getBody
Returns the underlying body.- Returns:
- The body. Never
null .
-
getHeaders
Description copied from interface:HttpResourceReturns 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:
getHeadersin interfaceHttpResource- Returns:
- The headers. Never
null .
-
getFirstHeader
Returns the first header with the given name (case-insensitive), ornull if absent.- Parameters:
name- The header name. Must not benull .- Returns:
- The first matching header, or
null if absent.
-
getContentType
Description copied from interface:HttpBodyReturns the MIME content type of this body, e.g."application/json; charset=UTF-8".- Specified by:
getContentTypein interfaceHttpBody- Returns:
- The content type, or
null if unknown.
-
getContentLength
Description copied from interface:HttpBodyReturns the content length in bytes, or-1if unknown.Implementations that buffer content may return the exact length; streaming implementations should return
-1to cause the transport to use chunked transfer encoding.- Specified by:
getContentLengthin interfaceHttpBody- Returns:
- The content length, or
-1if unknown.
-
isRepeatable
Description copied from interface:HttpBodyReturnstrueif this body can be written more than once.Streaming bodies backed by a one-shot
InputStreamshould returnfalse. Buffered bodies (byte arrays, strings, files) should returntrue.- Specified by:
isRepeatablein interfaceHttpBody- Returns:
trueif repeatable.
-
writeTo
Description copied from interface:HttpBodyWrites the body content to the given output stream.Implementations must not close
out.- Specified by:
writeToin interfaceHttpBody- Parameters:
out- The stream to write to. Nevernull .- Throws:
IOException- If an I/O error occurs.
-
toString
-