< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.activation;
  27 
  28 import java.io.*;
  29 import java.net.*;
  30 import java.util.*;


  31 import com.sun.activation.registries.MimeTypeFile;
  32 import com.sun.activation.registries.LogSupport;
  33 
  34 /**
  35  * This class extends FileTypeMap and provides data typing of files
  36  * via their file extension. It uses the {@code .mime.types} format. <p>
  37  *
  38  * <b>MIME types file search order:</b><p>
  39  * The MimetypesFileTypeMap looks in various places in the user's
  40  * system for MIME types file entries. When requests are made
  41  * to search for MIME types in the MimetypesFileTypeMap, it searches
  42  * MIME types files in the following order:
  43  * <ol>
  44  * <li> Programmatically added entries to the MimetypesFileTypeMap instance.
  45  * <li> The file {@code .mime.types} in the user's home directory.
  46  * <li> The file {@literal <}<i>java.home</i>{@literal >}{@code /lib/mime.types}.
  47  * <li> The file or resources named {@code META-INF/mime.types}.
  48  * <li> The file or resource named {@code META-INF/mimetypes.default}
  49  * (usually found only in the {@code activation.jar} file).
  50  * </ol>
  51  * <p>







  52  * <b>MIME types file format:</b>
  53  *
  54  * <pre>{@code
  55  * # comments begin with a '#'
  56  * # the format is <mime type> <space separated file extensions>
  57  * # for example:
  58  * text/plain    txt text TXT
  59  * # this would map file.txt, file.text, and file.TXT to
  60  * # the mime type "text/plain"
  61  * }</pre>
  62  *
  63  * @author Bart Calder
  64  * @author Bill Shannon
  65  *
  66  * @since 1.6
  67  */
  68 public class MimetypesFileTypeMap extends FileTypeMap {
  69     /*
  70      * We manage a collection of databases, searched in order.
  71      */
  72     private MimeTypeFile[] DB;
  73     private static final int PROG = 0;  // programmatically added entries
  74 
  75     private static String defaultType = "application/octet-stream";























  76 
  77     /**
  78      * The default constructor.
  79      */
  80     public MimetypesFileTypeMap() {
  81         Vector dbv = new Vector(5);     // usually 5 or less databases
  82         MimeTypeFile mf = null;
  83         dbv.addElement(null);           // place holder for PROG entry
  84 
  85         LogSupport.log("MimetypesFileTypeMap: load HOME");
  86         try {
  87             String user_home = System.getProperty("user.home");
  88 
  89             if (user_home != null) {
  90                 String path = user_home + File.separator + ".mime.types";
  91                 mf = loadFile(path);
  92                 if (mf != null)
  93                     dbv.addElement(mf);
  94             }
  95         } catch (SecurityException ex) {}
  96 
  97         LogSupport.log("MimetypesFileTypeMap: load SYS");
  98         try {
  99             // check system's home
 100             String system_mimetypes = System.getProperty("java.home") +
 101                 File.separator + "lib" + File.separator + "mime.types";
 102             mf = loadFile(system_mimetypes);
 103             if (mf != null)
 104                 dbv.addElement(mf);

 105         } catch (SecurityException ex) {}
 106 
 107         LogSupport.log("MimetypesFileTypeMap: load JAR");
 108         // load from the app's jar file
 109         loadAllResources(dbv, "META-INF/mime.types");
 110 
 111         LogSupport.log("MimetypesFileTypeMap: load DEF");
 112         mf = loadResource("/META-INF/mimetypes.default");
 113 
 114         if (mf != null)
 115             dbv.addElement(mf);
 116 
 117         DB = new MimeTypeFile[dbv.size()];
 118         dbv.copyInto(DB);
 119     }
 120 
 121     /**
 122      * Load from the named resource.
 123      */
 124     private MimeTypeFile loadResource(String name) {


 222 
 223     /**
 224      * Load the named file.
 225      */
 226     private MimeTypeFile loadFile(String name) {
 227         MimeTypeFile mtf = null;
 228 
 229         try {
 230             mtf = new MimeTypeFile(name);
 231         } catch (IOException e) {
 232             //  e.printStackTrace();
 233         }
 234         return mtf;
 235     }
 236 
 237     /**
 238      * Construct a MimetypesFileTypeMap with programmatic entries
 239      * added from the named file.
 240      *
 241      * @param mimeTypeFileName  the file name

 242      */
 243     public MimetypesFileTypeMap(String mimeTypeFileName) throws IOException {
 244         this();
 245         DB[PROG] = new MimeTypeFile(mimeTypeFileName);
 246     }
 247 
 248     /**
 249      * Construct a MimetypesFileTypeMap with programmatic entries
 250      * added from the InputStream.
 251      *
 252      * @param is        the input stream to read from
 253      */
 254     public MimetypesFileTypeMap(InputStream is) {
 255         this();
 256         try {
 257             DB[PROG] = new MimeTypeFile(is);
 258         } catch (IOException ex) {
 259             // XXX - really should throw it
 260         }
 261     }


   1 /*
   2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.activation;
  27 
  28 import java.io.*;
  29 import java.net.*;
  30 import java.util.*;
  31 import java.security.AccessController;
  32 import java.security.PrivilegedAction;
  33 import com.sun.activation.registries.MimeTypeFile;
  34 import com.sun.activation.registries.LogSupport;
  35 
  36 /**
  37  * This class extends FileTypeMap and provides data typing of files
  38  * via their file extension. It uses the {@code .mime.types} format. <p>
  39  *
  40  * <b>MIME types file search order:</b><p>
  41  * The MimetypesFileTypeMap looks in various places in the user's
  42  * system for MIME types file entries. When requests are made
  43  * to search for MIME types in the MimetypesFileTypeMap, it searches
  44  * MIME types files in the following order:
  45  * <ol>
  46  * <li> Programmatically added entries to the MimetypesFileTypeMap instance.
  47  * <li> The file {@code .mime.types} in the user's home directory.
  48  * <li> The file {@code mime.types} in the Java runtime.
  49  * <li> The file or resources named {@code META-INF/mime.types}.
  50  * <li> The file or resource named {@code META-INF/mimetypes.default}
  51  * (usually found only in the {@code activation.jar} file).
  52  * </ol>
  53  * <p>
  54  * (The current implementation looks for the {@code mime.types} file
  55  * in the Java runtime in the directory <i>java.home</i>{@code /conf}
  56  * if it exists, and otherwise in the directory
  57  * <i>java.home</i>{@code /lib}, where <i>java.home</i> is the value
  58  * of the "java.home" System property.  Note that the "conf" directory was
  59  * introduced in JDK 9.)
  60  * <p>
  61  * <b>MIME types file format:</b>
  62  *
  63  * <pre>{@code
  64  * # comments begin with a '#'
  65  * # the format is <mime type> <space separated file extensions>
  66  * # for example:
  67  * text/plain    txt text TXT
  68  * # this would map file.txt, file.text, and file.TXT to
  69  * # the mime type "text/plain"
  70  * }</pre>
  71  *
  72  * @author Bart Calder
  73  * @author Bill Shannon
  74  *
  75  * @since 1.6
  76  */
  77 public class MimetypesFileTypeMap extends FileTypeMap {
  78     /*
  79      * We manage a collection of databases, searched in order.
  80      */
  81     private MimeTypeFile[] DB;
  82     private static final int PROG = 0;  // programmatically added entries
  83 
  84     private static final String defaultType = "application/octet-stream";
  85 
  86     private static final String confDir;
  87 
  88     static {
  89         String dir = null;
  90         try {
  91             dir = (String)AccessController.doPrivileged(
  92                 new PrivilegedAction() {
  93                     public Object run() {
  94                         String home = System.getProperty("java.home");
  95                         String newdir = home + File.separator + "conf";
  96                         File conf = new File(newdir);
  97                         if (conf.exists())
  98                             return newdir + File.separator;
  99                         else
 100                             return home + File.separator + "lib" + File.separator;
 101                     }
 102                 });
 103         } catch (Exception ex) {
 104             // ignore any exceptions
 105         }
 106         confDir = dir;
 107     }
 108 
 109     /**
 110      * The default constructor.
 111      */
 112     public MimetypesFileTypeMap() {
 113         Vector dbv = new Vector(5);     // usually 5 or less databases
 114         MimeTypeFile mf = null;
 115         dbv.addElement(null);           // place holder for PROG entry
 116 
 117         LogSupport.log("MimetypesFileTypeMap: load HOME");
 118         try {
 119             String user_home = System.getProperty("user.home");
 120 
 121             if (user_home != null) {
 122                 String path = user_home + File.separator + ".mime.types";
 123                 mf = loadFile(path);
 124                 if (mf != null)
 125                     dbv.addElement(mf);
 126             }
 127         } catch (SecurityException ex) {}
 128 
 129         LogSupport.log("MimetypesFileTypeMap: load SYS");
 130         try {
 131             // check system's home
 132             if (confDir != null) {
 133                 mf = loadFile(confDir + "mime.types");

 134                 if (mf != null)
 135                     dbv.addElement(mf);
 136             }
 137         } catch (SecurityException ex) {}
 138 
 139         LogSupport.log("MimetypesFileTypeMap: load JAR");
 140         // load from the app's jar file
 141         loadAllResources(dbv, "META-INF/mime.types");
 142 
 143         LogSupport.log("MimetypesFileTypeMap: load DEF");
 144         mf = loadResource("/META-INF/mimetypes.default");
 145 
 146         if (mf != null)
 147             dbv.addElement(mf);
 148 
 149         DB = new MimeTypeFile[dbv.size()];
 150         dbv.copyInto(DB);
 151     }
 152 
 153     /**
 154      * Load from the named resource.
 155      */
 156     private MimeTypeFile loadResource(String name) {


 254 
 255     /**
 256      * Load the named file.
 257      */
 258     private MimeTypeFile loadFile(String name) {
 259         MimeTypeFile mtf = null;
 260 
 261         try {
 262             mtf = new MimeTypeFile(name);
 263         } catch (IOException e) {
 264             //  e.printStackTrace();
 265         }
 266         return mtf;
 267     }
 268 
 269     /**
 270      * Construct a MimetypesFileTypeMap with programmatic entries
 271      * added from the named file.
 272      *
 273      * @param mimeTypeFileName  the file name
 274      * @exception       IOException     for errors reading the file
 275      */
 276     public MimetypesFileTypeMap(String mimeTypeFileName) throws IOException {
 277         this();
 278         DB[PROG] = new MimeTypeFile(mimeTypeFileName);
 279     }
 280 
 281     /**
 282      * Construct a MimetypesFileTypeMap with programmatic entries
 283      * added from the InputStream.
 284      *
 285      * @param is        the input stream to read from
 286      */
 287     public MimetypesFileTypeMap(InputStream is) {
 288         this();
 289         try {
 290             DB[PROG] = new MimeTypeFile(is);
 291         } catch (IOException ex) {
 292             // XXX - really should throw it
 293         }
 294     }


< prev index next >