public class FileSuffixToContentType extends Object
A FileSuffixToContentType
instance
is a mapping of {fileSuffix→contentType}
.
fileSuffix
can be any string, usually starting with a separator char.
New mappings can be added with method map(fileSuffix, contentType)
.
Given a filePath, we can find
the longest fileSuffix that matches filePath,
and return the corresponding ContentType. For example:
mappings .txt → text/plain;charset=UTF-8 -en.txt → text/plain;charset=US-ASCII file "abc.txt" matches the 1st; file "xyz-en.txt" matches the 2nd.
Matching is case sensitive; "\" matches to "/".
You can map an empty fileSuffix to a ContentType as the fall-back mapping.
The mapping may be initially populated using the Apache mime.types file; revision 1506674 is used in this implementation. We add "charset=UTF-8" to all "text/xxx" content types in mime.types.
There is a global instance
that is used by StaticHandler
etc. App may add mappings to the global instance.
This class is thread-safe.
Constructor and Description |
---|
FileSuffixToContentType(boolean apache)
Create a new instance.
|
Instance Methods | |
---|---|
ContentType |
find(CharSequence filePath)
Find the ContentType of filePath.
|
ContentType |
map(CharSequence fileSuffix,
ContentType contentType)
Add a mapping
fileSuffix→contentType . |
Map<String,ContentType> |
asMap()
Return the mapping
{fileSuffix→contentType } as a Map. |
Static Method | |
FileSuffixToContentType |
getGlobalInstance()
The global instance.
|
public FileSuffixToContentType(boolean apache)
If apache==true
, the instance is populated with Apache mime.types.
public static FileSuffixToContentType getGlobalInstance()
The global instance is initially populated with Apache mime.types. App may add mappings to the global instance.
public ContentType find(CharSequence filePath)
Find the longest fileSuffix that matches filePath, and return the corresponding ContentType; return null if no matching is found.
public ContentType map(CharSequence fileSuffix, ContentType contentType)
fileSuffix→contentType
.
All "\" chars in fileSuffix will be replaced with "/".
fileSuffix
public Map<String,ContentType> asMap()
{fileSuffix→contentType
} as a Map.
The returned Map is read-only; it's mainly for diagnosis purpose.