com.bagdemir.eboda.ext.MimeDictionary.java Source code

Java tutorial

Introduction

Here is the source code for com.bagdemir.eboda.ext.MimeDictionary.java

Source

/*
 *   Copyright (C) 2013 "Erhan Bagdemir"
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.bagdemir.eboda.ext;

import static com.bagdemir.eboda.utils.Statics.DEFAULT_MIME_TYPE;
import static com.bagdemir.eboda.utils.Statics.MIME_TYPES_FILE;
import static com.bagdemir.eboda.utils.Statics.SPACE;
import static com.bagdemir.eboda.utils.Statics.TAB;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 * Dictionary of mime types.
 */
public class MimeDictionary {

    public static final Logger LOGGER = Logger.getLogger(MimeDictionary.class);
    private static MimeDictionary mimeDictionary = null;
    private Map<String, String> mimeTypes = new HashMap<>();

    private MimeDictionary() {
    }

    public static MimeDictionary newInstance() {
        if (mimeDictionary == null) {
            mimeDictionary = new MimeDictionary();
            mimeDictionary.initMimeTypes();
        }
        return mimeDictionary;
    }

    public String getMimeTypeFor(final String fileExtension) {
        if (mimeTypes.containsKey(fileExtension)) {
            return mimeTypes.get(fileExtension);
        }
        return DEFAULT_MIME_TYPE;
    }

    /**
     * parses mime types data file and builds a new file extension-to-mime type map.
     */
    private void initMimeTypes() {

        final InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(MIME_TYPES_FILE);
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(resourceAsStream));
            while (reader.ready()) {
                final String line = reader.readLine();
                if (StringUtils.isNotBlank(line)) {
                    // ignore commented lines.
                    if (isCommentLine(line)) {
                        continue;
                    }
                    processLine(line);
                }
            }
        } catch (FileNotFoundException e) {
            LOGGER.error(e);
        } catch (IOException e) {
            LOGGER.error(e);
            closeReader(reader);
        }
    }

    private void closeReader(final BufferedReader reader) {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ex) {
                LOGGER.error(ex);
            }
        }
    }

    /**
     * @param line
     * @return whether the line is a comment or not.
     */
    private boolean isCommentLine(final String line) {
        return line.trim().startsWith("#");
    }

    /**
     * extracts file extensions and put extension-mime type mapping into the mime types map.
     *
     * @param line
     */
    private void processLine(final String line) {
        final String[] lineArray = line.trim().split(TAB);
        if (containsFileExtension(lineArray)) {
            final String[] extensionArray = lineArray[1].trim().split(SPACE);
            for (String fileExtension : extensionArray) {
                mimeTypes.put(fileExtension, lineArray[0]);
            }
        }
    }

    /**
     * @param lineArray
     * @return whether the line contains file extensions
     */
    private boolean containsFileExtension(final String[] lineArray) {
        return lineArray != null && lineArray.length > 1;
    }

}