JAX-RS Accept header param compatibality

147 views Asked by At

If the Accept header param string has multiple media types that are acceptable such as:

text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2

How to find if the media type is compatible with a single MediaType object?

If the Accept header contains a single media type then it works.

MediaType.valueOf(acceptHeaderparam).isCompatible(MediaType.valueOf(contentType) 

Is there some method such as MediaType.isCompatible(String)?

NOTE: @Consumes header can not be used because the custom media types are supported and the all the media types can not be determined at the compile time.

1

There are 1 answers

0
Trash Can On

Something like this?

public boolean isCompatible(String mediaTypeStr, String contentType) {
    MediaType type = MediaType.valueOf(contentType);
    return Arrays.stream(mediaTypeStr.split(","))
                 .map(t -> MediaType.valueOf(t.trim()))
                 .anyMatch(type::isCompatible);
}