1 16 17 package org.springframework.mail.javamail; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 22 import javax.activation.FileTypeMap ; 23 import javax.activation.MimetypesFileTypeMap ; 24 25 import org.springframework.beans.factory.InitializingBean; 26 import org.springframework.core.io.ClassPathResource; 27 import org.springframework.core.io.Resource; 28 29 61 public class ConfigurableMimeFileTypeMap extends FileTypeMap implements InitializingBean { 62 63 66 private Resource mappingLocation = new ClassPathResource("mime.types", getClass()); 67 68 71 private String [] mappings; 72 73 77 private FileTypeMap fileTypeMap; 78 79 80 86 public void setMappingLocation(Resource mappingLocation) { 87 this.mappingLocation = mappingLocation; 88 } 89 90 96 public void setMappings(String [] mappings) { 97 this.mappings = mappings; 98 } 99 100 101 104 public void afterPropertiesSet() { 105 getFileTypeMap(); 106 } 107 108 115 protected final FileTypeMap getFileTypeMap() { 116 if (this.fileTypeMap == null) { 117 try { 118 this.fileTypeMap = createFileTypeMap(this.mappingLocation, this.mappings); 119 } 120 catch (IOException ex) { 121 throw new IllegalStateException ( 122 "Could not load specified MIME type mapping file: " + this.mappingLocation); 123 } 124 } 125 return fileTypeMap; 126 } 127 128 141 protected FileTypeMap createFileTypeMap(Resource mappingLocation, String [] mappings) throws IOException { 142 MimetypesFileTypeMap fileTypeMap = (mappingLocation != null) ? 143 new MimetypesFileTypeMap (mappingLocation.getInputStream()) : new MimetypesFileTypeMap (); 144 if (mappings != null) { 145 for (int i = 0; i < mappings.length; i++) { 146 fileTypeMap.addMimeTypes(mappings[i]); 147 } 148 } 149 return fileTypeMap; 150 } 151 152 153 157 public String getContentType(File file) { 158 return getFileTypeMap().getContentType(file); 159 } 160 161 165 public String getContentType(String fileName) { 166 return getFileTypeMap().getContentType(fileName); 167 } 168 169 } 170 | Popular Tags |