MIMEType { public static MIMEType valueOf(final String MIMEType) { Pattern pattern = Pattern.compile("([\\w-*]+)/([\\w-*+.]+);?(.*)?", Pattern.MULTILINE | Pattern.DOTALL); Matcher matcher = pattern.matcher(MIMEType); if (matcher.matches()) { Map<String, String> map = new LinkedHashMap<>(); String type = matcher.group(1); String subtype = matcher.group(2); if (matcher.groupCount() == 3) { String params = matcher.group(3); parseParams(params, map); } return new MIMEType(type, subtype, Collections.unmodifiableMap(map)); } else { throw new IllegalArgumentException("Not a valid MIMEType"); } } MIMEType(String primaryType, String subType); private  MIMEType(String primaryType, String subType, Map<String, String> parameters); MIMEType addParameter(String name, String value); String getSubType(); String getPrimaryType(); String getCharset(); @Override int hashCode(); @Override boolean equals(final Object object); boolean equalsWithoutParameters(final Object o); @Deprecated boolean equals(final Object o, final boolean includeParameters); boolean includes(MIMEType mimeType); boolean includes(String mimeType); String getParameter(String name); List<Parameter> getParameters(); @Override String toString(); static MIMEType valueOf(final String MIMEType); static MIMEType valueOf(final String primaryType, final String subType); static boolean includes(String mimeType1, String mimeType2); static final MIMEType ALL; static final MIMEType APPLICATION_OCTET_STREAM; }
@Test public void testValidMimeType() { MIMEType type = MIMEType.valueOf("foo/bar"); MIMEType newType = MIMEType.valueOf("foo", "bar"); assertEquals(type, newType); type = MIMEType.valueOf("application/vnd.collection+json"); newType = MIMEType.valueOf("application", "vnd.collection+json"); assertEquals(type, newType); }
@Test public void testInValidMimeType() { try { MIMEType.valueOf("foobar"); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } }
@Test public void testValidMimeTypeWithInvalidParameter() { try { MIMEType.valueOf("foo/bar;charset?UTF-8"); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } }