Home | History | Annotate | Download | only in net
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package libcore.net;
     18 
     19 import java.io.File;
     20 import java.io.FileInputStream;
     21 import java.io.IOException;
     22 import java.io.InputStream;
     23 import java.util.HashMap;
     24 import java.util.Map;
     25 import java.util.Properties;
     26 
     27 /**
     28  * Utilities for dealing with MIME types.
     29  * Used to implement java.net.URLConnection and android.webkit.MimeTypeMap.
     30  */
     31 public final class MimeUtils {
     32     private static final Map<String, String> mimeTypeToExtensionMap = new HashMap<String, String>();
     33 
     34     private static final Map<String, String> extensionToMimeTypeMap = new HashMap<String, String>();
     35 
     36     static {
     37         // The following table is based on /etc/mime.types data minus
     38         // chemical/* MIME types and MIME types that don't map to any
     39         // file extensions. We also exclude top-level domain names to
     40         // deal with cases like:
     41         //
     42         // mail.google.com/a/google.com
     43         //
     44         // and "active" MIME types (due to potential security issues).
     45 
     46         // Note that this list is _not_ in alphabetical order and must not be sorted.
     47         // The "most popular" extension must come first, so that it's the one returned
     48         // by guessExtensionFromMimeType.
     49 
     50         add("application/andrew-inset", "ez");
     51         add("application/dsptype", "tsp");
     52         add("application/hta", "hta");
     53         add("application/mac-binhex40", "hqx");
     54         add("application/mathematica", "nb");
     55         add("application/msaccess", "mdb");
     56         add("application/oda", "oda");
     57         add("application/ogg", "ogg");
     58         add("application/ogg", "oga");
     59         add("application/pdf", "pdf");
     60         add("application/pgp-keys", "key");
     61         add("application/pgp-signature", "pgp");
     62         add("application/pics-rules", "prf");
     63         add("application/pkix-cert", "cer");
     64         add("application/rar", "rar");
     65         add("application/rdf+xml", "rdf");
     66         add("application/rss+xml", "rss");
     67         add("application/zip", "zip");
     68         add("application/vnd.android.package-archive", "apk");
     69         add("application/vnd.cinderella", "cdy");
     70         add("application/vnd.ms-pki.stl", "stl");
     71         add("application/vnd.oasis.opendocument.database", "odb");
     72         add("application/vnd.oasis.opendocument.formula", "odf");
     73         add("application/vnd.oasis.opendocument.graphics", "odg");
     74         add("application/vnd.oasis.opendocument.graphics-template", "otg");
     75         add("application/vnd.oasis.opendocument.image", "odi");
     76         add("application/vnd.oasis.opendocument.spreadsheet", "ods");
     77         add("application/vnd.oasis.opendocument.spreadsheet-template", "ots");
     78         add("application/vnd.oasis.opendocument.text", "odt");
     79         add("application/vnd.oasis.opendocument.text-master", "odm");
     80         add("application/vnd.oasis.opendocument.text-template", "ott");
     81         add("application/vnd.oasis.opendocument.text-web", "oth");
     82         add("application/vnd.google-earth.kml+xml", "kml");
     83         add("application/vnd.google-earth.kmz", "kmz");
     84         add("application/msword", "doc");
     85         add("application/msword", "dot");
     86         add("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx");
     87         add("application/vnd.openxmlformats-officedocument.wordprocessingml.template", "dotx");
     88         add("application/vnd.ms-excel", "xls");
     89         add("application/vnd.ms-excel", "xlt");
     90         add("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx");
     91         add("application/vnd.openxmlformats-officedocument.spreadsheetml.template", "xltx");
     92         add("application/vnd.ms-powerpoint", "ppt");
     93         add("application/vnd.ms-powerpoint", "pot");
     94         add("application/vnd.ms-powerpoint", "pps");
     95         add("application/vnd.openxmlformats-officedocument.presentationml.presentation", "pptx");
     96         add("application/vnd.openxmlformats-officedocument.presentationml.template", "potx");
     97         add("application/vnd.openxmlformats-officedocument.presentationml.slideshow", "ppsx");
     98         add("application/vnd.rim.cod", "cod");
     99         add("application/vnd.smaf", "mmf");
    100         add("application/vnd.stardivision.calc", "sdc");
    101         add("application/vnd.stardivision.draw", "sda");
    102         add("application/vnd.stardivision.impress", "sdd");
    103         add("application/vnd.stardivision.impress", "sdp");
    104         add("application/vnd.stardivision.math", "smf");
    105         add("application/vnd.stardivision.writer", "sdw");
    106         add("application/vnd.stardivision.writer", "vor");
    107         add("application/vnd.stardivision.writer-global", "sgl");
    108         add("application/vnd.sun.xml.calc", "sxc");
    109         add("application/vnd.sun.xml.calc.template", "stc");
    110         add("application/vnd.sun.xml.draw", "sxd");
    111         add("application/vnd.sun.xml.draw.template", "std");
    112         add("application/vnd.sun.xml.impress", "sxi");
    113         add("application/vnd.sun.xml.impress.template", "sti");
    114         add("application/vnd.sun.xml.math", "sxm");
    115         add("application/vnd.sun.xml.writer", "sxw");
    116         add("application/vnd.sun.xml.writer.global", "sxg");
    117         add("application/vnd.sun.xml.writer.template", "stw");
    118         add("application/vnd.visio", "vsd");
    119         add("application/x-abiword", "abw");
    120         add("application/x-apple-diskimage", "dmg");
    121         add("application/x-bcpio", "bcpio");
    122         add("application/x-bittorrent", "torrent");
    123         add("application/x-cdf", "cdf");
    124         add("application/x-cdlink", "vcd");
    125         add("application/x-chess-pgn", "pgn");
    126         add("application/x-cpio", "cpio");
    127         add("application/x-debian-package", "deb");
    128         add("application/x-debian-package", "udeb");
    129         add("application/x-director", "dcr");
    130         add("application/x-director", "dir");
    131         add("application/x-director", "dxr");
    132         add("application/x-dms", "dms");
    133         add("application/x-doom", "wad");
    134         add("application/x-dvi", "dvi");
    135         add("application/x-font", "pfa");
    136         add("application/x-font", "pfb");
    137         add("application/x-font", "gsf");
    138         add("application/x-font", "pcf");
    139         add("application/x-font", "pcf.Z");
    140         add("application/x-freemind", "mm");
    141         // application/futuresplash isn't IANA, so application/x-futuresplash should come first.
    142         add("application/x-futuresplash", "spl");
    143         add("application/futuresplash", "spl");
    144         add("application/x-gnumeric", "gnumeric");
    145         add("application/x-go-sgf", "sgf");
    146         add("application/x-graphing-calculator", "gcf");
    147         add("application/x-gtar", "tgz");
    148         add("application/x-gtar", "gtar");
    149         add("application/x-gtar", "taz");
    150         add("application/x-hdf", "hdf");
    151         add("application/x-ica", "ica");
    152         add("application/x-internet-signup", "ins");
    153         add("application/x-internet-signup", "isp");
    154         add("application/x-iphone", "iii");
    155         add("application/x-iso9660-image", "iso");
    156         add("application/x-jmol", "jmz");
    157         add("application/x-kchart", "chrt");
    158         add("application/x-killustrator", "kil");
    159         add("application/x-koan", "skp");
    160         add("application/x-koan", "skd");
    161         add("application/x-koan", "skt");
    162         add("application/x-koan", "skm");
    163         add("application/x-kpresenter", "kpr");
    164         add("application/x-kpresenter", "kpt");
    165         add("application/x-kspread", "ksp");
    166         add("application/x-kword", "kwd");
    167         add("application/x-kword", "kwt");
    168         add("application/x-latex", "latex");
    169         add("application/x-lha", "lha");
    170         add("application/x-lzh", "lzh");
    171         add("application/x-lzx", "lzx");
    172         add("application/x-maker", "frm");
    173         add("application/x-maker", "maker");
    174         add("application/x-maker", "frame");
    175         add("application/x-maker", "fb");
    176         add("application/x-maker", "book");
    177         add("application/x-maker", "fbdoc");
    178         add("application/x-mif", "mif");
    179         add("application/x-ms-wmd", "wmd");
    180         add("application/x-ms-wmz", "wmz");
    181         add("application/x-msi", "msi");
    182         add("application/x-ns-proxy-autoconfig", "pac");
    183         add("application/x-nwc", "nwc");
    184         add("application/x-object", "o");
    185         add("application/x-oz-application", "oza");
    186         add("application/x-pem-file", "pem");
    187         add("application/x-pkcs12", "p12");
    188         add("application/x-pkcs12", "pfx");
    189         add("application/x-pkcs7-certreqresp", "p7r");
    190         add("application/x-pkcs7-crl", "crl");
    191         add("application/x-quicktimeplayer", "qtl");
    192         add("application/x-shar", "shar");
    193         add("application/x-shockwave-flash", "swf");
    194         add("application/x-stuffit", "sit");
    195         add("application/x-sv4cpio", "sv4cpio");
    196         add("application/x-sv4crc", "sv4crc");
    197         add("application/x-tar", "tar");
    198         add("application/x-texinfo", "texinfo");
    199         add("application/x-texinfo", "texi");
    200         add("application/x-troff", "t");
    201         add("application/x-troff", "roff");
    202         add("application/x-troff-man", "man");
    203         add("application/x-ustar", "ustar");
    204         add("application/x-wais-source", "src");
    205         add("application/x-wingz", "wz");
    206         add("application/x-webarchive", "webarchive");
    207         add("application/x-webarchive-xml", "webarchivexml");
    208         add("application/x-x509-ca-cert", "crt");
    209         add("application/x-x509-user-cert", "crt");
    210         add("application/x-x509-server-cert", "crt");
    211         add("application/x-xcf", "xcf");
    212         add("application/x-xfig", "fig");
    213         add("application/xhtml+xml", "xhtml");
    214         add("audio/3gpp", "3gpp");
    215         add("audio/aac", "aac");
    216         add("audio/aac-adts", "aac");
    217         add("audio/amr", "amr");
    218         add("audio/amr-wb", "awb");
    219         add("audio/basic", "snd");
    220         add("audio/flac", "flac");
    221         add("application/x-flac", "flac");
    222         add("audio/imelody", "imy");
    223         add("audio/midi", "mid");
    224         add("audio/midi", "midi");
    225         add("audio/midi", "ota");
    226         add("audio/midi", "kar");
    227         add("audio/midi", "rtttl");
    228         add("audio/midi", "xmf");
    229         add("audio/mobile-xmf", "mxmf");
    230         // add ".mp3" first so it will be the default for guessExtensionFromMimeType
    231         add("audio/mpeg", "mp3");
    232         add("audio/mpeg", "mpga");
    233         add("audio/mpeg", "mpega");
    234         add("audio/mpeg", "mp2");
    235         add("audio/mpeg", "m4a");
    236         add("audio/mpegurl", "m3u");
    237         add("audio/prs.sid", "sid");
    238         add("audio/x-aiff", "aif");
    239         add("audio/x-aiff", "aiff");
    240         add("audio/x-aiff", "aifc");
    241         add("audio/x-gsm", "gsm");
    242         add("audio/x-matroska", "mka");
    243         add("audio/x-mpegurl", "m3u");
    244         add("audio/x-ms-wma", "wma");
    245         add("audio/x-ms-wax", "wax");
    246         add("audio/x-pn-realaudio", "ra");
    247         add("audio/x-pn-realaudio", "rm");
    248         add("audio/x-pn-realaudio", "ram");
    249         add("audio/x-realaudio", "ra");
    250         add("audio/x-scpls", "pls");
    251         add("audio/x-sd2", "sd2");
    252         add("audio/x-wav", "wav");
    253         // image/bmp isn't IANA, so image/x-ms-bmp should come first.
    254         add("image/x-ms-bmp", "bmp");
    255         add("image/bmp", "bmp");
    256         add("image/gif", "gif");
    257         // image/ico isn't IANA, so image/x-icon should come first.
    258         add("image/x-icon", "ico");
    259         add("image/ico", "cur");
    260         add("image/ico", "ico");
    261         add("image/ief", "ief");
    262         // add ".jpg" first so it will be the default for guessExtensionFromMimeType
    263         add("image/jpeg", "jpg");
    264         add("image/jpeg", "jpeg");
    265         add("image/jpeg", "jpe");
    266         add("image/pcx", "pcx");
    267         add("image/png", "png");
    268         add("image/svg+xml", "svg");
    269         add("image/svg+xml", "svgz");
    270         add("image/tiff", "tiff");
    271         add("image/tiff", "tif");
    272         add("image/vnd.djvu", "djvu");
    273         add("image/vnd.djvu", "djv");
    274         add("image/vnd.wap.wbmp", "wbmp");
    275         add("image/webp", "webp");
    276         add("image/x-cmu-raster", "ras");
    277         add("image/x-coreldraw", "cdr");
    278         add("image/x-coreldrawpattern", "pat");
    279         add("image/x-coreldrawtemplate", "cdt");
    280         add("image/x-corelphotopaint", "cpt");
    281         add("image/x-jg", "art");
    282         add("image/x-jng", "jng");
    283         add("image/x-photoshop", "psd");
    284         add("image/x-portable-anymap", "pnm");
    285         add("image/x-portable-bitmap", "pbm");
    286         add("image/x-portable-graymap", "pgm");
    287         add("image/x-portable-pixmap", "ppm");
    288         add("image/x-rgb", "rgb");
    289         add("image/x-xbitmap", "xbm");
    290         add("image/x-xpixmap", "xpm");
    291         add("image/x-xwindowdump", "xwd");
    292         add("model/iges", "igs");
    293         add("model/iges", "iges");
    294         add("model/mesh", "msh");
    295         add("model/mesh", "mesh");
    296         add("model/mesh", "silo");
    297         add("text/calendar", "ics");
    298         add("text/calendar", "icz");
    299         add("text/comma-separated-values", "csv");
    300         add("text/css", "css");
    301         add("text/html", "htm");
    302         add("text/html", "html");
    303         add("text/h323", "323");
    304         add("text/iuls", "uls");
    305         add("text/mathml", "mml");
    306         // add ".txt" first so it will be the default for guessExtensionFromMimeType
    307         add("text/plain", "txt");
    308         add("text/plain", "asc");
    309         add("text/plain", "text");
    310         add("text/plain", "diff");
    311         add("text/plain", "po");     // reserve "pot" for vnd.ms-powerpoint
    312         add("text/richtext", "rtx");
    313         add("text/rtf", "rtf");
    314         add("text/text", "phps");
    315         add("text/tab-separated-values", "tsv");
    316         add("text/xml", "xml");
    317         add("text/x-bibtex", "bib");
    318         add("text/x-boo", "boo");
    319         add("text/x-c++hdr", "hpp");
    320         add("text/x-c++hdr", "h++");
    321         add("text/x-c++hdr", "hxx");
    322         add("text/x-c++hdr", "hh");
    323         add("text/x-c++src", "cpp");
    324         add("text/x-c++src", "c++");
    325         add("text/x-c++src", "cc");
    326         add("text/x-c++src", "cxx");
    327         add("text/x-chdr", "h");
    328         add("text/x-component", "htc");
    329         add("text/x-csh", "csh");
    330         add("text/x-csrc", "c");
    331         add("text/x-dsrc", "d");
    332         add("text/x-haskell", "hs");
    333         add("text/x-java", "java");
    334         add("text/x-literate-haskell", "lhs");
    335         add("text/x-moc", "moc");
    336         add("text/x-pascal", "p");
    337         add("text/x-pascal", "pas");
    338         add("text/x-pcs-gcd", "gcd");
    339         add("text/x-setext", "etx");
    340         add("text/x-tcl", "tcl");
    341         add("text/x-tex", "tex");
    342         add("text/x-tex", "ltx");
    343         add("text/x-tex", "sty");
    344         add("text/x-tex", "cls");
    345         add("text/x-vcalendar", "vcs");
    346         add("text/x-vcard", "vcf");
    347         add("video/3gpp", "3gpp");
    348         add("video/3gpp", "3gp");
    349         add("video/3gpp2", "3gpp2");
    350         add("video/3gpp2", "3g2");
    351         add("video/avi", "avi");
    352         add("video/dl", "dl");
    353         add("video/dv", "dif");
    354         add("video/dv", "dv");
    355         add("video/fli", "fli");
    356         add("video/m4v", "m4v");
    357         add("video/mp2ts", "ts");
    358         add("video/mpeg", "mpeg");
    359         add("video/mpeg", "mpg");
    360         add("video/mpeg", "mpe");
    361         add("video/mp4", "mp4");
    362         add("video/mpeg", "VOB");
    363         add("video/quicktime", "qt");
    364         add("video/quicktime", "mov");
    365         add("video/vnd.mpegurl", "mxu");
    366         add("video/webm", "webm");
    367         add("video/x-la-asf", "lsf");
    368         add("video/x-la-asf", "lsx");
    369         add("video/x-matroska", "mkv");
    370         add("video/x-mng", "mng");
    371         add("video/x-ms-asf", "asf");
    372         add("video/x-ms-asf", "asx");
    373         add("video/x-ms-wm", "wm");
    374         add("video/x-ms-wmv", "wmv");
    375         add("video/x-ms-wmx", "wmx");
    376         add("video/x-ms-wvx", "wvx");
    377         add("video/x-sgi-movie", "movie");
    378         add("video/x-webex", "wrf");
    379         add("x-conference/x-cooltalk", "ice");
    380         add("x-epoc/x-sisx-app", "sisx");
    381         applyOverrides();
    382     }
    383 
    384     private static void add(String mimeType, String extension) {
    385         // If we have an existing x -> y mapping, we do not want to
    386         // override it with another mapping x -> y2.
    387         // If a mime type maps to several extensions
    388         // the first extension added is considered the most popular
    389         // so we do not want to overwrite it later.
    390         if (!mimeTypeToExtensionMap.containsKey(mimeType)) {
    391             mimeTypeToExtensionMap.put(mimeType, extension);
    392         }
    393         if (!extensionToMimeTypeMap.containsKey(extension)) {
    394             extensionToMimeTypeMap.put(extension, mimeType);
    395         }
    396     }
    397 
    398     private static InputStream getContentTypesPropertiesStream() {
    399         // User override?
    400         String userTable = System.getProperty("content.types.user.table");
    401         if (userTable != null) {
    402             File f = new File(userTable);
    403             if (f.exists()) {
    404                 try {
    405                     return new FileInputStream(f);
    406                 } catch (IOException ignored) {
    407                 }
    408             }
    409         }
    410 
    411         // Standard location?
    412         File f = new File(System.getProperty("java.home"), "lib" + File.separator + "content-types.properties");
    413         if (f.exists()) {
    414             try {
    415                 return new FileInputStream(f);
    416             } catch (IOException ignored) {
    417             }
    418         }
    419 
    420         return null;
    421     }
    422 
    423     /**
    424      * This isn't what the RI does. The RI doesn't have hard-coded defaults, so supplying your
    425      * own "content.types.user.table" means you don't get any of the built-ins, and the built-ins
    426      * come from "$JAVA_HOME/lib/content-types.properties".
    427      */
    428     private static void applyOverrides() {
    429         // Get the appropriate InputStream to read overrides from, if any.
    430         InputStream stream = getContentTypesPropertiesStream();
    431         if (stream == null) {
    432             return;
    433         }
    434 
    435         try {
    436             try {
    437                 // Read the properties file...
    438                 Properties overrides = new Properties();
    439                 overrides.load(stream);
    440                 // And translate its mapping to ours...
    441                 for (Map.Entry<Object, Object> entry : overrides.entrySet()) {
    442                     String extension = (String) entry.getKey();
    443                     String mimeType = (String) entry.getValue();
    444                     add(mimeType, extension);
    445                 }
    446             } finally {
    447                 stream.close();
    448             }
    449         } catch (IOException ignored) {
    450         }
    451     }
    452 
    453     private MimeUtils() {
    454     }
    455 
    456     /**
    457      * Returns true if the given MIME type has an entry in the map.
    458      * @param mimeType A MIME type (i.e. text/plain)
    459      * @return True iff there is a mimeType entry in the map.
    460      */
    461     public static boolean hasMimeType(String mimeType) {
    462         if (mimeType == null || mimeType.isEmpty()) {
    463             return false;
    464         }
    465         return mimeTypeToExtensionMap.containsKey(mimeType);
    466     }
    467 
    468     /**
    469      * Returns the MIME type for the given extension.
    470      * @param extension A file extension without the leading '.'
    471      * @return The MIME type for the given extension or null iff there is none.
    472      */
    473     public static String guessMimeTypeFromExtension(String extension) {
    474         if (extension == null || extension.isEmpty()) {
    475             return null;
    476         }
    477         return extensionToMimeTypeMap.get(extension);
    478     }
    479 
    480     /**
    481      * Returns true if the given extension has a registered MIME type.
    482      * @param extension A file extension without the leading '.'
    483      * @return True iff there is an extension entry in the map.
    484      */
    485     public static boolean hasExtension(String extension) {
    486         if (extension == null || extension.isEmpty()) {
    487             return false;
    488         }
    489         return extensionToMimeTypeMap.containsKey(extension);
    490     }
    491 
    492     /**
    493      * Returns the registered extension for the given MIME type. Note that some
    494      * MIME types map to multiple extensions. This call will return the most
    495      * common extension for the given MIME type.
    496      * @param mimeType A MIME type (i.e. text/plain)
    497      * @return The extension for the given MIME type or null iff there is none.
    498      */
    499     public static String guessExtensionFromMimeType(String mimeType) {
    500         if (mimeType == null || mimeType.isEmpty()) {
    501             return null;
    502         }
    503         return mimeTypeToExtensionMap.get(mimeType);
    504     }
    505 }
    506