1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaf_classes/javax/activation/MimetypesFileTypeMap.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,328 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2013, 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 + */ 1.76 + private MimeTypeFile[] DB; 1.77 + private static final int PROG = 0; // programmatically added entries 1.78 + 1.79 + private static String defaultType = "application/octet-stream"; 1.80 + 1.81 + /** 1.82 + * The default constructor. 1.83 + */ 1.84 + public MimetypesFileTypeMap() { 1.85 + Vector dbv = new Vector(5); // usually 5 or less databases 1.86 + MimeTypeFile mf = null; 1.87 + dbv.addElement(null); // place holder for PROG entry 1.88 + 1.89 + LogSupport.log("MimetypesFileTypeMap: load HOME"); 1.90 + try { 1.91 + String user_home = System.getProperty("user.home"); 1.92 + 1.93 + if (user_home != null) { 1.94 + String path = user_home + File.separator + ".mime.types"; 1.95 + mf = loadFile(path); 1.96 + if (mf != null) 1.97 + dbv.addElement(mf); 1.98 + } 1.99 + } catch (SecurityException ex) {} 1.100 + 1.101 + LogSupport.log("MimetypesFileTypeMap: load SYS"); 1.102 + try { 1.103 + // check system's home 1.104 + String system_mimetypes = System.getProperty("java.home") + 1.105 + File.separator + "lib" + File.separator + "mime.types"; 1.106 + mf = loadFile(system_mimetypes); 1.107 + if (mf != null) 1.108 + dbv.addElement(mf); 1.109 + } catch (SecurityException ex) {} 1.110 + 1.111 + LogSupport.log("MimetypesFileTypeMap: load JAR"); 1.112 + // load from the app's jar file 1.113 + loadAllResources(dbv, "META-INF/mime.types"); 1.114 + 1.115 + LogSupport.log("MimetypesFileTypeMap: load DEF"); 1.116 + mf = loadResource("/META-INF/mimetypes.default"); 1.117 + 1.118 + if (mf != null) 1.119 + dbv.addElement(mf); 1.120 + 1.121 + DB = new MimeTypeFile[dbv.size()]; 1.122 + dbv.copyInto(DB); 1.123 + } 1.124 + 1.125 + /** 1.126 + * Load from the named resource. 1.127 + */ 1.128 + private MimeTypeFile loadResource(String name) { 1.129 + InputStream clis = null; 1.130 + try { 1.131 + clis = SecuritySupport.getResourceAsStream(this.getClass(), name); 1.132 + if (clis != null) { 1.133 + MimeTypeFile mf = new MimeTypeFile(clis); 1.134 + if (LogSupport.isLoggable()) 1.135 + LogSupport.log("MimetypesFileTypeMap: successfully " + 1.136 + "loaded mime types file: " + name); 1.137 + return mf; 1.138 + } else { 1.139 + if (LogSupport.isLoggable()) 1.140 + LogSupport.log("MimetypesFileTypeMap: not loading " + 1.141 + "mime types file: " + name); 1.142 + } 1.143 + } catch (IOException e) { 1.144 + if (LogSupport.isLoggable()) 1.145 + LogSupport.log("MimetypesFileTypeMap: can't load " + name, e); 1.146 + } catch (SecurityException sex) { 1.147 + if (LogSupport.isLoggable()) 1.148 + LogSupport.log("MimetypesFileTypeMap: can't load " + name, sex); 1.149 + } finally { 1.150 + try { 1.151 + if (clis != null) 1.152 + clis.close(); 1.153 + } catch (IOException ex) { } // ignore it 1.154 + } 1.155 + return null; 1.156 + } 1.157 + 1.158 + /** 1.159 + * Load all of the named resource. 1.160 + */ 1.161 + private void loadAllResources(Vector v, String name) { 1.162 + boolean anyLoaded = false; 1.163 + try { 1.164 + URL[] urls; 1.165 + ClassLoader cld = null; 1.166 + // First try the "application's" class loader. 1.167 + cld = SecuritySupport.getContextClassLoader(); 1.168 + if (cld == null) 1.169 + cld = this.getClass().getClassLoader(); 1.170 + if (cld != null) 1.171 + urls = SecuritySupport.getResources(cld, name); 1.172 + else 1.173 + urls = SecuritySupport.getSystemResources(name); 1.174 + if (urls != null) { 1.175 + if (LogSupport.isLoggable()) 1.176 + LogSupport.log("MimetypesFileTypeMap: getResources"); 1.177 + for (int i = 0; i < urls.length; i++) { 1.178 + URL url = urls[i]; 1.179 + InputStream clis = null; 1.180 + if (LogSupport.isLoggable()) 1.181 + LogSupport.log("MimetypesFileTypeMap: URL " + url); 1.182 + try { 1.183 + clis = SecuritySupport.openStream(url); 1.184 + if (clis != null) { 1.185 + v.addElement(new MimeTypeFile(clis)); 1.186 + anyLoaded = true; 1.187 + if (LogSupport.isLoggable()) 1.188 + LogSupport.log("MimetypesFileTypeMap: " + 1.189 + "successfully loaded " + 1.190 + "mime types from URL: " + url); 1.191 + } else { 1.192 + if (LogSupport.isLoggable()) 1.193 + LogSupport.log("MimetypesFileTypeMap: " + 1.194 + "not loading " + 1.195 + "mime types from URL: " + url); 1.196 + } 1.197 + } catch (IOException ioex) { 1.198 + if (LogSupport.isLoggable()) 1.199 + LogSupport.log("MimetypesFileTypeMap: can't load " + 1.200 + url, ioex); 1.201 + } catch (SecurityException sex) { 1.202 + if (LogSupport.isLoggable()) 1.203 + LogSupport.log("MimetypesFileTypeMap: can't load " + 1.204 + url, sex); 1.205 + } finally { 1.206 + try { 1.207 + if (clis != null) 1.208 + clis.close(); 1.209 + } catch (IOException cex) { } 1.210 + } 1.211 + } 1.212 + } 1.213 + } catch (Exception ex) { 1.214 + if (LogSupport.isLoggable()) 1.215 + LogSupport.log("MimetypesFileTypeMap: can't load " + name, ex); 1.216 + } 1.217 + 1.218 + // if failed to load anything, fall back to old technique, just in case 1.219 + if (!anyLoaded) { 1.220 + LogSupport.log("MimetypesFileTypeMap: !anyLoaded"); 1.221 + MimeTypeFile mf = loadResource("/" + name); 1.222 + if (mf != null) 1.223 + v.addElement(mf); 1.224 + } 1.225 + } 1.226 + 1.227 + /** 1.228 + * Load the named file. 1.229 + */ 1.230 + private MimeTypeFile loadFile(String name) { 1.231 + MimeTypeFile mtf = null; 1.232 + 1.233 + try { 1.234 + mtf = new MimeTypeFile(name); 1.235 + } catch (IOException e) { 1.236 + // e.printStackTrace(); 1.237 + } 1.238 + return mtf; 1.239 + } 1.240 + 1.241 + /** 1.242 + * Construct a MimetypesFileTypeMap with programmatic entries 1.243 + * added from the named file. 1.244 + * 1.245 + * @param mimeTypeFileName the file name 1.246 + */ 1.247 + public MimetypesFileTypeMap(String mimeTypeFileName) throws IOException { 1.248 + this(); 1.249 + DB[PROG] = new MimeTypeFile(mimeTypeFileName); 1.250 + } 1.251 + 1.252 + /** 1.253 + * Construct a MimetypesFileTypeMap with programmatic entries 1.254 + * added from the InputStream. 1.255 + * 1.256 + * @param is the input stream to read from 1.257 + */ 1.258 + public MimetypesFileTypeMap(InputStream is) { 1.259 + this(); 1.260 + try { 1.261 + DB[PROG] = new MimeTypeFile(is); 1.262 + } catch (IOException ex) { 1.263 + // XXX - really should throw it 1.264 + } 1.265 + } 1.266 + 1.267 + /** 1.268 + * Prepend the MIME type values to the registry. 1.269 + * 1.270 + * @param mime_types A .mime.types formatted string of entries. 1.271 + */ 1.272 + public synchronized void addMimeTypes(String mime_types) { 1.273 + // check to see if we have created the registry 1.274 + if (DB[PROG] == null) 1.275 + DB[PROG] = new MimeTypeFile(); // make one 1.276 + 1.277 + DB[PROG].appendToRegistry(mime_types); 1.278 + } 1.279 + 1.280 + /** 1.281 + * Return the MIME type of the file object. 1.282 + * The implementation in this class calls 1.283 + * <code>getContentType(f.getName())</code>. 1.284 + * 1.285 + * @param f the file 1.286 + * @return the file's MIME type 1.287 + */ 1.288 + public String getContentType(File f) { 1.289 + return this.getContentType(f.getName()); 1.290 + } 1.291 + 1.292 + /** 1.293 + * Return the MIME type based on the specified file name. 1.294 + * The MIME type entries are searched as described above under 1.295 + * <i>MIME types file search order</i>. 1.296 + * If no entry is found, the type "application/octet-stream" is returned. 1.297 + * 1.298 + * @param filename the file name 1.299 + * @return the file's MIME type 1.300 + */ 1.301 + public synchronized String getContentType(String filename) { 1.302 + int dot_pos = filename.lastIndexOf("."); // period index 1.303 + 1.304 + if (dot_pos < 0) 1.305 + return defaultType; 1.306 + 1.307 + String file_ext = filename.substring(dot_pos + 1); 1.308 + if (file_ext.length() == 0) 1.309 + return defaultType; 1.310 + 1.311 + for (int i = 0; i < DB.length; i++) { 1.312 + if (DB[i] == null) 1.313 + continue; 1.314 + String result = DB[i].getMIMETypeString(file_ext); 1.315 + if (result != null) 1.316 + return result; 1.317 + } 1.318 + return defaultType; 1.319 + } 1.320 + 1.321 + /** 1.322 + * for debugging... 1.323 + * 1.324 + public static void main(String[] argv) throws Exception { 1.325 + MimetypesFileTypeMap map = new MimetypesFileTypeMap(); 1.326 + System.out.println("File " + argv[0] + " has MIME type " + 1.327 + map.getContentType(argv[0])); 1.328 + System.exit(0); 1.329 + } 1.330 + */ 1.331 +}