@Test public void hasContentType() { assertFalse(HeaderUtils.hasContentType( EmptyHttpHeaders.INSTANCE, TEXT_PLAIN, null)); assertTrue(HeaderUtils.hasContentType( headersWithContentType(TEXT_PLAIN), TEXT_PLAIN, null)); assertFalse(HeaderUtils.hasContentType( headersWithContentType(TEXT_PLAIN), APPLICATION_JSON, null)); assertTrue(HeaderUtils.hasContentType( headersWithContentType(TEXT_PLAIN), TEXT_PLAIN, UTF_8)); assertFalse(HeaderUtils.hasContentType( headersWithContentType(TEXT_PLAIN), TEXT_PLAIN, UTF_16)); assertTrue(HeaderUtils.hasContentType( headersWithContentType(TEXT_PLAIN_UTF_8), TEXT_PLAIN, UTF_8)); assertFalse(HeaderUtils.hasContentType( headersWithContentType(TEXT_PLAIN_UTF_8), TEXT_PLAIN, US_ASCII)); assertTrue(HeaderUtils.hasContentType( headersWithContentType(APPLICATION_X_WWW_FORM_URLENCODED), APPLICATION_X_WWW_FORM_URLENCODED, UTF_8)); assertFalse(HeaderUtils.hasContentType( headersWithContentType(APPLICATION_X_WWW_FORM_URLENCODED), APPLICATION_X_WWW_FORM_URLENCODED, UTF_16)); assertTrue(HeaderUtils.hasContentType( headersWithContentType(APPLICATION_X_WWW_FORM_URLENCODED_UTF_8), APPLICATION_X_WWW_FORM_URLENCODED, UTF_8)); assertFalse(HeaderUtils.hasContentType( headersWithContentType(APPLICATION_X_WWW_FORM_URLENCODED_UTF_8), APPLICATION_X_WWW_FORM_URLENCODED, UTF_16)); assertTrue(HeaderUtils.hasContentType( headersWithContentType(of("text/plain")), TEXT_PLAIN, null)); assertTrue(HeaderUtils.hasContentType( headersWithContentType(of("Text/Plain")), TEXT_PLAIN, null)); assertTrue(HeaderUtils.hasContentType( headersWithContentType(of("Text/Plain")), TEXT_PLAIN, UTF_8)); assertTrue(HeaderUtils.hasContentType( headersWithContentType(of("text/plain;id=\"ABC@host.com\";charset=\"us-ascii\";total=2")), TEXT_PLAIN, null)); assertTrue(HeaderUtils.hasContentType( headersWithContentType(of("text/plain;id=\"ABC@host.com\";charset=utf-8;total=2")), TEXT_PLAIN, UTF_8)); assertTrue(HeaderUtils.hasContentType( headersWithContentType(of("text/plain;id=\"ABC@host.com\";charset=\"UTF-8\";total=2")), TEXT_PLAIN, UTF_8)); assertFalse(HeaderUtils.hasContentType( headersWithContentType(of("text/plain;id=\"ABC@host.com\";Charset=\"UTF-16\";total=2")), TEXT_PLAIN, UTF_8)); assertTrue(HeaderUtils.hasContentType( headersWithContentType(of("text/plain;id=\"ABC@host.com\";total=2")), TEXT_PLAIN, UTF_8)); assertFalse(HeaderUtils.hasContentType( headersWithContentType(of("text/plain;id=\"ABC@host.com\";total=2")), TEXT_PLAIN, UTF_16)); assertTrue(HeaderUtils.hasContentType( headersWithContentType(of("text/plain; charset=\"us-ascii\"")), TEXT_PLAIN, null)); assertTrue(HeaderUtils.hasContentType( headersWithContentType(of("text/plain; charset=\"us-ascii\"")), TEXT_PLAIN, US_ASCII)); assertTrue(HeaderUtils.hasContentType( headersWithContentType(of("text/plain;id=\"ABC@host.com\";charset=\"us-ascii\";total=2")), TEXT_PLAIN, US_ASCII)); assertTrue(HeaderUtils.hasContentType( headersWithContentType(of("image/png")), of("image/png"), null)); assertTrue(HeaderUtils.hasContentType( headersWithContentType(of("image/png")), of("image/png"), UTF_8)); assertFalse(HeaderUtils.hasContentType( headersWithContentType(of("image/png")), APPLICATION_X_WWW_FORM_URLENCODED, null)); assertFalse(HeaderUtils.hasContentType( headersWithContentType(of("image/png")), APPLICATION_X_WWW_FORM_URLENCODED, UTF_8)); assertTrue(HeaderUtils.hasContentType( headersWithContentType(of("image/png;charset=unknown-charset")), of("image/png"), null)); assertTrue(HeaderUtils.hasContentType( headersWithContentType(of("image/png;charset=unknown-charset")), of("image/png"), new Charset("unknown-charset", new String[0]) { @Override public boolean contains(final Charset cs) { return false; } @Override public CharsetDecoder newDecoder() { throw new UnsupportedOperationException(); } @Override public CharsetEncoder newEncoder() { throw new UnsupportedOperationException(); } })); }
public static boolean hasContentType(final HttpHeaders headers, final CharSequence expectedContentType, @Nullable final Charset expectedCharset) { final CharSequence contentTypeHeader = headers.get(CONTENT_TYPE); if (contentTypeHeader == null || contentTypeHeader.length() == 0) { return false; } if (expectedCharset == null) { if (contentEqualsIgnoreCase(expectedContentType, contentTypeHeader)) { return true; } return regionMatches(contentTypeHeader, true, 0, expectedContentType, 0, expectedContentType.length()); } if (!regionMatches(contentTypeHeader, true, 0, expectedContentType, 0, expectedContentType.length())) { return false; } if (UTF_8.equals(expectedCharset)) { if (contentTypeHeader.length() == expectedContentType.length()) { return true; } if (regionMatches(contentTypeHeader, true, expectedContentType.length(), "; charset=UTF-8", 0, 15)) { return true; } if (!hasCharset(contentTypeHeader)) { return true; } } Pattern pattern = CHARSET_PATTERNS.get(expectedCharset); if (pattern == null) { pattern = compileCharsetRegex(expectedCharset.name()); } return pattern.matcher(contentTypeHeader.subSequence(expectedContentType.length(), contentTypeHeader.length())) .matches(); }
HeaderUtils { public static boolean hasContentType(final HttpHeaders headers, final CharSequence expectedContentType, @Nullable final Charset expectedCharset) { final CharSequence contentTypeHeader = headers.get(CONTENT_TYPE); if (contentTypeHeader == null || contentTypeHeader.length() == 0) { return false; } if (expectedCharset == null) { if (contentEqualsIgnoreCase(expectedContentType, contentTypeHeader)) { return true; } return regionMatches(contentTypeHeader, true, 0, expectedContentType, 0, expectedContentType.length()); } if (!regionMatches(contentTypeHeader, true, 0, expectedContentType, 0, expectedContentType.length())) { return false; } if (UTF_8.equals(expectedCharset)) { if (contentTypeHeader.length() == expectedContentType.length()) { return true; } if (regionMatches(contentTypeHeader, true, expectedContentType.length(), "; charset=UTF-8", 0, 15)) { return true; } if (!hasCharset(contentTypeHeader)) { return true; } } Pattern pattern = CHARSET_PATTERNS.get(expectedCharset); if (pattern == null) { pattern = compileCharsetRegex(expectedCharset.name()); } return pattern.matcher(contentTypeHeader.subSequence(expectedContentType.length(), contentTypeHeader.length())) .matches(); } }
HeaderUtils { public static boolean hasContentType(final HttpHeaders headers, final CharSequence expectedContentType, @Nullable final Charset expectedCharset) { final CharSequence contentTypeHeader = headers.get(CONTENT_TYPE); if (contentTypeHeader == null || contentTypeHeader.length() == 0) { return false; } if (expectedCharset == null) { if (contentEqualsIgnoreCase(expectedContentType, contentTypeHeader)) { return true; } return regionMatches(contentTypeHeader, true, 0, expectedContentType, 0, expectedContentType.length()); } if (!regionMatches(contentTypeHeader, true, 0, expectedContentType, 0, expectedContentType.length())) { return false; } if (UTF_8.equals(expectedCharset)) { if (contentTypeHeader.length() == expectedContentType.length()) { return true; } if (regionMatches(contentTypeHeader, true, expectedContentType.length(), "; charset=UTF-8", 0, 15)) { return true; } if (!hasCharset(contentTypeHeader)) { return true; } } Pattern pattern = CHARSET_PATTERNS.get(expectedCharset); if (pattern == null) { pattern = compileCharsetRegex(expectedCharset.name()); } return pattern.matcher(contentTypeHeader.subSequence(expectedContentType.length(), contentTypeHeader.length())) .matches(); } private  HeaderUtils(); }
HeaderUtils { public static boolean hasContentType(final HttpHeaders headers, final CharSequence expectedContentType, @Nullable final Charset expectedCharset) { final CharSequence contentTypeHeader = headers.get(CONTENT_TYPE); if (contentTypeHeader == null || contentTypeHeader.length() == 0) { return false; } if (expectedCharset == null) { if (contentEqualsIgnoreCase(expectedContentType, contentTypeHeader)) { return true; } return regionMatches(contentTypeHeader, true, 0, expectedContentType, 0, expectedContentType.length()); } if (!regionMatches(contentTypeHeader, true, 0, expectedContentType, 0, expectedContentType.length())) { return false; } if (UTF_8.equals(expectedCharset)) { if (contentTypeHeader.length() == expectedContentType.length()) { return true; } if (regionMatches(contentTypeHeader, true, expectedContentType.length(), "; charset=UTF-8", 0, 15)) { return true; } if (!hasCharset(contentTypeHeader)) { return true; } } Pattern pattern = CHARSET_PATTERNS.get(expectedCharset); if (pattern == null) { pattern = compileCharsetRegex(expectedCharset.name()); } return pattern.matcher(contentTypeHeader.subSequence(expectedContentType.length(), contentTypeHeader.length())) .matches(); } private  HeaderUtils(); static boolean isTransferEncodingChunked(final HttpHeaders headers); static boolean domainMatches(final CharSequence requestDomain, @Nullable final CharSequence cookieDomain); static boolean pathMatches(final CharSequence requestPath, @Nullable final CharSequence cookiePath); static boolean isSetCookieNameMatches(final CharSequence setCookieString,
                                                 final CharSequence setCookieName); @Nullable static HttpSetCookie parseSetCookie(final CharSequence setCookieString,
                                               final CharSequence setCookieName,
                                               final boolean validate); @Nullable static HttpCookiePair parseCookiePair(final CharSequence cookieString,
                                                 final CharSequence cookiePairName); @Nullable static CharSequence removeCookiePairs(final CharSequence cookieString,
                                                 final CharSequence cookiePairName); static boolean hasContentType(final HttpHeaders headers,
                                         final CharSequence expectedContentType,
                                         @Nullable final Charset expectedCharset); }
HeaderUtils { public static boolean hasContentType(final HttpHeaders headers, final CharSequence expectedContentType, @Nullable final Charset expectedCharset) { final CharSequence contentTypeHeader = headers.get(CONTENT_TYPE); if (contentTypeHeader == null || contentTypeHeader.length() == 0) { return false; } if (expectedCharset == null) { if (contentEqualsIgnoreCase(expectedContentType, contentTypeHeader)) { return true; } return regionMatches(contentTypeHeader, true, 0, expectedContentType, 0, expectedContentType.length()); } if (!regionMatches(contentTypeHeader, true, 0, expectedContentType, 0, expectedContentType.length())) { return false; } if (UTF_8.equals(expectedCharset)) { if (contentTypeHeader.length() == expectedContentType.length()) { return true; } if (regionMatches(contentTypeHeader, true, expectedContentType.length(), "; charset=UTF-8", 0, 15)) { return true; } if (!hasCharset(contentTypeHeader)) { return true; } } Pattern pattern = CHARSET_PATTERNS.get(expectedCharset); if (pattern == null) { pattern = compileCharsetRegex(expectedCharset.name()); } return pattern.matcher(contentTypeHeader.subSequence(expectedContentType.length(), contentTypeHeader.length())) .matches(); } private  HeaderUtils(); static boolean isTransferEncodingChunked(final HttpHeaders headers); static boolean domainMatches(final CharSequence requestDomain, @Nullable final CharSequence cookieDomain); static boolean pathMatches(final CharSequence requestPath, @Nullable final CharSequence cookiePath); static boolean isSetCookieNameMatches(final CharSequence setCookieString,
                                                 final CharSequence setCookieName); @Nullable static HttpSetCookie parseSetCookie(final CharSequence setCookieString,
                                               final CharSequence setCookieName,
                                               final boolean validate); @Nullable static HttpCookiePair parseCookiePair(final CharSequence cookieString,
                                                 final CharSequence cookiePairName); @Nullable static CharSequence removeCookiePairs(final CharSequence cookieString,
                                                 final CharSequence cookiePairName); static boolean hasContentType(final HttpHeaders headers,
                                         final CharSequence expectedContentType,
                                         @Nullable final Charset expectedCharset); static final BiFunction<? super CharSequence, ? super CharSequence, CharSequence> DEFAULT_HEADER_FILTER; }