2 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
4 * This library is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License as published by the Free
6 * Software Foundation; either version 2.1 of the License, or (at your option)
9 * This library is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15 package com.liferay.portal.util;
18 import java.io.FileInputStream;
19 import java.io.FileNotFoundException;
20 import java.io.InputStream;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.HashSet;
24 import java.util.List;
26 import javax.activation.MimetypesFileTypeMap;
28 import com.liferay.portal.kernel.log.Log;
29 import com.liferay.portal.kernel.log.LogFactoryUtil;
30 import com.liferay.portal.kernel.util.ContentTypes;
31 import com.liferay.portal.kernel.util.MimeTypesOverride;
32 import com.liferay.portal.kernel.util.StringPool;
33 import com.liferay.portal.kernel.util.Validator;
34 import com.pentila.entSavoie.utils.ENTMainUtilsLocalServiceUtil;
36 import eu.medsea.mimeutil.MimeType;
37 import eu.medsea.mimeutil.MimeUtil;
38 import eu.medsea.mimeutil.TextMimeType;
39 import eu.medsea.mimeutil.detector.MagicMimeMimeDetector;
40 import eu.medsea.util.EncodingGuesser;
44 * Additional MIME types should be added to META-INF/mime.types or magic.mime.
47 * @author Jorge Ferrer
48 * @author Brian Wing Shun Chan
49 * @author Alexander Chow
51 public class MimeTypesOverrideImpl implements MimeTypesOverride {
53 public MimeTypesOverrideImpl() {
54 MimeUtil.registerMimeDetector(MagicMimeMimeDetector.class.getName());
56 Collection<String> encodings = new HashSet<String>();
58 encodings.add(StringPool.UTF8);
59 encodings.add(System.getProperty("file.encoding"));
61 EncodingGuesser.setSupportedEncodings(encodings);
64 _mimeTypes = new MimetypesFileTypeMap();
66 for (String mimeType : ENTMainUtilsLocalServiceUtil.getMimeTypes()) {
67 _mimeTypes.addMimeTypes(mimeType);
71 public String getContentType(File file) {
73 return getContentType(new FileInputStream(file), file.getName());
75 catch (FileNotFoundException fnfe) {
76 return getContentType(file.getName());
80 public String getContentType(InputStream inputStream, String fileName) {
81 String contentType = getContentType(fileName);
83 if (!contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
87 List<MimeType> mimeTypes = new ArrayList<MimeType>(
88 MimeUtil.getMimeTypes(inputStream));
90 MimeType mimeType = mimeTypes.get(0);
92 if ((mimeTypes.size() > 1) && (mimeType instanceof TextMimeType)) {
93 mimeType = mimeTypes.get(1);
97 mimeType.getMediaType() + StringPool.SLASH + mimeType.getSubType();
99 if (contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
100 contentType = getContentType(fileName);
102 else if (contentType.equals(ContentTypes.APPLICATION_ZIP)) {
103 String contentTypeByFileName = getContentType(fileName);
105 if (contentTypeByFileName.contains("vnd.openxmlformats")) {
106 contentType = contentTypeByFileName;
110 if (_log.isDebugEnabled()) {
112 "Content type " + contentType + " returned for " + fileName);
118 public String getContentType(String fileName) {
119 if (Validator.isNull(fileName)) {
120 return ContentTypes.APPLICATION_OCTET_STREAM;
123 if (!fileName.contains(StringPool.PERIOD)) {
124 fileName = StringPool.PERIOD + fileName;
127 String contentType = _mimeTypes.getContentType(fileName);
129 if (_log.isDebugEnabled()) {
131 "Content type " + contentType + " returned for file name " +
138 private static Log _log = LogFactoryUtil.getLog(MimeTypesOverrideImpl.class);
140 private MimetypesFileTypeMap _mimeTypes = new MimetypesFileTypeMap();