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

org.zodiac.sdk.nio.http.common.MimeMappings Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.sdk.nio.http.common;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zodiac.sdk.nio.common.FileMappings;
import org.zodiac.sdk.nio.common.OrderedProperties;

public abstract class MimeMappings extends FileMappings implements Iterable {

    private static Logger log = LoggerFactory.getLogger(MimeMappings.class);

    private static final String MIME_TYPE_PROPERTIES_FILE = "/META-INF/mimetypes.properties";

    private final Object addLock = new Object();
    private final Object removeLock = new Object();
    /*Map<扩展名, MIME>*/
    private final Map extensionMimeMap;
    /*Map*/
    private final Map mimeExtensionMap;

    /**
     * Create a new empty {@link MimeMappings} instance.
     */
    protected MimeMappings() {
        this.extensionMimeMap = new LinkedHashMap<>();
        this.mimeExtensionMap = new LinkedHashMap<>();

        loadMimetypeProperties();
    }

    @Override
    public Iterator iterator() {
        return getAll().iterator();
    }

    /**
     * Returns all defined mappings.
     * 
     * @return the mappings.
     */
    public Collection getAll() {
        return this.extensionMimeMap.values();
    }

    /**
     * Add a new mime mapping.
     * 
     * @param extension the file extension (excluding '.')
     * @param mimeType the mime type to extensionMimeMap
     * @return any previous mapping or {@code null}
     */
    public String add(String extension, String mimeType) {
        synchronized (addLock) {
            String _extension = null != extension ? extension.trim() : null;
            String _mimeType = null != mimeType ? mimeType.trim() : null;
            if (null == _extension || null == _mimeType) {
                return null;
            }

            Mapping mapping = new Mapping(_extension, _mimeType);
            Mapping previous = this.extensionMimeMap.put(_extension, mapping);
            this.mimeExtensionMap.put(_mimeType, mapping);
            return (previous == null ? null : previous.getMimeType());
        }
    }

    /**
     * Get a mime mapping for the given extension.
     * 
     * @param extension the file extension (excluding '.')
     * @return a mime mapping or {@code null}
     */
    public String getExtensionMimeMapping(String extension) {
        Mapping mapping = this.extensionMimeMap.get(extension);
        return (mapping == null ? null : mapping.getMimeType());
    }

    /**
     * Get a mime mapping for the given file suffix.
     * 
     * @param fileSuffix the file suffix (including '.')
     * @return a mime mapping or {@code null}
     */
    public String getFileSuffixMimeMapping(String fileSuffix) {
        Mapping mapping = this.extensionMimeMap.get(fileSuffix);
        return (mapping == null ? null : mapping.getFileSuffix().equalsIgnoreCase(fileSuffix) ? mapping.getMimeType() : null);
    }

    /**
     * Get a extension mapping for the given mime.
     * 
     * @param mime the MIME
     * @return a extension mapping or {@code null}
     */
    public String getMimeExtensionMapping(String mime) {
        Mapping mapping = this.mimeExtensionMap.get(mime);
        return (mapping == null ? null : mapping.getExtension());
    }

    /**
     * Get a file suffix mapping for the given mime.
     * 
     * @param mime the MIME
     * @return a file suffix mapping or {@code null}
     */
    public String getMimeFileSuffixMapping(String mime) {
        Mapping mapping = this.mimeExtensionMap.get(mime);
        return (mapping == null ? null : mapping.getFileSuffix());
    }

    /**
     * Remove an existing mapping.
     * 
     * @param extension the file extension (excluding '.')
     * @return the removed mime mapping or {@code null} if no item was removed
     */
    public String removeByExtension(String extension) {
        synchronized (removeLock) {
            Mapping previous = this.extensionMimeMap.remove(extension);
            if (previous == null) {
                return null;
            }

            this.mimeExtensionMap.remove(previous.getMimeType());
            return previous.getMimeType();
        }
    }

    /**
     * Remove an existing mapping.
     * 
     * @param mime the MIME
     * @return the removed extension mapping or {@code null} if no item was removed
     */
    public String removeByMime(String mime) {
        synchronized (removeLock) {
            Mapping previous = this.mimeExtensionMap.remove(mime);
            if (previous == null) {
                return null;
            }

            this.extensionMimeMap.remove(previous.getExtension());
            return previous.getExtension();
        }
    }

    @Override
    public int hashCode() {
        return this.extensionMimeMap.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if (obj instanceof MimeMappings) {
            MimeMappings other = (MimeMappings)obj;
            return this.extensionMimeMap.equals(other.extensionMimeMap);
        }
        return false;
    }

    private static String fileSuffix(String extension) {
        return String.format(".%s", ".", extension);
    }

    public static MimeMappings getInstance() {
        return MimeMappingsHolder.INSTANCE;
    }

    private void loadMimetypeProperties() {
        try {
            InputStream inputStream = MimeMappings.class.getResourceAsStream(MIME_TYPE_PROPERTIES_FILE);
            OrderedProperties orderedProperties = new OrderedProperties(inputStream);
            for (Object key : orderedProperties.keySet()) {
                String extension = key.toString();
                this.add(extension, orderedProperties.getProperty(extension));
            }
        } catch (IOException e) {
            log.error("Unexpected I/O error while loading %s .", MIME_TYPE_PROPERTIES_FILE);
        }
    }

    /**
     * A single mime mapping.
     */
    public final class Mapping {

        private final String extension;

        private final String fileSuffix;

        private final String mimeType;

        private Mapping(String extension, String mimeType) {
            Objects.requireNonNull(extension, "Extension must not be null");
            Objects.requireNonNull(mimeType, "MimeType must not be null");
            this.extension = extension;
            this.fileSuffix = fileSuffix(extension);
            this.mimeType = mimeType;
        }

        public String getExtension() {
            return this.extension;
        }

        public String getFileSuffix() {
            return fileSuffix;
        }

        public String getMimeType() {
            return this.mimeType;
        }

        @Override
        public String toString() {
            return "Mapping [extension=" + extension + ", fileSuffix=" + fileSuffix + ", mimeType=" + mimeType + "]";
        }

        @Override
        public int hashCode() {
            return extension.hashCode();
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Mapping other = (Mapping)obj;
            if (extension == null) {
                if (other.extension != null)
                    return false;
            } else if (!extension.equals(other.extension))
                return false;
            if (fileSuffix == null) {
                if (other.fileSuffix != null)
                    return false;
            } else if (!fileSuffix.equals(other.fileSuffix))
                return false;
            if (mimeType == null) {
                if (other.mimeType != null)
                    return false;
            } else if (!mimeType.equals(other.mimeType))
                return false;
            return true;
        }

    }

    private static class MimeMappingsHolder {
        private static final MimeMappings INSTANCE = new MimeMappings() {};
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy