Class BasicHttpEntity<SELF extends BasicHttpEntity<SELF>>
- Type Parameters:
SELF- The self type for fluent setters.
- All Implemented Interfaces:
HttpEntity
- Direct Known Subclasses:
ByteArrayEntity,FileEntity,ReaderEntity,SerializedEntity,StreamEntity,StringEntity
HttpEntity implementation with additional features.
Provides the following features:
- Caching.
- Fluent setters.
- Fluent assertions.
- Externally-supplied/dynamic content.
Immutability is expressed with the "funnel + nested Unmodifiable snapshot" paradigm: every fluent setter
routes through the single protected modify(Runnable) choke-point, and each concrete leaf gets a nested
X.Unmodifiable whose only behavioral override is a throwing modify(...). A leaf's
unmodifiable() factory returns a point-in-time snapshot of type X.Unmodifiable; any attempt to set a
property on it throws an UnsupportedOperationException.
This is a self-typed (CRTP) root: a leaf declares
Notes:
- Beans are not thread safe unless they're marked as unmodifiable.
See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConstructor.protectedBasicHttpEntity(BasicHttpEntity<?> copyFrom) Copy constructor.protectedBasicHttpEntity(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.Returns an assertion on the contents of this entity.asString()Converts the contents of this entity as a string.voidprotected <T> TcontentOrElse(T def) Returns the content of this entity.abstract SELFcopy()Creates a builder for this class initialized with the contents of this bean.booleanCompares the shared base content of two entities.protected voidfreeze()Deep-freeze hook invoked from the nestedUnmodifiableconstructor after the snapshot copy completes.Returns the charset to use when converting to and from stream-based resources.longintReturns the maximum number of bytes to read or write to and from stream-based resources.inthashCode()booleanisCached()Returnstrue if this entity is cached in-memory.booleanbooleanbooleanprotected booleanReturnstrue if the contents of this entity are provided through an external supplier.booleanReturnstrue if this bean is unmodifiable.protected SELFSingle mutation funnel — the only choke-point through which all state changes on this bean flow.protected final SELFself()Returns this object cast to the self type.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.abstract SELFReturns an unmodifiable snapshot of this bean.voidwriteTo(OutputStream outStream)
-
Field Details
-
EMPTY
An empty HttpEntity.
-
-
Constructor Details
-
BasicHttpEntity
protected BasicHttpEntity()Constructor. -
BasicHttpEntity
Copy constructor.- Parameters:
copyFrom- The bean being copied. Must not benull .
-
BasicHttpEntity
Constructor.- Parameters:
contentType- The entity content type. Can benull to omit an explicitContent-Type header.content- The entity content. Can benull (the effect depends on the concrete leaf type: e.g. treated as empty forStringEntity/ByteArrayEntity, or aNullPointerExceptionon read forStreamEntity/ReaderEntity/FileEntity).
-
-
Method Details
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
getContent
- Specified by:
getContentin interfaceHttpEntity- Throws:
IOExceptionUnsupportedOperationException
-
getContentEncoding
- Specified by:
getContentEncodingin interfaceHttpEntity
-
getContentLength
- Specified by:
getContentLengthin interfaceHttpEntity
-
getContentType
- Specified by:
getContentTypein interfaceHttpEntity
-
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.
-
isCached
Returnstrue if this entity is cached in-memory.- Returns:
true if this entity is cached in-memory.
-
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 anUnmodifiableBeansnapshot.
-
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.
-
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.
-
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 (the effect depends on the concrete leaf type: e.g. treated as empty forStringEntity/ByteArrayEntity, or aNullPointerExceptionon read forStreamEntity/ReaderEntity/FileEntity).- 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 (treated as a supplier that always returnsnull ).- 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.
-
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.
-
unmodifiable
Returns an unmodifiable snapshot of this bean.The returned instance is a point-in-time copy of the leaf's nested
Unmodifiabletype; any attempt to set a property on it throws anUnsupportedOperationException. This method is idempotent: if this bean is already unmodifiable it returns itself rather than taking another snapshot. The receiver is left unchanged (and still modifiable unless it was already unmodifiable).- Returns:
- An unmodifiable snapshot of this bean, or this bean if it is already unmodifiable.
-
writeTo
- Specified by:
writeToin interfaceHttpEntity- Throws:
IOException
-
equals
Compares the shared base content of two entities.This base implementation compares only the fields declared on
BasicHttpEntityand deliberately accepts anyBasicHttpEntitysubtype so that concrete leaves can delegate to it viasuper.equals(...). It does not gate on the concrete leaf type; each concrete leaf overridesequalsto first apply aninstanceof <ThatLeaf>gate (so two unrelated leaves with equal base content are not equal) and then callssuper.equals(...)to compare the inherited content. Usinginstanceof <ThatLeaf>(rather thangetClass() ==) is what preserves the immutability invariant thatbean.equals(bean.unmodifiable())holds, since eachX.Unmodifiablesnapshot is a subclass of its leafX. -
hashCode
-
asSafeBytes
- Returns:
- The contents of this entity as a 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.
-
self
Returns this object cast to the self type.- Returns:
- This object.
-
modify
Single mutation funnel — the only choke-point through which all state changes on this bean flow.Every fluent setter routes through this method. The nested
Unmodifiablesnapshot overrides only this method to throw, which freezes the entire mutation surface with a single override.- Parameters:
mutation- The state change to apply.- Returns:
- This object.
-
freeze
Deep-freeze hook invoked from the nestedUnmodifiableconstructor after the snapshot copy completes.Entities hold no mutable sub-beans (the content-type / content-encoding headers are immutable value objects, and the content buffer follows the copy-constructor's reference-copy behavior), so this is a documented no-op — the single throwing
modify(Runnable)override on the nestedUnmodifiablealready freezes the entire mutation surface. A leaf that adds its own mutable sub-bean would override this method (callingsuper.freeze()first).
-