Wed, 27 Apr 2016 01:27:09 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package javax.activation; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.io.*; |
aoqi@0 | 29 | import java.net.*; |
aoqi@0 | 30 | import java.util.*; |
aoqi@0 | 31 | import com.sun.activation.registries.MimeTypeFile; |
aoqi@0 | 32 | import com.sun.activation.registries.LogSupport; |
aoqi@0 | 33 | |
aoqi@0 | 34 | /** |
aoqi@0 | 35 | * This class extends FileTypeMap and provides data typing of files |
aoqi@0 | 36 | * via their file extension. It uses the <code>.mime.types</code> format. <p> |
aoqi@0 | 37 | * |
aoqi@0 | 38 | * <b>MIME types file search order:</b><p> |
aoqi@0 | 39 | * The MimetypesFileTypeMap looks in various places in the user's |
aoqi@0 | 40 | * system for MIME types file entries. When requests are made |
aoqi@0 | 41 | * to search for MIME types in the MimetypesFileTypeMap, it searches |
aoqi@0 | 42 | * MIME types files in the following order: |
aoqi@0 | 43 | * <p> |
aoqi@0 | 44 | * <ol> |
aoqi@0 | 45 | * <li> Programmatically added entries to the MimetypesFileTypeMap instance. |
aoqi@0 | 46 | * <li> The file <code>.mime.types</code> in the user's home directory. |
aoqi@0 | 47 | * <li> The file <<i>java.home</i>><code>/lib/mime.types</code>. |
aoqi@0 | 48 | * <li> The file or resources named <code>META-INF/mime.types</code>. |
aoqi@0 | 49 | * <li> The file or resource named <code>META-INF/mimetypes.default</code> |
aoqi@0 | 50 | * (usually found only in the <code>activation.jar</code> file). |
aoqi@0 | 51 | * </ol> |
aoqi@0 | 52 | * <p> |
aoqi@0 | 53 | * <b>MIME types file format:</b><p> |
aoqi@0 | 54 | * |
aoqi@0 | 55 | * <code> |
aoqi@0 | 56 | * # comments begin with a '#'<br> |
aoqi@0 | 57 | * # the format is <mime type> <space separated file extensions><br> |
aoqi@0 | 58 | * # for example:<br> |
aoqi@0 | 59 | * text/plain txt text TXT<br> |
aoqi@0 | 60 | * # this would map file.txt, file.text, and file.TXT to<br> |
aoqi@0 | 61 | * # the mime type "text/plain"<br> |
aoqi@0 | 62 | * </code> |
aoqi@0 | 63 | * |
aoqi@0 | 64 | * @author Bart Calder |
aoqi@0 | 65 | * @author Bill Shannon |
aoqi@0 | 66 | * |
aoqi@0 | 67 | * @since 1.6 |
aoqi@0 | 68 | */ |
aoqi@0 | 69 | public class MimetypesFileTypeMap extends FileTypeMap { |
aoqi@0 | 70 | /* |
aoqi@0 | 71 | * We manage a collection of databases, searched in order. |
aoqi@0 | 72 | */ |
aoqi@0 | 73 | private MimeTypeFile[] DB; |
aoqi@0 | 74 | private static final int PROG = 0; // programmatically added entries |
aoqi@0 | 75 | |
aoqi@0 | 76 | private static String defaultType = "application/octet-stream"; |
aoqi@0 | 77 | |
aoqi@0 | 78 | /** |
aoqi@0 | 79 | * The default constructor. |
aoqi@0 | 80 | */ |
aoqi@0 | 81 | public MimetypesFileTypeMap() { |
aoqi@0 | 82 | Vector dbv = new Vector(5); // usually 5 or less databases |
aoqi@0 | 83 | MimeTypeFile mf = null; |
aoqi@0 | 84 | dbv.addElement(null); // place holder for PROG entry |
aoqi@0 | 85 | |
aoqi@0 | 86 | LogSupport.log("MimetypesFileTypeMap: load HOME"); |
aoqi@0 | 87 | try { |
aoqi@0 | 88 | String user_home = System.getProperty("user.home"); |
aoqi@0 | 89 | |
aoqi@0 | 90 | if (user_home != null) { |
aoqi@0 | 91 | String path = user_home + File.separator + ".mime.types"; |
aoqi@0 | 92 | mf = loadFile(path); |
aoqi@0 | 93 | if (mf != null) |
aoqi@0 | 94 | dbv.addElement(mf); |
aoqi@0 | 95 | } |
aoqi@0 | 96 | } catch (SecurityException ex) {} |
aoqi@0 | 97 | |
aoqi@0 | 98 | LogSupport.log("MimetypesFileTypeMap: load SYS"); |
aoqi@0 | 99 | try { |
aoqi@0 | 100 | // check system's home |
aoqi@0 | 101 | String system_mimetypes = System.getProperty("java.home") + |
aoqi@0 | 102 | File.separator + "lib" + File.separator + "mime.types"; |
aoqi@0 | 103 | mf = loadFile(system_mimetypes); |
aoqi@0 | 104 | if (mf != null) |
aoqi@0 | 105 | dbv.addElement(mf); |
aoqi@0 | 106 | } catch (SecurityException ex) {} |
aoqi@0 | 107 | |
aoqi@0 | 108 | LogSupport.log("MimetypesFileTypeMap: load JAR"); |
aoqi@0 | 109 | // load from the app's jar file |
aoqi@0 | 110 | loadAllResources(dbv, "META-INF/mime.types"); |
aoqi@0 | 111 | |
aoqi@0 | 112 | LogSupport.log("MimetypesFileTypeMap: load DEF"); |
aoqi@0 | 113 | mf = loadResource("/META-INF/mimetypes.default"); |
aoqi@0 | 114 | |
aoqi@0 | 115 | if (mf != null) |
aoqi@0 | 116 | dbv.addElement(mf); |
aoqi@0 | 117 | |
aoqi@0 | 118 | DB = new MimeTypeFile[dbv.size()]; |
aoqi@0 | 119 | dbv.copyInto(DB); |
aoqi@0 | 120 | } |
aoqi@0 | 121 | |
aoqi@0 | 122 | /** |
aoqi@0 | 123 | * Load from the named resource. |
aoqi@0 | 124 | */ |
aoqi@0 | 125 | private MimeTypeFile loadResource(String name) { |
aoqi@0 | 126 | InputStream clis = null; |
aoqi@0 | 127 | try { |
aoqi@0 | 128 | clis = SecuritySupport.getResourceAsStream(this.getClass(), name); |
aoqi@0 | 129 | if (clis != null) { |
aoqi@0 | 130 | MimeTypeFile mf = new MimeTypeFile(clis); |
aoqi@0 | 131 | if (LogSupport.isLoggable()) |
aoqi@0 | 132 | LogSupport.log("MimetypesFileTypeMap: successfully " + |
aoqi@0 | 133 | "loaded mime types file: " + name); |
aoqi@0 | 134 | return mf; |
aoqi@0 | 135 | } else { |
aoqi@0 | 136 | if (LogSupport.isLoggable()) |
aoqi@0 | 137 | LogSupport.log("MimetypesFileTypeMap: not loading " + |
aoqi@0 | 138 | "mime types file: " + name); |
aoqi@0 | 139 | } |
aoqi@0 | 140 | } catch (IOException e) { |
aoqi@0 | 141 | if (LogSupport.isLoggable()) |
aoqi@0 | 142 | LogSupport.log("MimetypesFileTypeMap: can't load " + name, e); |
aoqi@0 | 143 | } catch (SecurityException sex) { |
aoqi@0 | 144 | if (LogSupport.isLoggable()) |
aoqi@0 | 145 | LogSupport.log("MimetypesFileTypeMap: can't load " + name, sex); |
aoqi@0 | 146 | } finally { |
aoqi@0 | 147 | try { |
aoqi@0 | 148 | if (clis != null) |
aoqi@0 | 149 | clis.close(); |
aoqi@0 | 150 | } catch (IOException ex) { } // ignore it |
aoqi@0 | 151 | } |
aoqi@0 | 152 | return null; |
aoqi@0 | 153 | } |
aoqi@0 | 154 | |
aoqi@0 | 155 | /** |
aoqi@0 | 156 | * Load all of the named resource. |
aoqi@0 | 157 | */ |
aoqi@0 | 158 | private void loadAllResources(Vector v, String name) { |
aoqi@0 | 159 | boolean anyLoaded = false; |
aoqi@0 | 160 | try { |
aoqi@0 | 161 | URL[] urls; |
aoqi@0 | 162 | ClassLoader cld = null; |
aoqi@0 | 163 | // First try the "application's" class loader. |
aoqi@0 | 164 | cld = SecuritySupport.getContextClassLoader(); |
aoqi@0 | 165 | if (cld == null) |
aoqi@0 | 166 | cld = this.getClass().getClassLoader(); |
aoqi@0 | 167 | if (cld != null) |
aoqi@0 | 168 | urls = SecuritySupport.getResources(cld, name); |
aoqi@0 | 169 | else |
aoqi@0 | 170 | urls = SecuritySupport.getSystemResources(name); |
aoqi@0 | 171 | if (urls != null) { |
aoqi@0 | 172 | if (LogSupport.isLoggable()) |
aoqi@0 | 173 | LogSupport.log("MimetypesFileTypeMap: getResources"); |
aoqi@0 | 174 | for (int i = 0; i < urls.length; i++) { |
aoqi@0 | 175 | URL url = urls[i]; |
aoqi@0 | 176 | InputStream clis = null; |
aoqi@0 | 177 | if (LogSupport.isLoggable()) |
aoqi@0 | 178 | LogSupport.log("MimetypesFileTypeMap: URL " + url); |
aoqi@0 | 179 | try { |
aoqi@0 | 180 | clis = SecuritySupport.openStream(url); |
aoqi@0 | 181 | if (clis != null) { |
aoqi@0 | 182 | v.addElement(new MimeTypeFile(clis)); |
aoqi@0 | 183 | anyLoaded = true; |
aoqi@0 | 184 | if (LogSupport.isLoggable()) |
aoqi@0 | 185 | LogSupport.log("MimetypesFileTypeMap: " + |
aoqi@0 | 186 | "successfully loaded " + |
aoqi@0 | 187 | "mime types from URL: " + url); |
aoqi@0 | 188 | } else { |
aoqi@0 | 189 | if (LogSupport.isLoggable()) |
aoqi@0 | 190 | LogSupport.log("MimetypesFileTypeMap: " + |
aoqi@0 | 191 | "not loading " + |
aoqi@0 | 192 | "mime types from URL: " + url); |
aoqi@0 | 193 | } |
aoqi@0 | 194 | } catch (IOException ioex) { |
aoqi@0 | 195 | if (LogSupport.isLoggable()) |
aoqi@0 | 196 | LogSupport.log("MimetypesFileTypeMap: can't load " + |
aoqi@0 | 197 | url, ioex); |
aoqi@0 | 198 | } catch (SecurityException sex) { |
aoqi@0 | 199 | if (LogSupport.isLoggable()) |
aoqi@0 | 200 | LogSupport.log("MimetypesFileTypeMap: can't load " + |
aoqi@0 | 201 | url, sex); |
aoqi@0 | 202 | } finally { |
aoqi@0 | 203 | try { |
aoqi@0 | 204 | if (clis != null) |
aoqi@0 | 205 | clis.close(); |
aoqi@0 | 206 | } catch (IOException cex) { } |
aoqi@0 | 207 | } |
aoqi@0 | 208 | } |
aoqi@0 | 209 | } |
aoqi@0 | 210 | } catch (Exception ex) { |
aoqi@0 | 211 | if (LogSupport.isLoggable()) |
aoqi@0 | 212 | LogSupport.log("MimetypesFileTypeMap: can't load " + name, ex); |
aoqi@0 | 213 | } |
aoqi@0 | 214 | |
aoqi@0 | 215 | // if failed to load anything, fall back to old technique, just in case |
aoqi@0 | 216 | if (!anyLoaded) { |
aoqi@0 | 217 | LogSupport.log("MimetypesFileTypeMap: !anyLoaded"); |
aoqi@0 | 218 | MimeTypeFile mf = loadResource("/" + name); |
aoqi@0 | 219 | if (mf != null) |
aoqi@0 | 220 | v.addElement(mf); |
aoqi@0 | 221 | } |
aoqi@0 | 222 | } |
aoqi@0 | 223 | |
aoqi@0 | 224 | /** |
aoqi@0 | 225 | * Load the named file. |
aoqi@0 | 226 | */ |
aoqi@0 | 227 | private MimeTypeFile loadFile(String name) { |
aoqi@0 | 228 | MimeTypeFile mtf = null; |
aoqi@0 | 229 | |
aoqi@0 | 230 | try { |
aoqi@0 | 231 | mtf = new MimeTypeFile(name); |
aoqi@0 | 232 | } catch (IOException e) { |
aoqi@0 | 233 | // e.printStackTrace(); |
aoqi@0 | 234 | } |
aoqi@0 | 235 | return mtf; |
aoqi@0 | 236 | } |
aoqi@0 | 237 | |
aoqi@0 | 238 | /** |
aoqi@0 | 239 | * Construct a MimetypesFileTypeMap with programmatic entries |
aoqi@0 | 240 | * added from the named file. |
aoqi@0 | 241 | * |
aoqi@0 | 242 | * @param mimeTypeFileName the file name |
aoqi@0 | 243 | */ |
aoqi@0 | 244 | public MimetypesFileTypeMap(String mimeTypeFileName) throws IOException { |
aoqi@0 | 245 | this(); |
aoqi@0 | 246 | DB[PROG] = new MimeTypeFile(mimeTypeFileName); |
aoqi@0 | 247 | } |
aoqi@0 | 248 | |
aoqi@0 | 249 | /** |
aoqi@0 | 250 | * Construct a MimetypesFileTypeMap with programmatic entries |
aoqi@0 | 251 | * added from the InputStream. |
aoqi@0 | 252 | * |
aoqi@0 | 253 | * @param is the input stream to read from |
aoqi@0 | 254 | */ |
aoqi@0 | 255 | public MimetypesFileTypeMap(InputStream is) { |
aoqi@0 | 256 | this(); |
aoqi@0 | 257 | try { |
aoqi@0 | 258 | DB[PROG] = new MimeTypeFile(is); |
aoqi@0 | 259 | } catch (IOException ex) { |
aoqi@0 | 260 | // XXX - really should throw it |
aoqi@0 | 261 | } |
aoqi@0 | 262 | } |
aoqi@0 | 263 | |
aoqi@0 | 264 | /** |
aoqi@0 | 265 | * Prepend the MIME type values to the registry. |
aoqi@0 | 266 | * |
aoqi@0 | 267 | * @param mime_types A .mime.types formatted string of entries. |
aoqi@0 | 268 | */ |
aoqi@0 | 269 | public synchronized void addMimeTypes(String mime_types) { |
aoqi@0 | 270 | // check to see if we have created the registry |
aoqi@0 | 271 | if (DB[PROG] == null) |
aoqi@0 | 272 | DB[PROG] = new MimeTypeFile(); // make one |
aoqi@0 | 273 | |
aoqi@0 | 274 | DB[PROG].appendToRegistry(mime_types); |
aoqi@0 | 275 | } |
aoqi@0 | 276 | |
aoqi@0 | 277 | /** |
aoqi@0 | 278 | * Return the MIME type of the file object. |
aoqi@0 | 279 | * The implementation in this class calls |
aoqi@0 | 280 | * <code>getContentType(f.getName())</code>. |
aoqi@0 | 281 | * |
aoqi@0 | 282 | * @param f the file |
aoqi@0 | 283 | * @return the file's MIME type |
aoqi@0 | 284 | */ |
aoqi@0 | 285 | public String getContentType(File f) { |
aoqi@0 | 286 | return this.getContentType(f.getName()); |
aoqi@0 | 287 | } |
aoqi@0 | 288 | |
aoqi@0 | 289 | /** |
aoqi@0 | 290 | * Return the MIME type based on the specified file name. |
aoqi@0 | 291 | * The MIME type entries are searched as described above under |
aoqi@0 | 292 | * <i>MIME types file search order</i>. |
aoqi@0 | 293 | * If no entry is found, the type "application/octet-stream" is returned. |
aoqi@0 | 294 | * |
aoqi@0 | 295 | * @param filename the file name |
aoqi@0 | 296 | * @return the file's MIME type |
aoqi@0 | 297 | */ |
aoqi@0 | 298 | public synchronized String getContentType(String filename) { |
aoqi@0 | 299 | int dot_pos = filename.lastIndexOf("."); // period index |
aoqi@0 | 300 | |
aoqi@0 | 301 | if (dot_pos < 0) |
aoqi@0 | 302 | return defaultType; |
aoqi@0 | 303 | |
aoqi@0 | 304 | String file_ext = filename.substring(dot_pos + 1); |
aoqi@0 | 305 | if (file_ext.length() == 0) |
aoqi@0 | 306 | return defaultType; |
aoqi@0 | 307 | |
aoqi@0 | 308 | for (int i = 0; i < DB.length; i++) { |
aoqi@0 | 309 | if (DB[i] == null) |
aoqi@0 | 310 | continue; |
aoqi@0 | 311 | String result = DB[i].getMIMETypeString(file_ext); |
aoqi@0 | 312 | if (result != null) |
aoqi@0 | 313 | return result; |
aoqi@0 | 314 | } |
aoqi@0 | 315 | return defaultType; |
aoqi@0 | 316 | } |
aoqi@0 | 317 | |
aoqi@0 | 318 | /** |
aoqi@0 | 319 | * for debugging... |
aoqi@0 | 320 | * |
aoqi@0 | 321 | public static void main(String[] argv) throws Exception { |
aoqi@0 | 322 | MimetypesFileTypeMap map = new MimetypesFileTypeMap(); |
aoqi@0 | 323 | System.out.println("File " + argv[0] + " has MIME type " + |
aoqi@0 | 324 | map.getContentType(argv[0])); |
aoqi@0 | 325 | System.exit(0); |
aoqi@0 | 326 | } |
aoqi@0 | 327 | */ |
aoqi@0 | 328 | } |