java.lang.Object
org.klomp.snark.web.MimeTypes

class MimeTypesextends Object
Based on MimeTypes from Jetty 6.1.26, heavily simplified and modified to remove all dependencies on Jetty libs.

Supports mime types only, not encodings. Does not support a default "*" mapping.

This is only for local mappings. Caller should use getServletContext().getMimeType() if this returns null.

  • Field Details

    • _mimeMap

      private final Map<String,String> _mimeMap
      File extension to content type, loaded from the mime database.
  • Constructor Details

    • MimeTypes

      public MimeTypes()
      Create an empty table, pre-loaded with the system MIME map on Unix hosts with at least 100MB of heap; skipped on Windows, macOS and small-heap configurations.
  • Method Details

    • loadMimeMap

      public void loadMimeMap(String resourcePath)
      Load MIME type mappings from a resource file.
      Parameters:
      resourcePath - the resource path in the classpath, without ".properties" extension
    • loadMimeMap

      private static void loadMimeMap(Map<String,String> map, String resourcePath)
      Tries both webapp and system class loader, since Jetty blocks its classes from the webapp class loader.
      Parameters:
      map - the map filled with lower-case extension to MIME type mappings
      resourcePath - the resource path in the classpath, without the ".properties" extension
    • loadSystemMimeTypes

      private void loadSystemMimeTypes()
      Load mime types from /etc/mime.types. Format: mimetype suffix1 suffix2 ...
    • getMimeByExtension

      public String getMimeByExtension(String filename)
      The MIME type by filename extension.

      Returns ONLY local mappings. Caller should use getServletContext().getMimeType() if this returns null.

      Parameters:
      filename - A file name
      Returns:
      MIME type matching the longest dot extension of the file name, or null if not found
    • addMimeMapping

      public void addMimeMapping(String extension, String type)
      Add a MIME mapping.
      Parameters:
      extension - the file extension (without the dot), case-insensitive
      type - the MIME type to associate with the extension