Class MimeTypes

java.lang.Object
org.eclipse.jetty.http.MimeTypes
Direct Known Subclasses:
MimeTypes.Mutable

public class MimeTypes extends Object
MIME Type enum and utilities
  • Field Details

    • ISO_8859_1

      public static final String ISO_8859_1
    • UTF8

      public static final String UTF8
    • UTF16

      public static final String UTF16
    • CACHE

      public static final Index<MimeTypes.Type> CACHE
    • _mimeMap

      protected final Map<String,String> _mimeMap
    • _inferredEncodings

      protected final Map<String,Charset> _inferredEncodings
    • _assumedEncodings

      protected final Map<String,Charset> _assumedEncodings
    • _assumedNoEncodings

      protected final Set<String> _assumedNoEncodings
    • DEFAULTS

      public static final MimeTypes DEFAULTS
  • Constructor Details

    • MimeTypes

      public MimeTypes()
    • MimeTypes

      public MimeTypes(MimeTypes defaults)
  • Method Details

    • getBase

      public static String getBase(String value)
      Get the base value, stripped of any parameters
      Parameters:
      value - The value
      Returns:
      A string with any semicolon separated parameters removed
    • getBaseType

      public static MimeTypes.Type getBaseType(String contentType)
      Get the base type of this type, which is the type without a charset specified
      Parameters:
      contentType - The mimetype as a string
      Returns:
      The base type or this type if it is a base type
    • isKnownLocale

      public static boolean isKnownLocale(Locale locale)
    • normalizeCharset

      public static String normalizeCharset(String charsetName)
      Convert alternate charset names (eg utf8) to normalized name (eg UTF-8).
      Parameters:
      charsetName - the charset to normalize
      Returns:
      the normalized charset (or null if normalized version not found)
    • normalizeCharset

      public static String normalizeCharset(String charsetName, int offset, int length)
      Convert alternate charset names (eg utf8) to normalized name (eg UTF-8).
      Parameters:
      charsetName - the charset to normalize
      offset - the offset in the charset
      length - the length of the charset in the input param
      Returns:
      the normalized charset (or null if not found)
    • getKnownCharset

      public static Charset getKnownCharset(String charsetName) throws UnsupportedEncodingException
      Parameters:
      charsetName - The name of the charset
      Returns:
      The Charset for the normalized name
      Throws:
      UnsupportedEncodingException - Thrown if the charset is not known to the JVM.
    • loadMimeProperties

      protected void loadMimeProperties(InputStream stream, String resourceName) throws IOException
      Throws:
      IOException
    • loadEncodings

      protected void loadEncodings(InputStream stream, String resourceName) throws IOException
      Throws:
      IOException
    • getCharset

      Get the explicit, assumed, or inferred Charset for a HttpField containing a mime type value
      Parameters:
      field - HttpField with a mime type value (e.g. Content-Type)
      Returns:
      A Charset or null;
      Throws:
      IllegalCharsetNameException - If the given charset name is illegal
      UnsupportedCharsetException - If no support for the named charset is available in this instance of the Java virtual machine
    • getCharset

      Get the explicit, assumed, or inferred Charset for a mime type
      Parameters:
      mimeType - String form or a mimeType
      Returns:
      A Charset or null;
      Throws:
      IllegalCharsetNameException - If the given charset name is illegal
      UnsupportedCharsetException - If no support for the named charset is available in this instance of the Java virtual machine
    • getMimeByExtension

      public String getMimeByExtension(String filename)
      Get the MIME type by filename extension.
      Parameters:
      filename - A file name
      Returns:
      MIME type matching the last dot extension of the file name, or matching "*" if none found.
    • getMimeForExtension

      public String getMimeForExtension(String extension)
    • getInferredCharset

      public Charset getInferredCharset(String contentType)
      Parameters:
      contentType - The content type to obtain a charset for.
      Returns:
      A Charset is returned if it can be inferred from content-type. This is essentially a default charset determined for the contentType. For example, the content-type "text/html" may be configured to have an inferred charset of "utf-8", in which case setting that content-type should result in a value of "text/html;charset=utf8".
      See Also:
    • getAssumedCharset

      public Charset getAssumedCharset(String contentType)
      Parameters:
      contentType - The content type to obtain a charset for.
      Returns:
      A Charset is returned if it can be assumed from content-type. This is essentially a known charset for the specific contentType. For example, the content-type "application/json" is specified to use utf-8, so it has an assumed charset of "utf-8". As this is universally known, there is no need to modify the the content-type which will just have a value of "application/json". Note that some content-types may be assumed to have no charset, in which case isCharsetAssumed(String) must be used.
      See Also:
    • isCharsetAssumed

      public boolean isCharsetAssumed(String contentType)
      Parameters:
      contentType - The content-type to obtain a charset for
      Returns:
      True if the content-type is assumed to have a specific charset (include assumed to have no charset. For example "application/json" is assumed as it has a specified charset of "utf-8". Another example is "image/jpeg", which is assumed to have no charset, so it would also return true.
    • getInferredCharsetName

      public String getInferredCharsetName(String contentType)
      Parameters:
      contentType - The content type to obtain a charset for.
      Returns:
      The string value of getInferredCharset(String)
      See Also:
    • getAssumedCharsetName

      public String getAssumedCharsetName(String contentType)
      Parameters:
      contentType - The content type to obtain a charset for.
      Returns:
      The string value of getAssumedCharset(String); or the empty string if the type is assumed to not have a charset; or null of the type has no assumed charset.
      See Also:
    • getMimeMap

      public Map<String,String> getMimeMap()
    • getMimeTypeFromContentType

      public static MimeTypes.Type getMimeTypeFromContentType(HttpField field)
    • getMimeTypeAsStringFromContentType

      public static String getMimeTypeAsStringFromContentType(HttpField field)
    • getCharsetFromContentType

      public static Charset getCharsetFromContentType(HttpField field)
      Efficiently extract the charset value from a Content-Type HttpField.
      Parameters:
      field - A Content-Type field.
      Returns:
      The Charset
    • getCharsetFromContentType

      public static String getCharsetFromContentType(String value)
      Efficiently extract the charset value from a Content-Type string
      Parameters:
      value - A content-type value (e.g. text/plain; charset=utf8).
      Returns:
      The charset value (e.g. utf-8).
    • getContentTypeWithoutCharset

      public static String getContentTypeWithoutCharset(String value)
      Efficiently extract the base mime-type from a content-type value
      Parameters:
      value - A content-type value (e.g. text/plain; charset=utf8).
      Returns:
      The base mime-type value (e.g. text/plain).