/** * 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. * * @return binary contents of the entity body */ public byte[] getBinaryContents() { // avoid re-extracting the contents if this method is called repeatedly if (binaryContents == null) { binaryContents = HttpObjectUtil.extractBinaryHttpEntityBody(httpMessage); } return binaryContents; }
/** * 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 {@link BrowserMobHttpUtil#DEFAULT_HTTP_CHARSET}). * * @param httpMessage HTTP message to extract entity body from * @return 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 */ public static String extractHttpEntityBody(FullHttpMessage httpMessage) { Charset charset; try { charset = getCharsetFromMessage(httpMessage); } catch (UnsupportedCharsetException e) { // the declared character set is not supported, so it is impossible to decode the contents of the message. log an error and throw an exception // to alert the client code. java.nio.charset.UnsupportedCharsetException cause = e.getUnsupportedCharsetExceptionCause(); String contentTypeHeader = HttpHeaders.getHeader(httpMessage, HttpHeaders.Names.CONTENT_TYPE); log.error("Cannot retrieve text contents of message because HTTP message declares a character set that is not supported on this platform. Content type header: {}.", contentTypeHeader, cause); throw cause; } return extractHttpEntityBody(httpMessage, charset); }
/** * 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. * * @param newBinaryContents new message contents */ public void setBinaryContents(byte[] newBinaryContents) { HttpObjectUtil.replaceBinaryHttpEntityBody(httpMessage, newBinaryContents); // replaced the contents, so clear the local cache binaryContents = null; textContents = null; }
/** * 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. * * @param newContents new message contents */ public void setTextContents(String newContents) { HttpObjectUtil.replaceTextHttpEntityBody(httpMessage, newContents); // replaced the contents, so clear the local cache textContents = null; binaryContents = null; }
/** * 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. * * @return 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 */ public String getTextContents() throws java.nio.charset.UnsupportedCharsetException { // avoid re-extracting the contents if this method is called repeatedly if (textContents == null) { textContents = HttpObjectUtil.extractHttpEntityBody(httpMessage); } return textContents; }
/** * 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. * * @param newContents new message contents */ public void setTextContents(String newContents) { HttpObjectUtil.replaceTextHttpEntityBody(httpMessage, newContents); // replaced the contents, so clear the local cache textContents = null; binaryContents = null; }
/** * 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. * * @return 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 */ public String getTextContents() throws java.nio.charset.UnsupportedCharsetException { // avoid re-extracting the contents if this method is called repeatedly if (textContents == null) { textContents = HttpObjectUtil.extractHttpEntityBody(httpMessage); } return textContents; }
/** * 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 {@link BrowserMobHttpUtil#DEFAULT_HTTP_CHARSET}). * * @param httpMessage HTTP message to extract entity body from * @return 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 */ public static String extractHttpEntityBody(FullHttpMessage httpMessage) { Charset charset; try { charset = getCharsetFromMessage(httpMessage); } catch (UnsupportedCharsetException e) { // the declared character set is not supported, so it is impossible to decode the contents of the message. log an error and throw an exception // to alert the client code. java.nio.charset.UnsupportedCharsetException cause = e.getUnsupportedCharsetExceptionCause(); String contentTypeHeader = HttpHeaders.getHeader(httpMessage, HttpHeaders.Names.CONTENT_TYPE); log.error("Cannot retrieve text contents of message because HTTP message declares a character set that is not supported on this platform. Content type header: {}.", contentTypeHeader, cause); throw cause; } return extractHttpEntityBody(httpMessage, charset); }
/** * 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 {@link BrowserMobHttpUtil#DEFAULT_HTTP_CHARSET} if none is specified. * <b>Note:</b> If the charset of the message is not supported on this platform, this will throw an {@link java.nio.charset.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 * * @param message the HTTP message to manipulate * @param newContents the new entity body contents * @throws java.nio.charset.UnsupportedCharsetException if the charset in the message is not supported on this platform */ public static void replaceTextHttpEntityBody(FullHttpMessage message, String newContents) { // get the content type for this message so we can encode the newContents into a byte stream appropriately String contentTypeHeader = message.headers().get(HttpHeaders.Names.CONTENT_TYPE); Charset messageCharset; try { messageCharset = BrowserMobHttpUtil.readCharsetInContentTypeHeader(contentTypeHeader); } catch (UnsupportedCharsetException e) { java.nio.charset.UnsupportedCharsetException cause = e.getUnsupportedCharsetExceptionCause() ; log.error("Found unsupported character set in Content-Type header '{}' while attempting to replace contents of HTTP message.", contentTypeHeader, cause); throw cause; } if (messageCharset == null) { messageCharset = BrowserMobHttpUtil.DEFAULT_HTTP_CHARSET; log.warn("No character set declared in HTTP message. Replacing text using default charset {}.", messageCharset); } byte[] contentBytes = newContents.getBytes(messageCharset); replaceBinaryHttpEntityBody(message, contentBytes); }
/** * 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. * * @return binary contents of the entity body */ public byte[] getBinaryContents() { // avoid re-extracting the contents if this method is called repeatedly if (binaryContents == null) { binaryContents = HttpObjectUtil.extractBinaryHttpEntityBody(httpMessage); } return binaryContents; }
/** * 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. * * @param newContents new message contents */ public void setTextContents(String newContents) { HttpObjectUtil.replaceTextHttpEntityBody(httpMessage, newContents); // replaced the contents, so clear the local cache textContents = null; binaryContents = null; }
/** * 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. * * @return 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 */ public String getTextContents() throws java.nio.charset.UnsupportedCharsetException { // avoid re-extracting the contents if this method is called repeatedly if (textContents == null) { textContents = HttpObjectUtil.extractHttpEntityBody(httpMessage); } return textContents; }
/** * 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 {@link BrowserMobHttpUtil#DEFAULT_HTTP_CHARSET}). * * @param httpMessage HTTP message to extract entity body from * @return 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 */ public static String extractHttpEntityBody(FullHttpMessage httpMessage) { Charset charset; try { charset = getCharsetFromMessage(httpMessage); } catch (UnsupportedCharsetException e) { // the declared character set is not supported, so it is impossible to decode the contents of the message. log an error and throw an exception // to alert the client code. java.nio.charset.UnsupportedCharsetException cause = e.getUnsupportedCharsetExceptionCause(); String contentTypeHeader = HttpHeaders.getHeader(httpMessage, HttpHeaders.Names.CONTENT_TYPE); log.error("Cannot retrieve text contents of message because HTTP message declares a character set that is not supported on this platform. Content type header: {}.", contentTypeHeader, cause); throw cause; } return extractHttpEntityBody(httpMessage, charset); }
/** * 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. * * @param newBinaryContents new message contents */ public void setBinaryContents(byte[] newBinaryContents) { HttpObjectUtil.replaceBinaryHttpEntityBody(httpMessage, newBinaryContents); // replaced the contents, so clear the local cache binaryContents = null; textContents = null; }
/** * 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. * * @return binary contents of the entity body */ public byte[] getBinaryContents() { // avoid re-extracting the contents if this method is called repeatedly if (binaryContents == null) { binaryContents = HttpObjectUtil.extractBinaryHttpEntityBody(httpMessage); } return binaryContents; }
/** * 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. * * @param newBinaryContents new message contents */ public void setBinaryContents(byte[] newBinaryContents) { HttpObjectUtil.replaceBinaryHttpEntityBody(httpMessage, newBinaryContents); // replaced the contents, so clear the local cache binaryContents = null; textContents = null; }
/** * 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 {@link BrowserMobHttpUtil#DEFAULT_HTTP_CHARSET} if none is specified. * <b>Note:</b> If the charset of the message is not supported on this platform, this will throw an {@link java.nio.charset.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 * * @param message the HTTP message to manipulate * @param newContents the new entity body contents * @throws java.nio.charset.UnsupportedCharsetException if the charset in the message is not supported on this platform */ public static void replaceTextHttpEntityBody(FullHttpMessage message, String newContents) { // get the content type for this message so we can encode the newContents into a byte stream appropriately String contentTypeHeader = message.headers().get(HttpHeaders.Names.CONTENT_TYPE); Charset messageCharset; try { messageCharset = BrowserMobHttpUtil.readCharsetInContentTypeHeader(contentTypeHeader); } catch (UnsupportedCharsetException e) { java.nio.charset.UnsupportedCharsetException cause = e.getUnsupportedCharsetExceptionCause() ; log.error("Found unsupported character set in Content-Type header '{}' while attempting to replace contents of HTTP message.", contentTypeHeader, cause); throw cause; } if (messageCharset == null) { messageCharset = BrowserMobHttpUtil.DEFAULT_HTTP_CHARSET; log.warn("No character set declared in HTTP message. Replacing text using default charset {}.", messageCharset); } byte[] contentBytes = newContents.getBytes(messageCharset); replaceBinaryHttpEntityBody(message, contentBytes); }
/** * 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 {@link BrowserMobHttpUtil#DEFAULT_HTTP_CHARSET} if none is specified. * <b>Note:</b> If the charset of the message is not supported on this platform, this will throw an {@link java.nio.charset.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 * * @param message the HTTP message to manipulate * @param newContents the new entity body contents * @throws java.nio.charset.UnsupportedCharsetException if the charset in the message is not supported on this platform */ public static void replaceTextHttpEntityBody(FullHttpMessage message, String newContents) { // get the content type for this message so we can encode the newContents into a byte stream appropriately String contentTypeHeader = message.headers().get(HttpHeaders.Names.CONTENT_TYPE); Charset messageCharset; try { messageCharset = BrowserMobHttpUtil.readCharsetInContentTypeHeader(contentTypeHeader); } catch (UnsupportedCharsetException e) { java.nio.charset.UnsupportedCharsetException cause = e.getUnsupportedCharsetExceptionCause() ; log.error("Found unsupported character set in Content-Type header '{}' while attempting to replace contents of HTTP message.", contentTypeHeader, cause); throw cause; } if (messageCharset == null) { messageCharset = BrowserMobHttpUtil.DEFAULT_HTTP_CHARSET; log.warn("No character set declared in HTTP message. Replacing text using default charset {}.", messageCharset); } byte[] contentBytes = newContents.getBytes(messageCharset); replaceBinaryHttpEntityBody(message, contentBytes); }