Class MimeTypeUtils

java.lang.Object
com.okta.commons.http.MimeTypeUtils

public final class MimeTypeUtils extends Object
Miscellaneous MimeType utility methods.
Since:
4.0
Author:
Arjen Poutsma, Rossen Stoyanchev
  • Field Details

  • Method Details

    • parseMimeType

      public static MimeType parseMimeType(String mimeType)
      Parse the given String into a single MimeType.
      Parameters:
      mimeType - the string to parse
      Returns:
      the mime type
      Throws:
      InvalidMimeTypeException - if the string cannot be parsed
    • parseMimeTypes

      public static List<MimeType> parseMimeTypes(String mimeTypes)
      Parse the given, comma-separated string into a list of MimeType objects.
      Parameters:
      mimeTypes - the string to parse
      Returns:
      the list of mime types
      Throws:
      IllegalArgumentException - if the string cannot be parsed
    • toString

      public static String toString(Collection<? extends MimeType> mimeTypes)
      Return a string representation of the given list of MimeType objects.
      Parameters:
      mimeTypes - the string to parse
      Returns:
      the list of mime types
      Throws:
      IllegalArgumentException - if the String cannot be parsed
    • sortBySpecificity

      public static void sortBySpecificity(List<MimeType> mimeTypes)
      Sorts the given list of MimeType objects by specificity.

      Given two mime types:

      1. if either mime type has a wildcard type, then the mime type without the wildcard is ordered before the other.
      2. if the two mime types have different types, then they are considered equal and remain their current order.
      3. if either mime type has a wildcard subtype , then the mime type without the wildcard is sorted before the other.
      4. if the two mime types have different subtypes, then they are considered equal and remain their current order.
      5. if the two mime types have a different amount of parameters, then the mime 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:
      mimeTypes - the list of mime types to be sorted
      See Also:
    • generateMultipartBoundary

      public static byte[] generateMultipartBoundary()
      Generate a random MIME boundary as bytes, often used in multipart mime types.
      Returns:
      a multipart boundary byte array
    • generateMultipartBoundaryString

      public static String generateMultipartBoundaryString()
      Generate a random MIME boundary as String, often used in multipart mime types.
      Returns:
      a multipart boundary string