View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  package org.apache.tomcat.util.http;
19  
20  import java.net.*;
21  import java.util.*;
22  
23  
24  /**
25   * A mime type map that implements the java.net.FileNameMap interface.
26   *
27   * @author James Duncan Davidson [duncan@eng.sun.com]
28   * @author Jason Hunter [jch@eng.sun.com]
29   */
30  public class MimeMap implements FileNameMap {
31  
32      // Defaults - all of them are "well-known" types,
33      // you can add using normal web.xml.
34      
35      public static Hashtable<String,String> defaultMap =
36          new Hashtable<String,String>(101);
37      static {
38          defaultMap.put("txt", "text/plain");
39          defaultMap.put("html","text/html");
40          defaultMap.put("htm", "text/html");
41          defaultMap.put("gif", "image/gif");
42          defaultMap.put("jpg", "image/jpeg");
43          defaultMap.put("jpe", "image/jpeg");
44          defaultMap.put("jpeg", "image/jpeg");
45          defaultMap.put("png", "image/png");
46                  defaultMap.put("java", "text/plain");
47          defaultMap.put("body", "text/html");
48          defaultMap.put("rtx", "text/richtext");
49          defaultMap.put("tsv", "text/tab-separated-values");
50          defaultMap.put("etx", "text/x-setext");
51          defaultMap.put("ps", "application/x-postscript");
52          defaultMap.put("class", "application/java");
53          defaultMap.put("csh", "application/x-csh");
54          defaultMap.put("sh", "application/x-sh");
55          defaultMap.put("tcl", "application/x-tcl");
56          defaultMap.put("tex", "application/x-tex");
57          defaultMap.put("texinfo", "application/x-texinfo");
58          defaultMap.put("texi", "application/x-texinfo");
59          defaultMap.put("t", "application/x-troff");
60          defaultMap.put("tr", "application/x-troff");
61          defaultMap.put("roff", "application/x-troff");
62          defaultMap.put("man", "application/x-troff-man");
63          defaultMap.put("me", "application/x-troff-me");
64          defaultMap.put("ms", "application/x-wais-source");
65          defaultMap.put("src", "application/x-wais-source");
66          defaultMap.put("zip", "application/zip");
67          defaultMap.put("bcpio", "application/x-bcpio");
68          defaultMap.put("cpio", "application/x-cpio");
69          defaultMap.put("gtar", "application/x-gtar");
70          defaultMap.put("shar", "application/x-shar");
71          defaultMap.put("sv4cpio", "application/x-sv4cpio");
72          defaultMap.put("sv4crc", "application/x-sv4crc");
73          defaultMap.put("tar", "application/x-tar");
74          defaultMap.put("ustar", "application/x-ustar");
75          defaultMap.put("dvi", "application/x-dvi");
76          defaultMap.put("hdf", "application/x-hdf");
77          defaultMap.put("latex", "application/x-latex");
78          defaultMap.put("bin", "application/octet-stream");
79          defaultMap.put("oda", "application/oda");
80          defaultMap.put("pdf", "application/pdf");
81          defaultMap.put("ps", "application/postscript");
82          defaultMap.put("eps", "application/postscript");
83          defaultMap.put("ai", "application/postscript");
84          defaultMap.put("rtf", "application/rtf");
85          defaultMap.put("nc", "application/x-netcdf");
86          defaultMap.put("cdf", "application/x-netcdf");
87          defaultMap.put("cer", "application/x-x509-ca-cert");
88          defaultMap.put("exe", "application/octet-stream");
89          defaultMap.put("gz", "application/x-gzip");
90          defaultMap.put("Z", "application/x-compress");
91          defaultMap.put("z", "application/x-compress");
92          defaultMap.put("hqx", "application/mac-binhex40");
93          defaultMap.put("mif", "application/x-mif");
94          defaultMap.put("ief", "image/ief");
95          defaultMap.put("tiff", "image/tiff");
96          defaultMap.put("tif", "image/tiff");
97          defaultMap.put("ras", "image/x-cmu-raster");
98          defaultMap.put("pnm", "image/x-portable-anymap");
99          defaultMap.put("pbm", "image/x-portable-bitmap");
100         defaultMap.put("pgm", "image/x-portable-graymap");
101         defaultMap.put("ppm", "image/x-portable-pixmap");
102         defaultMap.put("rgb", "image/x-rgb");
103         defaultMap.put("xbm", "image/x-xbitmap");
104         defaultMap.put("xpm", "image/x-xpixmap");
105         defaultMap.put("xwd", "image/x-xwindowdump");
106         defaultMap.put("au", "audio/basic");
107         defaultMap.put("snd", "audio/basic");
108         defaultMap.put("aif", "audio/x-aiff");
109         defaultMap.put("aiff", "audio/x-aiff");
110         defaultMap.put("aifc", "audio/x-aiff");
111         defaultMap.put("wav", "audio/x-wav");
112         defaultMap.put("mpeg", "video/mpeg");
113         defaultMap.put("mpg", "video/mpeg");
114         defaultMap.put("mpe", "video/mpeg");
115         defaultMap.put("qt", "video/quicktime");
116         defaultMap.put("mov", "video/quicktime");
117         defaultMap.put("avi", "video/x-msvideo");
118         defaultMap.put("movie", "video/x-sgi-movie");
119         defaultMap.put("avx", "video/x-rad-screenplay");
120         defaultMap.put("wrl", "x-world/x-vrml");
121         defaultMap.put("mpv2", "video/mpeg2");
122         
123         /* Add XML related MIMEs */
124         
125         defaultMap.put("xml", "text/xml");
126         defaultMap.put("xsl", "text/xml");        
127         defaultMap.put("svg", "image/svg+xml");
128         defaultMap.put("svgz", "image/svg+xml");
129         defaultMap.put("wbmp", "image/vnd.wap.wbmp");
130         defaultMap.put("wml", "text/vnd.wap.wml");
131         defaultMap.put("wmlc", "application/vnd.wap.wmlc");
132         defaultMap.put("wmls", "text/vnd.wap.wmlscript");
133         defaultMap.put("wmlscriptc", "application/vnd.wap.wmlscriptc");
134     }
135     
136 
137     private Hashtable<String,String> map = new Hashtable<String,String>();
138 
139     public void addContentType(String extn, String type) {
140         map.put(extn, type.toLowerCase());
141     }
142 
143     public Enumeration getExtensions() {
144         return map.keys();
145     }
146 
147     public String getContentType(String extn) {
148         String type = (String)map.get(extn.toLowerCase());
149         if( type == null ) type=(String)defaultMap.get( extn );
150         return type;
151     }
152 
153     public void removeContentType(String extn) {
154         map.remove(extn.toLowerCase());
155     }
156 
157     /** Get extension of file, without fragment id
158      */
159     public static String getExtension( String fileName ) {
160         // play it safe and get rid of any fragment id
161         // that might be there
162         int length=fileName.length();
163         
164         int newEnd = fileName.lastIndexOf('#');
165         if( newEnd== -1 ) newEnd=length;
166         // Instead of creating a new string.
167         //         if (i != -1) {
168         //             fileName = fileName.substring(0, i);
169         //         }
170         int i = fileName.lastIndexOf('.', newEnd );
171         if (i != -1) {
172              return  fileName.substring(i + 1, newEnd );
173         } else {
174             // no extension, no content type
175             return null;
176         }
177     }
178     
179     public String getContentTypeFor(String fileName) {
180         String extn=getExtension( fileName );
181         if (extn!=null) {
182             return getContentType(extn);
183         } else {
184             // no extension, no content type
185             return null;
186         }
187     }
188 
189 }