* The byte data, returned from the crawl, if any.
* @return The correctly, automatically guessed {@link MimeType} name.
*/
public String autoResolveContentType(String typeName, String url,
byte[] data) {
MimeType type = null;
String cleanedMimeType = null;
try {
cleanedMimeType = MimeTypeUtils.cleanMimeType(typeName) != null ? this.mimeTypes
.forName(MimeTypeUtils.cleanMimeType(typeName)).getName()
: null;
} catch (MimeTypeException mte) {
// Seems to be a malformed mime type name...
}
// first try to get the type from the cleaned type name
try {
type = cleanedMimeType != null ? this.mimeTypes
.forName(cleanedMimeType) : null;
} catch (MimeTypeException e) {
type = null;
}
// if returned null, or if it's the default type then try url resolution
if (type == null
|| (type != null && type.getName().equals(MimeTypes.OCTET_STREAM))) {
// If no mime-type header, or cannot find a corresponding registered
// mime-type, then guess a mime-type from the url pattern
type = this.mimeTypes.getMimeType(url) != null ? this.mimeTypes
.getMimeType(url) : type;
}
// if magic is enabled use mime magic to guess if the mime type returned
// from the magic guess is different than the one that's already set so
// far
// if it is, and it's not the default mime type, then go with the mime
// type
// returned by the magic
if (this.mimeMagic) {
MimeType magicType = this.mimeTypes.getMimeType(data);
if (magicType != null
&& !magicType.getName().equals(MimeTypes.OCTET_STREAM)
&& type != null
&& !type.getName().equals(magicType.getName())) {
// If magic enabled and the current mime type differs from that
// of the
// one returned from the magic, take the magic mimeType
type = magicType;
}