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.
The Servlet API specification provides a limited support for MIME type mappings :
mime-mapping elements of the the web application descriptor web.xml. Managing these mappings is presumably tedious. So servlet containers may provide reasonable defaults (or not).ServletContext.getMimeType(String) returns a MIME type for a given file name based on the extension of the filename. The mapping returned is based on the servlet container configuration as well as the web application descriptor's mime-mapping elements.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:
getExtension(String) -- given a MIME type this methods returns a primary extension. For example for the type text/plain this method will return txtgetMimeType(String) -- given a file name or just the extension) returns the mapped MIME type. For example for the filename sample.html (or just the extension html) this method will return text/htmlTwo more methods allow to programmatically add MIME type mappings:
registerMimeType(InputStream) registers additional mappings from the given input stream which is expected to be formated in traditional mime.types file format (see below).registerMimeType(String, String...) registers a single mapping for the give MIME type and the respective extensions.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:
getMimeType(String filename, InputStream content) -- given a filename and an InputStream that points to the file contents, this method first tries content-based detection using the stream, and falls back to filename-based detection if needed.Besides the MimeTypeService provided by Apache Sling, there is actually more:
SlingHttpServletRequest provides the getResponseContentType() method, which returns the preferred Content-Type for the response based on the requests extension. This method is implemented by Apache Sling using the MimeTypeService. So servlets and scripts may just call this method to set the content type of the response to the desired value.ServletContext instance whose implementation of the getMimeType(String) effectively calls the MimeTypeService.getMimeType(String) method.SlingHttpServletRequest.getResponseContentType() method. At the same time the response character set is also set to UTF-8 for text content types.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:
mime.types file maintained by Roy Fielding for the Apache httpd project and some extensions by Apache Sling.META-INF/mime.types which are loaded automatically by the Apache Sling MIME type mapping support bundle.MimeTypeService implementation as the multi-value string property mime.types. Each value of the property corresponds to a line in a MIME type configuration file (see below for the format).MimeTypeService.registerMapping methods.MimeTypeProvider. Additional mappings may be provided by service implementing the MimeTypeProvider interface. The MimeTypeService implementation will call these services in turn until a service returns a mapping provided there is no static configuration to responde to the mapping request.Please note, that existing mappings cannot be overwritten later. Thus mappings have an inherent priority:
mime.types fileMETA-INF/mime.types file in the order of their bundle-id (or startup order if started after the MIME type mapping support bundle)MimeTypeService.registerMimeType methodMimeTypeProvider servicesThe file format for MIME type mapping files is rather simple:
#) are ignored\s regular expression) separated values. The first value is the MIME type name and the remaining values defining mappings to file name extensions. The first listed file name extension is considered the default mapping and is returned by the MimeTypeService.getExtension(String) method. Entry lines consisting of just a mime type but no extensions are also (currently) ignored.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.
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.
