io.femo.http.drivers.DefaultMimeService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of http-jdk7 Show documentation
Show all versions of http-jdk7 Show documentation
An easy to use HTTP API, that supports synchronous and asynchronous execution of HTTP request.
On android only asynchronous driver is supported.
This library has been backported to jdk 7 to retain compatibility with android!
The newest version!
package io.femo.http.drivers;
import eu.medsea.mimeutil.MimeType;
import eu.medsea.mimeutil.MimeUtil;
import eu.medsea.mimeutil.detector.ExtensionMimeDetector;
import eu.medsea.mimeutil.detector.MagicMimeMimeDetector;
import eu.medsea.mimeutil.detector.MimeDetector;
import io.femo.http.MimeService;
import org.jetbrains.annotations.Contract;
import java.io.File;
/**
* Created by felix on 6/11/16.
*/
public class DefaultMimeService implements MimeService {
private ThreadLocal tlMagicMimeDetector = new ThreadLocal() {
@Contract(" -> !null")
@Override
protected MimeDetector initialValue() {
return new MagicMimeMimeDetector();
}
};
private ThreadLocal tlExtensionMimeDetector = new ThreadLocal() {
@Contract(" -> !null")
@Override
protected MimeDetector initialValue() {
return new ExtensionMimeDetector();
}
};
@Override
public String contentType(File file) {
MimeDetector magic = tlMagicMimeDetector.get();
MimeDetector extension = tlExtensionMimeDetector.get();
MimeType magicMimeType = MimeUtil.getMostSpecificMimeType(magic.getMimeTypes(file));
MimeType extensionMimeType = MimeUtil.getMostSpecificMimeType(extension.getMimeTypes(file));
if(magicMimeType == null || magicMimeType.toString().equals("application/octet-stream")) {
if(extensionMimeType == null) {
return "application/octet-stream";
}
return extensionMimeType.toString();
} else {
return magicMimeType.toString();
}
}
}