org.springframework.util

Class MimeType

  • java.lang.Object
    • org.springframework.util.MimeType
  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<MimeType>
    Direct Known Subclasses:
    MediaType


    public class MimeType
    extends java.lang.Object
    implements java.lang.Comparable<MimeType>, java.io.Serializable
    Represents a MIME Type, as originally defined in RFC 2046 and subsequently used in other Internet protocols including HTTP.

    This class, however, does not contain support for the q-parameters used in HTTP content negotiation. Those can be found in the subclass org.springframework.http.MediaType in the spring-web module.

    Consists of a type and a subtype. Also has functionality to parse MIME Type values from a String using valueOf(String). For more parsing options see MimeTypeUtils.

    Since:
    4.0
    Author:
    Arjen Poutsma, Juergen Hoeller, Rossen Stoyanchev, Sam Brannen
    See Also:
    MimeTypeUtils, Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field and Description
      protected static java.lang.String WILDCARD_TYPE 
    • Constructor Summary

      Constructors 
      Constructor and Description
      MimeType(MimeType other, java.nio.charset.Charset charset)
      Copy-constructor that copies the type, subtype, parameters of the given MimeType, and allows to set the specified character set.
      MimeType(MimeType other, java.util.Map<java.lang.String,java.lang.String> parameters)
      Copy-constructor that copies the type and subtype of the given MimeType, and allows for different parameter.
      MimeType(java.lang.String type)
      Create a new MimeType for the given primary type.
      MimeType(java.lang.String type, java.lang.String subtype)
      Create a new MimeType for the given primary type and subtype.
      MimeType(java.lang.String type, java.lang.String subtype, java.nio.charset.Charset charset)
      Create a new MimeType for the given type, subtype, and character set.
      MimeType(java.lang.String type, java.lang.String subtype, java.util.Map<java.lang.String,java.lang.String> parameters)
      Create a new MimeType for the given type, subtype, and parameters.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      protected void appendTo(java.lang.StringBuilder builder) 
      protected void checkParameters(java.lang.String attribute, java.lang.String value) 
      int compareTo(MimeType other)
      Compares this MIME Type to another alphabetically.
      boolean equals(java.lang.Object other) 
      java.nio.charset.Charset getCharset()
      Return the character set, as indicated by a charset parameter, if any.
      java.lang.String getParameter(java.lang.String name)
      Return a generic parameter value, given a parameter name.
      java.util.Map<java.lang.String,java.lang.String> getParameters()
      Return all generic parameter values.
      java.lang.String getSubtype()
      Return the subtype.
      java.lang.String getType()
      Return the primary type.
      int hashCode() 
      boolean includes(MimeType other)
      Indicate whether this MIME Type includes the given MIME Type.
      boolean isCompatibleWith(MimeType other)
      Indicate whether this MIME Type is compatible with the given MIME Type.
      boolean isConcrete()
      Indicates whether this MIME Type is concrete, i.e.
      boolean isWildcardSubtype()
      Indicates whether the subtype is the wildcard character * or the wildcard character followed by a suffix (e.g.
      boolean isWildcardType()
      Indicates whether the type is the wildcard character * or not.
      java.lang.String toString() 
      protected java.lang.String unquote(java.lang.String s) 
      static MimeType valueOf(java.lang.String value)
      Parse the given String value into a MimeType object, with this method name following the 'valueOf' naming convention (as supported by ConversionService.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

    • Constructor Detail

      • MimeType

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

        The subtype is set to "*", and the parameters are empty.

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

        public MimeType(java.lang.String type,
                        java.lang.String subtype)
        Create a new MimeType 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 contains illegal characters
      • MimeType

        public MimeType(java.lang.String type,
                        java.lang.String subtype,
                        java.nio.charset.Charset charset)
        Create a new MimeType 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 contains illegal characters
      • MimeType

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

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

        public MimeType(java.lang.String type,
                        java.lang.String subtype,
                        @Nullable
                        java.util.Map<java.lang.String,java.lang.String> parameters)
        Create a new MimeType 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 contains illegal characters
    • Method Detail

      • checkParameters

        protected void checkParameters(java.lang.String attribute,
                                       java.lang.String value)
      • unquote

        protected java.lang.String unquote(java.lang.String s)
      • isWildcardType

        public boolean isWildcardType()
        Indicates whether the type is the wildcard character * or not.
      • isWildcardSubtype

        public boolean isWildcardSubtype()
        Indicates whether the subtype is the wildcard character * or the wildcard character followed by a suffix (e.g. *+xml).
        Returns:
        whether the subtype is a wildcard
      • isConcrete

        public boolean isConcrete()
        Indicates whether this MIME Type is concrete, i.e. whether neither the type nor the subtype is a wildcard character *.
        Returns:
        whether this MIME Type is concrete
      • getType

        public java.lang.String getType()
        Return the primary type.
      • getSubtype

        public java.lang.String getSubtype()
        Return the subtype.
      • getCharset

        @Nullable
        public java.nio.charset.Charset getCharset()
        Return the character set, as indicated by a charset parameter, if any.
        Returns:
        the character set, or null if not available
        Since:
        4.3
      • getParameter

        @Nullable
        public java.lang.String getParameter(java.lang.String name)
        Return a generic parameter value, given a parameter name.
        Parameters:
        name - the parameter name
        Returns:
        the parameter value, or null if not present
      • getParameters

        public java.util.Map<java.lang.String,java.lang.String> getParameters()
        Return all generic parameter values.
        Returns:
        a read-only map (possibly empty, never null)
      • includes

        public boolean includes(@Nullable
                                MimeType other)
        Indicate whether this MIME Type includes the given MIME 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 MIME Type with which to compare
        Returns:
        true if this MIME Type includes the given MIME Type; false otherwise
      • isCompatibleWith

        public boolean isCompatibleWith(@Nullable
                                        MimeType other)
        Indicate whether this MIME Type is compatible with the given MIME Type.

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

        Parameters:
        other - the reference MIME Type with which to compare
        Returns:
        true if this MIME Type is compatible with the given MIME Type; false otherwise
      • equals

        public boolean equals(java.lang.Object other)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • appendTo

        protected void appendTo(java.lang.StringBuilder builder)
      • compareTo

        public int compareTo(MimeType other)
        Compares this MIME Type to another alphabetically.
        Specified by:
        compareTo in interface java.lang.Comparable<MimeType>
        Parameters:
        other - the MIME Type to compare to
        See Also:
        MimeTypeUtils.sortBySpecificity(List)