MediaTypeHelper { public static boolean isMatched(MediaType pattern, MediaType checkMe) { if (pattern == null || checkMe == null) { throw new IllegalArgumentException("Null arguments are not allowed"); } if (pattern.isWildcardType()) { return true; } if (pattern.getType().equalsIgnoreCase(checkMe.getType())) { if (pattern.isWildcardSubtype() || pattern.getSubtype().equalsIgnoreCase(checkMe.getSubtype())) { return true; } Matcher patternMatcher = EXT_SUBTYPE_PATTERN.matcher(pattern.getSubtype()); Matcher checkMeMatcher = EXT_SUBTYPE_PATTERN.matcher(checkMe.getSubtype()); if (patternMatcher.matches()) { String patternPrefix = patternMatcher.group(1); String patternSuffix = patternMatcher.group(2); if (!checkMeMatcher.matches()) { return checkMe.getSubtype().equalsIgnoreCase(patternPrefix) || checkMe.getSubtype().equalsIgnoreCase(patternSuffix); } String checkMePrefix = checkMeMatcher.group(1); String checkMeSuffix = checkMeMatcher.group(2); if (MEDIA_TYPE_WILDCARD.equals(patternSuffix) && patternPrefix.equalsIgnoreCase(checkMePrefix)) { return true; } if (MEDIA_TYPE_WILDCARD.equals(patternPrefix) && patternSuffix.equalsIgnoreCase(checkMeSuffix)) { return true; } } } return false; } private  MediaTypeHelper(); static Iterator<MediaType> createDescendingMediaTypeIterator(MediaType type); static List<MediaType> createConsumesList(Consumes mime); static List<MediaType> createProducesList(Produces mime); static boolean isConsume(List<MediaType> consumes, MediaType contentType); static AcceptMediaType findFistCompatibleAcceptMediaType(List<AcceptMediaType> acceptMediaTypes,
                                                                    List<MediaType> producedByResource); static boolean isCompatible(MediaType mediaTypeOne, MediaType mediaTypeTwo); static boolean isMatched(MediaType pattern, MediaType checkMe); static final MediaType WADL_TYPE; static final String EXT_PREFIX_SUBTYPE; static final Pattern EXT_SUBTYPE_PATTERN; static final Pattern EXT_SUFFIX_SUBTYPE_PATTERN; static final Pattern EXT_PREFIX_SUBTYPE_PATTERN; }
@UseDataProvider("forMatchingOfMediaTypes") @Test public void matchingOfMediaTypes(MediaType mediaTypeOne, MediaType mediaTypeTwo, boolean oneMatchedToTwo, boolean twoMatchedToOne) { assertEquals(oneMatchedToTwo, MediaTypeHelper.isMatched(mediaTypeOne, mediaTypeTwo)); assertEquals(twoMatchedToOne, MediaTypeHelper.isMatched(mediaTypeTwo, mediaTypeOne)); }
@Test(expected = IllegalArgumentException.class) public void throwsExceptionWhenFirstMediaTypeForMatchingIsNull() { MediaTypeHelper.isMatched(null, new MediaType()); }
@Test(expected = IllegalArgumentException.class) public void throwsExceptionWhenSecondMediaTypeForMatchingIsNull() { MediaTypeHelper.isMatched(new MediaType(), null); }