Search in sources :

Example 1 with FileTypeMap

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;
}
Also used : FileTypeMap(javax.activation.FileTypeMap)

Example 2 with 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);
}
Also used : FileTypeMap(javax.activation.FileTypeMap) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap)

Example 3 with FileTypeMap

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";
        }
    };
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) HashMap(java.util.HashMap) FileTypeMap(javax.activation.FileTypeMap) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 4 with FileTypeMap

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);
}
Also used : FileTypeMap(javax.activation.FileTypeMap) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap)

Example 5 with FileTypeMap

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"));
}
Also used : MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) FileTypeMap(javax.activation.FileTypeMap) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) Test(org.junit.Test)

Aggregations

FileTypeMap (javax.activation.FileTypeMap)6 MimetypesFileTypeMap (javax.activation.MimetypesFileTypeMap)4 ClassPath (com.kyj.fx.voeditor.visual.main.model.vo.ClassPath)1 ClassPathEntry (com.kyj.fx.voeditor.visual.main.model.vo.ClassPathEntry)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 HashMap (java.util.HashMap)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)1 Test (org.junit.Test)1 Document (org.w3c.dom.Document)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1 InputSource (org.xml.sax.InputSource)1