Class HttpObjectUtil


  • public class HttpObjectUtil
    extends java.lang.Object
    Utility class to assist with manipulation of HttpObject instances, including HttpMessage and HttpContent.
    • Constructor Summary

      Constructors 
      Constructor Description
      HttpObjectUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static byte[] extractBinaryHttpEntityBody​(io.netty.handler.codec.http.HttpContent httpContent)
      Extracts the binary contents from an HTTP message.
      static java.lang.String extractHttpEntityBody​(io.netty.handler.codec.http.FullHttpMessage httpMessage)
      Extracts the entity body from a FullHttpMessage, according to the character set in the message's Content-Type header.
      static java.lang.String extractHttpEntityBody​(io.netty.handler.codec.http.HttpContent httpContent, java.nio.charset.Charset charset)
      Extracts the entity body from an HTTP content object, according to the specified character set.
      static java.nio.charset.Charset getCharsetFromMessage​(io.netty.handler.codec.http.HttpMessage httpMessage)
      Derives the charset from the Content-Type header in the HttpMessage.
      static void replaceBinaryHttpEntityBody​(io.netty.handler.codec.http.FullHttpMessage message, byte[] newBinaryContents)
      Replaces an HTTP entity body with the specified binary contents.
      static void replaceTextHttpEntityBody​(io.netty.handler.codec.http.FullHttpMessage message, java.lang.String newContents)
      Replaces the entity body of the message with the specified contents.
      • Methods inherited from class java.lang.Object

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

      • HttpObjectUtil

        public HttpObjectUtil()
    • Method Detail

      • replaceTextHttpEntityBody

        public static void replaceTextHttpEntityBody​(io.netty.handler.codec.http.FullHttpMessage message,
                                                     java.lang.String newContents)
        Replaces the entity body of the message with the specified contents. Encodes the message contents according to charset in the message's Content-Type header, or uses BrowserUpHttpUtil.DEFAULT_HTTP_CHARSET if none is specified. Note: If the charset of the message is not supported on this platform, this will throw an UnsupportedCharsetException. TODO: Currently this method only works for FullHttpMessages, since it must modify the Content-Length header; determine if this may be applied to chunked messages as well
        Parameters:
        message - the HTTP message to manipulate
        newContents - the new entity body contents
        Throws:
        java.nio.charset.UnsupportedCharsetException - if the charset in the message is not supported on this platform
      • replaceBinaryHttpEntityBody

        public static void replaceBinaryHttpEntityBody​(io.netty.handler.codec.http.FullHttpMessage message,
                                                       byte[] newBinaryContents)
        Replaces an HTTP entity body with the specified binary contents. TODO: Currently this method only works for FullHttpMessages, since it must modify the Content-Length header; determine if this may be applied to chunked messages as well
        Parameters:
        message - the HTTP message to manipulate
        newBinaryContents - the new entity body contents
      • extractHttpEntityBody

        public static java.lang.String extractHttpEntityBody​(io.netty.handler.codec.http.HttpContent httpContent,
                                                             java.nio.charset.Charset charset)
        Extracts the entity body from an HTTP content object, according to the specified character set. The character set cannot be null. If the character set is not specified or is unknown, you still must specify a suitable default charset (see BrowserUpHttpUtil.DEFAULT_HTTP_CHARSET).
        Parameters:
        httpContent - HTTP content object to extract the entity body from
        charset - character set of the entity body
        Returns:
        String representation of the entity body
        Throws:
        java.lang.IllegalArgumentException - if the charset is null
      • extractHttpEntityBody

        public static java.lang.String extractHttpEntityBody​(io.netty.handler.codec.http.FullHttpMessage httpMessage)
        Extracts the entity body from a FullHttpMessage, according to the character set in the message's Content-Type header. If the Content-Type header is not present or does not specify a charset, assumes the ISO-8859-1 character set (see BrowserUpHttpUtil.DEFAULT_HTTP_CHARSET).
        Parameters:
        httpMessage - HTTP message to extract entity body from
        Returns:
        String representation of the entity body
        Throws:
        java.nio.charset.UnsupportedCharsetException - if there is a charset specified in the content-type header, but it is not supported
      • getCharsetFromMessage

        public static java.nio.charset.Charset getCharsetFromMessage​(io.netty.handler.codec.http.HttpMessage httpMessage)
                                                              throws UnsupportedCharsetException
        Derives the charset from the Content-Type header in the HttpMessage. If the Content-Type header is not present or does not contain a character set, this method returns the ISO-8859-1 character set. See BrowserUpHttpUtil.readCharsetInContentTypeHeader(String) for more details.
        Parameters:
        httpMessage - HTTP message to extract charset from
        Returns:
        the charset associated with the HTTP message, or the default charset if none is present
        Throws:
        UnsupportedCharsetException - if there is a charset specified in the content-type header, but it is not supported
      • extractBinaryHttpEntityBody

        public static byte[] extractBinaryHttpEntityBody​(io.netty.handler.codec.http.HttpContent httpContent)
        Extracts the binary contents from an HTTP message.
        Parameters:
        httpContent - HTTP content object to extract the entity body from
        Returns:
        binary contents of the HTTP message