Module methanol

Class MediaType


  • public final class MediaType
    extends java.lang.Object
    A MIME type. The text representation of this class can be used as the value of the Content-Type HTTP header.

    A MediaType also defines a media range. A media range has either both wildcard type and subtype, both concrete type and subtype, or a concrete type and a wildcard subtype (but not a wildcard type and a concrete subtype), with the character * denoting a wildcard. Inclusion in media ranges can be tested using any of includes(MediaType) or isCompatibleWith(MediaType), with the latter being symmetric among operands.

    Case-insensitive attributes such as the type, subtype, parameter names or the value of the charset parameter are converted into lower-case.

    • Field Detail

      • APPLICATION_ANY

        public static final MediaType APPLICATION_ANY
        application/*
      • IMAGE_ANY

        public static final MediaType IMAGE_ANY
        image/*
      • TEXT_ANY

        public static final MediaType TEXT_ANY
        text/*
      • APPLICATION_FORM_URLENCODED

        public static final MediaType APPLICATION_FORM_URLENCODED
        application/x-www-form-urlencoded
      • APPLICATION_JSON

        public static final MediaType APPLICATION_JSON
        application/json
      • APPLICATION_OCTET_STREAM

        public static final MediaType APPLICATION_OCTET_STREAM
        application/octet-stream
      • APPLICATION_XHTML_XML

        public static final MediaType APPLICATION_XHTML_XML
        application/xhtml+xml
      • APPLICATION_XML

        public static final MediaType APPLICATION_XML
        application/xml
      • APPLICATION_X_PROTOBUF

        public static final MediaType APPLICATION_X_PROTOBUF
        application/x-protobuf
      • IMAGE_GIF

        public static final MediaType IMAGE_GIF
        image/gif
      • IMAGE_JPEG

        public static final MediaType IMAGE_JPEG
        image/jpeg
      • IMAGE_PNG

        public static final MediaType IMAGE_PNG
        image/png
      • TEXT_HTML

        public static final MediaType TEXT_HTML
        text/html
      • TEXT_MARKDOWN

        public static final MediaType TEXT_MARKDOWN
        text/markdown
      • TEXT_PLAIN

        public static final MediaType TEXT_PLAIN
        text/plain
      • TEXT_XML

        public static final MediaType TEXT_XML
        text/xml
    • Method Detail

      • type

        public java.lang.String type()
        Returns the general type.
      • subtype

        public java.lang.String subtype()
        Returns the subtype.
      • parameters

        public java.util.Map<java.lang.String,​java.lang.String> parameters()
        Returns an immutable map representing the parameters.
      • charset

        public java.util.Optional<java.nio.charset.Charset> charset()
        Returns an Optional representing the value of the charset parameter. An empty Optional is returned if no such parameter exists.
        Throws:
        java.nio.charset.IllegalCharsetNameException - if a charset parameter exists the value of which is invalid
        java.nio.charset.UnsupportedCharsetException - if a charset parameter exists the value of which is not supported in this JVM
      • charsetOrDefault

        public java.nio.charset.Charset charsetOrDefault​(java.nio.charset.Charset defaultCharset)
        Returns either the value of the charset parameter or the given default charset if no such parameter exists or if the charset has no support in this JVM.
        Throws:
        java.nio.charset.IllegalCharsetNameException - if a charset parameter exists the value of which is invalid
      • hasWildcard

        public boolean hasWildcard()
        Returns true if this media type is */* or if it has a wildcard subtype.
      • includes

        public boolean includes​(MediaType other)
        Returns whether this media type includes the given one. A media type includes the other if the former's parameters is a subset of the latter's and either the former is a wildcard type that includes the latter or both have equal concrete type and subtype.
      • isCompatibleWith

        public boolean isCompatibleWith​(MediaType other)
        Returns whether this media type is compatible with the given one. Two media types are compatible if either of them includes the other.
      • withCharset

        public MediaType withCharset​(java.nio.charset.Charset charset)
        Returns a new MediaType with this instance's type, subtype and parameters but with the name of the given charset as the value of the charset parameter.
      • withParameter

        public MediaType withParameter​(java.lang.String name,
                                       java.lang.String value)
        Returns a new MediaType with this instance's type, subtype and parameters but with the value of the parameter specified by the given name set to the given value.
        Throws:
        java.lang.IllegalArgumentException - if the given name or value is invalid
      • withParameters

        public MediaType withParameters​(java.util.Map<java.lang.String,​java.lang.String> parameters)
        Returns a new MediaType with this instance's type, subtype and parameters but with each of the given parameters' names set to their corresponding values.
        Throws:
        java.lang.IllegalArgumentException - if any of the given parameters is invalid
      • equals

        public boolean equals​(@Nullable java.lang.Object obj)
        Tests the given object for equality with this instance. true is returned if the given object is a MediaType and both instances' type, subtype and parameters are equal.
        Overrides:
        equals in class java.lang.Object
        Parameters:
        obj - the object to test for equality
      • hashCode

        public int hashCode()
        Returns a hashcode for this media type.
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Returns a text representation of this media type that is compatible with the value of the Content-Type header.
        Overrides:
        toString in class java.lang.Object
      • of

        public static MediaType of​(java.lang.String type,
                                   java.lang.String subtype)
        Returns a new MediaType with the given type and subtype.
        Throws:
        java.lang.IllegalArgumentException - if the given type or subtype is invalid
      • of

        public static MediaType of​(java.lang.String type,
                                   java.lang.String subtype,
                                   java.util.Map<java.lang.String,​java.lang.String> parameters)
        Returns a new MediaType with the given type, subtype and parameters.
        Throws:
        java.lang.IllegalArgumentException - if the given type, subtype or any of the given parameters is invalid
      • parse

        public static MediaType parse​(java.lang.String value)
        Parses the given string into a MediaType instance.
        Throws:
        java.lang.IllegalArgumentException - if the given string is an invalid media type