eu.medsea.mimeutil
Class MimeUtil

java.lang.Object
  extended by eu.medsea.mimeutil.MimeUtil

public class MimeUtil
extends Object

The MimeUtil utility is a utility class that allows applications to detect, work with and manipulate mime types.

A mime or "Multipurpose Internet Mail Extension" type is an Internet standard that is important outside of just e-mail use. Mime is used extensively in other communications protocols such as HTTP for web communications. IANA "Internet Assigned Numbers Authority" is responsible for the standardisation and publication of mime types. Basically any resource on any computer that can be located via a URI can be assigned a mime type. So for instance, JPEG images have a mime type of image/jpg. Some resources can have multiple mime types associated with them such as files with an XML extension have the mime types text/xml and application/xml and even specialised versions of xml such as image/svg+xml for SVG image files.

To do this MimeUtil uses registered MimeDetector(s) that are delegated too in sequence to actually perform the detection. There a three MimeDetector(s) registered by default that perform detection based on file extensions, file globing and magic number detection using the Unix file(1) magic.mime files. Please refer to the java doc for each of these MimeDetector(s) for a description of how they actually perform their particular detection process.

It is important to note that mime matching is not an exact science, meaning that a positive match does not guarantee that the returned mime type is actually correct. It is a best guess method of matching and the matched mime types should be used with this in mind.

New MimeDetector(s) can easily be created and registered with MimeUtil to extend it's functionality beyond these initial detection strategies by extending the AbstractMimeDetector class. To see how to implement your own MimeDetector and register it with MimeUtil take a look at the java doc and source code for the ExtensionMimeDetector, MagicMimeMimeDetector and GlobingMimeDetector classes. Previously registered MimeDetector(s) can also be un-registered at any time.

The order that the MimeDetector(s) are executed is defined by the priority of the individual MimeDetector(s) and MimeDetector(s) with the same priority are executed in the order they are registered.

The resulting Collection of mime types returned in response to a getMimeTypes(...) call is a normalised list of the accumulation of mime types returned by each of the registered MimeDetector(s) that implement the specified getMimeTypes(...) methods. This Collection of mime types can be influenced using MimeHandler(s) that can be registered against one or more MimeDetector(s) that are able to manipulate the Collection of mime types that will be returned to the client.

All methods in this class that return a Collection object actually return a MimeTypeHashSet that implements both the Set and Collection interfaces.

Author:
Steven McArdle.

Field Summary
static MimeType DIRECTORY_MIME_TYPE
          Mime type used to identify a directory
static MimeType UNKNOWN_MIME_TYPE
          Mime type used to identify a directory
 
Constructor Summary
MimeUtil()
           
 
Method Summary
static void addKnownMimeType(MimeType mimeType)
          While MimeType(s) are being loaded by the MimeDetector(s) they should be added to the list of known mime types.
static void addKnownMimeType(String mimeType)
          While MimeType(s) are being loaded by the MimeDetector(s) they should be added to the list of known mime types.
static void addMimeDetector(MimeDetector mimeDetector)
          Add a MimeDetector to the MimeDetector registry.
static String getExtension(File file)
          Get the extension part of a file name defined by the file parameter.
static String getExtension(String fileName)
          Get the extension part of a file name defined by the fileName parameter.
static MimeType getFirstMimeType(String mimeTypes)
          Get the first in a comma separated list of mime types.
static String getMediaType(String mimeType)
          Utility method to get the major or media part of a mime type i.e.
static MimeDetector getMimeDetector(String name)
          Get a registered MimeDetector by name.
static double getMimeQuality(String mimeType)
          Utility method to get the quality part of a mime type.
static Collection getMimeTypes(byte[] data)
          TODO: Add description
static Collection getMimeTypes(byte[] data, MimeType unknownMimeType)
          TODO: Add desription
static Collection getMimeTypes(File file)
          Get all of the matching mime types for this file object.
static Collection getMimeTypes(File file, MimeType unknownMimeType)
          Get all of the matching mime types for this file object.
static Collection getMimeTypes(InputStream in)
          Get all of the matching mime types for this InputStream object.
static Collection getMimeTypes(InputStream in, MimeType unknownMimeType)
          Get all of the matching mime types for this InputStream object.
static Collection getMimeTypes(String fileName)
          Get all of the matching mime types for this file name.
static Collection getMimeTypes(String fileName, MimeType unknownMimeType)
          Get all of the matching mime types for this file name .
static Collection getMimeTypes(URLConnection url)
          Get all of the matching mime types for this URLConnection object.
static Collection getMimeTypes(URLConnection url, MimeType unknownMimeType)
          Get all of the matching mime types for this URLConnection object.
static MimeType getMostSpecificMimeType(Collection mimeTypes)
          Get the most specific match of the Collection of mime types passed in.
static ByteOrder getNativeOrder()
          Get the native byte order of the OS on which you are running.
static MimeType getPreferedMimeType(String accept, String canProvide)
          Gives you the best match for your requirements.
static double getQuality(String mimeType)
          Get the quality parameter of this mime type i.e.
static String getSubType(String mimeType)
          Utility method to get the minor part of a mime type i.e.
static boolean isMimeTypeKnown(MimeType mimeType)
          Check to see if this mime type is one of the types seen during initialisation or has been added at some later stage using addKnownMimeType(...)
static boolean isMimeTypeKnown(String mimeType)
          Check to see if this mime type is one of the types seen during initialisation or has been added at some later stage using addKnownMimeType(...)
static boolean isTextMimeType(MimeType mimeType)
          Utility convenience method to check if a particular MimeType instance is actually a TextMimeType.
static MimeDetector removeMimeDetector(MimeDetector mimeDetector)
          Remove a previously registered MimeDetector
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DIRECTORY_MIME_TYPE

public static final MimeType DIRECTORY_MIME_TYPE
Mime type used to identify a directory


UNKNOWN_MIME_TYPE

public static final MimeType UNKNOWN_MIME_TYPE
Mime type used to identify a directory

Constructor Detail

MimeUtil

public MimeUtil()
Method Detail

addKnownMimeType

public static void addKnownMimeType(MimeType mimeType)
While MimeType(s) are being loaded by the MimeDetector(s) they should be added to the list of known mime types. It is not mandatory for MimeDetector(s) to do so but they should where possible so that the list is as complete as possible. You can add other mime types to this list using this method. You can then use the isMimeTypeKnown(...) utility method to see if a mime type you have matches one that the utility has already seen.

This can be used to limit the mime types you work with i.e. if its not been loaded then don't bother using it as it won't match. This is no guarantee that a match will not be found as it is possible that a particular MimeDetector does not have an initialisation phase that loads all of the mime types it will match.

For instance if you had a mime type of abc/xyz and passed this to isMimeTypeKnown(...) it would return false unless you specifically add this to the know mime types using this method.

Parameters:
mimeType - a mime type you want to add to the known mime types. Duplicates are ignored.
See Also:
isMimeTypeKnown(String mimetype)

addKnownMimeType

public static void addKnownMimeType(String mimeType)
While MimeType(s) are being loaded by the MimeDetector(s) they should be added to the list of known mime types. It is not mandatory for MimeDetector(s) to do so but they should where possible so that the list is as complete as possible. You can add other mime types to this list using this method. You can then use the isMimeTypeKnown(...) utility method to see if a mime type you have matches one that the utility has already seen.

This can be used to limit the mime types you work with i.e. if its not been loaded then don't bother using it as it won't match. This is no guarantee that a match will not be found as it is possible that a particular MimeDetector does not have an initialisation phase that loads all of the mime types it will match.

For instance if you had a mime type of abc/xyz and passed this to isMimeTypeKnown(...) it would return false unless you specifically add this to the know mime types using this method.

Parameters:
mimeType - a mime type you want to add to the known mime types. Duplicates are ignored.
See Also:
isMimeTypeKnown(String mimetype)

addMimeDetector

public static void addMimeDetector(MimeDetector mimeDetector)
Add a MimeDetector to the MimeDetector registry. MimeDetector(s) are effectively singletons as they are keyed against their fully qualified class name.

Parameters:
mimeDetector. - This must be an AbstractMimeDetector class and not a MimeUtil interface. This enforces that all custom MimeDetector(s) extend the AbstractMimeDetector rather than just implementing the MimeDetector interface.
See Also:
MimeDetector, MimeDetector

getExtension

public static String getExtension(File file)
Get the extension part of a file name defined by the file parameter.

Parameters:
file - a file object
Returns:
the file extension or null if it does not have one.

getExtension

public static String getExtension(String fileName)
Get the extension part of a file name defined by the fileName parameter. There may be no extension or it could be a single part extension such as .bat or a multi-part extension such as tar.gz

Parameters:
fileName - a relative or absolute path to a file
Returns:
the file extension or null if it does not have one.

getFirstMimeType

public static MimeType getFirstMimeType(String mimeTypes)
Get the first in a comma separated list of mime types. Useful when using extension mapping that can return multiple mime types separate by commas and you only want the first one.

Parameters:
mimeTypes - comma separated list of mime types
Returns:
first in a comma separated list of mime types or null if the mimeTypes string is null or empty

getMediaType

public static String getMediaType(String mimeType)
                           throws MimeException
Utility method to get the major or media part of a mime type i.e. the bit before the '/' character

Parameters:
mimeType - you want to get the media part from
Returns:
media type of the mime type
Throws:
MimeException - if you pass in an invalid mime type structure

getMimeQuality

public static double getMimeQuality(String mimeType)
                             throws MimeException
Utility method to get the quality part of a mime type. If it does not exist then it is always set to q=1.0 unless it's a wild card. For the major component wild card the value is set to 0.01 For the minor component wild card the value is set to 0.02

Thanks to the Apache organisation or these settings.

Parameters:
mimeType - a valid mime type string with or without a valid q parameter
Returns:
the quality value of the mime type either calculated from the rules above or the actual value defined.
Throws:
MimeException - this is thrown if the mime type pattern is invalid.

getMimeDetector

public static MimeDetector getMimeDetector(String name)
Get a registered MimeDetector by name.

Parameters:
name - the name of a registered MimeDetector. This is always the fully qualified name of the class implementing the MimeDetector.
Returns:

getMimeTypes

public static Collection getMimeTypes(byte[] data)
                               throws MimeException
TODO: Add description

Parameters:
data -
Returns:
Throws:
MimeException

getMimeTypes

public static Collection getMimeTypes(byte[] data,
                                      MimeType unknownMimeType)
                               throws MimeException
TODO: Add desription

Parameters:
data -
unknownMimeType -
Throws:
MimeException

getMimeTypes

public static Collection getMimeTypes(File file)
                               throws MimeException
Get all of the matching mime types for this file object. The method delegates down to each of the registered MimeHandler(s) and returns a normalised list of all matching mime types. If no matching mime types are found the returned Collection will contain the default UNKNOWN_MIME_TYPE

Parameters:
file - the File object to detect.
Returns:
collection of matching MimeType(s)
Throws:
MimeException - if there are problems such as reading files generated when the MimeHandler(s) executed.

getMimeTypes

public static Collection getMimeTypes(File file,
                                      MimeType unknownMimeType)
                               throws MimeException
Get all of the matching mime types for this file object. The method delegates down to each of the registered MimeHandler(s) and returns a normalised list of all matching mime types. If no matching mime types are found the returned Collection will contain the unknownMimeType passed in.

Parameters:
file - the File object to detect.
unknownMimeType. -
Returns:
the Collection of matching mime types. If the collection would be empty i.e. no matches then this will contain the passed in parameter unknownMimeType
Throws:
MimeException - if there are problems such as reading files generated when the MimeHandler(s) executed.

getMimeTypes

public static Collection getMimeTypes(InputStream in)
                               throws MimeException
Get all of the matching mime types for this InputStream object. The method delegates down to each of the registered MimeHandler(s) and returns a normalised list of all matching mime types. If no matching mime types are found the returned Collection will contain the default UNKNOWN_MIME_TYPE

Parameters:
in - InputStream to detect.
Returns:
Throws:
MimeException - if there are problems such as reading files generated when the MimeHandler(s) executed.

getMimeTypes

public static Collection getMimeTypes(InputStream in,
                                      MimeType unknownMimeType)
                               throws MimeException
Get all of the matching mime types for this InputStream object. The method delegates down to each of the registered MimeHandler(s) and returns a normalised list of all matching mime types. If no matching mime types are found the returned Collection will contain the unknownMimeType passed in.

Parameters:
in - the InputStream object to detect.
unknownMimeType. -
Returns:
the Collection of matching mime types. If the collection would be empty i.e. no matches then this will contain the passed in parameter unknownMimeType
Throws:
MimeException - if there are problems such as reading files generated when the MimeHandler(s) executed.

getMimeTypes

public static Collection getMimeTypes(String fileName)
                               throws MimeException
Get all of the matching mime types for this file name. The method delegates down to each of the registered MimeHandler(s) and returns a normalised list of all matching mime types. If no matching mime types are found the returned Collection will contain the default UNKNOWN_MIME_TYPE

Parameters:
fileName - the name of a file to detect.
Returns:
collection of matching MimeType(s)
Throws:
MimeException - if there are problems such as reading files generated when the MimeHandler(s) executed.

getMimeTypes

public static Collection getMimeTypes(String fileName,
                                      MimeType unknownMimeType)
                               throws MimeException
Get all of the matching mime types for this file name . The method delegates down to each of the registered MimeHandler(s) and returns a normalised list of all matching mime types. If no matching mime types are found the returned Collection will contain the unknownMimeType passed in.

Parameters:
fileName - the name of a file to detect.
unknownMimeType. -
Returns:
the Collection of matching mime types. If the collection would be empty i.e. no matches then this will contain the passed in parameter unknownMimeType
Throws:
MimeException - if there are problems such as reading files generated when the MimeHandler(s) executed.

getMimeTypes

public static Collection getMimeTypes(URLConnection url)
                               throws MimeException
Get all of the matching mime types for this URLConnection object. The method delegates down to each of the registered MimeHandler(s) and returns a normalised list of all matching mime types. If no matching mime types are found the returned Collection will contain the default UNKNOWN_MIME_TYPE

Parameters:
url - a URL to detect.
Returns:
collection of matching MimeType(s)
Throws:
MimeException - if there are problems such as reading files generated when the MimeHandler(s) executed.

getMimeTypes

public static Collection getMimeTypes(URLConnection url,
                                      MimeType unknownMimeType)
                               throws MimeException
Get all of the matching mime types for this URLConnection object. The method delegates down to each of the registered MimeHandler(s) and returns a normalised list of all matching mime types. If no matching mime types are found the returned Collection will contain the unknownMimeType passed in.

Parameters:
url - the URL to detect.
unknownMimeType. -
Returns:
the Collection of matching mime types. If the collection would be empty i.e. no matches then this will contain the passed in parameter unknownMimeType
Throws:
MimeException - if there are problems such as reading files generated when the MimeHandler(s) executed.

getNativeOrder

public static ByteOrder getNativeOrder()
Get the native byte order of the OS on which you are running. It will be either big or little endian. This is used internally for the magic mime rules mapping.

Returns:
ByteOrder

getPreferedMimeType

public static MimeType getPreferedMimeType(String accept,
                                           String canProvide)
Gives you the best match for your requirements.

You can pass the accept header from a browser request to this method along with a comma separated list of possible mime types returned from say getExtensionMimeTypes(...) and the best match according to the accept header will be returned.

The following is typical of what may be specified in an HTTP Accept header:

Accept: text/xml, application/xml, application/xhtml+xml, text/html;q=0.9, text/plain;q=0.8, video/x-mng, image/png, image/jpeg, image/gif;q=0.2, text/css, */*;q=0.1

The quality parameter (q) indicates how well the user agent handles the MIME type. A value of 1 indicates the MIME type is understood perfectly, and a value of 0 indicates the MIME type isn't understood at all.

The reason the image/gif MIME type contains a quality parameter of 0.2, is to indicate that PNG & JPEG are preferred over GIF if the server is using content negotiation to deliver either a PNG or a GIF to user agents. Similarly, the text/html quality parameter has been lowered a little, to ensure that the XML MIME types are given in preference if content negotiation is being used to serve an XHTML document.

Parameters:
accept - is a comma separated list of mime types you can accept including QoS parameters. Can pass the Accept: header directly.
canProvide - is a comma separated list of mime types that can be provided such as that returned from a call to getExtensionMimeTypes(...)
Returns:
the best matching mime type possible.

getMostSpecificMimeType

public static MimeType getMostSpecificMimeType(Collection mimeTypes)
Get the most specific match of the Collection of mime types passed in. The Collection

Parameters:
mimeTypes - this should be the Collection of mime types returned from a getMimeTypes(...) call.
Returns:
the most specific MimeType. If more than one of the mime types in the Collection have the same value then the first one found with this value in the Collection is returned.

getSubType

public static String getSubType(String mimeType)
                         throws MimeException
Utility method to get the minor part of a mime type i.e. the bit after the '/' character

Parameters:
mimeType - you want to get the minor part from
Returns:
sub type of the mime type
Throws:
MimeException - if you pass in an invalid mime type structure

isMimeTypeKnown

public static boolean isMimeTypeKnown(MimeType mimeType)
Check to see if this mime type is one of the types seen during initialisation or has been added at some later stage using addKnownMimeType(...)

Parameters:
mimeType -
Returns:
true if the mimeType is in the list else false is returned
See Also:
addKnownMimeType(String mimetype)

isMimeTypeKnown

public static boolean isMimeTypeKnown(String mimeType)
Check to see if this mime type is one of the types seen during initialisation or has been added at some later stage using addKnownMimeType(...)

Parameters:
mimeType -
Returns:
true if the mimeType is in the list else false is returned
See Also:
addKnownMimeType(String mimetype)

isTextMimeType

public static boolean isTextMimeType(MimeType mimeType)
Utility convenience method to check if a particular MimeType instance is actually a TextMimeType. Used when iterating over a collection of MimeType's to help with casting to enable access the the TextMimeType methods not available to a standard MimeType.

Parameters:
mimeType -
Returns:
true if the passed in instance is a TextMimeType
See Also:
MimeType, TextMimeType

removeMimeDetector

public static MimeDetector removeMimeDetector(MimeDetector mimeDetector)
Remove a previously registered MimeDetector

Parameters:
mimeDetector -
Returns:
the MimeDetector that was removed from the registry else null.

getQuality

public static double getQuality(String mimeType)
                         throws MimeException
Get the quality parameter of this mime type i.e. the q= property. This method implements a value system similar to that used by the apache server i.e. if the media type is a * then it's q value is set to 0.01 and if the sub type is a * then the q value is set to 0.02 unless a specific q value is specified. If a q property is set it is limited to a max value of 1.0

Parameters:
mimeType -
Returns:
the quality value as a double between 0.0 and 1.0
Throws:
MimeException


Copyright © 2007-2009 Medsea Business Solutions S.L.. All Rights Reserved.