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 com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
018    
019    import java.io.File;
020    import java.io.InputStream;
021    
022    import java.util.Set;
023    
024    /**
025     * @author Jorge Ferrer
026     * @author Brian Wing Shun Chan
027     * @author Alexander Chow
028     */
029    public class MimeTypesUtil {
030    
031            public static String getContentType(File file) {
032                    return getMimeTypes().getContentType(file);
033            }
034    
035            public static String getContentType(File file, String fileName) {
036                    return getMimeTypes().getContentType(file, fileName);
037            }
038    
039            /**
040             * Returns the content type from an input stream and file name.
041             *
042             * @param  inputStream the input stream of the content (optionally
043             *         <code>null</code>)
044             * @param  fileName the full name or extension of file (e.g., "Test.doc",
045             *         ".doc")
046             * @return the content type if it is a supported format or an empty string
047             *         if it is an unsupported format
048             */
049            public static String getContentType(
050                    InputStream inputStream, String fileName) {
051    
052                    return getMimeTypes().getContentType(inputStream, fileName);
053            }
054    
055            /**
056             * Returns the content type from a file name.
057             *
058             * @param  fileName the full name or extension of the file (e.g.,
059             *         "Test.doc", ".doc")
060             * @return the content type if it is a supported format or an empty string
061             *         if it is an unsupported format
062             */
063            public static String getContentType(String fileName) {
064                    return getMimeTypes().getContentType(fileName);
065            }
066    
067            /**
068             * Returns the possible file extensions for a given content type.
069             *
070             * @param  contentType the content type of the file (e.g., "image/jpeg")
071             * @return the set of extensions if it is a known content type or an empty
072             *         set if it is an unknown content type
073             */
074            public static Set<String> getExtensions(String contentType) {
075                    return getMimeTypes().getExtensions(contentType);
076            }
077    
078            public static MimeTypes getMimeTypes() {
079                    PortalRuntimePermission.checkGetBeanProperty(MimeTypesUtil.class);
080    
081                    return _mimeTypes;
082            }
083    
084            public static boolean isWebImage(String mimeType) {
085                    return getMimeTypes().isWebImage(mimeType);
086            }
087    
088            public void setMimeTypes(MimeTypes mimeTypes) {
089                    PortalRuntimePermission.checkSetBeanProperty(getClass());
090    
091                    _mimeTypes = mimeTypes;
092            }
093    
094            private static MimeTypes _mimeTypes;
095    
096    }