Java源码示例:net.lightbody.bmp.exception.UnsupportedCharsetException

示例1
/**
 * 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);
}
 
示例2
/**
 * 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);
}
 
示例3
/**
 * 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);
}
 
示例4
/**
 * 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);
}
 
示例5
/**
 * 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);
}
 
示例6
/**
 * 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);
}
 
示例7
/**
 * 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 {@link BrowserMobHttpUtil#readCharsetInContentTypeHeader(String)}
 * for more details.
 *
 * @param httpMessage HTTP message to extract charset from
 * @return 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
 */
public static Charset getCharsetFromMessage(HttpMessage httpMessage) throws UnsupportedCharsetException {
    String contentTypeHeader = HttpHeaders.getHeader(httpMessage, HttpHeaders.Names.CONTENT_TYPE);

    Charset charset = BrowserMobHttpUtil.readCharsetInContentTypeHeader(contentTypeHeader);
    if (charset == null) {
        return BrowserMobHttpUtil.DEFAULT_HTTP_CHARSET;
    }

    return charset;
}
 
示例8
/**
 * 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;
}
 
示例9
protected void captureResponseContent(HttpResponse httpResponse, byte[] fullMessage) {
    // force binary if the content encoding is not supported
    boolean forceBinary = false;

    String contentType = HttpHeaders.getHeader(httpResponse, HttpHeaders.Names.CONTENT_TYPE);
    if (contentType == null) {
        log.warn("No content type specified in response from {}. Content will be treated as {}", originalRequest.getUri(), BrowserMobHttpUtil.UNKNOWN_CONTENT_TYPE);
        contentType = BrowserMobHttpUtil.UNKNOWN_CONTENT_TYPE;
    }

    if (responseCaptureFilter.isResponseCompressed() && !responseCaptureFilter.isDecompressionSuccessful()) {
        log.warn("Unable to decompress content with encoding: {}. Contents will be encoded as base64 binary data.", responseCaptureFilter.getContentEncoding());

        forceBinary = true;
    }

    Charset charset;
    try {
        charset = BrowserMobHttpUtil.readCharsetInContentTypeHeader(contentType);
    } catch (UnsupportedCharsetException e) {
        log.warn("Found unsupported character set in Content-Type header '{}' in HTTP response from {}. Content will not be captured in HAR.", contentType, originalRequest.getUri(), e);
        return;
    }

    if (charset == null) {
        // no charset specified, so use the default -- but log a message since this might not encode the data correctly
        charset = BrowserMobHttpUtil.DEFAULT_HTTP_CHARSET;
        log.debug("No charset specified; using charset {} to decode contents from {}", charset, originalRequest.getUri());
    }

    if (!forceBinary && BrowserMobHttpUtil.hasTextualContent(contentType)) {
        String text = BrowserMobHttpUtil.getContentAsString(fullMessage, charset);
        harEntry.getResponse().getContent().setText(text);
    } else if (dataToCapture.contains(CaptureType.RESPONSE_BINARY_CONTENT)) {
        harEntry.getResponse().getContent().setText(BaseEncoding.base64().encode(fullMessage));
        harEntry.getResponse().getContent().setEncoding("base64");
    }

    harEntry.getResponse().getContent().setSize(fullMessage.length);
}
 
示例10
/**
 * 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 {@link BrowserMobHttpUtil#readCharsetInContentTypeHeader(String)}
 * for more details.
 *
 * @param httpMessage HTTP message to extract charset from
 * @return 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
 */
public static Charset getCharsetFromMessage(HttpMessage httpMessage) throws UnsupportedCharsetException {
    String contentTypeHeader = HttpHeaders.getHeader(httpMessage, HttpHeaders.Names.CONTENT_TYPE);

    Charset charset = BrowserMobHttpUtil.readCharsetInContentTypeHeader(contentTypeHeader);
    if (charset == null) {
        return BrowserMobHttpUtil.DEFAULT_HTTP_CHARSET;
    }

    return charset;
}
 
示例11
/**
 * 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;
}
 
示例12
/**
 * 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 {@link BrowserMobHttpUtil#readCharsetInContentTypeHeader(String)}
 * for more details.
 *
 * @param httpMessage HTTP message to extract charset from
 * @return 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
 */
public static Charset getCharsetFromMessage(HttpMessage httpMessage) throws UnsupportedCharsetException {
    String contentTypeHeader = HttpHeaders.getHeader(httpMessage, HttpHeaders.Names.CONTENT_TYPE);

    Charset charset = BrowserMobHttpUtil.readCharsetInContentTypeHeader(contentTypeHeader);
    if (charset == null) {
        return BrowserMobHttpUtil.DEFAULT_HTTP_CHARSET;
    }

    return charset;
}
 
示例13
/**
 * 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;
}
 
示例14
protected void captureResponseContent(HttpResponse httpResponse, byte[] fullMessage) {
    // force binary if the content encoding is not supported
    boolean forceBinary = false;

    String contentType = HttpHeaders.getHeader(httpResponse, HttpHeaders.Names.CONTENT_TYPE);
    if (contentType == null) {
        log.warn("No content type specified in response from {}. Content will be treated as {}", originalRequest.getUri(), BrowserMobHttpUtil.UNKNOWN_CONTENT_TYPE);
        contentType = BrowserMobHttpUtil.UNKNOWN_CONTENT_TYPE;
    }

    if (responseCaptureFilter.isResponseCompressed() && !responseCaptureFilter.isDecompressionSuccessful()) {
        log.warn("Unable to decompress content with encoding: {}. Contents will be encoded as base64 binary data.", responseCaptureFilter.getContentEncoding());

        forceBinary = true;
    }

    Charset charset;
    try {
        charset = BrowserMobHttpUtil.readCharsetInContentTypeHeader(contentType);
    } catch (UnsupportedCharsetException e) {
        log.warn("Found unsupported character set in Content-Type header '{}' in HTTP response from {}. Content will not be captured in HAR.", contentType, originalRequest.getUri(), e);
        return;
    }

    if (charset == null) {
        // no charset specified, so use the default -- but log a message since this might not encode the data correctly
        charset = BrowserMobHttpUtil.DEFAULT_HTTP_CHARSET;
        log.debug("No charset specified; using charset {} to decode contents from {}", charset, originalRequest.getUri());
    }

    if (!forceBinary && BrowserMobHttpUtil.hasTextualContent(contentType)) {
        String text = BrowserMobHttpUtil.getContentAsString(fullMessage, charset);
        harEntry.getResponse().getContent().setText(text);
    } else if (dataToCapture.contains(CaptureType.RESPONSE_BINARY_CONTENT)) {
        harEntry.getResponse().getContent().setText(BaseEncoding.base64().encode(fullMessage));
        harEntry.getResponse().getContent().setEncoding("base64");
    }

    harEntry.getResponse().getContent().setSize(fullMessage.length);
}
 
示例15
protected void captureRequestContent(HttpRequest httpRequest, byte[] fullMessage) {
    if (fullMessage.length == 0) {
        return;
    }

    String contentType = HttpHeaders.getHeader(httpRequest, HttpHeaders.Names.CONTENT_TYPE);
    if (contentType == null) {
        log.warn("No content type specified in request to {}. Content will be treated as {}", httpRequest.getUri(), BrowserMobHttpUtil.UNKNOWN_CONTENT_TYPE);
        contentType = BrowserMobHttpUtil.UNKNOWN_CONTENT_TYPE;
    }

    HarPostData postData = new HarPostData();
    harEntry.getRequest().setPostData(postData);

    postData.setMimeType(contentType);

    boolean urlEncoded;
    if (contentType.startsWith(HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED)) {
        urlEncoded = true;
    } else {
        urlEncoded = false;
    }

    Charset charset;
    try {
         charset = BrowserMobHttpUtil.readCharsetInContentTypeHeader(contentType);
    } catch (UnsupportedCharsetException e) {
        log.warn("Found unsupported character set in Content-Type header '{}' in HTTP request to {}. Content will not be captured in HAR.", contentType, httpRequest.getUri(), e);
        return;
    }

    if (charset == null) {
        // no charset specified, so use the default -- but log a message since this might not encode the data correctly
        charset = BrowserMobHttpUtil.DEFAULT_HTTP_CHARSET;
        log.debug("No charset specified; using charset {} to decode contents to {}", charset, httpRequest.getUri());
    }

    if (urlEncoded) {
        String textContents = BrowserMobHttpUtil.getContentAsString(fullMessage, charset);

        QueryStringDecoder queryStringDecoder = new QueryStringDecoder(textContents, charset, false);

        ImmutableList.Builder<HarPostDataParam> paramBuilder = ImmutableList.builder();

        for (Map.Entry<String, List<String>> entry : queryStringDecoder.parameters().entrySet()) {
            for (String value : entry.getValue()) {
                paramBuilder.add(new HarPostDataParam(entry.getKey(), value));
            }
        }

        harEntry.getRequest().getPostData().setParams(paramBuilder.build());
    } else {
        //TODO: implement capture of files and multipart form data

        // not URL encoded, so let's grab the body of the POST and capture that
        String postBody = BrowserMobHttpUtil.getContentAsString(fullMessage, charset);
        harEntry.getRequest().getPostData().setText(postBody);
    }
}
 
示例16
protected void captureRequestContent(HttpRequest httpRequest, byte[] fullMessage) {
    if (fullMessage.length == 0) {
        return;
    }

    String contentType = HttpHeaders.getHeader(httpRequest, HttpHeaders.Names.CONTENT_TYPE);
    if (contentType == null) {
        log.warn("No content type specified in request to {}. Content will be treated as {}", httpRequest.getUri(), BrowserMobHttpUtil.UNKNOWN_CONTENT_TYPE);
        contentType = BrowserMobHttpUtil.UNKNOWN_CONTENT_TYPE;
    }

    HarPostData postData = new HarPostData();
    harEntry.getRequest().setPostData(postData);

    postData.setMimeType(contentType);

    boolean urlEncoded;
    if (contentType.startsWith(HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED)) {
        urlEncoded = true;
    } else {
        urlEncoded = false;
    }

    Charset charset;
    try {
         charset = BrowserMobHttpUtil.readCharsetInContentTypeHeader(contentType);
    } catch (UnsupportedCharsetException e) {
        log.warn("Found unsupported character set in Content-Type header '{}' in HTTP request to {}. Content will not be captured in HAR.", contentType, httpRequest.getUri(), e);
        return;
    }

    if (charset == null) {
        // no charset specified, so use the default -- but log a message since this might not encode the data correctly
        charset = BrowserMobHttpUtil.DEFAULT_HTTP_CHARSET;
        log.debug("No charset specified; using charset {} to decode contents to {}", charset, httpRequest.getUri());
    }

    if (urlEncoded) {
        String textContents = BrowserMobHttpUtil.getContentAsString(fullMessage, charset);

        QueryStringDecoder queryStringDecoder = new QueryStringDecoder(textContents, charset, false);

        ImmutableList.Builder<HarPostDataParam> paramBuilder = ImmutableList.builder();

        for (Map.Entry<String, List<String>> entry : queryStringDecoder.parameters().entrySet()) {
            for (String value : entry.getValue()) {
                paramBuilder.add(new HarPostDataParam(entry.getKey(), value));
            }
        }

        harEntry.getRequest().getPostData().setParams(paramBuilder.build());
    } else {
        //TODO: implement capture of files and multipart form data

        // not URL encoded, so let's grab the body of the POST and capture that
        String postBody = BrowserMobHttpUtil.getContentAsString(fullMessage, charset);
        harEntry.getRequest().getPostData().setText(postBody);
    }
}