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
ConstructorsConstructorDescriptionBasicResource(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 voidThrows anUnsupportedOperationExceptionif 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.voidcopy()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.longReturns access to the underlying builder for the HTTP entity.Returns the list of headers associated with this resource.booleanbooleanbooleanbooleanReturnstrue 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.voidwriteTo(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
-
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.
-
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.
-
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.
-
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
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
- Specified by:
consumeContentin interfaceHttpEntity- Throws:
IOException
-
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.
-
getContent
- Specified by:
getContentin interfaceHttpEntity- Throws:
IOExceptionUnsupportedOperationException
-
getContentEncoding
- Specified by:
getContentEncodingin interfaceHttpEntity
-
getContentLength
- Specified by:
getContentLengthin interfaceHttpEntity
-
getContentType
- Specified by:
getContentTypein interfaceHttpEntity
-
getEntity
Returns access to the underlying builder for the HTTP entity.- Returns:
- The underlying builder for the HTTP entity.
-
getHeaders
Description copied from interface:HttpResourceReturns 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:
getHeadersin interfaceHttpResource- Returns:
- The list of headers associated with this resource.
-
isChunked
- Specified by:
isChunkedin interfaceHttpEntity
-
isRepeatable
- Specified by:
isRepeatablein interfaceHttpEntity
-
isStreaming
- Specified by:
isStreamingin interfaceHttpEntity
-
isUnmodifiable
Returnstrue if this bean is unmodifiable.- Returns:
true if this bean is unmodifiable.
-
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.
-
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
-
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
StringEntityuse this to allow the entity content to be resolved at serialization time.- Parameters:
value- The entity content, can benull .- 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.
-
setContentLength
Sets the content length on this entity bean.- Parameters:
value- The newContent-Length header value, or-1 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.
-
setContentType
Sets the content type on this entity bean.- Parameters:
value- The newContent-Type header, ornull to unset.- Returns:
- This object.
-
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.
-
setHeaders
Sets the specified headers in this builder.- Parameters:
values- The headers to add.null values are ignored.- Returns:
- This object.
-
setHeaders
Sets the specified headers in this builder.- Parameters:
value- The new value.- Returns:
- This object.
-
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.
-
writeTo
- Specified by:
writeToin interfaceHttpEntity- Throws:
IOException
-
assertModifiable
Throws anUnsupportedOperationExceptionif the unmodifiable flag is set on this bean.
-