1 17 package org.alfresco.repo.content; 18 19 import java.util.ArrayList ; 20 import java.util.Collections ; 21 import java.util.HashMap ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import org.alfresco.config.Config; 26 import org.alfresco.config.ConfigElement; 27 import org.alfresco.config.ConfigLookupContext; 28 import org.alfresco.config.ConfigService; 29 import org.alfresco.error.AlfrescoRuntimeException; 30 import org.alfresco.service.cmr.repository.MimetypeService; 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 34 41 public class MimetypeMap implements MimetypeService 42 { 43 public static final String MIMETYPE_TEXT_PLAIN = "text/plain"; 44 public static final String MIMETYPE_TEXT_CSS = "text/css"; 45 public static final String MIMETYPE_XML = "text/xml"; 46 public static final String MIMETYPE_HTML = "text/html"; 47 public static final String MIMETYPE_XHTML = "application/xhtml+xml"; 48 public static final String MIMETYPE_PDF = "application/pdf"; 49 public static final String MIMETYPE_WORD = "application/msword"; 50 public static final String MIMETYPE_EXCEL = "application/vnd.excel"; 51 public static final String MIMETYPE_BINARY = "application/octet-stream"; 52 public static final String MIMETYPE_PPT = "application/vnd.powerpoint"; 53 public static final String MIMETYPE_FLASH = "application/x-shockwave-flash"; 54 public static final String MIMETYPE_IMAGE_GIF = "image/gif"; 55 public static final String MIMETYPE_IMAGE_JPEG = "image/jpeg"; 56 public static final String MIMETYPE_IMAGE_RGB = "image/x-rgb"; 57 public static final String MIMETYPE_OPENDOCUMENT_TEXT = "application/vnd.oasis.opendocument.text"; 59 public static final String MIMETYPE_OPENDOCUMENT_TEXT_TEMPLATE = "application/vnd.oasis.opendocument.text-template"; 60 public static final String MIMETYPE_OPENDOCUMENT_GRAPHICS = "application/vnd.oasis.opendocument.graphics"; 61 public static final String MIMETYPE_OPENDOCUMENT_GRAPHICS_TEMPLATE= "application/vnd.oasis.opendocument.graphics-template"; 62 public static final String MIMETYPE_OPENDOCUMENT_PRESENTATION= "application/vnd.oasis.opendocument.presentation"; 63 public static final String MIMETYPE_OPENDOCUMENT_PRESENTATION_TEMPLATE= "application/vnd.oasis.opendocument.presentation-template"; 64 public static final String MIMETYPE_OPENDOCUMENT_SPREADSHEET= "application/vnd.oasis.opendocument.spreadsheet"; 65 public static final String MIMETYPE_OPENDOCUMENT_SPREADSHEET_TEMPLATE= "application/vnd.oasis.opendocument.spreadsheet-template"; 66 public static final String MIMETYPE_OPENDOCUMENT_CHART= "application/vnd.oasis.opendocument.chart"; 67 public static final String MIMETYPE_OPENDOCUMENT_CHART_TEMPLATE= "applicationvnd.oasis.opendocument.chart-template"; 68 public static final String MIMETYPE_OPENDOCUMENT_IMAGE= "application/vnd.oasis.opendocument.image"; 69 public static final String MIMETYPE_OPENDOCUMENT_IMAGE_TEMPLATE= "applicationvnd.oasis.opendocument.image-template"; 70 public static final String MIMETYPE_OPENDOCUMENT_FORMULA= "application/vnd.oasis.opendocument.formula"; 71 public static final String MIMETYPE_OPENDOCUMENT_FORMULA_TEMPLATE= "applicationvnd.oasis.opendocument.formula-template"; 72 public static final String MIMETYPE_OPENDOCUMENT_TEXT_MASTER= "application/vnd.oasis.opendocument.text-master"; 73 public static final String MIMETYPE_OPENDOCUMENT_TEXT_WEB= "application/vnd.oasis.opendocument.text-web"; 74 public static final String MIMETYPE_OPENDOCUMENT_DATABASE= "application/vnd.oasis.opendocument.database"; 75 public static final String MIMETYPE_OPENOFFICE1_WRITER = "application/vnd.sun.xml.writer"; 77 public static final String MIMETYPE_OPENOFFICE1_CALC = "application/vnd.sun.xml.calc"; 78 public static final String MIMETYPE_OPENOFFICE1_DRAW = "application/vnd.sun.xml.draw"; 79 public static final String MIMETYPE_OPENOFFICE1_IMPRESS = "application/vnd.sun.xml.impress"; 80 public static final String MIMETYPE_STAROFFICE5_DRAW = "application/vnd.stardivision.draw"; 82 public static final String MIMETYPE_STAROFFICE5_CALC = "application/vnd.stardivision.calc"; 83 public static final String MIMETYPE_STAROFFICE5_IMPRESS = "application/vnd.stardivision.impress"; 84 public static final String MIMETYPE_STAROFFICE5_IMPRESS_PACKED = "application/vnd.stardivision.impress-packed"; 85 public static final String MIMETYPE_STAROFFICE5_CHART = "application/vnd.stardivision.chart"; 86 public static final String MIMETYPE_STAROFFICE5_WRITER = "application/vnd.stardivision.writer"; 87 public static final String MIMETYPE_STAROFFICE5_WRITER_GLOBAL = "application/vnd.stardivision.writer-global"; 88 public static final String MIMETYPE_STAROFFICE5_MATH = "application/vnd.stardivision.math"; 89 public static final String MIMETYPE_MP3 = "audio/x-mpeg"; 91 public static final String MIMETYPE_ACP = "application/acp"; 93 94 private static final String CONFIG_AREA = "mimetype-map"; 95 private static final String CONFIG_CONDITION = "Mimetype Map"; 96 private static final String ELEMENT_MIMETYPES = "mimetypes"; 97 private static final String ATTR_MIMETYPE = "mimetype"; 98 private static final String ATTR_DISPLAY = "display"; 99 private static final String ATTR_DEFAULT = "default"; 100 101 private static final Log logger = LogFactory.getLog(MimetypeMap.class); 102 103 private ConfigService configService; 104 105 private List <String > mimetypes; 106 private Map <String , String > extensionsByMimetype; 107 private Map <String , String > mimetypesByExtension; 108 private Map <String , String > displaysByMimetype; 109 private Map <String , String > displaysByExtension; 110 111 114 public MimetypeMap(ConfigService configService) 115 { 116 this.configService = configService; 117 } 118 119 122 public void init() 123 { 124 this.mimetypes = new ArrayList <String >(40); 125 this.extensionsByMimetype = new HashMap <String , String >(59); 126 this.mimetypesByExtension = new HashMap <String , String >(59); 127 this.displaysByMimetype = new HashMap <String , String >(59); 128 this.displaysByExtension = new HashMap <String , String >(59); 129 130 Config config = configService.getConfig(CONFIG_CONDITION, new ConfigLookupContext(CONFIG_AREA)); 131 ConfigElement mimetypesElement = config.getConfigElement(ELEMENT_MIMETYPES); 132 List <ConfigElement> mimetypes = mimetypesElement.getChildren(); 133 int count = 0; 134 for (ConfigElement mimetypeElement : mimetypes) 135 { 136 count++; 137 String mimetype = mimetypeElement.getAttribute(ATTR_MIMETYPE); 139 if (mimetype == null || mimetype.length() == 0) 140 { 141 logger.warn("Ignoring empty mimetype " + count); 142 continue; 143 } 144 mimetype = mimetype.toLowerCase(); 146 if (this.mimetypes.contains(mimetype)) 147 { 148 throw new AlfrescoRuntimeException("Duplicate mimetype definition: " + mimetype); 149 } 150 this.mimetypes.add(mimetype); 151 String mimetypeDisplay = mimetypeElement.getAttribute(ATTR_DISPLAY); 153 if (mimetypeDisplay != null && mimetypeDisplay.length() > 0) 154 { 155 this.displaysByMimetype.put(mimetype, mimetypeDisplay); 156 } 157 158 boolean isFirst = true; 160 List <ConfigElement> extensions = mimetypeElement.getChildren(); 161 for (ConfigElement extensionElement : extensions) 162 { 163 String extension = extensionElement.getValue(); 165 if (extension == null || extension.length() == 0) 166 { 167 logger.warn("Ignoring empty extension for mimetype: " + mimetype); 168 continue; 169 } 170 extension = extension.toLowerCase(); 172 this.mimetypesByExtension.put(extension, mimetype); 173 String extensionDisplay = extensionElement.getAttribute(ATTR_DISPLAY); 175 if (extensionDisplay != null && extensionDisplay.length() > 0) 176 { 177 this.displaysByExtension.put(extension, extensionDisplay); 178 } 179 else if (mimetypeDisplay != null && mimetypeDisplay.length() > 0) 180 { 181 this.displaysByExtension.put(extension, mimetypeDisplay); 183 } 184 String isDefaultStr = extensionElement.getAttribute(ATTR_DEFAULT); 186 boolean isDefault = Boolean.parseBoolean(isDefaultStr); 187 if (isDefault || isFirst) 188 { 189 this.extensionsByMimetype.put(mimetype, extension); 190 } 191 isFirst = false; 192 } 193 if (extensions.size() == 0) 195 { 196 logger.warn("No extensions defined for mimetype: " + mimetype); 197 } 198 } 199 200 this.mimetypes = Collections.unmodifiableList(this.mimetypes); 202 this.extensionsByMimetype = Collections.unmodifiableMap(this.extensionsByMimetype); 203 this.mimetypesByExtension = Collections.unmodifiableMap(this.mimetypesByExtension); 204 this.displaysByMimetype = Collections.unmodifiableMap(this.displaysByMimetype); 205 this.displaysByExtension = Collections.unmodifiableMap(this.displaysByExtension); 206 } 207 208 213 public String getExtension(String mimetype) 214 { 215 String extension = extensionsByMimetype.get(mimetype); 216 if (extension == null) 217 { 218 throw new AlfrescoRuntimeException("No extension available for mimetype: " + mimetype); 219 } 220 return extension; 221 } 222 223 public Map <String , String > getDisplaysByExtension() 224 { 225 return displaysByExtension; 226 } 227 228 public Map <String , String > getDisplaysByMimetype() 229 { 230 return displaysByMimetype; 231 } 232 233 public Map <String , String > getExtensionsByMimetype() 234 { 235 return extensionsByMimetype; 236 } 237 238 public List <String > getMimetypes() 239 { 240 return mimetypes; 241 } 242 243 public Map <String , String > getMimetypesByExtension() 244 { 245 return mimetypesByExtension; 246 } 247 248 251 public String guessMimetype(String filename) 252 { 253 filename = filename.toLowerCase(); 254 String mimetype = MIMETYPE_BINARY; 255 int index = filename.lastIndexOf('.'); 257 if (index > -1 && (index < filename.length() - 1)) 258 { 259 String extension = filename.substring(index + 1); 260 if (mimetypesByExtension.containsKey(extension)) 261 { 262 mimetype = mimetypesByExtension.get(extension); 263 } 264 } 265 return mimetype; 266 } 267 } 268 | Popular Tags |