All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fordream.http.MimeHelper Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
package fordream.http;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * According the ext. to return MIME.
 */
public class MimeHelper {
    private static MimeHelper instance;
    private static Object locker;
    public Map map;

    static {
        locker = new Object();
    }

    public static MimeHelper instance() {
        if (instance == null) // double check
            synchronized (locker) {
                if (instance == null)
                    instance = new MimeHelper();
            }

        return instance;
    }

    private MimeHelper() {
        //String dir = AppProperty.userDir() + File.separator;
        ObjectMapper objectMapper = new ObjectMapper();
        //File config = new File(dir + "mime.json");
        String url = MimeHelper.class.getClassLoader().getResource("mime.json").getFile();
        File config = new File(url);

        this.map = this.loadExtraMime(config);

        if (this.map == null) {
            throw new Error("init mime helper fail.");
        }
    }

    public String getMime(String file) {
        int dotPos = file.lastIndexOf(".");
        String ext = file.substring(dotPos + 1).toLowerCase();
        String mime = map.get(ext);
        if (mime == null)
            mime = "application/octet-stream";
        return mime;
    }

    public String getMime(File file) {
        return getMime(file.getAbsolutePath());
    }

    private Map loadExtraMime(File config) {
        if (config.exists()) {
            try {
                Map map = new ObjectMapper().readValue(config, HashMap.class);
                return map;
            } catch (IOException e) {
                System.err.println(String.format("IOException -> %s", e.getMessage()));
            }
        } else {
            System.err.println(String.format("%s not exists.", config.getAbsolutePath()));
        }
        return null;
    }

    /**
     * load extra configuration, override inner config.
     *
     * @param configUrl config file url
     */
    public void loadExtraMime(String configUrl) {
        File config = new File(configUrl);
        Map map = this.loadExtraMime(config);
        if (map != null) {
            for (Iterator iterator = map.keySet().iterator(); iterator.hasNext(); ) {
                String key = iterator.next();
                String value = map.get(key);
                this.map.put(key, value);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy