Home » Documentation » Bundles

MIME Type Support (commons.mime and commons.contentdetection)

Support for MIME type mappings is generally a problematic issue. On the one hand applications have to take care to stay up to date with their mappings on the other hands in web applications it is tedious to maintain the mappings. Apache Sling takes a very user and deployment friendly approadch to this problem which is described in detail on this page.

Servlet API Support

The Servlet API specification provides a limited support for MIME type mappings :

The Sling MimeTypeService

Already at the start of the Sling project we realized, that just basing the MIME type mapping decisions on the servlet container will not yield acceptable results. For this reason the Apache Sling projects provides a spezialized and configurable service supporting such mappings: The MimeTypeService provided by the org.apache.sling.commons.mime bundle.

This service provides access to registered MIME types and their mappings with two methods:

Two more methods allow to programmatically add MIME type mappings:

The Sling ContentAwareMimeTypeService

For content-based mime type detection (as opposed to filename-based detection), the org.apache.sling.commons.contentdetection bundle provides the ContentAwareMimeTypeService, which takes an InputStream that's analyzed to detect its mime type, using Apache Tika by default:

And More...

Besides the MimeTypeService provided by Apache Sling, there is actually more:

Configuring MIME Type Mappings

The implementation of the MimeTypeService in the Apache Sling MIME type mapping support (org.apache.sling.commons.mime) bundle supports a numnber of ways to configure and extend the set of MIME type mappings:

Please note, that existing mappings cannot be overwritten later. Thus mappings have an inherent priority:

  1. Mappings provided by the Apache httpd project's mime.types file
  2. Extensions by the Apache Sling MIME type mapping support bundle
  3. Bundles providing a META-INF/mime.types file in the order of their bundle-id (or startup order if started after the MIME type mapping support bundle)
  4. OSGi Configuration. Note that bundles started after the MIME type mapping support bundle may not overwrite mappings provided by the OSGi configuration. This may sounds like a problem, but in reality this should genrally not matter
  5. Mappings registered calling the MimeTypeService.registerMimeType method
  6. Mappings provided by MimeTypeProvider services

MIME Type Mapping File Format

The file format for MIME type mapping files is rather simple:

THe data line format described here also applies to configuration provided by the values of the mime.types property of the MIME type service configuration. The file format description applies to all META-INF/mime.types files provided by the bundles as well as input streams supplied to the MimeTypeService.registerMimeType(InputStream) method.

Web Console Plugin

The Apache Sling MIME type mapping support bundle implements a plugin for the Apache Felix Web Console which may be consulted to investigate the current contents of the MIME type mapping tables.

Mime Types Web Console Plugin

Rev. 1682193 by bdelacretaz on Thu, 28 May 2015 09:51:42 +0000
Apache Sling, Sling, Apache, the Apache feather logo, and the Apache Sling project logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.