Package com.browserup.bup.util
Class HttpObjectUtil
- java.lang.Object
-
- com.browserup.bup.util.HttpObjectUtil
-
public class HttpObjectUtil extends java.lang.ObjectUtility class to assist with manipulation ofHttpObjectinstances, includingHttpMessageandHttpContent.
-
-
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.StringextractHttpEntityBody(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.StringextractHttpEntityBody(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.CharsetgetCharsetFromMessage(io.netty.handler.codec.http.HttpMessage httpMessage)Derives the charset from the Content-Type header in the HttpMessage.static voidreplaceBinaryHttpEntityBody(io.netty.handler.codec.http.FullHttpMessage message, byte[] newBinaryContents)Replaces an HTTP entity body with the specified binary contents.static voidreplaceTextHttpEntityBody(io.netty.handler.codec.http.FullHttpMessage message, java.lang.String newContents)Replaces the entity body of the message with the specified contents.
-
-
-
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 usesBrowserUpHttpUtil.DEFAULT_HTTP_CHARSETif none is specified. Note: If the charset of the message is not supported on this platform, this will throw anUnsupportedCharsetException. 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 manipulatenewContents- 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 manipulatenewBinaryContents- 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 (seeBrowserUpHttpUtil.DEFAULT_HTTP_CHARSET).- Parameters:
httpContent- HTTP content object to extract the entity body fromcharset- 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 (seeBrowserUpHttpUtil.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 UnsupportedCharsetExceptionDerives 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. SeeBrowserUpHttpUtil.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
-
-