/** * 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 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 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); }