eu.medsea.util
Class MimeUtil

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

Deprecated. Use MimeUtil instead!

public class MimeUtil
extends Object

The MimeUtil utility class is used to detect mime types from either a files extension or by looking into the file at various offsets and comparing or looking for certain values in the same way as the Unix file(1) command.

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

Both the file extension mapping and the magic mime number rules can be extended by the user.

The extension mime mappings are loaded in the following way.

  1. Load the properties file from the mime utility jar named eu.medsea.mime.mime-types.properties.
  2. Locate and load a file named .mime-types.properties from the users home directory if one exists.
  3. Locate and load a file named mime-types.properties from the classpath if one exists
  4. locate and load a file named by the JVM property mime-mappings i.e. -Dmime-mappings=../my-mime-types.properties
Each property file loaded will add to the list of extensions. If there is a clash of extension names then the last one loaded wins, this makes it possible to completely change the mime types associated to a file extension declared in previous property files.

We acquired many mappings from many different sources on the net for the extension mappings. The internal list is quite large and there can be many associated mime types. These may not match what you are expecting so you can add the mapping you want to change to your own property file following the rules above. If you provide a mapping for an extension then any previously loaded mappings will be removed and only the mappings you define will be returned. This can be used to map certain extensions that are incorrectly returned for our environment defined in the internal property file.

If we have not provided a mapping for a file extension that you know the mime type for you can add this to your custom property file so that a correct mime type is returned for you.

The magic mime rules files are loaded in the following way.

  1. From a JVM system property magic-mime i.e -Dmagic-mime=../my/magic/mime/rules
  2. From any file named magic.mime that can be found on the classpath
  3. From a file named .magic.mime in the users home directory
  4. From the normal Unix locations /usr/share/file/magic.mime and /etc/magic.mime (in that order)
  5. From the internal magic.mime file eu.medsea.mime.magic.mime if, and only if, no files are located in step 4 above.
Each rule file is appended to the end of the existing rules so the earlier in the sequence you define a rule means this will take precedence over rules loaded later.

As with the extension mappings you can add new mime mapping rules using the syntax defined for the Unix magic.mime file by placing these rules in any of the files or locations listed above. You can also change an existing mapping rule by redefining the existing rule in one of the files listed above. This is handy for some of the more sketchy rules defined in the existing Unix magic.mime files.

When using the utility methods we always try to return a mime type even if no mapping can be found. In the case of no mapping we have defined the utility to return application/octet-stream by default. This can be overridden and you can have a no-match return any mime type you define, even unofficial mime types that you made up just for your application such as application/x-unknown-mime-type. This allows your application to provide special handling on a no-match such as executing a custom business process.

We use the application/directory mime type to identify directories. Even though this is not an official mime type it seems to be well accepted on the net as an unofficial mime type so we thought it was OK for us to use as well.

Author:
Steven McArdle.

Field Summary
static String DIRECTORY_MIME_TYPE
          Deprecated. Mime type used to identify a directory
static String UNKNOWN_MIME_TYPE
          Deprecated. Mime type used to identify no match
 
Constructor Summary
MimeUtil()
          Deprecated.  
 
Method Summary
static void addKnownMimeType(String mimeType)
          Deprecated. While all of the property files and magic.mime files are being loaded the utility keeps a list of mime types it's seen.
static String getExtensionMimeTypes(File file)
          Deprecated. Get the mime type of a file using file extension mappings.
static String getExtensionMimeTypes(String fname)
          Deprecated. Get the mime type of a file using file extension mappings.
static String getFileExtension(File file)
          Deprecated. Get the extension part of a file name defined by the file parameter.
static String getFileExtension(String fileName)
          Deprecated. Get the extension part of a file name defined by the fname parameter.
static String getFirstMimeType(String mimeTypes)
          Deprecated. Get the first in a comma separated list of mime types.
static String getMagicMimeType(File file)
          Deprecated. Get the mime type of a file using the magic.mime rules files.
static String getMagicMimeType(String fname)
          Deprecated. Get the mime type of a file using the magic.mime rules files.
static String getMajorComponent(String mimeType)
          Deprecated. Utility method to get the major part of a mime type i.e.
static double getMimeQuality(String mimeType)
          Deprecated. Utility method to get the quality part of a mime type.
static String getMimeType(File f)
          Deprecated. This is a convenience method where the order of lookup is set to extension mapping first.
static String getMimeType(File file, boolean extFirst)
          Deprecated. Get the mime type of a file using a File object which can be relative to the JVM or an absolute path.
static String getMimeType(InputStream in)
          Deprecated. Get the mime type of the data in the specified InputStream.
static String getMimeType(String fname)
          Deprecated. This is a convenience method where the order of lookup is set to extension mapping first.
static String getMimeType(String fname, boolean extFirst)
          Deprecated. Get the mime type of a file using a path which can be relative to the JVM or an absolute path.
static String getMinorComponent(String mimeType)
          Deprecated. Utility method to get the minor part of a mime type i.e.
static ByteOrder getNativeOrder()
          Deprecated. Get the native byte order of the OS on which you are running.
static String getPreferedMimeType(String accept, String canProvide)
          Deprecated. Gives you the best match for your requirements.
static boolean isMimeTypeKnown(String mimeType)
          Deprecated. 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 void setUnknownMimeType(String mimeType)
          Deprecated. The default mime type returned by a no match i.e.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

UNKNOWN_MIME_TYPE

public static String UNKNOWN_MIME_TYPE
Deprecated. 
Mime type used to identify no match


DIRECTORY_MIME_TYPE

public static final String DIRECTORY_MIME_TYPE
Deprecated. 
Mime type used to identify a directory

Constructor Detail

MimeUtil

public MimeUtil()
Deprecated. 
Method Detail

getNativeOrder

public static ByteOrder getNativeOrder()
Deprecated. 
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

getMimeType

public static String getMimeType(InputStream in)
                          throws MimeException
Deprecated. 
Get the mime type of the data in the specified InputStream. Therefore, the InputStream must support mark and reset (see InputStream.markSupported()). If it does not support mark and reset, an MimeException is thrown.

Parameters:
in - the stream from which to read the data.
Returns:
the mime type. Never returns null (if the mime type cannot be found, UNKNOWN_MIME_TYPE is returned).
Throws:
MimeException - if the specified InputStream does not support mark and reset (see InputStream.markSupported()).

getMimeType

public static String getMimeType(String fname,
                                 boolean extFirst)
                          throws MimeException
Deprecated. 
Get the mime type of a file using a path which can be relative to the JVM or an absolute path. The path can point to a file or directory location and if the path does not point to an actual file or directory the UNKNOWN_MIME_TYPEis returned.

Their is an exception to this and that is if the fname parameter does NOT point to a real file or directory and extFirst is true then a match against the file extension could be found and would be returned.

Parameters:
fname - points to a file or directory
extFirst - if true will first use file extension mapping and then then magic.mime rules. If false it will try to match the other way around i.e. magic.mime rules and then file extension.
Returns:
the mime type. Never returns null (if the mime type cannot be found, UNKNOWN_MIME_TYPE is returned).
Throws:
MimeException - if while using the magic.mime rules there is a problem processing the file.

getMimeType

public static String getMimeType(String fname)
                          throws MimeException
Deprecated. 
This is a convenience method where the order of lookup is set to extension mapping first.

Throws:
MimeException
See Also:
getMimeType(String fname, boolean extFirst)

getMimeType

public static String getMimeType(File file,
                                 boolean extFirst)
                          throws MimeException
Deprecated. 
Get the mime type of a file using a File object which can be relative to the JVM or an absolute path. The path can point to a file or directory location and if the path does not point to an actual file or directory the UNKNOWN_MIME_TYPEis returned.

Their is an exception to this and that is if the file parameter does NOT point to a real file or directory and extFirst is true then a match against the file extension could be found and would be returned.

Parameters:
file - points to a file or directory
extFirst - if true will first use file extension mapping and then then magic.mime rules. If false it will try to match the other way around i.e. magic.mime rules and then file extension.
Returns:
the mime type. Never returns null (if the mime type cannot be found, UNKNOWN_MIME_TYPE is returned).
Throws:
MimeException - if while using the magic.mime rules there is a problem processing the file.

getMimeType

public static String getMimeType(File f)
                          throws MimeException
Deprecated. 
This is a convenience method where the order of lookup is set to extension mapping first.

Throws:
MimeException
See Also:
getMimeType(File f, boolean extFirst)

getPreferedMimeType

public static String getPreferedMimeType(String accept,
                                         String canProvide)
Deprecated. 
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.

getMimeQuality

public static double getMimeQuality(String mimeType)
                             throws MimeException
Deprecated. 
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.

getMagicMimeType

public static String getMagicMimeType(File file)
                               throws MimeException
Deprecated. 
Get the mime type of a file using the magic.mime rules files.

Parameters:
file - is a File object that points to a file or directory.
Returns:
the mime type. Never returns null (if the mime type cannot be found, UNKNOWN_MIME_TYPE is returned).
Throws:
MimeException - if the file cannot be parsed.

getMajorComponent

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

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

getMinorComponent

public static String getMinorComponent(String mimeType)
                                throws MimeException
Deprecated. 
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:
minor component of the mime type
Throws:
MimeException - if you pass in an invalid mime type structure

getFileExtension

public static String getFileExtension(File file)
Deprecated. 
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.

getFileExtension

public static String getFileExtension(String fileName)
Deprecated. 
Get the extension part of a file name defined by the fname 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.

addKnownMimeType

public static void addKnownMimeType(String mimeType)
Deprecated. 
While all of the property files and magic.mime files are being loaded the utility keeps a list of mime types it's seen. 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 already understands.

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)

isMimeTypeKnown

public static boolean isMimeTypeKnown(String mimeType)
Deprecated. 
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)

getFirstMimeType

public static String getFirstMimeType(String mimeTypes)
Deprecated. 
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. Will return UNKNOWN_MIME_TYPE if the passed in list is null or empty.

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

getExtensionMimeTypes

public static String getExtensionMimeTypes(File file)
Deprecated. 
Get the mime type of a file using file extension mappings. The file path can be a relative or absolute or can be a completely non-existent file as only the extension is important.

Parameters:
file - is a File object that points to a file or directory. If the file or directory cannot be found UNKNOWN_MIME_TYPE is returned.
Returns:
the mime type. Never returns null (if the mime type cannot be found, UNKNOWN_MIME_TYPE is returned).
Throws:
MimeException - if the file cannot be parsed.

getExtensionMimeTypes

public static String getExtensionMimeTypes(String fname)
Deprecated. 
Get the mime type of a file using file extension mappings. The file path can be a relative or absolute or can be a completely non-existent file as only the extension is important.

Parameters:
fname - is a path that points to a file or directory. If the file or directory cannot be found UNKNOWN_MIME_TYPE is returned.
Returns:
the mime type. Never returns null (if the mime type cannot be found, UNKNOWN_MIME_TYPE is returned).
Throws:
MimeException - if the file cannot be parsed.

getMagicMimeType

public static String getMagicMimeType(String fname)
                               throws MimeException
Deprecated. 
Get the mime type of a file using the magic.mime rules files.

Parameters:
fname - is a path location to a file or directory.
Returns:
the mime type. Never returns null (if the mime type cannot be found, UNKNOWN_MIME_TYPE is returned).
Throws:
MimeException - if the file cannot be parsed.

setUnknownMimeType

public static void setUnknownMimeType(String mimeType)
Deprecated. 
The default mime type returned by a no match i.e. is not matched in either the extension mapping or magic.mime rules is application/octet-stream. However, applications may want to treat a no match different from a match that could return application/octet-stream. This method allows you to set a different mime type to represent a no match such as a custom mime type like application/x-unknown-mime-type

Parameters:
mimeType - set the default returned mime type for a no match.


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