KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > http > MimeMap


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 defaultMap=new Hashtable(101);
36     static {
37         defaultMap.put("txt", "text/plain");
38         defaultMap.put("html","text/html");
39         defaultMap.put("htm", "text/html");
40         defaultMap.put("gif", "image/gif");
41         defaultMap.put("jpg", "image/jpeg");
42         defaultMap.put("jpe", "image/jpeg");
43         defaultMap.put("jpeg", "image/jpeg");
44         defaultMap.put("png", "image/png");
45         defaultMap.put("java", "text/plain");
46         defaultMap.put("body", "text/html");
47         defaultMap.put("rtx", "text/richtext");
48         defaultMap.put("tsv", "text/tab-separated-values");
49         defaultMap.put("etx", "text/x-setext");
50         defaultMap.put("ps", "application/x-postscript");
51         defaultMap.put("class", "application/java");
52         defaultMap.put("csh", "application/x-csh");
53         defaultMap.put("sh", "application/x-sh");
54         defaultMap.put("tcl", "application/x-tcl");
55         defaultMap.put("tex", "application/x-tex");
56         defaultMap.put("texinfo", "application/x-texinfo");
57         defaultMap.put("texi", "application/x-texinfo");
58         defaultMap.put("t", "application/x-troff");
59         defaultMap.put("tr", "application/x-troff");
60         defaultMap.put("roff", "application/x-troff");
61         defaultMap.put("man", "application/x-troff-man");
62         defaultMap.put("me", "application/x-troff-me");
63         defaultMap.put("ms", "application/x-wais-source");
64         defaultMap.put("src", "application/x-wais-source");
65         defaultMap.put("zip", "application/zip");
66         defaultMap.put("bcpio", "application/x-bcpio");
67         defaultMap.put("cpio", "application/x-cpio");
68         defaultMap.put("gtar", "application/x-gtar");
69         defaultMap.put("shar", "application/x-shar");
70         defaultMap.put("sv4cpio", "application/x-sv4cpio");
71         defaultMap.put("sv4crc", "application/x-sv4crc");
72         defaultMap.put("tar", "application/x-tar");
73         defaultMap.put("ustar", "application/x-ustar");
74         defaultMap.put("dvi", "application/x-dvi");
75         defaultMap.put("hdf", "application/x-hdf");
76         defaultMap.put("latex", "application/x-latex");
77         defaultMap.put("bin", "application/octet-stream");
78         defaultMap.put("oda", "application/oda");
79         defaultMap.put("pdf", "application/pdf");
80         defaultMap.put("ps", "application/postscript");
81         defaultMap.put("eps", "application/postscript");
82         defaultMap.put("ai", "application/postscript");
83         defaultMap.put("rtf", "application/rtf");
84         defaultMap.put("nc", "application/x-netcdf");
85         defaultMap.put("cdf", "application/x-netcdf");
86         defaultMap.put("cer", "application/x-x509-ca-cert");
87         defaultMap.put("exe", "application/octet-stream");
88         defaultMap.put("gz", "application/x-gzip");
89         defaultMap.put("Z", "application/x-compress");
90         defaultMap.put("z", "application/x-compress");
91         defaultMap.put("hqx", "application/mac-binhex40");
92         defaultMap.put("mif", "application/x-mif");
93         defaultMap.put("ief", "image/ief");
94         defaultMap.put("tiff", "image/tiff");
95         defaultMap.put("tif", "image/tiff");
96         defaultMap.put("ras", "image/x-cmu-raster");
97         defaultMap.put("pnm", "image/x-portable-anymap");
98         defaultMap.put("pbm", "image/x-portable-bitmap");
99         defaultMap.put("pgm", "image/x-portable-graymap");
100         defaultMap.put("ppm", "image/x-portable-pixmap");
101         defaultMap.put("rgb", "image/x-rgb");
102         defaultMap.put("xbm", "image/x-xbitmap");
103         defaultMap.put("xpm", "image/x-xpixmap");
104         defaultMap.put("xwd", "image/x-xwindowdump");
105         defaultMap.put("au", "audio/basic");
106         defaultMap.put("snd", "audio/basic");
107         defaultMap.put("aif", "audio/x-aiff");
108         defaultMap.put("aiff", "audio/x-aiff");
109         defaultMap.put("aifc", "audio/x-aiff");
110         defaultMap.put("wav", "audio/x-wav");
111         defaultMap.put("mpeg", "video/mpeg");
112         defaultMap.put("mpg", "video/mpeg");
113         defaultMap.put("mpe", "video/mpeg");
114         defaultMap.put("qt", "video/quicktime");
115         defaultMap.put("mov", "video/quicktime");
116         defaultMap.put("avi", "video/x-msvideo");
117         defaultMap.put("movie", "video/x-sgi-movie");
118         defaultMap.put("avx", "video/x-rad-screenplay");
119         defaultMap.put("wrl", "x-world/x-vrml");
120         defaultMap.put("mpv2", "video/mpeg2");
121         
122         /* Add XML related MIMEs */
123         
124         defaultMap.put("xml", "text/xml");
125         defaultMap.put("xsl", "text/xml");
126         defaultMap.put("svg", "image/svg+xml");
127         defaultMap.put("svgz", "image/svg+xml");
128         defaultMap.put("wbmp", "image/vnd.wap.wbmp");
129         defaultMap.put("wml", "text/vnd.wap.wml");
130         defaultMap.put("wmlc", "application/vnd.wap.wmlc");
131         defaultMap.put("wmls", "text/vnd.wap.wmlscript");
132         defaultMap.put("wmlscriptc", "application/vnd.wap.wmlscriptc");
133     }
134     
135
136     private Hashtable map = new Hashtable();
137
138     public void addContentType(String JavaDoc extn, String JavaDoc type) {
139         map.put(extn, type.toLowerCase());
140     }
141
142     public Enumeration getExtensions() {
143         return map.keys();
144     }
145
146     public String JavaDoc getContentType(String JavaDoc extn) {
147         String JavaDoc type = (String JavaDoc)map.get(extn.toLowerCase());
148     if( type == null ) type=(String JavaDoc)defaultMap.get( extn );
149     return type;
150     }
151
152     public void removeContentType(String JavaDoc extn) {
153         map.remove(extn.toLowerCase());
154     }
155
156     /** Get extension of file, without fragment id
157      */

158     public static String JavaDoc getExtension( String JavaDoc fileName ) {
159         // play it safe and get rid of any fragment id
160
// that might be there
161
int length=fileName.length();
162     
163         int newEnd = fileName.lastIndexOf('#');
164     if( newEnd== -1 ) newEnd=length;
165     // Instead of creating a new string.
166
// if (i != -1) {
167
// fileName = fileName.substring(0, i);
168
// }
169
int i = fileName.lastIndexOf('.', newEnd );
170         if (i != -1) {
171              return fileName.substring(i + 1, newEnd );
172         } else {
173             // no extension, no content type
174
return null;
175         }
176     }
177     
178     public String JavaDoc getContentTypeFor(String JavaDoc fileName) {
179     String JavaDoc extn=getExtension( fileName );
180         if (extn!=null) {
181             return getContentType(extn);
182         } else {
183             // no extension, no content type
184
return null;
185         }
186     }
187
188 }
189
Popular Tags