Class MediaTypeUtils


  • public abstract class MediaTypeUtils
    extends java.lang.Object
    Miscellaneous MediaType utility methods.
    Author:
    Arjen Poutsma, Rossen Stoyanchev, Dimitrios Liapis, Brian Clozel
    • Field Detail

      • ALL

        public static final MediaType ALL
        Public constant media type that includes all media ranges (i.e. "*/*").
      • APPLICATION_JSON

        public static final MediaType APPLICATION_JSON
        Public constant media type for application/json.
      • APPLICATION_OCTET_STREAM

        public static final MediaType APPLICATION_OCTET_STREAM
        Public constant media type for application/octet-stream.
      • APPLICATION_XML

        public static final MediaType APPLICATION_XML
        Public constant media type for application/xml.
      • IMAGE_GIF

        public static final MediaType IMAGE_GIF
        Public constant media type for image/gif.
      • IMAGE_JPEG

        public static final MediaType IMAGE_JPEG
        Public constant media type for image/jpeg.
      • IMAGE_PNG

        public static final MediaType IMAGE_PNG
        Public constant media type for image/png.
      • TEXT_HTML

        public static final MediaType TEXT_HTML
        Public constant media type for text/html.
      • TEXT_PLAIN

        public static final MediaType TEXT_PLAIN
        Public constant media type for text/plain.
      • TEXT_XML

        public static final MediaType TEXT_XML
        Public constant media type for text/xml.
    • Constructor Detail

      • MediaTypeUtils

        public MediaTypeUtils()
    • Method Detail

      • parseMediaType

        public static MediaType parseMediaType​(java.lang.String mediaType)
        Parse the given String into a single MediaType. Recently parsed MediaType are cached for further retrieval.
        Parameters:
        mediaType - the string to parse
        Returns:
        the media type
        Throws:
        InvalidMediaTypeException - if the string cannot be parsed
      • parseMediaTypes

        public static java.util.List<MediaType> parseMediaTypes​(java.lang.String mediaTypes)
        Parse the comma-separated string into a list of MediaType objects.
        Parameters:
        mediaTypes - the string to parse
        Returns:
        the list of media types
        Throws:
        InvalidMediaTypeException - if the string cannot be parsed
      • tokenize

        public static java.util.List<java.lang.String> tokenize​(java.lang.String mediaTypes)
        Tokenize the given comma-separated string of MediaType objects into a List<String>. Unlike simple tokenization by ",", this method takes into account quoted parameters.
        Parameters:
        mediaTypes - the string to tokenize
        Returns:
        the list of tokens
      • toString

        public static java.lang.String toString​(java.util.Collection<MediaType> mediaTypes)
        Return a string representation of the given list of MediaType objects.
        Parameters:
        mediaTypes - the string to parse
        Returns:
        the list of media types
        Throws:
        java.lang.IllegalArgumentException - if the String cannot be parsed
      • sortBySpecificity

        public static void sortBySpecificity​(java.util.List<MediaType> mediaTypes)
        Sorts the given list of MediaType objects by specificity.

        Given two media types:

        1. if either media type has a wildcard type, then the media type without the wildcard is ordered before the other.
        2. if the two media types have different types, then they are considered equal and remain their current order.
        3. if either media type has a wildcard subtype , then the media type without the wildcard is sorted before the other.
        4. if the two media types have different subtypes, then they are considered equal and remain their current order.
        5. if the two media types have a different amount of parameters, then the media type with the most parameters is ordered before the other.

        For example:

        audio/basic < audio/* < */*
        audio/basic;level=1 < audio/basic
        audio/basic == text/html
        audio/basic == audio/wave
        Parameters:
        mediaTypes - the list of media types to be sorted
        See Also:
        HTTP 1.1: Semantics and Content, section 5.3.2