Class HttpMessageContents


  • public class HttpMessageContents
    extends java.lang.Object
    Helper class to wrap the contents of an HttpMessage. Contains convenience methods to extract and manipulate the contents of the wrapped HttpMessage. TODO: Currently this class only wraps FullHttpMessages, since it must modify the Content-Length header; determine if this may be applied to chunked messages as well
    • Constructor Summary

      Constructors 
      Constructor Description
      HttpMessageContents​(io.netty.handler.codec.http.FullHttpMessage httpMessage)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      byte[] getBinaryContents()
      Retrieves the binary contents of this message.
      java.nio.charset.Charset getCharset()
      Retrieves the character set of the entity body.
      java.lang.String getContentType()
      Retrieves the Content-Type header of this message.
      java.lang.String getTextContents()
      Retrieves the contents of this message as a String, decoded according to the message's Content-Type header.
      boolean isText()
      Returns true if this message's Content-Type header indicates that it contains a textual data type.
      void setBinaryContents​(byte[] newBinaryContents)
      Replaces the contents of the wrapped HttpMessage with the specified binary contents.
      void setTextContents​(java.lang.String newContents)
      Replaces the contents of the wrapped HttpMessage with the specified text contents, encoding them in the character set specified by the message's Content-Type header.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • HttpMessageContents

        public HttpMessageContents​(io.netty.handler.codec.http.FullHttpMessage httpMessage)
    • Method Detail

      • setTextContents

        public void setTextContents​(java.lang.String newContents)
        Replaces the contents of the wrapped HttpMessage with the specified text contents, encoding them in the character set specified by the message's Content-Type header. Note that this method does not update the Content-Type header, so if the content type will change as a result of this call, the Content-Type header should be updated before calling this method.
        Parameters:
        newContents - new message contents
      • setBinaryContents

        public void setBinaryContents​(byte[] newBinaryContents)
        Replaces the contents of the wrapped HttpMessage with the specified binary contents. Note that this method does not update the Content-Type header, so if the content type will change as a result of this call, the Content-Type header should be updated before calling this method.
        Parameters:
        newBinaryContents - new message contents
      • getTextContents

        public java.lang.String getTextContents()
                                         throws java.nio.charset.UnsupportedCharsetException
        Retrieves the contents of this message as a String, decoded according to the message's Content-Type header. This method caches the contents, so repeated calls to this method should not incur a penalty; however, modifications to the message contents outside of this class will result in stale data returned from this method.
        Returns:
        String representation of the entity body
        Throws:
        java.nio.charset.UnsupportedCharsetException - if the character set declared in the message is not supported on this platform
      • getBinaryContents

        public byte[] getBinaryContents()
        Retrieves the binary contents of this message. This method caches the contents, so repeated calls to this method should not incur a penalty; however, modifications to the message contents outside of this class will result in stale data returned from this method.
        Returns:
        binary contents of the entity body
      • getContentType

        public java.lang.String getContentType()
        Retrieves the Content-Type header of this message. If no Content-Type is present, returns the assumed default Content-Type (see BrowserUpHttpUtil.UNKNOWN_CONTENT_TYPE).
        Returns:
        the message's content type
      • getCharset

        public java.nio.charset.Charset getCharset()
                                            throws java.nio.charset.UnsupportedCharsetException
        Retrieves the character set of the entity body. If the Content-Type is not a textual type, this value is meaningless. If no character set is specified, this method will return the default ISO-8859-1 character set. If the Content-Type specifies a character set, but the character set is not supported on this platform, this method throws an UnsupportedCharsetException.
        Returns:
        the entity body's character set
        Throws:
        java.nio.charset.UnsupportedCharsetException - if the character set declared in the message is not supported on this platform
      • isText

        public boolean isText()
        Returns true if this message's Content-Type header indicates that it contains a textual data type. See BrowserUpHttpUtil.hasTextualContent(String).
        Returns:
        true if the Content-Type header is a textual type, otherwise false