001 /** 002 * Copyright (c) 2000-2013 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 /** 032 * Returns the content type from the file. 033 * 034 * @param file the file of the content 035 * @return the content type if it is a supported format or 036 * "application/octet-stream" if it is an unsupported format 037 */ 038 public static String getContentType(File file) { 039 return getMimeTypes().getContentType(file); 040 } 041 042 /** 043 * Returns the content type from the file and file name. 044 * 045 * @param file the file of the content (optionally <code>null</code>) 046 * @param fileName the full name or extension of the file (e.g., 047 * "Test.doc", ".doc") 048 * @return the content type if it is a supported format or 049 * "application/octet-stream" if it is an unsupported format 050 */ 051 public static String getContentType(File file, String fileName) { 052 return getMimeTypes().getContentType(file, fileName); 053 } 054 055 /** 056 * Returns the content type from the input stream and file name. 057 * 058 * <p> 059 * The input stream is not reset upon return of this method. This needs to 060 * be handled by the caller if the input stream is to be reused. 061 * Alternatively, use the method {@link #getContentType(File, String)}. 062 * </p> 063 * 064 * @param inputStream the input stream of the content (optionally 065 * <code>null</code>) 066 * @param fileName the full name or extension of the file (e.g., 067 * "Test.doc", ".doc") 068 * @return the content type if it is a supported format or 069 * "application/octet-stream" if it is an unsupported format 070 */ 071 public static String getContentType( 072 InputStream inputStream, String fileName) { 073 074 return getMimeTypes().getContentType(inputStream, fileName); 075 } 076 077 /** 078 * Returns the content type from the file name. 079 * 080 * @param fileName the full name or extension of the file (e.g., 081 * "Test.doc", ".doc") 082 * @return the content type if it is a supported format or 083 * "application/octet-stream" if it is an unsupported format 084 */ 085 public static String getContentType(String fileName) { 086 return getMimeTypes().getContentType(fileName); 087 } 088 089 /** 090 * Returns the content type from the file extension. 091 * 092 * @param extension the extension of the file (e.g., "doc") 093 * @return the content type if it is a supported format or 094 * "application/octet-stream" if it is an unsupported format 095 */ 096 public static String getExtensionContentType(String extension) { 097 return getMimeTypes().getExtensionContentType(extension); 098 } 099 100 /** 101 * Returns the possible file extensions for the content type. 102 * 103 * @param contentType the content type of the file (e.g., "image/jpeg") 104 * @return the set of extensions if it is a known content type or an empty 105 * set if it is an unknown content type 106 */ 107 public static Set<String> getExtensions(String contentType) { 108 return getMimeTypes().getExtensions(contentType); 109 } 110 111 public static MimeTypes getMimeTypes() { 112 PortalRuntimePermission.checkGetBeanProperty(MimeTypesUtil.class); 113 114 return _mimeTypes; 115 } 116 117 public static boolean isWebImage(String mimeType) { 118 return getMimeTypes().isWebImage(mimeType); 119 } 120 121 public void setMimeTypes(MimeTypes mimeTypes) { 122 PortalRuntimePermission.checkSetBeanProperty(getClass()); 123 124 _mimeTypes = mimeTypes; 125 } 126 127 private static MimeTypes _mimeTypes; 128 129 }