org.modeshape.common.util
Class MimeTypeUtil

java.lang.Object
  extended by org.modeshape.common.util.MimeTypeUtil
Direct Known Subclasses:
ExtensionBasedMimeTypeDetector

@Immutable
public class MimeTypeUtil
extends Object

A simple utility that determines an appropriate MIME type by matching the extension of the supplied filename against a set of known file extensions.

This utility class may be instantiated to create a new instance with its own set of mappings. If desired, the default mappings will be loaded using the current thread's context class loader, and will be loaded from the /org/modeshape/mime.types file located on the classpath. The load(InputStream, Map) method may be used to load custom mappings (in the standard format) into a Map instance, which can then be passed to one of the constructors.


Field Summary
static String MIME_TYPE_EXTENSIONS_RESOURCE_PATH
          The default location of the properties file containing the extension patterns to MIME types.
 
Constructor Summary
MimeTypeUtil()
          Create a default instance of the extension-based MIME type detector.
MimeTypeUtil(Map<String,String> extensionsToMimeTypes)
          Create an instance of the extension-based MIME type detector by using the supplied mappings.
MimeTypeUtil(Map<String,String> extensionsToMimeTypes, boolean initWithDefaults)
          Create an instance of the extension-based MIME type detector by using the supplied mappings.
 
Method Summary
protected static Map<String,String> getDefaultMappings()
          Load the default extensions from MIME_TYPE_EXTENSIONS_RESOURCE_PATH, which can either be a property file or a tab-delimited *nix-style MIME types file (common in web servers and libraries).
static Map<String,String> load(InputStream stream, Map<String,Set<String>> duplicateMimeTypesByExtension)
          Load the extensions from the supplied stream, which may provide the contents in the format of property file or a tab-delimited *nix-style MIME types file (common in web servers and libraries).
 String mimeTypeOf(File file)
          Returns the MIME-type of the file using its name.
 String mimeTypeOf(String filename)
          Returns the MIME-type of a file given the supplied name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MIME_TYPE_EXTENSIONS_RESOURCE_PATH

public static final String MIME_TYPE_EXTENSIONS_RESOURCE_PATH
The default location of the properties file containing the extension patterns to MIME types. Value is ""org/modeshape/mime.types" ".

See Also:
Constant Field Values
Constructor Detail

MimeTypeUtil

public MimeTypeUtil()
Create a default instance of the extension-based MIME type detector. The set of extension patterns to MIME-types is loaded from the "org/modeshape/mime.types" classpath resource.


MimeTypeUtil

public MimeTypeUtil(Map<String,String> extensionsToMimeTypes)
Create an instance of the extension-based MIME type detector by using the supplied mappings. The set of extension patterns to MIME-types is loaded from the "org/modeshape/mime.types" classpath resource, but the supplied extension mappings override any default mappings.

Parameters:
extensionsToMimeTypes - the mapping of extension patterns to MIME types, which will override the default mappings; may be null if the default mappings are to be used

MimeTypeUtil

public MimeTypeUtil(Map<String,String> extensionsToMimeTypes,
                    boolean initWithDefaults)
Create an instance of the extension-based MIME type detector by using the supplied mappings. If requested, the set of extension patterns to MIME-types is loaded from the "org/modeshape/mime.types" classpath resource and any supplied extension mappings override any default mappings.

Parameters:
extensionsToMimeTypes - the mapping of extension patterns to MIME types, which will override the default mappings; may be null if the default mappings are to be used
initWithDefaults - true if the default mappings are to be loaded first, or false if the default mappings are not to be used at all
Method Detail

getDefaultMappings

protected static Map<String,String> getDefaultMappings()
Load the default extensions from MIME_TYPE_EXTENSIONS_RESOURCE_PATH, which can either be a property file or a tab-delimited *nix-style MIME types file (common in web servers and libraries). If an extension applies to more than one MIME type, the first one in the file wins.

Returns:
the default mappings; never null

load

public static Map<String,String> load(InputStream stream,
                                      Map<String,Set<String>> duplicateMimeTypesByExtension)
Load the extensions from the supplied stream, which may provide the contents in the format of property file or a tab-delimited *nix-style MIME types file (common in web servers and libraries). If an extension applies to more than one MIME type, the first one in the file wins.

Parameters:
stream - the stream containing the content; may not be null
duplicateMimeTypesByExtension - a map into which any extension should be placed if there are multiple MIME types that apply; may be null if this information is not required
Returns:
the default mappings; never null

mimeTypeOf

public String mimeTypeOf(String filename)
Returns the MIME-type of a file given the supplied name. If the MIME-type cannot be determined, a null is returned.

Parameters:
filename - the file name; may be null.
Returns:
The MIME-type of the file, or optionally null if the MIME-type could not be determined.

mimeTypeOf

public String mimeTypeOf(File file)
Returns the MIME-type of the file using its name. If the MIME-type cannot be determined, a null is returned.

Parameters:
file - the file; may be null.
Returns:
The MIME-type of the file, or optionally null if the MIME-type could not be determined.


Copyright © 2008-2012 JBoss, a division of Red Hat. All Rights Reserved.