Class MimeType

java.lang.Object
org.springframework.util.MimeType
All Implemented Interfaces:
serializable, Comparable<MimeType>
Direct Known subclasses:
MediaType

public class MimeType extends Object implements Comparable<MimeType>, 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:
  • Field Details

  • Constructor Details

    • MimeType

      public&nbsp;MimeType(string&nbsp;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:
      IllegalArgumentException - if any of the parameters contains illegal characters
    • MimeType

      public&nbsp;MimeType(string&nbsp;type, string&nbsp;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:
      IllegalArgumentException - if any of the parameters contains illegal characters
    • MimeType

      public&nbsp;MimeType(string&nbsp;type, string&nbsp;subtype, Charset&nbsp;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:
      IllegalArgumentException - if any of the parameters contains illegal characters
    • MimeType

      public&nbsp;MimeType(MimeType&nbsp;other, Charset&nbsp;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:
      IllegalArgumentException - if any of the parameters contains illegal characters
      since:
      4.3
    • MimeType

      public&nbsp;MimeType(MimeType&nbsp;other, @Nullable Map<string,string>&nbsp;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:
      IllegalArgumentException - if any of the parameters contains illegal characters
    • MimeType

      public&nbsp;MimeType(string&nbsp;type, string&nbsp;subtype, @Nullable Map<string,string>&nbsp;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:
      IllegalArgumentException - if any of the parameters contains illegal characters
    • MimeType

      protected&nbsp;MimeType(MimeType&nbsp;other)
      Copy-constructor that copies the type, subtype and parameters of the given MimeType, skipping checks performed in other constructors.
      Parameters:
      other - the other MimeType
      since:
      5.3
  • Method Details

    • checkParameters

      protected&nbsp;void&nbsp;checkParameters(string&nbsp;parameter, string&nbsp;value)
    • unquote

      protected&nbsp;string&nbsp;unquote(string&nbsp;s)
    • isWildcardType

      public&nbsp;boolean&nbsp;isWildcardType()
      Indicates whether the type is the wildcard character * or not.
    • isWildcardsubtype

      public&nbsp;boolean&nbsp;isWildcardsubtype()
      Indicates whether the subtype is the wildcard character * or the wildcard character followed by a suffix (for example, *+xml).
      Returns:
      whether the subtype is a wildcard
    • isConcrete

      public&nbsp;boolean&nbsp;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&nbsp;string&nbsp;getType()
      Return the primary type.
    • getsubtype

      public&nbsp;string&nbsp;getsubtype()
      Return the subtype.
    • getsubtypesuffix

      public&nbsp;@Nullable string&nbsp;getsubtypesuffix()
      Return the subtype suffix as defined in RFC 6839.
      since:
      5.3
    • getCharset

      public&nbsp;@Nullable Charset&nbsp;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

      public&nbsp;@Nullable string&nbsp;getParameter(string&nbsp;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&nbsp;Map<string,string>&nbsp;getParameters()
      Return all generic parameter values.
      Returns:
      a read-only map (possibly empty, never null)
    • includes

      public&nbsp;boolean&nbsp;includes(@Nullable MimeType&nbsp;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&nbsp;boolean&nbsp;isCompatibleWith(@Nullable MimeType&nbsp;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(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
    • equalsTypeAndsubtype

      public&nbsp;boolean&nbsp;equalsTypeAndsubtype(@Nullable MimeType&nbsp;other)
      similar to equals(Object) but based on the type and subtype only, i.e. ignoring parameters.
      Parameters:
      other - the other mime type to compare to
      Returns:
      whether the two mime types have the same type and subtype
      since:
      5.1.4
    • isPresentIn

      public&nbsp;boolean&nbsp;isPresentIn(Collection<? extends MimeType>&nbsp;mimeTypes)
      Unlike Collection.contains(Object) which relies on equals(Object), this method only checks the type and the subtype, but otherwise ignores parameters.
      Parameters:
      mimeTypes - the list of mime types to perform the check against
      Returns:
      whether the list contains the given mime type
      since:
      5.1.4
    • equals

      public&nbsp;boolean&nbsp;equals(@Nullable Object&nbsp;other)
      Overrides:
      equals&nbsp;in class&nbsp;Object
    • hashCode

      public&nbsp;int&nbsp;hashCode()
      Overrides:
      hashCode&nbsp;in class&nbsp;Object
    • tostring

      public&nbsp;string&nbsp;tostring()
      Overrides:
      tostring&nbsp;in class&nbsp;Object
    • appendTo

      protected&nbsp;void&nbsp;appendTo(stringBuilder&nbsp;builder)
    • compareTo

      public&nbsp;int&nbsp;compareTo(MimeType&nbsp;other)
      Compares this MIME Type to another alphabetically.
      specified by:
      compareTo&nbsp;in interface&nbsp;Comparable<MimeType>
      Parameters:
      other - the MIME Type to compare to
    • isMorespecific

      public&nbsp;boolean&nbsp;isMorespecific(MimeType&nbsp;other)
      Indicates whether this MimeType is more specific than the given type.
      1. if this mime type has a wildcard type, and the other does not, then this method returns false.
      2. if this mime type does not have a wildcard type, and the other does, then this method returns true.
      3. if this mime type has a wildcard type, and the other does not, then this method returns false.
      4. if this mime type does not have a wildcard type, and the other does, then this method returns true.
      5. if the two mime types have identical type and subtype, then the mime type with the most parameters is more specific than the other.
      6. Otherwise, this method returns false.
      Parameters:
      other - the MimeType to be compared
      Returns:
      the result of the comparison
      since:
      6.0
      see Also:
    • isLessspecific

      public&nbsp;boolean&nbsp;isLessspecific(MimeType&nbsp;other)
      Indicates whether this MimeType is less specific than the given type.
      1. if this mime type has a wildcard type, and the other does not, then this method returns true.
      2. if this mime type does not have a wildcard type, and the other does, then this method returns false.
      3. if this mime type has a wildcard type, and the other does not, then this method returns true.
      4. if this mime type does not have a wildcard type, and the other does, then this method returns false.
      5. if the two mime types have identical type and subtype, then the mime type with the least parameters is less specific than the other.
      6. Otherwise, this method returns false.
      Parameters:
      other - the MimeType to be compared
      Returns:
      the result of the comparison
      since:
      6.0
      see Also:
    • valueOf

      public static&nbsp;MimeType&nbsp;valueOf(string&nbsp;value)
      Parse the given string value into a MimeType object, with this method name following the 'valueOf' naming convention (as supported by Conversionservice).
      see Also: