1 21 22 27 28 package javax.activation; 29 30 import java.io.*; 31 import java.net.*; 32 import java.util.*; 33 import com.sun.activation.registries.MimeTypeFile; 34 import com.sun.activation.registries.LogSupport; 35 36 69 public class MimetypesFileTypeMap extends FileTypeMap { 70 76 private static MimeTypeFile defDB = null; 77 private MimeTypeFile[] DB; 78 private static final int PROG = 0; 80 private static String defaultType = "application/octet-stream"; 81 82 85 public MimetypesFileTypeMap() { 86 Vector dbv = new Vector(5); MimeTypeFile mf = null; 88 dbv.addElement(null); 90 LogSupport.log("MimetypesFileTypeMap: load HOME"); 91 try { 92 String user_home = System.getProperty("user.home"); 93 94 if (user_home != null) { 95 String path = user_home + File.separator + ".mime.types"; 96 mf = loadFile(path); 97 if (mf != null) 98 dbv.addElement(mf); 99 } 100 } catch (SecurityException ex) {} 101 102 LogSupport.log("MimetypesFileTypeMap: load SYS"); 103 try { 104 String system_mimetypes = System.getProperty("java.home") + 106 File.separator + "lib" + File.separator + "mime.types"; 107 mf = loadFile(system_mimetypes); 108 if (mf != null) 109 dbv.addElement(mf); 110 } catch (SecurityException ex) {} 111 112 LogSupport.log("MimetypesFileTypeMap: load JAR"); 113 loadAllResources(dbv, "META-INF/mime.types"); 115 116 LogSupport.log("MimetypesFileTypeMap: load DEF"); 117 synchronized (MimetypesFileTypeMap .class) { 118 if (defDB == null) 120 defDB = loadResource("/META-INF/mimetypes.default"); 121 } 122 123 if (defDB != null) 124 dbv.addElement(defDB); 125 126 DB = new MimeTypeFile[dbv.size()]; 127 dbv.copyInto(DB); 128 } 129 130 133 private MimeTypeFile loadResource(String name) { 134 InputStream clis = null; 135 try { 136 clis = SecuritySupport.getResourceAsStream(this.getClass(), name); 137 if (clis != null) { 138 MimeTypeFile mf = new MimeTypeFile(clis); 139 if (LogSupport.isLoggable()) 140 LogSupport.log("MimetypesFileTypeMap: successfully " + 141 "loaded mime types file: " + name); 142 return mf; 143 } else { 144 if (LogSupport.isLoggable()) 145 LogSupport.log("MimetypesFileTypeMap: not loading " + 146 "mime types file: " + name); 147 } 148 } catch (IOException e) { 149 if (LogSupport.isLoggable()) 150 LogSupport.log("MimetypesFileTypeMap: can't load " + name, e); 151 } catch (SecurityException sex) { 152 if (LogSupport.isLoggable()) 153 LogSupport.log("MimetypesFileTypeMap: can't load " + name, sex); 154 } finally { 155 try { 156 if (clis != null) 157 clis.close(); 158 } catch (IOException ex) { } } 160 return null; 161 } 162 163 166 private void loadAllResources(Vector v, String name) { 167 boolean anyLoaded = false; 168 try { 169 URL[] urls; 170 ClassLoader cld = null; 171 cld = SecuritySupport.getContextClassLoader(); 173 if (cld == null) 174 cld = this.getClass().getClassLoader(); 175 if (cld != null) 176 urls = SecuritySupport.getResources(cld, name); 177 else 178 urls = SecuritySupport.getSystemResources(name); 179 if (urls != null) { 180 if (LogSupport.isLoggable()) 181 LogSupport.log("MimetypesFileTypeMap: getResources"); 182 for (int i = 0; i < urls.length; i++) { 183 URL url = urls[i]; 184 InputStream clis = null; 185 if (LogSupport.isLoggable()) 186 LogSupport.log("MimetypesFileTypeMap: URL " + url); 187 try { 188 clis = SecuritySupport.openStream(url); 189 if (clis != null) { 190 v.addElement(new MimeTypeFile(clis)); 191 anyLoaded = true; 192 if (LogSupport.isLoggable()) 193 LogSupport.log("MimetypesFileTypeMap: " + 194 "successfully loaded " + 195 "mime types from URL: " + url); 196 } else { 197 if (LogSupport.isLoggable()) 198 LogSupport.log("MimetypesFileTypeMap: " + 199 "not loading " + 200 "mime types from URL: " + url); 201 } 202 } catch (IOException ioex) { 203 if (LogSupport.isLoggable()) 204 LogSupport.log("MimetypesFileTypeMap: can't load " + 205 url, ioex); 206 } catch (SecurityException sex) { 207 if (LogSupport.isLoggable()) 208 LogSupport.log("MimetypesFileTypeMap: can't load " + 209 url, sex); 210 } finally { 211 try { 212 if (clis != null) 213 clis.close(); 214 } catch (IOException cex) { } 215 } 216 } 217 } 218 } catch (Exception ex) { 219 if (LogSupport.isLoggable()) 220 LogSupport.log("MimetypesFileTypeMap: can't load " + name, ex); 221 } 222 223 if (!anyLoaded) { 225 LogSupport.log("MimetypesFileTypeMap: !anyLoaded"); 226 MimeTypeFile mf = loadResource("/" + name); 227 if (mf != null) 228 v.addElement(mf); 229 } 230 } 231 232 235 private MimeTypeFile loadFile(String name) { 236 MimeTypeFile mtf = null; 237 238 try { 239 mtf = new MimeTypeFile(name); 240 } catch (IOException e) { 241 } 243 return mtf; 244 } 245 246 252 public MimetypesFileTypeMap(String mimeTypeFileName) throws IOException { 253 this(); 254 DB[PROG] = new MimeTypeFile(mimeTypeFileName); 255 } 256 257 263 public MimetypesFileTypeMap(InputStream is) { 264 this(); 265 try { 266 DB[PROG] = new MimeTypeFile(is); 267 } catch (IOException ex) { 268 } 270 } 271 272 277 public synchronized void addMimeTypes(String mime_types) { 278 if (DB[PROG] == null) 280 DB[PROG] = new MimeTypeFile(); 282 DB[PROG].appendToRegistry(mime_types); 283 } 284 285 293 public String getContentType(File f) { 294 return this.getContentType(f.getName()); 295 } 296 297 306 public synchronized String getContentType(String filename) { 307 int dot_pos = filename.lastIndexOf("."); 309 if (dot_pos < 0) 310 return defaultType; 311 312 String file_ext = filename.substring(dot_pos + 1); 313 if (file_ext.length() == 0) 314 return defaultType; 315 316 for (int i = 0; i < DB.length; i++) { 317 if (DB[i] == null) 318 continue; 319 String result = DB[i].getMIMETypeString(file_ext); 320 if (result != null) 321 return result; 322 } 323 return defaultType; 324 } 325 326 336 } 337 | Popular Tags |