Class KiwiMediaTypes
Google Guava contains the MediaType
class which represents an Internet Media Type. It contains some useful methods, but not some (what we think are)
basic utilities such as the ability to check if a media type is XML. Both text/xml and
application/xml represent XML documents, so checking whether a media type is XML should take this into
account. But the Guava MediaType#is method doesn't permit comparing like this when the type is different
but the subtype is the same.
The Jakarta Rest MediaType
is also useful, but its equals method unfortunately compares type, subtype, and parameters so
you cannot use it to check if a media type such as "text/xml; charset=utf-8" equals the "text/xml" media type,
since it will include the charset parameter in the comparison and return false.
- Implementation Note:
- Internally this uses Guava's
MediaTypeto parse String media types.
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanisHtml(jakarta.ws.rs.core.MediaType mediaType) Checks if the Jakarta Rest media type is "text/html", ignoring parameters such that "text/html; charset=utf-8" is considered HTML.static booleanChecks if the media type is "text/html", ignoring parameters such that "text/html; charset=utf-8" is considered HTML.static booleanisJson(jakarta.ws.rs.core.MediaType mediaType) Checks if the Jakarta Rest media type is "application/json", ignoring parameters such that "application/json; charset=utf-8" is considered JSON.static booleanChecks if the media type is "application/json", ignoring parameters such that "application/json; charset=utf-8" is considered JSON.static booleanisPlainText(jakarta.ws.rs.core.MediaType mediaType) Checks if the Jakarta Rest media type is "text/plain", ignoring parameters such that "text/plain; version=0.0.4; charset=utf-8" is considered plain text.static booleanisPlainText(String mediaType) Checks if the media type is "text/plain", ignoring parameters such that "text/plain; version=0.0.4; charset=utf-8" is considered plain text.static booleanisXml(jakarta.ws.rs.core.MediaType mediaType) Checks if the Jakarta Rest media type is "application/xml" or "text/xml", ignoring parameters such that "text/xml; charset=utf-8" is considered XML.static booleanChecks if the media type is "application/xml" or "text/xml", ignoring parameters such that "text/xml; charset=utf-8" is considered XML.static booleanmatchesMediaType(jakarta.ws.rs.core.MediaType mediaType, jakarta.ws.rs.core.MediaType mediaTypeToMatch) Checks ifmediaTypehas a type and subtype that matchesmediaTypeToMatch, which must only contain a type and subtype.static booleanmatchesMediaType(jakarta.ws.rs.core.MediaType mediaType, String mediaTypeToMatch) Checks ifmediaTypehas a type and subtype that matchesmediaTypeToMatch, which must be in the formattype/subtype.static booleanmatchesMediaType(String mediaType, String mediaTypeToMatch) Checks ifmediaTypehas a type and subtype that matchesmediaTypeToMatch, which must be in the formattype/subtype.static booleanmatchesSubtype(String mediaType, String subtypeToMatch) Checks ifmediaTypehas a subtype that matchessubtypeToMatch.static booleanmatchesType(String mediaType, String typeToMatch) Checks ifmediaTypehas a type that matchestypeToMatch.static booleanmatchesTypeAndSubtype(jakarta.ws.rs.core.MediaType mediaType, String typeToMatch, String subtypeToMatch) Checks ifmediaTypehas a type and subtype that matchestypeToMatchandsubtypeToMatch.static booleanmatchesTypeAndSubtype(jakarta.ws.rs.core.MediaType mediaType, Set<String> typesToMatch, String subtypeToMatch) Checks ifmediaTypehas a type intypesToMatchand a subtype that matchessubtypeToMatch.static booleanmatchesTypeAndSubtype(String mediaType, String typeToMatch, String subtypeToMatch) Checks ifmediaTypehas a type and subtype that matchestypeToMatchandsubtypeToMatch.static booleanmatchesTypeAndSubtype(String mediaType, Set<String> typesToMatch, String subtypeToMatch) Checks ifmediaTypehas a type intypesToMatchand a subtype that matchessubtypeToMatch.static StringtoStringWithoutParameters(jakarta.ws.rs.core.MediaType mediaType) Get the string value of the givenMediaTypewith only the type/subtype.static StringwithoutParameters(String mediaType) Strip any parameters frommediaType, returning a value in the formattype/subtype.
-
Method Details
-
isXml
public static boolean isXml(jakarta.ws.rs.core.MediaType mediaType) Checks if the Jakarta Rest media type is "application/xml" or "text/xml", ignoring parameters such that "text/xml; charset=utf-8" is considered XML.To use this method, the jakarta.ws.rs:jakarta.ws.rs-api dependency must be present.
- Parameters:
mediaType- the Jakarta Rest media type to check- Returns:
- true if the media type is an XML type ignoring any parameters, otherwise false
-
isXml
Checks if the media type is "application/xml" or "text/xml", ignoring parameters such that "text/xml; charset=utf-8" is considered XML.- Parameters:
mediaType- the media type to check- Returns:
- true if the media type is an XML type ignoring any parameters, otherwise false
-
isHtml
public static boolean isHtml(jakarta.ws.rs.core.MediaType mediaType) Checks if the Jakarta Rest media type is "text/html", ignoring parameters such that "text/html; charset=utf-8" is considered HTML.To use this method, the jakarta.ws.rs:jakarta.ws.rs-api dependency must be present.
- Parameters:
mediaType- the Jakarta Rest media type to check- Returns:
- true if the media type is HTML ignoring any parameters, otherwise false
-
isHtml
Checks if the media type is "text/html", ignoring parameters such that "text/html; charset=utf-8" is considered HTML.- Parameters:
mediaType- the media type to check- Returns:
- true if the media type is HTML ignoring any parameters, otherwise false
-
isJson
public static boolean isJson(jakarta.ws.rs.core.MediaType mediaType) Checks if the Jakarta Rest media type is "application/json", ignoring parameters such that "application/json; charset=utf-8" is considered JSON.To use this method, the jakarta.ws.rs:jakarta.ws.rs-api dependency must be present.
- Parameters:
mediaType- the Jakarta Rest media type to check- Returns:
- true if the media type is JSON ignoring any parameters, otherwise false
-
isJson
Checks if the media type is "application/json", ignoring parameters such that "application/json; charset=utf-8" is considered JSON.- Parameters:
mediaType- the media type to check- Returns:
- true if the media type is JSON ignoring any parameters, otherwise false
-
isPlainText
public static boolean isPlainText(jakarta.ws.rs.core.MediaType mediaType) Checks if the Jakarta Rest media type is "text/plain", ignoring parameters such that "text/plain; version=0.0.4; charset=utf-8" is considered plain text.To use this method, the jakarta.ws.rs:jakarta.ws.rs-api dependency must be present.
- Parameters:
mediaType- the Jakarta Rest media type to check- Returns:
- true if the media type is plain text ignoring any parameters, otherwise false
-
isPlainText
Checks if the media type is "text/plain", ignoring parameters such that "text/plain; version=0.0.4; charset=utf-8" is considered plain text.- Parameters:
mediaType- the media type to check- Returns:
- true if the media type is plain text ignoring any parameters, otherwise false
-
toStringWithoutParameters
Get the string value of the givenMediaTypewith only the type/subtype.To use this method, the jakarta.ws.rs:jakarta.ws.rs-api dependency must be present.
- Parameters:
mediaType- the Jakarta Rest media type to strip parameters from- Implementation Note:
- This method concatenates the type and subtype of the MediaType because the MediaType#toString requires a Jakarta RS implementation to create a RuntimeDelegate which is then used to convert to a String. Presumably, if this method is used, the implementation is available. However, just in case it isn't, this method manually creates the media type string using the type and subtype, since they are just fields in MediaType and don't need a Jakarta RS implementation to be available.
-
withoutParameters
Strip any parameters frommediaType, returning a value in the formattype/subtype.This is a convenience method that delegates to Guava's
MediaTypeand which handles parsing the String value to aMediaType, removing the parameters, and converting to a String.- Parameters:
mediaType- the media type to strip parameters from- Returns:
- the "plain" media type as
type/subtype - See Also:
-
MediaType.parse(String)MediaType.withoutParameters()
-
matchesType
Checks ifmediaTypehas a type that matchestypeToMatch.For example, if you want to check that the
typepart of the media type of an HTTP response is"text", you would write:matchesType(response.mediaTypeAsString(), "text").- Parameters:
mediaType- the media type to checktypeToMatch- the type to match- Returns:
- true if the types match, otherwise false
-
matchesSubtype
Checks ifmediaTypehas a subtype that matchessubtypeToMatch.For example, if you want to check that a media type has the subtype
"xml", you would write:matchesSubtype(response.mediaTypeAsString(), "xml")- Parameters:
mediaType- the media type to checksubtypeToMatch- the subtype to match- Returns:
- true if the subtypes match, otherwise false
-
matchesMediaType
public static boolean matchesMediaType(jakarta.ws.rs.core.MediaType mediaType, String mediaTypeToMatch) Checks ifmediaTypehas a type and subtype that matchesmediaTypeToMatch, which must be in the formattype/subtype. Ignores parameters inmediaTypesuch as "version" and "charset" intext/plain; version=0.0.4; charset=utf-8.For example, if you want to check that the media type of an HTTP response is HTML (
text/html), you would write:matchesMediaType(response.mediaType(), "text/html").To use this method, the jakarta.ws.rs:jakarta.ws.rs-api dependency must be present.
- Parameters:
mediaType- the Jakarta Rest media type to checkmediaTypeToMatch- the media type to match (must be in exact formattype/subtype)- Returns:
- true if the type and subtype both match ignoring parameters, otherwise false
-
matchesMediaType
public static boolean matchesMediaType(jakarta.ws.rs.core.MediaType mediaType, jakarta.ws.rs.core.MediaType mediaTypeToMatch) Checks ifmediaTypehas a type and subtype that matchesmediaTypeToMatch, which must only contain a type and subtype. Ignores parameters inmediaTypesuch as "version" and "charset" intext/plain; version=0.0.4; charset=utf-8.For example, if you want to check that the media type of an HTTP response is HTML (
text/html), you would write:matchesMediaType(response.mediaType(), MediaType.TEXT_HTML).To use this method, the jakarta.ws.rs:jakarta.ws.rs-api dependency must be present.
- Parameters:
mediaType- the Jakarta Rest media type to checkmediaTypeToMatch- the Jakarta Rest media type to match (must contain onlytypeandsubtype)- Returns:
- true if the type and subtype both match ignoring parameters, otherwise false
-
matchesMediaType
Checks ifmediaTypehas a type and subtype that matchesmediaTypeToMatch, which must be in the formattype/subtype. Ignores parameters inmediaTypesuch as "version" and "charset" intext/plain; version=0.0.4; charset=utf-8.For example, if you want to check that the media type of an HTTP response is HTML (
text/html), you would write:matchesMediaType(response.mediaTypeAsString(), "text/html").- Parameters:
mediaType- the media type to checkmediaTypeToMatch- the media type to match (must be in exact formattype/subtype)- Returns:
- true if the type and subtype both match ignoring parameters, otherwise false
-
matchesTypeAndSubtype
public static boolean matchesTypeAndSubtype(jakarta.ws.rs.core.MediaType mediaType, String typeToMatch, String subtypeToMatch) Checks ifmediaTypehas a type and subtype that matchestypeToMatchandsubtypeToMatch.For example, if you want to check that the media type of an HTTP response is HTML (
text/html), you would write:matchesTypeAndSubtype(response.mediaType(), "text", "html").To use this method, the jakarta.ws.rs:jakarta.ws.rs-api dependency must be present.
- Parameters:
mediaType- the Jakarta Rest media type to checktypeToMatch- the type to matchsubtypeToMatch- the subtype to match- Returns:
- true if the type and subtype both match, otherwise false
-
matchesTypeAndSubtype
public static boolean matchesTypeAndSubtype(String mediaType, String typeToMatch, String subtypeToMatch) Checks ifmediaTypehas a type and subtype that matchestypeToMatchandsubtypeToMatch.For example, if you want to check that the media type of an HTTP response is HTML (
text/html), you would write:matchesTypeAndSubtype(response.mediaTypeAsString(), "text", "html").- Parameters:
mediaType- the media type to checktypeToMatch- the type to matchsubtypeToMatch- the subtype to match- Returns:
- true if the type and subtype both match, otherwise false
-
matchesTypeAndSubtype
public static boolean matchesTypeAndSubtype(jakarta.ws.rs.core.MediaType mediaType, Set<String> typesToMatch, String subtypeToMatch) Checks ifmediaTypehas a type intypesToMatchand a subtype that matchessubtypeToMatch.This method lets you test for subtypes which can have more than one type. For example, "application/xml" and "text/xml" are both considered valid XML types.
For example, if you want to check that the media type of an HTTP response is XML (
text/xmlorapplication/xml) you would write:matchesTypeAndSubtype(response.mediaType(), Set.of("text", "application"), "xml").To use this method, the jakarta.ws.rs:jakarta.ws.rs-api dependency must be present.
- Parameters:
mediaType- the Jakarta Rest media type to checktypesToMatch- the types to match (any one of them is considered a match)subtypeToMatch- the subtype to match- Returns:
- true if the type is any of the acceptable types and subtype matches, otherwise false
-
matchesTypeAndSubtype
public static boolean matchesTypeAndSubtype(String mediaType, Set<String> typesToMatch, String subtypeToMatch) Checks ifmediaTypehas a type intypesToMatchand a subtype that matchessubtypeToMatch.This method lets you test for subtypes which can have more than one type. For example, "application/xml" and "text/xml" are both considered valid XML types. For example, if you want to check that the media type of an HTTP response is XML (
text/xmlorapplication/xml) you would write:matchesTypeAndSubtype(response.mediaTypeAsString(), Set.of("text", "application"), "xml").- Parameters:
mediaType- the media type to checktypesToMatch- the types to match (any one of them is considered a match)subtypeToMatch- the subtype to match- Returns:
- true if the type is any of the acceptable types and subtype matches, otherwise false
-