001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.ContentTypes;
020    import com.liferay.portal.kernel.util.MimeTypes;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    
024    import eu.medsea.mimeutil.MimeType;
025    import eu.medsea.mimeutil.MimeUtil;
026    import eu.medsea.mimeutil.TextMimeType;
027    import eu.medsea.mimeutil.detector.MagicMimeMimeDetector;
028    import eu.medsea.util.EncodingGuesser;
029    
030    import java.io.File;
031    import java.io.FileInputStream;
032    import java.io.FileNotFoundException;
033    import java.io.InputStream;
034    
035    import java.util.ArrayList;
036    import java.util.Collection;
037    import java.util.HashSet;
038    import java.util.List;
039    
040    import javax.activation.MimetypesFileTypeMap;
041    
042    /**
043     * <p>
044     * Additional MIME types should be added to META-INF/mime.types or magic.mime.
045     * </p>
046     *
047     * @author Jorge Ferrer
048     * @author Brian Wing Shun Chan
049     * @author Alexander Chow
050     */
051    public class MimeTypesImpl implements MimeTypes {
052    
053            public MimeTypesImpl() {
054                    MimeUtil.registerMimeDetector(MagicMimeMimeDetector.class.getName());
055    
056                    Collection<String> encodings = new HashSet<String>();
057    
058                    encodings.add(StringPool.UTF8);
059                    encodings.add(System.getProperty("file.encoding"));
060    
061                    EncodingGuesser.setSupportedEncodings(encodings);
062            }
063    
064            public String getContentType(File file) {
065                    try {
066                            return getContentType(new FileInputStream(file), file.getName());
067                    }
068                    catch (FileNotFoundException fnfe) {
069                            return getContentType(file.getName());
070                    }
071            }
072    
073            public String getContentType(InputStream inputStream, String fileName) {
074                    String contentType = getContentType(fileName);
075    
076                    if (!contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
077                            return contentType;
078                    }
079    
080                    List<MimeType> mimeTypes = new ArrayList<MimeType>(
081                            MimeUtil.getMimeTypes(inputStream));
082    
083                    MimeType mimeType = mimeTypes.get(0);
084    
085                    if ((mimeTypes.size() > 1) && (mimeType instanceof TextMimeType)) {
086                            mimeType = mimeTypes.get(1);
087                    }
088    
089                    contentType =
090                            mimeType.getMediaType() + StringPool.SLASH + mimeType.getSubType();
091    
092                    if (contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
093                            contentType = getContentType(fileName);
094                    }
095                    else if (contentType.equals(ContentTypes.APPLICATION_ZIP)) {
096                            String contentTypeByFileName = getContentType(fileName);
097    
098                            if (contentTypeByFileName.contains("vnd.openxmlformats")) {
099                                    contentType = contentTypeByFileName;
100                            }
101                    }
102    
103                    if (_log.isDebugEnabled()) {
104                            _log.debug(
105                                    "Content type " + contentType + " returned for " + fileName);
106                    }
107    
108                    return contentType;
109            }
110    
111            public String getContentType(String fileName) {
112                    if (Validator.isNull(fileName)) {
113                            return ContentTypes.APPLICATION_OCTET_STREAM;
114                    }
115    
116                    if (!fileName.contains(StringPool.PERIOD)) {
117                            fileName = StringPool.PERIOD + fileName;
118                    }
119    
120                    String contentType = _mimeTypes.getContentType(fileName);
121    
122                    if (_log.isDebugEnabled()) {
123                            _log.debug(
124                                    "Content type " + contentType + " returned for file name " +
125                                            fileName);
126                    }
127    
128                    return contentType;
129            }
130    
131            private static Log _log = LogFactoryUtil.getLog(MimeTypesImpl.class);
132    
133            private MimetypesFileTypeMap _mimeTypes = new MimetypesFileTypeMap();
134    
135    }