Package org.apache.juneau.http.entity
Class BasicHttpEntity
java.lang.Object
org.apache.juneau.http.entity.BasicHttpEntity
- All Implemented Interfaces:
HttpEntity
- Direct Known Subclasses:
ByteArrayEntity
,FileEntity
,ReaderEntity
,SerializedEntity
,StreamEntity
,StringEntity
A basic
HttpEntity
implementation with additional features.
Provides the following features:
- Caching.
- Fluent setters.
- Fluent assertions.
- Externally-supplied/dynamic content.
See Also:
-
Field Summary
-
Constructor Summary
ConstructorDescriptionConstructor.BasicHttpEntity
(BasicHttpEntity copyFrom) Copy constructor.BasicHttpEntity
(ContentType contentType, Object content) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]
asBytes()
Converts the contents of this entity as a byte array.protected byte[]
Returns an assertion on the contents of this entity.protected final void
Throws anUnsupportedOperationException
if the unmodifiable flag is set on this bean.Returns an assertion on the contents of this entity.asString()
Converts the contents of this entity as a string.void
protected <T> T
contentOrElse
(T def) Returns the content of this entity.copy()
Creates a builder for this class initialized with the contents of this bean.Returns the charset to use when converting to and from stream-based resources.long
int
Returns the maximum number of bytes to read or write to and from stream-based resources.boolean
isCached()
Returnstrue if this entity is cached in-memory.boolean
boolean
boolean
protected boolean
Returnstrue if the contents of this entity are provided through an external supplier.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.setCharset
(Charset value) Specifies the charset to use when converting to and from stream-based resources.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.setMaxLength
(int value) Specifies the maximum number of bytes to read or write to and from stream-based resources.Specifies whether this bean should be unmodifiable.void
writeTo
(OutputStream outStream)
-
Field Details
-
EMPTY
An empty HttpEntity.
-
-
Constructor Details
-
BasicHttpEntity
public BasicHttpEntity()Constructor. -
BasicHttpEntity
Constructor.- Parameters:
contentType
- The entity content type.content
- The entity content.
-
BasicHttpEntity
Copy constructor.- Parameters:
copyFrom
- The bean being copied.
-
-
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.
-
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. -
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.
-
isCached
Returnstrue if this entity is cached in-memory.- Returns:
true if this entity is cached in-memory.
-
setCharset
Specifies the charset to use when converting to and from stream-based resources.- Parameters:
value
- The new value. Ifnull ,UTF-8 is assumed.- Returns:
- This object.
-
getCharset
Returns the charset to use when converting to and from stream-based resources.- Returns:
- The charset to use when converting to and from stream-based resources.
-
setMaxLength
Specifies the maximum number of bytes to read or write to and from stream-based resources.Implementation is not universal.
- Parameters:
value
- The new value. The default is-1 which means read everything.- Returns:
- This object.
-
getMaxLength
Returns the maximum number of bytes to read or write to and from stream-based resources.- Returns:
- The maximum number of bytes to read or write to and from stream-based resources.
-
asString
Converts the contents of this entity 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 this entity 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.
-
asSafeBytes
- Returns:
- The contents of this entity as a byte array.
-
assertString
Returns an assertion on the contents of this entity.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 this entity.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.
-
contentOrElse
Returns the content of this entity.- Type Parameters:
T
- The value type.- Parameters:
def
- The default value ifnull .- Returns:
- The content object.
-
isSupplied
Returnstrue if the contents of this entity are provided through an external supplier.Externally supplied content generally means the content length cannot be reliably determined based on the content.
- Returns:
true if the contents of this entity are provided through an external supplier.
-
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
-