Class Entity
- java.lang.Object
- 
- org.forgerock.http.protocol.Entity
 
- 
- All Implemented Interfaces:
- Closeable,- AutoCloseable
 
 public final class Entity extends Object implements Closeable Message content. An entity allows to access a message's content in a repeatable way. It provides various convenience methods for accessing the content, transparently handling content encoding like withnewDecodedContentReader(Charset), ornewDecodedContentInputStream(). Callingclose()on the entity fully closes the underlying stream of data.Several convenience methods are provided for accessing the entity as either byte,string,form, orJSONcontent.Respective asynchronous accessors are also available (and should be preferred): There are 2 ways to feed the Entity: either synchronously through thesetRawContentInputStream(BranchingInputStream)or asynchronously through thesetContent(Publisher). This class is not thread-safe, there is absolutely no guarantee of its behaviour if it is used in concurrent ways.
- 
- 
Field SummaryFields Modifier and Type Field Description static StringAPPLICATION_JSON_CHARSET_UTF_8The Content-Type used when setting the entity to JSON.static StringAPPLICATION_X_WWW_FORM_URLENCODEDThe Content-Type when setting the entity to form.
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes all resources associated with this entity.voidcopyDecodedContentTo(OutputStream out)Copies the decoded content of this entity to the provided writer.voidcopyDecodedContentTo(Writer out)Copies the decoded content of this entity to the provided writer.voidcopyRawContentTo(OutputStream out)Copies the raw content of this entity to the provided output stream.io.reactivex.rxjava3.core.Flowable<CharBuffer>decodedContentAsFlowable(Charset charset)Returns a publisher of chars representing decoded content of this entity, honouring thecharsetif provided.byte[]getBytes()Returns a byte array containing a copy of the decoded content of this entity.Promise<byte[],IOException>getBytesAsync()Returns a promise of the decoded entity content as a byte array.FormgetForm()Returns a copy of the "application/x-www-form-urlencoded" entity decoded as a form.Promise<Form,IOException>getFormAsync()Returns a promise of a copy of the "application/x-www-form-urlencoded" entity decoded as a form.ObjectgetJson()Returns the content of this entity decoded as a JSON object.Promise<Object,IOException>getJsonAsync()Returns a promise of the content of this entity decoded as a JSON object.io.reactivex.rxjava3.core.Flowable<ByteBuffer>getRawContentFlowable()Returns a publisher of bytes representing the raw content of this entity.InputStreamgetRawContentInputStream()Returns an input stream representing the raw content of this entity.Promise<InputStream,NeverThrowsException>getRawContentInputStreamAsync()Returns a promise of an input stream representing the raw content of this entity.StringgetString()Returns the content of this entity decoded as a string.Promise<String,IOException>getStringAsync()Returns a promise of the content of this entity decoded as a string.booleanisDecodedContentEmpty()Returnstrueif this entity's decoded content is empty.booleanisRawContentEmpty()Returnstrueif this entity's raw content is empty.InputStreamnewDecodedContentInputStream()Returns a branched input stream representing the decoded content of this entity.BufferedReadernewDecodedContentReader(Charset charset)Returns a branched reader representing the decoded content of this entity.io.reactivex.rxjava3.core.Flowable<ByteBuffer>newDecompressedContentFlowable()Returns a publisher of bytes representing the decompressed content of this entity, according to theContent-Encodingheader.voidsetBytes(byte[] value)Sets the content of this entity to the raw data contained in the provided byte array.voidsetContent(org.reactivestreams.Publisher<ByteBuffer> publisher)Sets the content of this entity to the provided publisher.voidsetEmpty()Mark this entity as being empty.voidsetForm(Form form)Sets the content of this entity to the String representation of the providedForm.voidsetJson(Object value)Sets the content of this entity to the JSON representation of the provided object.voidsetRawContentInputStream(BranchingInputStream is)Sets the content of this entity to the provided input stream.voidsetString(String value)Sets the content of this entity to the provided string.StringtoString()Returns a description of this entity content (not the content itself).
 
- 
- 
- 
Field Detail- 
APPLICATION_JSON_CHARSET_UTF_8public static final String APPLICATION_JSON_CHARSET_UTF_8 The Content-Type used when setting the entity to JSON.- See Also:
- Constant Field Values
 
 - 
APPLICATION_X_WWW_FORM_URLENCODEDpublic static final String APPLICATION_X_WWW_FORM_URLENCODED The Content-Type when setting the entity to form.- See Also:
- Constant Field Values
 
 
- 
 - 
Method Detail- 
isRawContentEmptypublic boolean isRawContentEmpty() Returnstrueif this entity's raw content is empty.- Returns:
- trueif this entity's raw content is empty.
 
 - 
isDecodedContentEmptypublic boolean isDecodedContentEmpty() Returnstrueif this entity's decoded content is empty.- Returns:
- trueif this entity's decoded content is empty.
 
 - 
setEmptypublic void setEmpty() Mark this entity as being empty.
 - 
closepublic void close() Closes all resources associated with this entity. Any open streams will be closed, and the underlying content reset back to a zero length.- Specified by:
- closein interface- AutoCloseable
- Specified by:
- closein interface- Closeable
 
 - 
copyDecodedContentTopublic void copyDecodedContentTo(OutputStream out) throws IOException Copies the decoded content of this entity to the provided writer. After the method returns it will no longer be possible to read data from this entity. This method does not push or pop branches. It does, however, decode the content according to theContent-Encodingheader if it is present in the message.- Parameters:
- out- The destination writer.
- Throws:
- IOException- If an IO error occurred while copying the decoded content.
 
 - 
copyDecodedContentTopublic void copyDecodedContentTo(Writer out) throws IOException Copies the decoded content of this entity to the provided writer. After the method returns it will no longer be possible to read data from this entity. This method does not push or pop branches. It does, however, decode the content according to theContent-EncodingandContent-Typeheaders if they are present in the message.- Parameters:
- out- The destination writer.
- Throws:
- IOException- If an IO error occurred while copying the decoded content.
 
 - 
copyRawContentTopublic void copyRawContentTo(OutputStream out) throws IOException Copies the raw content of this entity to the provided output stream. After the method returns it will no longer be possible to read data from this entity. This method does not push or pop branches nor does it perform any decoding of the raw data.- Parameters:
- out- The destination output stream.
- Throws:
- IOException- If an IO error occurred while copying the raw content.
 
 - 
getBytespublic byte[] getBytes() throws IOExceptionReturns a byte array containing a copy of the decoded content of this entity. Calling this method does not change the state of the underlying input stream. Subsequent changes to the content of this entity will not be reflected in the returned byte array, nor will changes in the returned byte array be reflected in the content.- Returns:
- A byte array containing a copy of the decoded content of this
         entity (never null).
- Throws:
- IOException- If an IO error occurred while reading the content.
 
 - 
getBytesAsyncpublic Promise<byte[],IOException> getBytesAsync() Returns a promise of the decoded entity content as a byte array.Calling this method does not change the state of the underlying input stream. Subsequent changes to the content of this entity will not be reflected in the returned byte array, nor will changes in the returned byte array be reflected in the content. The returned promise will fail with an IOExceptionif an IO error occurred while reading the content.- Returns:
- A promise of byte array containing a copy of the decoded content of this
         entity (never null).
 
 - 
getJsonpublic Object getJson() throws IOException Returns the content of this entity decoded as a JSON object. Calling this method does not change the state of the underlying input stream. Subsequent changes to the content of this entity will not be reflected in the returned JSON object, nor will changes in the returned JSON object be reflected in the content.- Returns:
- The content of this entity decoded as a JSON object, which will
         be nullonly if the content represents the JSONnullvalue.
- Throws:
- IOException- If an IO error occurred while reading the content or if the JSON is malformed.
 
 - 
getJsonAsyncpublic Promise<Object,IOException> getJsonAsync() Returns a promise of the content of this entity decoded as a JSON object.Calling this method does not change the state of the underlying input stream. Subsequent changes to the content of this entity will not be reflected in the returned JSON object, nor will changes in the returned JSON object be reflected in the content. The returned promise will fail with an IOExceptionif an IO error occurred while reading the content.- Returns:
- A promise of the content of this entity decoded as a JSON object, which will
         be nullonly if the content represents the JSONnullvalue.
 
 - 
getRawContentInputStreampublic InputStream getRawContentInputStream() Returns an input stream representing the raw content of this entity. Reading from the input stream will update the state of this entity.- Returns:
- An input stream representing the raw content of this entity.
 
 - 
getRawContentInputStreamAsyncpublic Promise<InputStream,NeverThrowsException> getRawContentInputStreamAsync() Returns a promise of an input stream representing the raw content of this entity. Reading from the input stream will update the state of this entity. ThePromiseonly completes once theInputStreamis in a state where it can be fully read without blocking/polling. EXPERIMENTAL: This method is experimental and is subject to changes or even disappear at any time. It is not recommended to use it.- Returns:
- An input stream representing the raw content of this entity.
 
 - 
getStringpublic String getString() throws IOException Returns the content of this entity decoded as a string. Calling this method does not change the state of the underlying input stream. Subsequent changes to the content of this entity will not be reflected in the returned string, nor will changes in the returned string be reflected in the content.- Returns:
- The content of this entity decoded as a string (never
         null).
- Throws:
- IOException- If an IO error occurred while reading the content.
 
 - 
getStringAsyncpublic Promise<String,IOException> getStringAsync() Returns a promise of the content of this entity decoded as a string.Calling this method does not change the state of the underlying input stream. Subsequent changes to the content of this entity will not be reflected in the returned string, nor will changes in the returned string be reflected in the content. The returned promise will fail with an IOExceptionif an IO error occurred while reading the content.- Returns:
- The content of this entity decoded as a string (never null).
 
 - 
getFormpublic Form getForm() throws IOException Returns a copy of the "application/x-www-form-urlencoded" entity decoded as a form. Modifications to the returned form are not reflected in this entity.- Returns:
- The "application/x-www-form-urlencoded" entity as a form.
- Throws:
- IOException- if the entity cannot be read entirely as a string.
- See Also:
- getFormAsync()
 
 - 
getFormAsyncpublic Promise<Form,IOException> getFormAsync() Returns a promise of a copy of the "application/x-www-form-urlencoded" entity decoded as a form. Modifications to the returned form are not reflected in this entity.The returned promise will fail with an IOExceptionif an IO error occurred while reading the content.- Returns:
- The promise of the "application/x-www-form-urlencoded" entity as a form.
- See Also:
- getStringAsync()
 
 - 
newDecodedContentInputStreampublic InputStream newDecodedContentInputStream() throws IOException Returns a branched input stream representing the decoded content of this entity. Reading from the returned input stream will NOT update the state of this entity.The entity will be decompressed based on any codings that are specified in the Content-Encodingheader.Note: The caller is responsible for calling the input stream's closemethod when it is finished reading the entity.- Returns:
- A buffered input stream for reading the decoded entity.
- Throws:
- UnsupportedEncodingException- If content encoding are not supported.
- IOException- If an IO error occurred while reading the content.
 
 - 
newDecodedContentReaderpublic BufferedReader newDecodedContentReader(Charset charset) throws IOException Returns a branched reader representing the decoded content of this entity. Reading from the returned reader will NOT update the state of this entity.The entity will be decoded and/or decompressed based on any codings that are specified in the Content-Encodingheader.If charsetis notnullthen it will be used to decode the entity, otherwise the character set specified in the message'sContent-Typeheader (if present) will be used, otherwise the defaultISO-8859-1character set.Note: The caller is responsible for calling the reader's closemethod when it is finished reading the entity.- Parameters:
- charset- The character set to decode with, or message-specified or default if- null.
- Returns:
- A buffered reader for reading the decoded entity.
- Throws:
- UnsupportedEncodingException- If content encoding or charset are not supported.
- IOException- If an IO error occurred while reading the content.
 
 - 
setBytespublic void setBytes(byte[] value) Sets the content of this entity to the raw data contained in the provided byte array. Calling this method will close any existing streams associated with the entity. Also sets theContent-Lengthheader, overwriting any existing header.Note: This method does not attempt to encode the entity based-on any codings specified in the Content-Encodingheader.- Parameters:
- value- A byte array containing the raw data.
 
 - 
setJsonpublic void setJson(Object value) Sets the content of this entity to the JSON representation of the provided object. Calling this method will close any existing streams associated with the entity. Also sets theContent-TypeandContent-Lengthheaders, overwriting any existing header.Note: This method does not attempt to encode the entity based-on any codings specified in the Content-Encodingheader.- Parameters:
- value- The object whose JSON representation is to be store in this entity.
 
 - 
setFormpublic void setForm(Form form) Sets the content of this entity to the String representation of the providedForm. Calling this method will close any existing streams associated with the entity. Also sets theContent-TypeandContent-Lengthheaders, overwriting any existing header.Note: This method does not attempt to encode the entity based on any encoding specified in the Content-Encodingheader.- Parameters:
- form- The form parameters to be stored in this entity.
 
 - 
setRawContentInputStreampublic void setRawContentInputStream(BranchingInputStream is) Sets the content of this entity to the provided input stream. Calling this method will close any existing streams associated with the entity. No headers will be set.- Parameters:
- is- The input stream.
 
 - 
setContentpublic void setContent(org.reactivestreams.Publisher<ByteBuffer> publisher) Sets the content of this entity to the provided publisher. Calling this method will close any existing streams associated with the entity. No headers will be set. EXPERIMENTAL: This method is experimental and is subject to changes or even disappear at any time. It is not recommended to use it.- Parameters:
- publisher- The publisher of bytes that will feed the Entity.
 
 - 
getRawContentFlowablepublic io.reactivex.rxjava3.core.Flowable<ByteBuffer> getRawContentFlowable() Returns a publisher of bytes representing the raw content of this entity. Reading from the flow of bytes may update update the state of this entity. EXPERIMENTAL: This method is experimental and is subject to changes or even disappear at any time. It is not recommended to use it.- Returns:
- a publisher of bytes representing the raw content of this entity.
 
 - 
newDecompressedContentFlowablepublic io.reactivex.rxjava3.core.Flowable<ByteBuffer> newDecompressedContentFlowable() Returns a publisher of bytes representing the decompressed content of this entity, according to theContent-Encodingheader. Reading from the flow of bytes will not update the state of this entity. EXPERIMENTAL: This method is experimental and is subject to changes or even disappear at any time. It is not recommended to use it. The returnedFlowablemay convey some exceptions:-  UnsupportedEncodingException: if content encoding are not supported.
-  IOException: if an IO error occurred while reading the content.
 - Returns:
- a publisher of bytes representing the decompressed content of this entity.
 
-  
 - 
decodedContentAsFlowablepublic io.reactivex.rxjava3.core.Flowable<CharBuffer> decodedContentAsFlowable(Charset charset) Returns a publisher of chars representing decoded content of this entity, honouring thecharsetif provided. Reading from the flow of bytes will not update the state of this entity.If charsetis notnullthen it will be used to decode the entity, otherwise the character set specified in the message'sContent-Typeheader (if present) will be used, otherwise the defaultISO-8859-1character set. EXPERIMENTAL: This method is experimental and is subject to changes or even disappear at any time. It is not recommended to use it. The returnedFlowablemay convey some exceptions:-  IOException: if an IO error occurred while reading the content, this transformer may fail the flowable withUnsupportedEncodingExceptionin case of unsupported content encoding
 - Parameters:
- charset- The character set to decode with, or message-specified or default if- null.
- Returns:
- a publisher of bytes representing the decompressed and decoded content of this entity.
 
-  
 - 
setStringpublic void setString(String value) Sets the content of this entity to the provided string. Calling this method will close any existing streams associated with the entity. Also sets theContent-Lengthheader, overwriting any existing header.The character set specified in the message's Content-Typeheader (if present) will be used, otherwise the defaultISO-8859-1character set.Note: This method does not attempt to encode the entity based-on any codings specified in the Content-Encodingheader.- Parameters:
- value- The string whose value is to be store in this entity.
 
 
- 
 
-