1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
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)
7    * any later version.
8    *
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
12   * details.
13   */
14  
15  package com.liferay.portal.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.ContentTypes;
20  import com.liferay.portal.kernel.util.MimeTypes;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.Validator;
23  
24  import eu.medsea.mimeutil.MimeType;
25  import eu.medsea.mimeutil.MimeUtil;
26  import eu.medsea.mimeutil.TextMimeType;
27  import eu.medsea.mimeutil.detector.MagicMimeMimeDetector;
28  import eu.medsea.util.EncodingGuesser;
29  
30  import java.io.File;
31  import java.io.FileInputStream;
32  import java.io.FileNotFoundException;
33  import java.io.InputStream;
34  
35  import java.util.ArrayList;
36  import java.util.Collection;
37  import java.util.HashSet;
38  import java.util.List;
39  
40  import javax.activation.MimetypesFileTypeMap;
41  
42  /**
43   * <a href="MimeTypesImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * <p>
46   * Additional MIME types should be added to META-INF/mime.types or magic.mime.
47   * </p>
48   *
49   * @author Jorge Ferrer
50   * @author Brian Wing Shun Chan
51   * @author Alexander Chow
52   */
53  public class MimeTypesImpl implements MimeTypes {
54  
55      public MimeTypesImpl() {
56          MimeUtil.registerMimeDetector(MagicMimeMimeDetector.class.getName());
57  
58          Collection<String> encodings = new HashSet<String>();
59  
60          encodings.add(StringPool.UTF8);
61          encodings.add(System.getProperty("file.encoding"));
62  
63          EncodingGuesser.setSupportedEncodings(encodings);
64      }
65  
66      public String getContentType(File file) {
67          try {
68              return getContentType(new FileInputStream(file), file.getName());
69          }
70          catch (FileNotFoundException fnfe) {
71              return getContentType(file.getName());
72          }
73      }
74  
75      public String getContentType(InputStream inputStream, String fileName) {
76          String contentType = getContentType(fileName);
77  
78          if (!contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
79              return contentType;
80          }
81  
82          List<MimeType> mimeTypes = new ArrayList<MimeType>(
83              MimeUtil.getMimeTypes(inputStream));
84  
85          MimeType mimeType = mimeTypes.get(0);
86  
87          if ((mimeTypes.size() > 1) && (mimeType instanceof TextMimeType)) {
88              mimeType = mimeTypes.get(1);
89          }
90  
91          contentType =
92              mimeType.getMediaType() + StringPool.SLASH + mimeType.getSubType();
93  
94          if (contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
95              contentType = getContentType(fileName);
96          }
97          else if (contentType.equals(ContentTypes.APPLICATION_ZIP)) {
98              String contentTypeByFileName = getContentType(fileName);
99  
100             if (contentTypeByFileName.contains("vnd.openxmlformats")) {
101                 contentType = contentTypeByFileName;
102             }
103         }
104 
105         if (_log.isDebugEnabled()) {
106             _log.debug(
107                 "Content type " + contentType + " returned for " + fileName);
108         }
109 
110         return contentType;
111     }
112 
113     public String getContentType(String fileName) {
114         if (Validator.isNull(fileName)) {
115             return ContentTypes.APPLICATION_OCTET_STREAM;
116         }
117 
118         if (!fileName.contains(StringPool.PERIOD)) {
119             fileName = StringPool.PERIOD + fileName;
120         }
121 
122         String contentType = _mimeTypes.getContentType(fileName);
123 
124         if (_log.isDebugEnabled()) {
125             _log.debug(
126                 "Content type " + contentType + " returned for file name " +
127                     fileName);
128         }
129 
130         return contentType;
131     }
132 
133     private static Log _log = LogFactoryUtil.getLog(MimeTypesImpl.class);
134 
135     private MimetypesFileTypeMap _mimeTypes = new MimetypesFileTypeMap();
136 
137 }