Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Licensed to the Apache Software Foundation (ASF) under one or more
   *  contributor license agreements.  See the NOTICE file distributed with
   *  this work for additional information regarding copyright ownership.
   *  The ASF licenses this file to You under the Apache License, Version 2.0
   *  (the "License"); you may not use this file except in compliance with
   *  the License.  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
  *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 
 package org.apache.tomcat.util.http;
 
 import java.net.*;
 import java.util.*;


A mime type map that implements the java.net.FileNameMap interface.

Author(s):
James Duncan Davidson [duncan@eng.sun.com]
Jason Hunter [jch@eng.sun.com]
 
 public class MimeMap implements FileNameMap {
 
     // Defaults - all of them are "well-known" types,
     // you can add using normal web.xml.
     
     public static Hashtable<String,StringdefaultMap=new Hashtable<String,String>(101);
     static {
         .put("txt""text/plain");
         .put("css""text/css");
         .put("html","text/html");
         .put("htm""text/html");
         .put("gif""image/gif");
         .put("jpg""image/jpeg");
         .put("jpe""image/jpeg");
         .put("jpeg""image/jpeg");
         .put("png""image/png");
 		.put("java""text/plain");
         .put("body""text/html");
         .put("rtx""text/richtext");
         .put("tsv""text/tab-separated-values");
         .put("etx""text/x-setext");
         .put("ps""application/x-postscript");
         .put("class""application/java");
         .put("csh""application/x-csh");
         .put("sh""application/x-sh");
         .put("tcl""application/x-tcl");
         .put("tex""application/x-tex");
         .put("texinfo""application/x-texinfo");
         .put("texi""application/x-texinfo");
         .put("t""application/x-troff");
         .put("tr""application/x-troff");
         .put("roff""application/x-troff");
         .put("man""application/x-troff-man");
         .put("me""application/x-troff-me");
         .put("ms""application/x-wais-source");
         .put("src""application/x-wais-source");
         .put("zip""application/zip");
         .put("bcpio""application/x-bcpio");
         .put("cpio""application/x-cpio");
         .put("gtar""application/x-gtar");
         .put("shar""application/x-shar");
         .put("sv4cpio""application/x-sv4cpio");
         .put("sv4crc""application/x-sv4crc");
         .put("tar""application/x-tar");
         .put("ustar""application/x-ustar");
         .put("dvi""application/x-dvi");
         .put("hdf""application/x-hdf");
         .put("latex""application/x-latex");
         .put("bin""application/octet-stream");
         .put("oda""application/oda");
         .put("pdf""application/pdf");
         .put("ps""application/postscript");
         .put("eps""application/postscript");
         .put("ai""application/postscript");
         .put("rtf""application/rtf");
         .put("nc""application/x-netcdf");
         .put("cdf""application/x-netcdf");
         .put("cer""application/x-x509-ca-cert");
         .put("exe""application/octet-stream");
         .put("gz""application/x-gzip");
         .put("Z""application/x-compress");
         .put("z""application/x-compress");
         .put("hqx""application/mac-binhex40");
         .put("mif""application/x-mif");
         .put("ief""image/ief");
         .put("tiff""image/tiff");
         .put("tif""image/tiff");
         .put("ras""image/x-cmu-raster");
         .put("pnm""image/x-portable-anymap");
         .put("pbm""image/x-portable-bitmap");
        .put("pgm""image/x-portable-graymap");
        .put("ppm""image/x-portable-pixmap");
        .put("rgb""image/x-rgb");
        .put("xbm""image/x-xbitmap");
        .put("xpm""image/x-xpixmap");
        .put("xwd""image/x-xwindowdump");
        .put("au""audio/basic");
        .put("snd""audio/basic");
        .put("aif""audio/x-aiff");
        .put("aiff""audio/x-aiff");
        .put("aifc""audio/x-aiff");
        .put("wav""audio/x-wav");
        .put("mpeg""video/mpeg");
        .put("mpg""video/mpeg");
        .put("mpe""video/mpeg");
        .put("qt""video/quicktime");
        .put("mov""video/quicktime");
        .put("avi""video/x-msvideo");
        .put("movie""video/x-sgi-movie");
        .put("avx""video/x-rad-screenplay");
        .put("wrl""x-world/x-vrml");
        .put("mpv2""video/mpeg2");
        
        /* Add XML related MIMEs */
        
        .put("xml""text/xml");
        .put("xsl""text/xml");        
        .put("svg""image/svg+xml");
        .put("svgz""image/svg+xml");
        .put("wbmp""image/vnd.wap.wbmp");
        .put("wml""text/vnd.wap.wml");
        .put("wmlc""application/vnd.wap.wmlc");
        .put("wmls""text/vnd.wap.wmlscript");
        .put("wmlscriptc""application/vnd.wap.wmlscriptc");
    }
    
    private Hashtable map = new Hashtable();
    public void addContentType(String extnString type) {
        .put(extntype.toLowerCase(.));
    }
    public Enumeration getExtensions() {
        return .keys();
    }
    public String getMimeType(String ext) {
        return getContentTypeFor(ext);
    }
    
    public String getContentType(String extn) {
        String type = (String).get(extn.toLowerCase(.));
	iftype == null ) type=(String).getextn );
	return type;
    }
    public void removeContentType(String extn) {
        .remove(extn.toLowerCase(.));
    }

    
Get extension of file, without fragment id
    public static String getExtensionString fileName ) {
        // play it safe and get rid of any fragment id
        // that might be there
	int length=fileName.length();
        int newEnd = fileName.lastIndexOf('#');
	ifnewEnd== -1 ) newEnd=length;
	// Instead of creating a new string.
	//         if (i != -1) {
	//             fileName = fileName.substring(0, i);
	//         }
        int i = fileName.lastIndexOf('.'newEnd );
        if (i != -1) {
             return  fileName.substring(i + 1, newEnd );
        } else {
            // no extension, no content type
            return null;
        }
    }
    
    public String getContentTypeFor(String fileName) {
	String extn=getExtensionfileName );
        if (extn!=null) {
            return getContentType(extn);
        } else {
            // no extension, no content type
            return null;
        }
    }
New to GrepCode? Check out our FAQ X