@Test public void getUncompressedContent() throws Exception { String HEX_BYTES = "E29C93"; byte[] bytes = BaseEncoding.base16().decode(HEX_BYTES); String text = new String(bytes, UTF_8); System.out.format("text: %s%n", text); MediaType contentType = MediaType.PLAIN_TEXT_UTF_8.withoutParameters(); TypedContent translation = Hars.getUncompressedContent(contentType.toString(), text, null, null, null, null, null, StandardCharsets.UTF_8); ByteSource data = translation.asByteSource(); String hexActual = BaseEncoding.base16().encode(data.read()); assertEquals("result", HEX_BYTES, hexActual); }
static TypedContent getUncompressedContent(@Nullable String contentType, @Nullable String text, @Nullable Long bodySize, @Nullable Long contentSize, @Nullable String contentEncodingHeaderValue, @Nullable String harContentEncoding, @SuppressWarnings("unused") @Nullable String comment, Charset defaultCharset) { contentType = Strings.nullToEmpty(contentType).trim(); if (contentType.isEmpty()) { contentType = MediaType.OCTET_STREAM.toString(); } MediaType mediaType; try { mediaType = MediaType.parse(contentType); } catch (RuntimeException e) { log.info("failed to parse content-type \"{}\"", StringEscapeUtils.escapeJava(contentType)); mediaType = MediaType.OCTET_STREAM; } if (text == null) { return TypedContent.identity(ByteSource.empty(), mediaType); } boolean base64 = isBase64Encoded(contentType, text, harContentEncoding, bodySize); if (base64) { ByteSource decodedDataSource = decodingSource(text, contentEncodingHeaderValue, harContentEncoding, bodySize, contentSize); return TypedContent.identity(decodedDataSource, mediaType); } else { @Nullable Charset charset_ = null; try { charset_ = MediaType.parse(contentType).charset().orNull(); } catch (RuntimeException ignore) { } Charset adjustedCharset = adjustCharset(text, charset_, defaultCharset); ByteSource data = CharSource.wrap(text).asByteSource(adjustedCharset); MediaType adjustedContentType = mediaType.withCharset(adjustedCharset); return TypedContent.identity(data, adjustedContentType); } }
Hars { static TypedContent getUncompressedContent(@Nullable String contentType, @Nullable String text, @Nullable Long bodySize, @Nullable Long contentSize, @Nullable String contentEncodingHeaderValue, @Nullable String harContentEncoding, @SuppressWarnings("unused") @Nullable String comment, Charset defaultCharset) { contentType = Strings.nullToEmpty(contentType).trim(); if (contentType.isEmpty()) { contentType = MediaType.OCTET_STREAM.toString(); } MediaType mediaType; try { mediaType = MediaType.parse(contentType); } catch (RuntimeException e) { log.info("failed to parse content-type \"{}\"", StringEscapeUtils.escapeJava(contentType)); mediaType = MediaType.OCTET_STREAM; } if (text == null) { return TypedContent.identity(ByteSource.empty(), mediaType); } boolean base64 = isBase64Encoded(contentType, text, harContentEncoding, bodySize); if (base64) { ByteSource decodedDataSource = decodingSource(text, contentEncodingHeaderValue, harContentEncoding, bodySize, contentSize); return TypedContent.identity(decodedDataSource, mediaType); } else { @Nullable Charset charset_ = null; try { charset_ = MediaType.parse(contentType).charset().orNull(); } catch (RuntimeException ignore) { } Charset adjustedCharset = adjustCharset(text, charset_, defaultCharset); ByteSource data = CharSource.wrap(text).asByteSource(adjustedCharset); MediaType adjustedContentType = mediaType.withCharset(adjustedCharset); return TypedContent.identity(data, adjustedContentType); } } }
Hars { static TypedContent getUncompressedContent(@Nullable String contentType, @Nullable String text, @Nullable Long bodySize, @Nullable Long contentSize, @Nullable String contentEncodingHeaderValue, @Nullable String harContentEncoding, @SuppressWarnings("unused") @Nullable String comment, Charset defaultCharset) { contentType = Strings.nullToEmpty(contentType).trim(); if (contentType.isEmpty()) { contentType = MediaType.OCTET_STREAM.toString(); } MediaType mediaType; try { mediaType = MediaType.parse(contentType); } catch (RuntimeException e) { log.info("failed to parse content-type \"{}\"", StringEscapeUtils.escapeJava(contentType)); mediaType = MediaType.OCTET_STREAM; } if (text == null) { return TypedContent.identity(ByteSource.empty(), mediaType); } boolean base64 = isBase64Encoded(contentType, text, harContentEncoding, bodySize); if (base64) { ByteSource decodedDataSource = decodingSource(text, contentEncodingHeaderValue, harContentEncoding, bodySize, contentSize); return TypedContent.identity(decodedDataSource, mediaType); } else { @Nullable Charset charset_ = null; try { charset_ = MediaType.parse(contentType).charset().orNull(); } catch (RuntimeException ignore) { } Charset adjustedCharset = adjustCharset(text, charset_, defaultCharset); ByteSource data = CharSource.wrap(text).asByteSource(adjustedCharset); MediaType adjustedContentType = mediaType.withCharset(adjustedCharset); return TypedContent.identity(data, adjustedContentType); } }  }
Hars { static TypedContent getUncompressedContent(@Nullable String contentType, @Nullable String text, @Nullable Long bodySize, @Nullable Long contentSize, @Nullable String contentEncodingHeaderValue, @Nullable String harContentEncoding, @SuppressWarnings("unused") @Nullable String comment, Charset defaultCharset) { contentType = Strings.nullToEmpty(contentType).trim(); if (contentType.isEmpty()) { contentType = MediaType.OCTET_STREAM.toString(); } MediaType mediaType; try { mediaType = MediaType.parse(contentType); } catch (RuntimeException e) { log.info("failed to parse content-type \"{}\"", StringEscapeUtils.escapeJava(contentType)); mediaType = MediaType.OCTET_STREAM; } if (text == null) { return TypedContent.identity(ByteSource.empty(), mediaType); } boolean base64 = isBase64Encoded(contentType, text, harContentEncoding, bodySize); if (base64) { ByteSource decodedDataSource = decodingSource(text, contentEncodingHeaderValue, harContentEncoding, bodySize, contentSize); return TypedContent.identity(decodedDataSource, mediaType); } else { @Nullable Charset charset_ = null; try { charset_ = MediaType.parse(contentType).charset().orNull(); } catch (RuntimeException ignore) { } Charset adjustedCharset = adjustCharset(text, charset_, defaultCharset); ByteSource data = CharSource.wrap(text).asByteSource(adjustedCharset); MediaType adjustedContentType = mediaType.withCharset(adjustedCharset); return TypedContent.identity(data, adjustedContentType); } }  static boolean isBase64Encoded(String contentType, String text, @Nullable String harContentEncoding, @Nullable Long bodySize); @Nullable static ByteSource getRequestPostData(@Nullable List<NameValuePair> params, String contentType, String postDataText, @Nullable Long requestBodySize, @Nullable String postDataComment, Charset defaultCharset); static TypedContent translateResponseContent(              String contentType,
                                                                      @Nullable String text,
                                                                      @Nullable Long bodySize,
                                                                      @Nullable Long contentSize,
                                                                      @Nullable String contentEncodingHeaderValue,
                                                                      @Nullable String harContentEncoding,
                                                                      @SuppressWarnings("unused") @Nullable String comment,
                                                                      Charset defaultCharset); static HarHeader newHarHeader(String name, String value); }
Hars { static TypedContent getUncompressedContent(@Nullable String contentType, @Nullable String text, @Nullable Long bodySize, @Nullable Long contentSize, @Nullable String contentEncodingHeaderValue, @Nullable String harContentEncoding, @SuppressWarnings("unused") @Nullable String comment, Charset defaultCharset) { contentType = Strings.nullToEmpty(contentType).trim(); if (contentType.isEmpty()) { contentType = MediaType.OCTET_STREAM.toString(); } MediaType mediaType; try { mediaType = MediaType.parse(contentType); } catch (RuntimeException e) { log.info("failed to parse content-type \"{}\"", StringEscapeUtils.escapeJava(contentType)); mediaType = MediaType.OCTET_STREAM; } if (text == null) { return TypedContent.identity(ByteSource.empty(), mediaType); } boolean base64 = isBase64Encoded(contentType, text, harContentEncoding, bodySize); if (base64) { ByteSource decodedDataSource = decodingSource(text, contentEncodingHeaderValue, harContentEncoding, bodySize, contentSize); return TypedContent.identity(decodedDataSource, mediaType); } else { @Nullable Charset charset_ = null; try { charset_ = MediaType.parse(contentType).charset().orNull(); } catch (RuntimeException ignore) { } Charset adjustedCharset = adjustCharset(text, charset_, defaultCharset); ByteSource data = CharSource.wrap(text).asByteSource(adjustedCharset); MediaType adjustedContentType = mediaType.withCharset(adjustedCharset); return TypedContent.identity(data, adjustedContentType); } }  static boolean isBase64Encoded(String contentType, String text, @Nullable String harContentEncoding, @Nullable Long bodySize); @Nullable static ByteSource getRequestPostData(@Nullable List<NameValuePair> params, String contentType, String postDataText, @Nullable Long requestBodySize, @Nullable String postDataComment, Charset defaultCharset); static TypedContent translateResponseContent(              String contentType,
                                                                      @Nullable String text,
                                                                      @Nullable Long bodySize,
                                                                      @Nullable Long contentSize,
                                                                      @Nullable String contentEncodingHeaderValue,
                                                                      @Nullable String harContentEncoding,
                                                                      @SuppressWarnings("unused") @Nullable String comment,
                                                                      Charset defaultCharset); static HarHeader newHarHeader(String name, String value);  }