MIME Type Support has been created by Felix Meschberger (May 17, 2009).

Content:

MIME Type Support

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 :

  • Mappings may be defined in the 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).
  • The 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.

Enter Sling: The 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.

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 txt
  • getMimeType(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/html

Two more method 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.

And more...

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

  • The 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.
  • Each Servlet (and JSP scripts) is initialized by Apache Sling with a ServletContext instance whose implementation of the getMimeType(String) effectively calls the MimeTypeService.getMimeType(String) method.
  • The Scripting support infrastructure of Sling sets the response content type on behalf of the script to the default value as returned by the SlingHttpServletRequest.getResponseContentType() method. At the same time the response character set is also set to UTF-8 for

Reply via email to