Consider adding new KiwiMediaTypes utility class #679
Replies: 4 comments
-
To support the above methods, can start with some base methods, e.g. // Compares only type and subtype
public static boolean areEqual(String mediaType1, String mediaType2) { ... }
public static boolean areEqual(MediaType mediaType1, MediaType mediaType2) { ... } Could also include "supporting" methods to check if the type matches, if the subtype matches, etc. e.g. public static boolean matchesType(MediaType mediaType, String typeToMatch) { ... }
public static boolean matchesSubtype(MediaType mediaType, String subtypeToMatch) { ... } Might also want to include methods like this one |
Beta Was this translation helpful? Give feedback.
-
The "strict" methods don't make sense after looking at this again, because the parameters are just additional information, e.g. The "areEqual" methods as well as the "matchesType" and "matchesSubtype" do make sense, because they are comparing two media types, e.g. var mediaType = response.getMediaType();
var typeMatches = matchesType(mediaType, "text");
var subtypeMatches = matchesSubtype(mediaType, "xml"); |
Beta Was this translation helpful? Give feedback.
-
These media type utilities are being added first to kiwi-beta in Add media type utilities They can be migrated into kiwi later if we decide we like them enough. |
Beta Was this translation helpful? Give feedback.
-
I am marking this discussed as Closed since it is now in kiwi-beta and may be moved to kiwi at some point. |
Beta Was this translation helpful? Give feedback.
-
The JAX-RS
MediaType
is aclass
, not anenum
, so instances must be compared usingequals
and not==
. So, this is not valid:this is what you need to to instead:
But this brings up another point, which is that it would be useful to have methods like
isXml(MediaType)
andisXml(String)
that can handle either of the possible XML types. Other useful methods would beisJson
,isPlainText
,isHtml
, and so on.We would also need to research whether there are any things that could mess all this up, e.g. the various parameters that can be tacked on to media types such as
text/html; charset=UTF-8
. The JAX-RSMediaType#equals
takes into account the type, subtype, and parameters. Thustext/html
does not equaltext/html; charset=UTF-8
.We could have different variations that either include or don't include the parameters. Normally, I'd want to just see if something is
text/xml
(orapplication/xml
) to determine if it's XML content and ignore the parameters. So you could have:In all of the above proposed methods, both
text/xml
andapplication/xml
are both considered XML.See Mozilla's MIME types for more information.
Beta Was this translation helpful? Give feedback.
All reactions