001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.tomcat.util.http;
019:
020: import java.net.*;
021: import java.util.*;
022:
023: /**
024: * A mime type map that implements the java.net.FileNameMap interface.
025: *
026: * @author James Duncan Davidson [duncan@eng.sun.com]
027: * @author Jason Hunter [jch@eng.sun.com]
028: */
029: public class MimeMap implements FileNameMap {
030:
031: // Defaults - all of them are "well-known" types,
032: // you can add using normal web.xml.
033:
034: public static Hashtable<String, String> defaultMap = new Hashtable<String, String>(
035: 101);
036: static {
037: defaultMap.put("txt", "text/plain");
038: defaultMap.put("html", "text/html");
039: defaultMap.put("htm", "text/html");
040: defaultMap.put("gif", "image/gif");
041: defaultMap.put("jpg", "image/jpeg");
042: defaultMap.put("jpe", "image/jpeg");
043: defaultMap.put("jpeg", "image/jpeg");
044: defaultMap.put("png", "image/png");
045: defaultMap.put("java", "text/plain");
046: defaultMap.put("body", "text/html");
047: defaultMap.put("rtx", "text/richtext");
048: defaultMap.put("tsv", "text/tab-separated-values");
049: defaultMap.put("etx", "text/x-setext");
050: defaultMap.put("ps", "application/x-postscript");
051: defaultMap.put("class", "application/java");
052: defaultMap.put("csh", "application/x-csh");
053: defaultMap.put("sh", "application/x-sh");
054: defaultMap.put("tcl", "application/x-tcl");
055: defaultMap.put("tex", "application/x-tex");
056: defaultMap.put("texinfo", "application/x-texinfo");
057: defaultMap.put("texi", "application/x-texinfo");
058: defaultMap.put("t", "application/x-troff");
059: defaultMap.put("tr", "application/x-troff");
060: defaultMap.put("roff", "application/x-troff");
061: defaultMap.put("man", "application/x-troff-man");
062: defaultMap.put("me", "application/x-troff-me");
063: defaultMap.put("ms", "application/x-wais-source");
064: defaultMap.put("src", "application/x-wais-source");
065: defaultMap.put("zip", "application/zip");
066: defaultMap.put("bcpio", "application/x-bcpio");
067: defaultMap.put("cpio", "application/x-cpio");
068: defaultMap.put("gtar", "application/x-gtar");
069: defaultMap.put("shar", "application/x-shar");
070: defaultMap.put("sv4cpio", "application/x-sv4cpio");
071: defaultMap.put("sv4crc", "application/x-sv4crc");
072: defaultMap.put("tar", "application/x-tar");
073: defaultMap.put("ustar", "application/x-ustar");
074: defaultMap.put("dvi", "application/x-dvi");
075: defaultMap.put("hdf", "application/x-hdf");
076: defaultMap.put("latex", "application/x-latex");
077: defaultMap.put("bin", "application/octet-stream");
078: defaultMap.put("oda", "application/oda");
079: defaultMap.put("pdf", "application/pdf");
080: defaultMap.put("ps", "application/postscript");
081: defaultMap.put("eps", "application/postscript");
082: defaultMap.put("ai", "application/postscript");
083: defaultMap.put("rtf", "application/rtf");
084: defaultMap.put("nc", "application/x-netcdf");
085: defaultMap.put("cdf", "application/x-netcdf");
086: defaultMap.put("cer", "application/x-x509-ca-cert");
087: defaultMap.put("exe", "application/octet-stream");
088: defaultMap.put("gz", "application/x-gzip");
089: defaultMap.put("Z", "application/x-compress");
090: defaultMap.put("z", "application/x-compress");
091: defaultMap.put("hqx", "application/mac-binhex40");
092: defaultMap.put("mif", "application/x-mif");
093: defaultMap.put("ief", "image/ief");
094: defaultMap.put("tiff", "image/tiff");
095: defaultMap.put("tif", "image/tiff");
096: defaultMap.put("ras", "image/x-cmu-raster");
097: defaultMap.put("pnm", "image/x-portable-anymap");
098: defaultMap.put("pbm", "image/x-portable-bitmap");
099: 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: private Hashtable<String, String> map = new Hashtable<String, String>();
136:
137: public void addContentType(String extn, String type) {
138: map.put(extn, type.toLowerCase());
139: }
140:
141: public Enumeration getExtensions() {
142: return map.keys();
143: }
144:
145: public String getContentType(String extn) {
146: String type = (String) map.get(extn.toLowerCase());
147: if (type == null)
148: type = (String) defaultMap.get(extn);
149: return type;
150: }
151:
152: public void removeContentType(String extn) {
153: map.remove(extn.toLowerCase());
154: }
155:
156: /** Get extension of file, without fragment id
157: */
158: public static String getExtension(String 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)
165: 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: }
|