001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.tomcat.util.http;
018:
019: import java.net.*;
020: import java.util.*;
021:
022: /**
023: * A mime type map that implements the java.net.FileNameMap interface.
024: *
025: * @author James Duncan Davidson [duncan@eng.sun.com]
026: * @author Jason Hunter [jch@eng.sun.com]
027: */
028: public class MimeMap implements FileNameMap {
029:
030: // Defaults - all of them are "well-known" types,
031: // you can add using normal web.xml.
032:
033: public static Hashtable defaultMap = new Hashtable(101);
034: static {
035: defaultMap.put("txt", "text/plain");
036: defaultMap.put("html", "text/html");
037: defaultMap.put("htm", "text/html");
038: defaultMap.put("gif", "image/gif");
039: defaultMap.put("jpg", "image/jpeg");
040: defaultMap.put("jpe", "image/jpeg");
041: defaultMap.put("jpeg", "image/jpeg");
042: defaultMap.put("java", "text/plain");
043: defaultMap.put("body", "text/html");
044: defaultMap.put("rtx", "text/richtext");
045: defaultMap.put("tsv", "text/tab-separated-values");
046: defaultMap.put("etx", "text/x-setext");
047: defaultMap.put("ps", "application/x-postscript");
048: defaultMap.put("class", "application/java");
049: defaultMap.put("csh", "application/x-csh");
050: defaultMap.put("sh", "application/x-sh");
051: defaultMap.put("tcl", "application/x-tcl");
052: defaultMap.put("tex", "application/x-tex");
053: defaultMap.put("texinfo", "application/x-texinfo");
054: defaultMap.put("texi", "application/x-texinfo");
055: defaultMap.put("t", "application/x-troff");
056: defaultMap.put("tr", "application/x-troff");
057: defaultMap.put("roff", "application/x-troff");
058: defaultMap.put("man", "application/x-troff-man");
059: defaultMap.put("me", "application/x-troff-me");
060: defaultMap.put("ms", "application/x-wais-source");
061: defaultMap.put("src", "application/x-wais-source");
062: defaultMap.put("zip", "application/zip");
063: defaultMap.put("bcpio", "application/x-bcpio");
064: defaultMap.put("cpio", "application/x-cpio");
065: defaultMap.put("gtar", "application/x-gtar");
066: defaultMap.put("shar", "application/x-shar");
067: defaultMap.put("sv4cpio", "application/x-sv4cpio");
068: defaultMap.put("sv4crc", "application/x-sv4crc");
069: defaultMap.put("tar", "application/x-tar");
070: defaultMap.put("ustar", "application/x-ustar");
071: defaultMap.put("dvi", "application/x-dvi");
072: defaultMap.put("hdf", "application/x-hdf");
073: defaultMap.put("latex", "application/x-latex");
074: defaultMap.put("bin", "application/octet-stream");
075: defaultMap.put("oda", "application/oda");
076: defaultMap.put("pdf", "application/pdf");
077: defaultMap.put("ps", "application/postscript");
078: defaultMap.put("eps", "application/postscript");
079: defaultMap.put("ai", "application/postscript");
080: defaultMap.put("rtf", "application/rtf");
081: defaultMap.put("nc", "application/x-netcdf");
082: defaultMap.put("cdf", "application/x-netcdf");
083: defaultMap.put("cer", "application/x-x509-ca-cert");
084: defaultMap.put("exe", "application/octet-stream");
085: defaultMap.put("gz", "application/x-gzip");
086: defaultMap.put("Z", "application/x-compress");
087: defaultMap.put("z", "application/x-compress");
088: defaultMap.put("hqx", "application/mac-binhex40");
089: defaultMap.put("mif", "application/x-mif");
090: defaultMap.put("ief", "image/ief");
091: defaultMap.put("tiff", "image/tiff");
092: defaultMap.put("tif", "image/tiff");
093: defaultMap.put("ras", "image/x-cmu-raster");
094: defaultMap.put("pnm", "image/x-portable-anymap");
095: defaultMap.put("pbm", "image/x-portable-bitmap");
096: defaultMap.put("pgm", "image/x-portable-graymap");
097: defaultMap.put("ppm", "image/x-portable-pixmap");
098: defaultMap.put("rgb", "image/x-rgb");
099: defaultMap.put("xbm", "image/x-xbitmap");
100: defaultMap.put("xpm", "image/x-xpixmap");
101: defaultMap.put("xwd", "image/x-xwindowdump");
102: defaultMap.put("au", "audio/basic");
103: defaultMap.put("snd", "audio/basic");
104: defaultMap.put("aif", "audio/x-aiff");
105: defaultMap.put("aiff", "audio/x-aiff");
106: defaultMap.put("aifc", "audio/x-aiff");
107: defaultMap.put("wav", "audio/x-wav");
108: defaultMap.put("mpeg", "video/mpeg");
109: defaultMap.put("mpg", "video/mpeg");
110: defaultMap.put("mpe", "video/mpeg");
111: defaultMap.put("qt", "video/quicktime");
112: defaultMap.put("mov", "video/quicktime");
113: defaultMap.put("avi", "video/x-msvideo");
114: defaultMap.put("movie", "video/x-sgi-movie");
115: defaultMap.put("avx", "video/x-rad-screenplay");
116: defaultMap.put("wrl", "x-world/x-vrml");
117: defaultMap.put("mpv2", "video/mpeg2");
118:
119: /* Add XML related MIMEs */
120:
121: defaultMap.put("xml", "text/xml");
122: defaultMap.put("xsl", "text/xml");
123: defaultMap.put("svg", "image/svg+xml");
124: defaultMap.put("svgz", "image/svg+xml");
125: defaultMap.put("wbmp", "image/vnd.wap.wbmp");
126: defaultMap.put("wml", "text/vnd.wap.wml");
127: defaultMap.put("wmlc", "application/vnd.wap.wmlc");
128: defaultMap.put("wmls", "text/vnd.wap.wmlscript");
129: defaultMap.put("wmlscriptc", "application/vnd.wap.wmlscriptc");
130: }
131:
132: private Hashtable map = new Hashtable();
133:
134: public void addContentType(String extn, String type) {
135: map.put(extn, type.toLowerCase());
136: }
137:
138: public Enumeration getExtensions() {
139: return map.keys();
140: }
141:
142: public String getContentType(String extn) {
143: String type = (String) map.get(extn.toLowerCase());
144: if (type == null)
145: type = (String) defaultMap.get(extn);
146: return type;
147: }
148:
149: public void removeContentType(String extn) {
150: map.remove(extn.toLowerCase());
151: }
152:
153: /** Get extension of file, without fragment id
154: */
155: public static String getExtension(String fileName) {
156: // play it safe and get rid of any fragment id
157: // that might be there
158: int length = fileName.length();
159:
160: int newEnd = fileName.lastIndexOf('#');
161: if (newEnd == -1)
162: newEnd = length;
163: // Instead of creating a new string.
164: // if (i != -1) {
165: // fileName = fileName.substring(0, i);
166: // }
167: int i = fileName.lastIndexOf('.', newEnd);
168: if (i != -1) {
169: return fileName.substring(i + 1, newEnd);
170: } else {
171: // no extension, no content type
172: return null;
173: }
174: }
175:
176: public String getContentTypeFor(String fileName) {
177: String extn = getExtension(fileName);
178: if (extn != null) {
179: return getContentType(extn);
180: } else {
181: // no extension, no content type
182: return null;
183: }
184: }
185:
186: }
|