001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.io.File;
018    import java.io.InputStream;
019    
020    import java.util.Set;
021    
022    /**
023     * @author Jorge Ferrer
024     * @author Brian Wing Shun Chan
025     * @author Alexander Chow
026     */
027    public class MimeTypesUtil {
028    
029            public static String getContentType(File file) {
030                    return getMimeTypes().getContentType(file);
031            }
032    
033            public static String getContentType(File file, String title) {
034                    return getMimeTypes().getContentType(file, title);
035            }
036    
037            /**
038             * Determine the content type from an input stream and file name.
039             *
040             * @param  fileName full name or extension of file (e.g., "Test.doc",
041             *         ".doc")
042             * @return content type if it is a supported format or an empty string if it
043             *         is an unsupported format
044             */
045            public static String getContentType(
046                    InputStream inputStream, String fileName) {
047    
048                    return getMimeTypes().getContentType(inputStream, fileName);
049            }
050    
051            /**
052             * Determine the content type from a file name.
053             *
054             * @param  fileName full name or extension of file (e.g., "Test.doc",
055             *         ".doc")
056             * @return content type if it is a supported format or an empty string if it
057             *         is an unsupported format
058             */
059            public static String getContentType(String fileName) {
060                    return getMimeTypes().getContentType(fileName);
061            }
062    
063            /**
064             * Determine the possible file extensions for a given content type.
065             *
066             * @param  contentType content type of file (e.g., "image/jpeg")
067             * @return set of extensions if it is a known content type or an empty set
068             *         if it is an unknown content type
069             */
070            public static Set<String> getExtensions(String contentType) {
071                    return getMimeTypes().getExtensions(contentType);
072            }
073    
074            public static MimeTypes getMimeTypes() {
075                    return _mimeTypes;
076            }
077    
078            public void setMimeTypes(MimeTypes mimeTypes) {
079                    _mimeTypes = mimeTypes;
080            }
081    
082            private static MimeTypes _mimeTypes;
083    
084    }