/** * Indicates whether the {@linkplain #getSubtype() subtype} is the wildcard * character <code>*</code> or the wildcard character followed by a suffix * (e.g. <code>*+xml</code>). * @return whether the subtype is a wildcard */ public boolean isWildcardSubtype() { return WILDCARD_TYPE.equals(getSubtype()) || getSubtype().startsWith("*+"); }
/** * Indicates whether the {@linkplain #getSubtype() subtype} is the wildcard * character <code>*</code> or the wildcard character followed by a suffix * (e.g. <code>*+xml</code>). * @return whether the subtype is a wildcard */ public boolean isWildcardSubtype() { return WILDCARD_TYPE.equals(getSubtype()) || getSubtype().startsWith("*+"); }
/** * Copy-constructor that copies the type and subtype of the given {@code MimeType}, * and allows for different parameter. * @param other the other MimeType * @param parameters the parameters (may be {@code null}) * @throws IllegalArgumentException if any of the parameters contains illegal characters */ public MimeType(MimeType other, @Nullable Map<String, String> parameters) { this(other.getType(), other.getSubtype(), parameters); }
if (getSubtype().equals(other.getSubtype())) { return true; int thisPlusIdx = getSubtype().lastIndexOf('+'); if (thisPlusIdx == -1) { return true; int otherPlusIdx = other.getSubtype().lastIndexOf('+'); if (otherPlusIdx != -1) { String thisSubtypeNoSuffix = getSubtype().substring(0, thisPlusIdx); String thisSubtypeSuffix = getSubtype().substring(thisPlusIdx + 1); String otherSubtypeSuffix = other.getSubtype().substring(otherPlusIdx + 1); if (thisSubtypeSuffix.equals(otherSubtypeSuffix) && WILDCARD_TYPE.equals(thisSubtypeNoSuffix)) { return true;
/** * Copy-constructor that copies the type and subtype of the given {@code MimeType}, * and allows for different parameter. * @param other the other MimeType * @param parameters the parameters (may be {@code null}) * @throws IllegalArgumentException if any of the parameters contains illegal characters */ public MimeType(MimeType other, @Nullable Map<String, String> parameters) { this(other.getType(), other.getSubtype(), parameters); }
if (getSubtype().equals(other.getSubtype())) { return true; int thisPlusIdx = getSubtype().lastIndexOf('+'); int otherPlusIdx = other.getSubtype().lastIndexOf('+'); if (thisPlusIdx == -1 && otherPlusIdx == -1) { return true; String thisSubtypeNoSuffix = getSubtype().substring(0, thisPlusIdx); String otherSubtypeNoSuffix = other.getSubtype().substring(0, otherPlusIdx); String thisSubtypeSuffix = getSubtype().substring(thisPlusIdx + 1); String otherSubtypeSuffix = other.getSubtype().substring(otherPlusIdx + 1); if (thisSubtypeSuffix.equals(otherSubtypeSuffix) && (WILDCARD_TYPE.equals(thisSubtypeNoSuffix) || WILDCARD_TYPE.equals(otherSubtypeNoSuffix))) {
protected boolean supportsMimeType(@Nullable MessageHeaders headers) { if (getSupportedMimeTypes().isEmpty()) { return true; } MimeType mimeType = getMimeType(headers); if (mimeType == null) { return !isStrictContentTypeMatch(); } for (MimeType current : getSupportedMimeTypes()) { if (current.getType().equals(mimeType.getType()) && current.getSubtype().equals(mimeType.getSubtype())) { return true; } } return false; }
protected boolean isJsonContentType(Request request) { String mime = request.headers("Content-Type"); if (isBlank(mime)) { return false; } try { MimeType mimeType = MimeType.valueOf(mime); return "application".equals(mimeType.getType()) && "json".equals(mimeType.getSubtype()); } catch (InvalidMimeTypeException e) { return false; } }
/** * Re-create the given mime type as a media type. * @since 5.0 */ public static MediaType asMediaType(MimeType mimeType) { if (mimeType instanceof MediaType) { return (MediaType) mimeType; } return new MediaType(mimeType.getType(), mimeType.getSubtype(), mimeType.getParameters()); }
/** * Copy-constructor that copies the type, subtype, parameters of the given {@code MimeType}, * and allows to set the specified character set. * @param other the other MimeType * @param charset the character set * @throws IllegalArgumentException if any of the parameters contains illegal characters * @since 4.3 */ public MimeType(MimeType other, Charset charset) { this(other.getType(), other.getSubtype(), addCharsetParameter(charset, other.getParameters())); }
return comp; comp = getSubtype().compareToIgnoreCase(other.getSubtype()); if (comp != 0) { return comp;
/** * Copy-constructor that copies the type, subtype, parameters of the given {@code MimeType}, * and allows to set the specified character set. * @param other the other MimeType * @param charset the character set * @throws IllegalArgumentException if any of the parameters contains illegal characters * @since 4.3 */ public MimeType(MimeType other, Charset charset) { this(other.getType(), other.getSubtype(), addCharsetParameter(charset, other.getParameters())); }
/** * Re-create the given mime type as a media type. * @since 5.0 */ public static MediaType asMediaType(MimeType mimeType) { if (mimeType instanceof MediaType) { return (MediaType) mimeType; } return new MediaType(mimeType.getType(), mimeType.getSubtype(), mimeType.getParameters()); }
/** * Parse the given String into a single {@code MediaType}. * @param mediaType the string to parse * @return the media type * @throws InvalidMediaTypeException if the media type value cannot be parsed */ public static MediaType parseMediaType(String mediaType) { MimeType type; try { type = MimeTypeUtils.parseMimeType(mediaType); } catch (InvalidMimeTypeException ex) { throw new InvalidMediaTypeException(ex); } try { return new MediaType(type.getType(), type.getSubtype(), type.getParameters()); } catch (IllegalArgumentException ex) { throw new InvalidMediaTypeException(mediaType, ex.getMessage()); } }
@Override public int compare(T mimeType1, T mimeType2) { if (mimeType1.isWildcardType() && !mimeType2.isWildcardType()) { // */* < audio/* return 1; } else if (mimeType2.isWildcardType() && !mimeType1.isWildcardType()) { // audio/* > */* return -1; } else if (!mimeType1.getType().equals(mimeType2.getType())) { // audio/basic == text/html return 0; } else { // mediaType1.getType().equals(mediaType2.getType()) if (mimeType1.isWildcardSubtype() && !mimeType2.isWildcardSubtype()) { // audio/* < audio/basic return 1; } else if (mimeType2.isWildcardSubtype() && !mimeType1.isWildcardSubtype()) { // audio/basic > audio/* return -1; } else if (!mimeType1.getSubtype().equals(mimeType2.getSubtype())) { // audio/basic == audio/wave return 0; } else { // mediaType2.getSubtype().equals(mediaType2.getSubtype()) return compareParameters(mimeType1, mimeType2); } } }
/** * Parse the given String into a single {@code MediaType}. * @param mediaType the string to parse * @return the media type * @throws InvalidMediaTypeException if the media type value cannot be parsed */ public static MediaType parseMediaType(String mediaType) { MimeType type; try { type = MimeTypeUtils.parseMimeType(mediaType); } catch (InvalidMimeTypeException ex) { throw new InvalidMediaTypeException(ex); } try { return new MediaType(type.getType(), type.getSubtype(), type.getParameters()); } catch (IllegalArgumentException ex) { throw new InvalidMediaTypeException(mediaType, ex.getMessage()); } }
@Test public void parseMimeType() { String s = "audio/*"; MimeType mimeType = MimeTypeUtils.parseMimeType(s); assertEquals("Invalid type", "audio", mimeType.getType()); assertEquals("Invalid subtype", "*", mimeType.getSubtype()); }
@Test public void parseQuotedCharset() { String s = "application/xml;charset=\"utf-8\""; MimeType mimeType = MimeType.valueOf(s); assertEquals("Invalid type", "application", mimeType.getType()); assertEquals("Invalid subtype", "xml", mimeType.getSubtype()); assertEquals("Invalid charset", StandardCharsets.UTF_8, mimeType.getCharset()); }
@Test public void parseCharset() { String s = "text/html; charset=iso-8859-1"; MimeType mimeType = MimeType.valueOf(s); assertEquals("Invalid type", "text", mimeType.getType()); assertEquals("Invalid subtype", "html", mimeType.getSubtype()); assertEquals("Invalid charset", StandardCharsets.ISO_8859_1, mimeType.getCharset()); }
@Test public void parseQuotedSeparator() { String s = "application/xop+xml;charset=utf-8;type=\"application/soap+xml;action=\\\"http://x.y.z\\\"\""; MimeType mimeType = MimeType.valueOf(s); assertEquals("Invalid type", "application", mimeType.getType()); assertEquals("Invalid subtype", "xop+xml", mimeType.getSubtype()); assertEquals("Invalid charset", StandardCharsets.UTF_8, mimeType.getCharset()); assertEquals("\"application/soap+xml;action=\\\"http://x.y.z\\\"\"", mimeType.getParameter("type")); }