eu.medsea.mimeutil.detector
Class ExtensionMimeDetector

java.lang.Object
  extended by eu.medsea.mimeutil.detector.MimeDetector
      extended by eu.medsea.mimeutil.detector.ExtensionMimeDetector

public class ExtensionMimeDetector
extends MimeDetector

The extension mime mappings are loaded in the following way.

  1. Load the properties file from the mime utility jar named eu.medsea.mimeutil.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 understood by MimeUtil. If there is a clash of extension names then the last one loaded wins, i.e they are not adative, this makes it possible to completely change the mime types associated to a file extension declared in previously loaded property files. The extensions are also case sensitive meaning that bat, bAt, BAT and Bat can all be recognised individually. If however, no match is found using case sensitive matching then it will perform an insensitive match by lower casing the extension of the file to be matched first.

Fortunately, we have compiled a relatively large list of mappings into a java properties file from information gleaned from many sites on the Internet. This file resides in the eu.medsea.util.mime-types.properties file and is not guaranteed to be correct or contain all the known mappings for a file extension type. This is not a complete or exhaustive list as that would have proven too difficult to compile for this project. So instead we give you the opportunity to extend and override these mappings for yourself as defined above. Obviously, to use this method you don't actually need a file object, you just need a file name with an extension. Also, if you have given or renamed a file using a different extension than the one that it would normally be associated with then this mapping will return the wrong mime-type and if the file has no extension at all, such as Make, then it's not going to be possible to determine a mime type using this technique

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 files so that a correct mime type is returned for you.

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.

This class is auto loaded by MimeUtil as it has an entry in the file called MimeDetectors. MimeUtil reads this file at startup and calls Class.forName() on each entry found. This mean the MimeDetector must have a no arg constructor.

Author:
Steven McArdle.

Constructor Summary
ExtensionMimeDetector()
           
 
Method Summary
 String getDescription()
          Abstract method to be implement by concrete MimeDetector(s).
 Collection getMimeTypesByteArray(byte[] data)
          This method is required by the abstract MimeDetector class.
 Collection getMimeTypesFile(File file)
          Get the mime type of a file using extension mappings.
 Collection getMimeTypesFileName(String fileName)
          Get the mime type of a file name using file name extension mappings.
 Collection getMimeTypesInputStream(InputStream in)
          This method is required by the abstract MimeDetector class.
 Collection getMimeTypesURL(URL url)
          Get the mime type of a URL using extension mappings.
 
Methods inherited from class eu.medsea.mimeutil.detector.MimeDetector
closeStream, delete, getMimeTypes, getMimeTypes, getMimeTypes, getMimeTypes, getMimeTypes, getName, init
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExtensionMimeDetector

public ExtensionMimeDetector()
Method Detail

getDescription

public String getDescription()
Description copied from class: MimeDetector
Abstract method to be implement by concrete MimeDetector(s).

Specified by:
getDescription in class MimeDetector
Returns:
description of this MimeDetector

getMimeTypesFile

public Collection getMimeTypesFile(File file)
                            throws MimeException
Get the mime type of a file using extension mappings. The file path can be a relative or absolute path or can refer to a completely non-existent file as only the extension is important here.

Specified by:
getMimeTypesFile in class MimeDetector
Parameters:
file - points to a file or directory. May not actually exist
Returns:
collection of the matched mime types.
Throws:
MimeException - if errors occur.

getMimeTypesURL

public Collection getMimeTypesURL(URL url)
                           throws MimeException
Get the mime type of a URL using extension mappings. Only the extension is important here.

Specified by:
getMimeTypesURL in class MimeDetector
Parameters:
url - is a valid URL
Returns:
collection of the matched mime types.
Throws:
MimeException - if errors occur.

getMimeTypesFileName

public Collection getMimeTypesFileName(String fileName)
                                throws MimeException
Get the mime type of a file name using file name extension mappings. The file name path can be a relative or absolute path or can refer to a completely non-existent file as only the extension is important here.

Specified by:
getMimeTypesFileName in class MimeDetector
Parameters:
fileName - points to a file or directory. May not actually exist
Returns:
collection of the matched mime types.
Throws:
MimeException - if errors occur.

getMimeTypesInputStream

public Collection getMimeTypesInputStream(InputStream in)
                                   throws UnsupportedOperationException
This method is required by the abstract MimeDetector class. As we do not support extension mapping of streams we just throw an UnsupportedOperationException. This ensures that the getMimeTypes(...) methods ignore this method. We could also have just returned an empty collection.

Specified by:
getMimeTypesInputStream in class MimeDetector
Parameters:
in - InputStream.
Returns:
Collection of matched MimeType(s)
Throws:
UnsupportedOperationException

getMimeTypesByteArray

public Collection getMimeTypesByteArray(byte[] data)
                                 throws UnsupportedOperationException
This method is required by the abstract MimeDetector class. As we do not support extension mapping of byte arrays we just throw an UnsupportedOperationException. This ensures that the getMimeTypes(...) methods ignore this method. We could also have just returned an empty collection.

Specified by:
getMimeTypesByteArray in class MimeDetector
Parameters:
data - byte []. Is a byte array that you want to parse for matching mime types.
Returns:
Collection of matched MimeType(s)
Throws:
UnsupportedOperationException


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