use of javax.activation.FileTypeMap in project spring-framework by spring-projects.
the class MimeMessageHelper method getDefaultFileTypeMap.
/**
* Determine the default Java Activation FileTypeMap for the given MimeMessage.
* @param mimeMessage the passed-in MimeMessage
* @return the default FileTypeMap associated with the MimeMessage,
* or a default ConfigurableMimeFileTypeMap if none found for the message
* @see ConfigurableMimeFileTypeMap
*/
protected FileTypeMap getDefaultFileTypeMap(MimeMessage mimeMessage) {
if (mimeMessage instanceof SmartMimeMessage) {
FileTypeMap fileTypeMap = ((SmartMimeMessage) mimeMessage).getDefaultFileTypeMap();
if (fileTypeMap != null) {
return fileTypeMap;
}
}
ConfigurableMimeFileTypeMap fileTypeMap = new ConfigurableMimeFileTypeMap();
fileTypeMap.afterPropertiesSet();
return fileTypeMap;
}
use of javax.activation.FileTypeMap in project collect by openforis.
the class Controllers method writeFileToResponse.
public static void writeFileToResponse(HttpServletResponse response, File file, String outputFileName) throws FileNotFoundException, IOException {
FileTypeMap defaultFileTypeMap = MimetypesFileTypeMap.getDefaultFileTypeMap();
String contentType = defaultFileTypeMap.getContentType(outputFileName);
writeFileToResponse(response, file, outputFileName, contentType);
}
use of javax.activation.FileTypeMap in project jaggery by wso2.
the class FileHostObject method loadMimeMap.
@SuppressFBWarnings("PATH_TRAVERSAL_IN")
private static FileTypeMap loadMimeMap() throws ScriptException {
String configDirPath = CarbonUtils.getEtcCarbonConfigDirPath();
File configFile = new File(configDirPath, RESOURCE_MEDIA_TYPE_MAPPINGS_FILE);
if (!configFile.exists()) {
String msg = "Resource media type definitions file (mime.types) file does " + "not exist in the path " + configDirPath;
log.error(msg);
throw new ScriptException(msg);
}
final Map<String, String> mimeMappings = new HashMap<String, String>();
final String mappings;
try {
mappings = FileUtils.readFileToString(configFile, "UTF-8");
} catch (IOException e) {
String msg = "Error opening resource media type definitions file " + "(mime.types) : " + e.getMessage();
throw new ScriptException(msg, e);
}
String[] lines = mappings.split("[\\r\\n]+");
for (String line : lines) {
if (!line.startsWith("#")) {
String[] parts = line.split("\\s+");
for (int i = 1; i < parts.length; i++) {
mimeMappings.put(parts[i], parts[0]);
}
}
}
return new FileTypeMap() {
@Override
public String getContentType(File file) {
return getContentType(file.getName());
}
@Override
public String getContentType(String fileName) {
int i = fileName.lastIndexOf('.');
if (i > 0) {
String mimeType = mimeMappings.get(fileName.substring(i + 1));
if (mimeType != null) {
return mimeType;
}
}
return "application/octet-stream";
}
};
}
use of javax.activation.FileTypeMap in project yyl_example by Relucent.
the class FileTypeMapExample method main.
public static void main(String[] args) {
String filename = System.getProperty("user.dir") + "/src/main/resources/yyl/example/basic/sound/doraemon.mid";
FileTypeMap map = MimetypesFileTypeMap.getDefaultFileTypeMap();
String contentType = map.getContentType(filename);
System.out.println(contentType);
}
use of javax.activation.FileTypeMap in project spring-framework by spring-projects.
the class MockServletContextTests method getMimeTypeWithCustomConfiguredType.
/**
* Introduced to dispel claims in a thread on Stack Overflow:
* <a href="http://stackoverflow.com/questions/22986109/testing-spring-managed-servlet">Testing Spring managed servlet</a>
*/
@Test
public void getMimeTypeWithCustomConfiguredType() {
FileTypeMap defaultFileTypeMap = FileTypeMap.getDefaultFileTypeMap();
assertThat(defaultFileTypeMap, instanceOf(MimetypesFileTypeMap.class));
MimetypesFileTypeMap mimetypesFileTypeMap = (MimetypesFileTypeMap) defaultFileTypeMap;
mimetypesFileTypeMap.addMimeTypes("text/enigma enigma");
assertEquals("text/enigma", sc.getMimeType("filename.enigma"));
}
Aggregations