src/share/jaf_classes/javax/activation/MimetypesFileTypeMap.java

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 494
2fcd3ddb57a6
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

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

mercurial