This project has retired. For details please refer to its Attic page.
MimeTypes xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.commons.impl;
20  
21  import java.io.File;
22  import java.util.HashMap;
23  import java.util.Locale;
24  import java.util.Map;
25  
26  import javax.activation.MimetypesFileTypeMap;
27  
28  @SuppressWarnings("PMD.AvoidDuplicateLiterals")
29  public final class MimeTypes {
30  
31      private static final String OCTET_STREAM = "application/octet-stream";
32      private static final MimetypesFileTypeMap TYPE_MAP = new MimetypesFileTypeMap();
33      private static final Map<String, String> MIME2EXT = new HashMap<String, String>();
34  
35      private MimeTypes() {
36      }
37  
38      static {
39          // MIME type to extension
40          MIME2EXT.put("application/octet-stream", "");
41          MIME2EXT.put("application/envoy", "evy");
42          MIME2EXT.put("application/epub+zip", "epub");
43          MIME2EXT.put("application/fractals", "fif");
44          MIME2EXT.put("application/futuresplash", "spl");
45          MIME2EXT.put("application/hta", "hta");
46          MIME2EXT.put("application/java-archive", "jar");
47          MIME2EXT.put("application/java-serialized-object", "ser");
48          MIME2EXT.put("application/java-vm", "class");
49          MIME2EXT.put("application/javascript", "js");
50          MIME2EXT.put("application/json", "json");
51          MIME2EXT.put("application/mac-binhex40", "hqx");
52          MIME2EXT.put("application/msword", "doc");
53          MIME2EXT.put("application/oda", "oda");
54          MIME2EXT.put("application/olescript", "axs");
55          MIME2EXT.put("application/onenote", "onetoc");
56          MIME2EXT.put("application/pdf", "pdf");
57          MIME2EXT.put("application/pics-rules", "prf");
58          MIME2EXT.put("application/pkcs10", "p10");
59          MIME2EXT.put("application/pkix-cert", "cer");
60          MIME2EXT.put("application/pkix-crl", "crl");
61          MIME2EXT.put("application/postscript", "ps");
62          MIME2EXT.put("application/rtf", "rtf");
63          MIME2EXT.put("application/vnd.android.package-archive", "apk");
64          MIME2EXT.put("application/vnd.apple.keynote", "key");
65          MIME2EXT.put("application/vnd.apple.numbers", "numbers");
66          MIME2EXT.put("application/vnd.apple.pages", "pages");
67          MIME2EXT.put("application/vnd.framemaker", "fm");
68          MIME2EXT.put("application/vnd.ms-excel", "xls");
69          MIME2EXT.put("application/vnd.ms-pkicertstore", "sst");
70          MIME2EXT.put("application/vnd.ms-pkiseccat", "cat");
71          MIME2EXT.put("application/vnd.ms-pkistl", "stl");
72          MIME2EXT.put("application/vnd.ms-powerpoint", "ppt");
73          MIME2EXT.put("application/vnd.ms-project", "mpp");
74          MIME2EXT.put("application/vnd.ms-works", "wps");
75          MIME2EXT.put("application/vnd.oasis.opendocument.chart", "odc");
76          MIME2EXT.put("application/vnd.oasis.opendocument.chart-template", "otc");
77          MIME2EXT.put("application/vnd.oasis.opendocument.database", "odb");
78          MIME2EXT.put("application/vnd.oasis.opendocument.formula", "odf");
79          MIME2EXT.put("application/vnd.oasis.opendocument.formula-template", "odft");
80          MIME2EXT.put("application/vnd.oasis.opendocument.graphics", "odg");
81          MIME2EXT.put("application/vnd.oasis.opendocument.graphics-template", "otg");
82          MIME2EXT.put("application/vnd.oasis.opendocument.image", "odi");
83          MIME2EXT.put("application/vnd.oasis.opendocument.image-template", "oti");
84          MIME2EXT.put("application/vnd.oasis.opendocument.presentation", "odp");
85          MIME2EXT.put("application/vnd.oasis.opendocument.presentation-template", "otp");
86          MIME2EXT.put("application/vnd.oasis.opendocument.spreadsheet", "ods");
87          MIME2EXT.put("application/vnd.oasis.opendocument.spreadsheet-template", "ots");
88          MIME2EXT.put("application/vnd.oasis.opendocument.text", "odt");
89          MIME2EXT.put("application/vnd.oasis.opendocument.text-master", "odm");
90          MIME2EXT.put("application/vnd.oasis.opendocument.text-template", "ott");
91          MIME2EXT.put("application/vnd.oasis.opendocument.text-web", "oth");
92          MIME2EXT.put("application/vnd.openxmlformats-officedocument.presentationml.presentation", "pptx");
93          MIME2EXT.put("application/vnd.openxmlformats-officedocument.presentationml.slideshow", "ppsx");
94          MIME2EXT.put("application/vnd.openxmlformats-officedocument.presentationml.template", "potx");
95          MIME2EXT.put("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx");
96          MIME2EXT.put("application/vnd.openxmlformats-officedocument.spreadsheetml.template", "xltx");
97          MIME2EXT.put("application/winhlp", "hlp");
98          MIME2EXT.put("application/x-cdf", "cdf");
99          MIME2EXT.put("application/x-compress", "z");
100         MIME2EXT.put("application/x-compressed", "tgz");
101         MIME2EXT.put("application/x-cpio", "cpio");
102         MIME2EXT.put("application/x-csh", "csh");
103         MIME2EXT.put("application/x-director", "dxr");
104         MIME2EXT.put("application/x-dvi", "dvi");
105         MIME2EXT.put("application/x-gtar", "gtar");
106         MIME2EXT.put("application/x-gzip", "gz");
107         MIME2EXT.put("application/x-hdf", "hdf");
108         MIME2EXT.put("application/x-internet-signup", "isp");
109         MIME2EXT.put("application/x-iphone", "iii");
110         MIME2EXT.put("application/x-iwork-keynote-sffkey", "key");
111         MIME2EXT.put("application/x-iwork-numbers-sffnumber", "numbers");
112         MIME2EXT.put("application/x-iwork-pages-sffpages", "pages");
113         MIME2EXT.put("application/x-javascript", "js");
114         MIME2EXT.put("application/x-latex", "latex");
115         MIME2EXT.put("application/x-msaccess", "mdb");
116         MIME2EXT.put("application/x-mscardfile", "crd");
117         MIME2EXT.put("application/x-msclip", "clp");
118         MIME2EXT.put("application/x-msdownload", "dll");
119         MIME2EXT.put("application/x-msmediaview", "mvb");
120         MIME2EXT.put("application/x-msmetafile", "wmf");
121         MIME2EXT.put("application/x-mspublisher", "pub");
122         MIME2EXT.put("application/x-msschedule", "scd");
123         MIME2EXT.put("application/x-msterminal", "trm");
124         MIME2EXT.put("application/x-mswrite", "wri");
125         MIME2EXT.put("application/x-perfmon", "pmw");
126         MIME2EXT.put("application/x-pkcs12", "pfx");
127         MIME2EXT.put("application/x-pkcs12v", "p12");
128         MIME2EXT.put("application/x-pkcs7-certificates", "p7b");
129         MIME2EXT.put("application/x-pkcs7-certreqresp", "p7r");
130         MIME2EXT.put("application/x-pkcs7-mime", "p7m");
131         MIME2EXT.put("application/x-pkcs7-signature", "p7s");
132         MIME2EXT.put("application/x-sh", "sh");
133         MIME2EXT.put("application/x-shar", "shar");
134         MIME2EXT.put("application/x-shockwave-flash", "swf");
135         MIME2EXT.put("application/x-stuffit", "sit");
136         MIME2EXT.put("application/x-tar", "tar");
137         MIME2EXT.put("application/x-tcl", "tcl");
138         MIME2EXT.put("application/x-tex", "tex");
139         MIME2EXT.put("application/x-texinfo", "texinfo");
140         MIME2EXT.put("application/x-troff-man", "man");
141         MIME2EXT.put("application/x-troff-me", "me");
142         MIME2EXT.put("application/x-troff-ms", "ms");
143         MIME2EXT.put("application/x-troff", "tr");
144         MIME2EXT.put("application/x-ustar", "ustar");
145         MIME2EXT.put("application/x-wais-source", "src");
146         MIME2EXT.put("application/x-x509-ca-cert", "cer");
147         MIME2EXT.put("application/ynd.ms-pkipko", "vpko");
148         MIME2EXT.put("application/zip", "zip");
149         MIME2EXT.put("audio/basic", "snd");
150         MIME2EXT.put("audio/flac", "flac");
151         MIME2EXT.put("audio/mp4", "m4a");
152         MIME2EXT.put("audio/mid", "mid");
153         MIME2EXT.put("audio/midi", "mid");
154         MIME2EXT.put("audio/mpeg", "mp3");
155         MIME2EXT.put("audio/ogg", "ogg");
156         MIME2EXT.put("audio/webm", "webm");
157         MIME2EXT.put("audio/x-aiff", "aif");
158         MIME2EXT.put("audio/x-m4a", "m4a");
159         MIME2EXT.put("audio/x-mpegurl", "m3u");
160         MIME2EXT.put("audio/x-ms-wax", "wax");
161         MIME2EXT.put("audio/x-ms-wma", "wma");
162         MIME2EXT.put("audio/x-pn-realaudio", "ram");
163         MIME2EXT.put("audio/x-wav", "wav");
164         MIME2EXT.put("application/vnd.openxmlformats-officedocument.wordprocessingml.template", "doct");
165         MIME2EXT.put("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx");
166         MIME2EXT.put("image/bmp", "bmp");
167         MIME2EXT.put("image/cis-cod", "cod");
168         MIME2EXT.put("image/gif", "gif");
169         MIME2EXT.put("image/ief", "ief");
170         MIME2EXT.put("image/jpeg", "jpeg");
171         MIME2EXT.put("image/pipeg", "jfif");
172         MIME2EXT.put("image/png", "png");
173         MIME2EXT.put("image/svg+xml", "svg");
174         MIME2EXT.put("image/tiff", "tiff");
175         MIME2EXT.put("image/webp", "webp");
176         MIME2EXT.put("image/x-cmu-raster", "ras");
177         MIME2EXT.put("image/x-cmx", "cmx");
178         MIME2EXT.put("image/x-icon", "ico");
179         MIME2EXT.put("image/x-portable-anymap", "pnm");
180         MIME2EXT.put("image/x-portable-bitmap", "pbm");
181         MIME2EXT.put("image/x-portable-graymap", "pgm");
182         MIME2EXT.put("image/x-portable-pixmap", "ppm");
183         MIME2EXT.put("image/x-rgb", "rgb");
184         MIME2EXT.put("image/x-xbitmap", "xbm");
185         MIME2EXT.put("image/x-xpixmap", "xpm");
186         MIME2EXT.put("image/x-xwindowdump", "xwd");
187         MIME2EXT.put("message/rfc822", "mhtml");
188         MIME2EXT.put("text/css", "css");
189         MIME2EXT.put("text/html", "html");
190         MIME2EXT.put("text/iuls", "uls");
191         MIME2EXT.put("text/plain", "txt");
192         MIME2EXT.put("text/richtext", "rtx");
193         MIME2EXT.put("text/scriptlet", "sct");
194         MIME2EXT.put("text/tab-separated-values", "tsv");
195         MIME2EXT.put("text/webviewhtml", "htt");
196         MIME2EXT.put("text/x-component", "htc");
197         MIME2EXT.put("text/x-setext", "etx");
198         MIME2EXT.put("text/x-vcard", "vcf");
199         MIME2EXT.put("text/xml", "xml");
200         MIME2EXT.put("video/mp4", "mp4");
201         MIME2EXT.put("video/mpeg", "mpeg");
202         MIME2EXT.put("video/mpegv", "mpe");
203         MIME2EXT.put("video/ogg", "ogv");
204         MIME2EXT.put("video/quicktime", "mov");
205         MIME2EXT.put("video/quicktime", "qt");
206         MIME2EXT.put("video/webm", "webm");
207         MIME2EXT.put("video/x-f4v", "f4v");
208         MIME2EXT.put("video/x-fli", "fli");
209         MIME2EXT.put("video/x-flv", "flv");
210         MIME2EXT.put("video/x-la-asf", "lsf");
211         MIME2EXT.put("video/x-m4v", "m4v");
212         MIME2EXT.put("video/x-ms-asf", "asf");
213         MIME2EXT.put("video/x-msvideo", "avi");
214         MIME2EXT.put("video/x-sgi-movie", "movie");
215         MIME2EXT.put("x-world/x-vrml", "vrml");
216     }
217 
218     /**
219      * Returns the MIME type for file extension.
220      */
221     public static String getMIMEType(String ext) {
222         if (ext == null) {
223             return OCTET_STREAM;
224         }
225 
226         int x = ext.lastIndexOf('.');
227         if (x > -1) {
228             ext = ext.substring(x + 1);
229         }
230 
231         return TYPE_MAP.getContentType("x." + ext.toLowerCase(Locale.ENGLISH));
232     }
233 
234     /**
235      * Returns the MIME type for a file.
236      */
237     public static String getMIMEType(File file) {
238         if (file == null) {
239             return OCTET_STREAM;
240         }
241 
242         return TYPE_MAP.getContentType(file);
243     }
244 
245     /**
246      * Guesses a extension from a MIME type.
247      */
248     public static String getExtension(String mimeType) {
249         if (mimeType == null) {
250             return "";
251         }
252 
253         int x = mimeType.indexOf(';');
254         if (x > -1) {
255             mimeType = mimeType.substring(0, x);
256         }
257         mimeType = mimeType.trim().toLowerCase(Locale.ENGLISH);
258 
259         String extension = MIME2EXT.get(mimeType);
260         return ((extension == null || extension.length() == 0) ? "" : "." + extension);
261     }
262 }