Class BasicResource
- All Implemented Interfaces:
HttpEntity
,HttpResource
- Direct Known Subclasses:
ByteArrayResource
,FileResource
,ReaderResource
,StreamResource
,StringResource
HttpResource
implementation with additional features.
Provides the following features:
- Caching.
- Fluent setters.
- Fluent assertions.
- Externally-supplied/dynamic content.
See Also:
-
Constructor Summary
ConstructorDescriptionBasicResource
(HttpResponse response) Constructor.BasicResource
(BasicHttpEntity entity) Constructor.BasicResource
(BasicResource copyFrom) Copy constructor. -
Method Summary
Modifier and TypeMethodDescriptionAppends the specified header to the end of the headers in this builder.addHeaders
(Header... values) Appends the specified headers to the end of the headers in this builder.byte[]
asBytes()
Converts the contents of the entity of this resource as a byte array.Returns an assertion on the contents of the entity of this resource.protected final void
Throws anUnsupportedOperationException
if the unmodifiable flag is set on this bean.Returns an assertion on the contents of the entity of this resource.asString()
Converts the contents of the entity of this resource as a string.void
copy()
Creates a builder for this class initialized with the contents of this bean.copyFrom
(HttpResponse response) Copies the contents of the specified HTTP response to this builder.long
Returns access to the underlying builder for the HTTP entity.Returns the list of headers associated with this resource.boolean
boolean
boolean
boolean
Returnstrue if this bean is unmodifiable.Specifies that the contents of this resource should be cached into an internal byte array so that it can be read multiple times.Sets the 'chunked' flag value totrue .setChunked
(boolean value) Sets the 'chunked' flag value.setContent
(Object value) Sets the content on this entity bean.setContent
(Supplier<?> value) Sets the content on this entity bean from a supplier.setContentEncoding
(String value) Sets the content encoding header on this entity bean.Sets the content encoding header on this entity bean.setContentLength
(long value) Sets the content length on this entity bean.setContentType
(String value) Sets the content type on this entity bean.setContentType
(ContentType value) Sets the content type on this entity bean.Sets the specified header in this builder.setHeaders
(Header... values) Sets the specified headers in this builder.setHeaders
(HeaderList value) Sets the specified headers in this builder.Specifies whether this bean should be unmodifiable.void
writeTo
(OutputStream outStream)
-
Constructor Details
-
BasicResource
Constructor.- Parameters:
entity
- The entity that makes up this resource content.
-
BasicResource
Copy constructor.- Parameters:
copyFrom
- The bean bean copied.
-
BasicResource
Constructor.This is the constructor used when parsing an HTTP response.
- Parameters:
response
- The HTTP response to copy from. Must not benull .- Throws:
IOException
- Rethrown fromHttpEntity.getContent()
.
-
-
Method Details
-
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
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
Returnstrue if this bean is unmodifiable.- Returns:
true if this bean is unmodifiable.
-
assertModifiable
Throws anUnsupportedOperationException
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 benull .- Returns:
- This object.
-
setContent
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 benull .- Returns:
- This object.
-
setContentType
Sets the content type on this entity bean.- Parameters:
value
- The newContent-Type header, ornull to unset.- Returns:
- This object.
-
setContentType
Sets the content type on this entity bean.- Parameters:
value
- The newContent-Type header, ornull to unset.- Returns:
- This object.
-
setContentLength
Sets the content length on this entity bean.- Parameters:
value
- The newContent-Length header value, or-1 to unset.- Returns:
- This object.
-
setContentEncoding
Sets the content encoding header on this entity bean.- Parameters:
value
- The newContent-Encoding header, ornull to unset.- Returns:
- This object.
-
setContentEncoding
Sets the content encoding header on this entity bean.- Parameters:
value
- The newContent-Encoding header, ornull to unset.- Returns:
- This object.
-
setChunked
Sets the 'chunked' flag value totrue .Notes:
- If the
HttpEntity.getContentLength()
method returns a negative value, the HttpClient code will always use chunked encoding.
- Returns:
- This object.
- If the
-
setChunked
Sets the 'chunked' flag value.Notes:
- If the
HttpEntity.getContentLength()
method returns a negative value, the HttpClient code will always use chunked encoding.
- Parameters:
value
- The new value for this flag.- Returns:
- This object.
- If the
-
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
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
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
Sets the specified headers in this builder.- Parameters:
values
- The headers to add.null values are ignored.- Returns:
- This object.
-
addHeaders
Appends the specified headers to the end of the headers in this builder.- Parameters:
values
- The headers to set.null headers and headers withnull names or values are ignored.- Returns:
- This object.
-
asString
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
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
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
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
- Specified by:
getContentLength
in interfaceHttpEntity
-
isRepeatable
- Specified by:
isRepeatable
in interfaceHttpEntity
-
isChunked
- Specified by:
isChunked
in interfaceHttpEntity
-
getContentType
- Specified by:
getContentType
in interfaceHttpEntity
-
getContentEncoding
- Specified by:
getContentEncoding
in interfaceHttpEntity
-
isStreaming
- Specified by:
isStreaming
in interfaceHttpEntity
-
consumeContent
- Specified by:
consumeContent
in interfaceHttpEntity
- Throws:
IOException
-
getContent
- Specified by:
getContent
in interfaceHttpEntity
- Throws:
IOException
UnsupportedOperationException
-
writeTo
- Specified by:
writeTo
in interfaceHttpEntity
- 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 , andContent-Length ).- Specified by:
getHeaders
in interfaceHttpResource
- Returns:
- The list of headers associated with this resource.
-