Class KiwiMediaTypes
- java.lang.Object
-
- org.kiwiproject.beta.net.KiwiMediaTypes
-
@Beta public final class KiwiMediaTypes extends Object
Some utilities for working with media types.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/xmlandapplication/xmlrepresent XML documents, so checking whether a media type is XML should take this into account. But the GuavaMediaType#ismethod doesn't permit comparing like this when the type is different but the subtype is the same.The Jakarta RS MediaType is also useful, but its
equalsmethod 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
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleanisJson(String mediaType)Checks if media type is "application/json", ignoring parameters such that "application/json; charset=utf-8" is considered JSON.static booleanisJson(javax.ws.rs.core.MediaType mediaType)Checks if media type is "application/json", ignoring parameters such that "application/json; charset=utf-8" is considered JSON.static booleanisXml(String mediaType)Checks if media type is "application/xml" or "text/xml", ignoring parameters such that "text/xml; charset=utf-8" is considered XML.static booleanisXml(javax.ws.rs.core.MediaType mediaType)Checks if media type is "application/xml" or "text/xml", ignoring parameters such that "text/xml; charset=utf-8" is considered XML.static booleanmatchesSubtype(String mediaType, String subtypeToMatch)Checks if the given media type has a type that matches the given value.static booleanmatchesType(String mediaType, String typeToMatch)Checks if the given media type has a type that matches the given value.static StringtoStringWithoutParameters(javax.ws.rs.core.MediaType mediaType)Get the string value of the givenMediaTypewith only the type/subtype.
-
-
-
Method Detail
-
isXml
public static boolean isXml(javax.ws.rs.core.MediaType mediaType)
Checks if 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 media type to check- Returns:
- true if the media type is an XML type ignoring any parameters, otherwise false
-
isXml
public static boolean isXml(String mediaType)
Checks if 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
-
isJson
public static boolean isJson(javax.ws.rs.core.MediaType mediaType)
Checks if 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 media type to check- Returns:
- true if the media type is JSON ignoring any parameters, otherwise false
-
toStringWithoutParameters
public static String toStringWithoutParameters(javax.ws.rs.core.MediaType mediaType)
Get the string value of the givenMediaTypewith only the type/subtype.- Implementation Note:
- This method concatenates the type and subtype of the MediaType because the MediaType#toString requires a Jakarta RS implementation in order to create a RuntimeDelegate which is then used to convert to a String. Presumably if this method is used, the implementation is available, but 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.
-
isJson
public static boolean isJson(String mediaType)
Checks if 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
-
matchesType
public static boolean matchesType(String mediaType, String typeToMatch)
Checks if the given media type has a type that matches the given value.- Parameters:
mediaType- the media type to checktypeToMatch- the type to match- Returns:
- true if the types match, otherwise false
-
matchesSubtype
public static boolean matchesSubtype(String mediaType, String subtypeToMatch)
Checks if the given media type has a type that matches the given value.- Parameters:
mediaType- the media type to checksubtypeToMatch- the subtype to match- Returns:
- true if the subtypes match, otherwise false
-
-