1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaf_classes/javax/activation/MimetypesFileTypeMap.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,336 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package javax.activation; 1.30 + 1.31 +import java.io.*; 1.32 +import java.net.*; 1.33 +import java.util.*; 1.34 +import com.sun.activation.registries.MimeTypeFile; 1.35 +import com.sun.activation.registries.LogSupport; 1.36 + 1.37 +/** 1.38 + * This class extends FileTypeMap and provides data typing of files 1.39 + * via their file extension. It uses the <code>.mime.types</code> format. <p> 1.40 + * 1.41 + * <b>MIME types file search order:</b><p> 1.42 + * The MimetypesFileTypeMap looks in various places in the user's 1.43 + * system for MIME types file entries. When requests are made 1.44 + * to search for MIME types in the MimetypesFileTypeMap, it searches 1.45 + * MIME types files in the following order: 1.46 + * <p> 1.47 + * <ol> 1.48 + * <li> Programmatically added entries to the MimetypesFileTypeMap instance. 1.49 + * <li> The file <code>.mime.types</code> in the user's home directory. 1.50 + * <li> The file <<i>java.home</i>><code>/lib/mime.types</code>. 1.51 + * <li> The file or resources named <code>META-INF/mime.types</code>. 1.52 + * <li> The file or resource named <code>META-INF/mimetypes.default</code> 1.53 + * (usually found only in the <code>activation.jar</code> file). 1.54 + * </ol> 1.55 + * <p> 1.56 + * <b>MIME types file format:</b><p> 1.57 + * 1.58 + * <code> 1.59 + * # comments begin with a '#'<br> 1.60 + * # the format is <mime type> <space separated file extensions><br> 1.61 + * # for example:<br> 1.62 + * text/plain txt text TXT<br> 1.63 + * # this would map file.txt, file.text, and file.TXT to<br> 1.64 + * # the mime type "text/plain"<br> 1.65 + * </code> 1.66 + * 1.67 + * @author Bart Calder 1.68 + * @author Bill Shannon 1.69 + * 1.70 + * @since 1.6 1.71 + */ 1.72 +public class MimetypesFileTypeMap extends FileTypeMap { 1.73 + /* 1.74 + * We manage a collection of databases, searched in order. 1.75 + * The default database is shared between all instances 1.76 + * of this class. 1.77 + * XXX - Can we safely share more databases between instances? 1.78 + */ 1.79 + private static MimeTypeFile defDB = null; 1.80 + private MimeTypeFile[] DB; 1.81 + private static final int PROG = 0; // programmatically added entries 1.82 + 1.83 + private static String defaultType = "application/octet-stream"; 1.84 + 1.85 + /** 1.86 + * The default constructor. 1.87 + */ 1.88 + public MimetypesFileTypeMap() { 1.89 + Vector dbv = new Vector(5); // usually 5 or less databases 1.90 + MimeTypeFile mf = null; 1.91 + dbv.addElement(null); // place holder for PROG entry 1.92 + 1.93 + LogSupport.log("MimetypesFileTypeMap: load HOME"); 1.94 + try { 1.95 + String user_home = System.getProperty("user.home"); 1.96 + 1.97 + if (user_home != null) { 1.98 + String path = user_home + File.separator + ".mime.types"; 1.99 + mf = loadFile(path); 1.100 + if (mf != null) 1.101 + dbv.addElement(mf); 1.102 + } 1.103 + } catch (SecurityException ex) {} 1.104 + 1.105 + LogSupport.log("MimetypesFileTypeMap: load SYS"); 1.106 + try { 1.107 + // check system's home 1.108 + String system_mimetypes = System.getProperty("java.home") + 1.109 + File.separator + "lib" + File.separator + "mime.types"; 1.110 + mf = loadFile(system_mimetypes); 1.111 + if (mf != null) 1.112 + dbv.addElement(mf); 1.113 + } catch (SecurityException ex) {} 1.114 + 1.115 + LogSupport.log("MimetypesFileTypeMap: load JAR"); 1.116 + // load from the app's jar file 1.117 + loadAllResources(dbv, "META-INF/mime.types"); 1.118 + 1.119 + LogSupport.log("MimetypesFileTypeMap: load DEF"); 1.120 + synchronized (MimetypesFileTypeMap.class) { 1.121 + // see if another instance has created this yet. 1.122 + if (defDB == null) 1.123 + defDB = loadResource("/META-INF/mimetypes.default"); 1.124 + } 1.125 + 1.126 + if (defDB != null) 1.127 + dbv.addElement(defDB); 1.128 + 1.129 + DB = new MimeTypeFile[dbv.size()]; 1.130 + dbv.copyInto(DB); 1.131 + } 1.132 + 1.133 + /** 1.134 + * Load from the named resource. 1.135 + */ 1.136 + private MimeTypeFile loadResource(String name) { 1.137 + InputStream clis = null; 1.138 + try { 1.139 + clis = SecuritySupport.getResourceAsStream(this.getClass(), name); 1.140 + if (clis != null) { 1.141 + MimeTypeFile mf = new MimeTypeFile(clis); 1.142 + if (LogSupport.isLoggable()) 1.143 + LogSupport.log("MimetypesFileTypeMap: successfully " + 1.144 + "loaded mime types file: " + name); 1.145 + return mf; 1.146 + } else { 1.147 + if (LogSupport.isLoggable()) 1.148 + LogSupport.log("MimetypesFileTypeMap: not loading " + 1.149 + "mime types file: " + name); 1.150 + } 1.151 + } catch (IOException e) { 1.152 + if (LogSupport.isLoggable()) 1.153 + LogSupport.log("MimetypesFileTypeMap: can't load " + name, e); 1.154 + } catch (SecurityException sex) { 1.155 + if (LogSupport.isLoggable()) 1.156 + LogSupport.log("MimetypesFileTypeMap: can't load " + name, sex); 1.157 + } finally { 1.158 + try { 1.159 + if (clis != null) 1.160 + clis.close(); 1.161 + } catch (IOException ex) { } // ignore it 1.162 + } 1.163 + return null; 1.164 + } 1.165 + 1.166 + /** 1.167 + * Load all of the named resource. 1.168 + */ 1.169 + private void loadAllResources(Vector v, String name) { 1.170 + boolean anyLoaded = false; 1.171 + try { 1.172 + URL[] urls; 1.173 + ClassLoader cld = null; 1.174 + // First try the "application's" class loader. 1.175 + cld = SecuritySupport.getContextClassLoader(); 1.176 + if (cld == null) 1.177 + cld = this.getClass().getClassLoader(); 1.178 + if (cld != null) 1.179 + urls = SecuritySupport.getResources(cld, name); 1.180 + else 1.181 + urls = SecuritySupport.getSystemResources(name); 1.182 + if (urls != null) { 1.183 + if (LogSupport.isLoggable()) 1.184 + LogSupport.log("MimetypesFileTypeMap: getResources"); 1.185 + for (int i = 0; i < urls.length; i++) { 1.186 + URL url = urls[i]; 1.187 + InputStream clis = null; 1.188 + if (LogSupport.isLoggable()) 1.189 + LogSupport.log("MimetypesFileTypeMap: URL " + url); 1.190 + try { 1.191 + clis = SecuritySupport.openStream(url); 1.192 + if (clis != null) { 1.193 + v.addElement(new MimeTypeFile(clis)); 1.194 + anyLoaded = true; 1.195 + if (LogSupport.isLoggable()) 1.196 + LogSupport.log("MimetypesFileTypeMap: " + 1.197 + "successfully loaded " + 1.198 + "mime types from URL: " + url); 1.199 + } else { 1.200 + if (LogSupport.isLoggable()) 1.201 + LogSupport.log("MimetypesFileTypeMap: " + 1.202 + "not loading " + 1.203 + "mime types from URL: " + url); 1.204 + } 1.205 + } catch (IOException ioex) { 1.206 + if (LogSupport.isLoggable()) 1.207 + LogSupport.log("MimetypesFileTypeMap: can't load " + 1.208 + url, ioex); 1.209 + } catch (SecurityException sex) { 1.210 + if (LogSupport.isLoggable()) 1.211 + LogSupport.log("MimetypesFileTypeMap: can't load " + 1.212 + url, sex); 1.213 + } finally { 1.214 + try { 1.215 + if (clis != null) 1.216 + clis.close(); 1.217 + } catch (IOException cex) { } 1.218 + } 1.219 + } 1.220 + } 1.221 + } catch (Exception ex) { 1.222 + if (LogSupport.isLoggable()) 1.223 + LogSupport.log("MimetypesFileTypeMap: can't load " + name, ex); 1.224 + } 1.225 + 1.226 + // if failed to load anything, fall back to old technique, just in case 1.227 + if (!anyLoaded) { 1.228 + LogSupport.log("MimetypesFileTypeMap: !anyLoaded"); 1.229 + MimeTypeFile mf = loadResource("/" + name); 1.230 + if (mf != null) 1.231 + v.addElement(mf); 1.232 + } 1.233 + } 1.234 + 1.235 + /** 1.236 + * Load the named file. 1.237 + */ 1.238 + private MimeTypeFile loadFile(String name) { 1.239 + MimeTypeFile mtf = null; 1.240 + 1.241 + try { 1.242 + mtf = new MimeTypeFile(name); 1.243 + } catch (IOException e) { 1.244 + // e.printStackTrace(); 1.245 + } 1.246 + return mtf; 1.247 + } 1.248 + 1.249 + /** 1.250 + * Construct a MimetypesFileTypeMap with programmatic entries 1.251 + * added from the named file. 1.252 + * 1.253 + * @param mimeTypeFileName the file name 1.254 + */ 1.255 + public MimetypesFileTypeMap(String mimeTypeFileName) throws IOException { 1.256 + this(); 1.257 + DB[PROG] = new MimeTypeFile(mimeTypeFileName); 1.258 + } 1.259 + 1.260 + /** 1.261 + * Construct a MimetypesFileTypeMap with programmatic entries 1.262 + * added from the InputStream. 1.263 + * 1.264 + * @param is the input stream to read from 1.265 + */ 1.266 + public MimetypesFileTypeMap(InputStream is) { 1.267 + this(); 1.268 + try { 1.269 + DB[PROG] = new MimeTypeFile(is); 1.270 + } catch (IOException ex) { 1.271 + // XXX - really should throw it 1.272 + } 1.273 + } 1.274 + 1.275 + /** 1.276 + * Prepend the MIME type values to the registry. 1.277 + * 1.278 + * @param mime_types A .mime.types formatted string of entries. 1.279 + */ 1.280 + public synchronized void addMimeTypes(String mime_types) { 1.281 + // check to see if we have created the registry 1.282 + if (DB[PROG] == null) 1.283 + DB[PROG] = new MimeTypeFile(); // make one 1.284 + 1.285 + DB[PROG].appendToRegistry(mime_types); 1.286 + } 1.287 + 1.288 + /** 1.289 + * Return the MIME type of the file object. 1.290 + * The implementation in this class calls 1.291 + * <code>getContentType(f.getName())</code>. 1.292 + * 1.293 + * @param f the file 1.294 + * @return the file's MIME type 1.295 + */ 1.296 + public String getContentType(File f) { 1.297 + return this.getContentType(f.getName()); 1.298 + } 1.299 + 1.300 + /** 1.301 + * Return the MIME type based on the specified file name. 1.302 + * The MIME type entries are searched as described above under 1.303 + * <i>MIME types file search order</i>. 1.304 + * If no entry is found, the type "application/octet-stream" is returned. 1.305 + * 1.306 + * @param filename the file name 1.307 + * @return the file's MIME type 1.308 + */ 1.309 + public synchronized String getContentType(String filename) { 1.310 + int dot_pos = filename.lastIndexOf("."); // period index 1.311 + 1.312 + if (dot_pos < 0) 1.313 + return defaultType; 1.314 + 1.315 + String file_ext = filename.substring(dot_pos + 1); 1.316 + if (file_ext.length() == 0) 1.317 + return defaultType; 1.318 + 1.319 + for (int i = 0; i < DB.length; i++) { 1.320 + if (DB[i] == null) 1.321 + continue; 1.322 + String result = DB[i].getMIMETypeString(file_ext); 1.323 + if (result != null) 1.324 + return result; 1.325 + } 1.326 + return defaultType; 1.327 + } 1.328 + 1.329 + /** 1.330 + * for debugging... 1.331 + * 1.332 + public static void main(String[] argv) throws Exception { 1.333 + MimetypesFileTypeMap map = new MimetypesFileTypeMap(); 1.334 + System.out.println("File " + argv[0] + " has MIME type " + 1.335 + map.getContentType(argv[0])); 1.336 + System.exit(0); 1.337 + } 1.338 + */ 1.339 +}