Class MediaType

    • Constructor Detail

      • MediaType

        public MediaType​(java.lang.String type)
        Create a new MediaType for the given primary type.

        The subtype is set to "*", parameters empty.

        Parameters:
        type - the primary type
        Throws:
        java.lang.IllegalArgumentException - if any of the parameters contain illegal characters
      • MediaType

        public MediaType​(java.lang.String type,
                         java.lang.String subtype)
        Create a new MediaType for the given primary type and subtype.

        The parameters are empty.

        Parameters:
        type - the primary type
        subtype - the subtype
        Throws:
        java.lang.IllegalArgumentException - if any of the parameters contain illegal characters
      • MediaType

        public MediaType​(java.lang.String type,
                         java.lang.String subtype,
                         java.nio.charset.Charset charset)
        Create a new MediaType for the given type, subtype, and character set.
        Parameters:
        type - the primary type
        subtype - the subtype
        charset - the character set
        Throws:
        java.lang.IllegalArgumentException - if any of the parameters contain illegal characters
      • MediaType

        public MediaType​(java.lang.String type,
                         java.lang.String subtype,
                         double qualityValue)
        Create a new MediaType for the given type, subtype, and quality value.
        Parameters:
        type - the primary type
        subtype - the subtype
        qualityValue - the quality value
        Throws:
        java.lang.IllegalArgumentException - if any of the parameters contain illegal characters
      • MediaType

        public MediaType​(MediaType other,
                         java.nio.charset.Charset charset)
        Copy-constructor that copies the type, subtype and parameters of the given MediaType, and allows to set the specified character set.
        Parameters:
        other - the other media type
        charset - the character set
        Throws:
        java.lang.IllegalArgumentException - if any of the parameters contain illegal characters
        Since:
        4.3
      • MediaType

        public MediaType​(MediaType other,
                         java.util.Map<java.lang.String,​java.lang.String> parameters)
        Copy-constructor that copies the type and subtype of the given MediaType, and allows for different parameter.
        Parameters:
        other - the other media type
        parameters - the parameters, may be null
        Throws:
        java.lang.IllegalArgumentException - if any of the parameters contain illegal characters
      • MediaType

        public MediaType​(java.lang.String type,
                         java.lang.String subtype,
                         java.util.Map<java.lang.String,​java.lang.String> parameters)
        Create a new MediaType for the given type, subtype, and parameters.
        Parameters:
        type - the primary type
        subtype - the subtype
        parameters - the parameters, may be null
        Throws:
        java.lang.IllegalArgumentException - if any of the parameters contain illegal characters
    • Method Detail

      • checkParameters

        protected void checkParameters​(java.lang.String attribute,
                                       java.lang.String value)
        Overrides:
        checkParameters in class MimeType
      • getQualityValue

        public double getQualityValue()
        Return the quality value, as indicated by a q parameter, if any. Defaults to 1.0.
        Returns:
        the quality factory
      • includes

        public boolean includes​(MediaType other)
        Indicate whether this MediaType includes the given media type.

        For instance, text/* includes text/plain and text/html, and application/*+xml includes application/soap+xml, etc. This method is not symmetric.

        Parameters:
        other - the reference media type with which to compare
        Returns:
        true if this media type includes the given media type; false otherwise
      • isCompatibleWith

        public boolean isCompatibleWith​(MediaType other)
        Indicate whether this MediaType is compatible with the given media type.

        For instance, text/* is compatible with text/plain, text/html, and vice versa. In effect, this method is similar to includes(MediaType), except that it is symmetric.

        Parameters:
        other - the reference media type with which to compare
        Returns:
        true if this media type is compatible with the given media type; false otherwise
      • copyQualityValue

        public MediaType copyQualityValue​(MediaType mediaType)
        Return a replica of this instance with the quality value of the given MediaType.
        Returns:
        the same instance if the given MediaType doesn't have a quality value, or a new one otherwise
      • removeQualityValue

        public MediaType removeQualityValue()
        Return a replica of this instance with its quality value removed.
        Returns:
        the same instance if the media type doesn't contain a quality value, or a new one otherwise
      • valueOf

        public static MediaType valueOf​(java.lang.String value)
        Parse the given String value into a MediaType object, with this method name following the 'valueOf' naming convention.
        Parameters:
        value - the string to parse
        Throws:
        InvalidMediaTypeException - if the media type value cannot be parsed
        See Also:
        parseMediaType(String)
      • parseMediaType

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

        public static java.util.List<MediaType> parseMediaTypes​(java.lang.String mediaTypes)
        Parse the given comma-separated string into a list of MediaType objects.

        This method can be used to parse an Accept or Content-Type header.

        Parameters:
        mediaTypes - the string to parse
        Returns:
        the list of media types
        Throws:
        InvalidMediaTypeException - if the media type value cannot be parsed
      • parseMediaTypes

        public static java.util.List<MediaType> parseMediaTypes​(java.util.List<java.lang.String> mediaTypes)
        Parse the given list of (potentially) comma-separated strings into a list of MediaType objects.

        This method can be used to parse an Accept or Content-Type header.

        Parameters:
        mediaTypes - the string to parse
        Returns:
        the list of media types
        Throws:
        InvalidMediaTypeException - if the media type value cannot be parsed
        Since:
        4.3.2
      • asMediaTypes

        public static java.util.List<MediaType> asMediaTypes​(java.util.List<MimeType> mimeTypes)
        Re-create the given mime types as media types.
        Since:
        5.0
      • asMediaType

        public static MediaType asMediaType​(MimeType mimeType)
        Re-create the given mime type as a media type.
        Since:
        5.0
      • toString

        public static java.lang.String toString​(java.util.Collection<MediaType> mediaTypes)
        Return a string representation of the given list of MediaType objects.

        This method can be used to for an Accept or Content-Type header.

        Parameters:
        mediaTypes - the media types to create a string representation for
        Returns:
        the string representation
      • 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 different quality value, then the media type with the highest quality value is ordered before the other.
        6. 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/* < audio/*;q=0.7; audio/*;q=0.3
        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
      • sortByQualityValue

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

        Given two media types:

        1. if the two media types have different quality value, then the media type with the highest quality value is ordered before the other.
        2. if either media type has a wildcard type, then the media type without the wildcard is ordered before the other.
        3. if the two media types have different types, then they are considered equal and remain their current order.
        4. if either media type has a wildcard subtype, then the media type without the wildcard is sorted before the other.
        5. if the two media types have different subtypes, then they are considered equal and remain their current order.
        6. if the two media types have a different amount of parameters, then the media type with the most parameters is ordered before the other.
        Parameters:
        mediaTypes - the list of media types to be sorted
        See Also:
        getQualityValue()
      • sortBySpecificityAndQuality

        public static void sortBySpecificityAndQuality​(java.util.List<MediaType> mediaTypes)
        Sorts the given list of MediaType objects by specificity as the primary criteria and quality value the secondary.
        See Also:
        sortBySpecificity(List), sortByQualityValue(List)