You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ad...@apache.org on 2003/06/26 12:27:56 UTC
cvs commit: jakarta-commons/httpclient/src/contrib/org/apache/commons/httpclient/contrib/methods/multipart ContentType.java ContentTypeFilePart.java
adrian 2003/06/26 03:27:56
Added: httpclient/src/contrib/org/apache/commons/httpclient/contrib/methods/multipart
ContentType.java ContentTypeFilePart.java
Log:
Added classes for detecting the content type.
PR: Bug 20986
Submitted by: Eric Devlin with modifications from Adrian Sutton
Revision Changes Path
1.1 jakarta-commons/httpclient/src/contrib/org/apache/commons/httpclient/contrib/methods/multipart/ContentType.java
Index: ContentType.java
===================================================================
/*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.commons.httpclient.contrib.methods.multipart;
import java.io.File;
/**
* This class provides mappings from file name extensions to content types.
*
* @author <a href="mailto:emdevlin@charter.net">Eric Devlin</a>
* @author <a href="mailto:adrian@intencha.com">Adrian Sutton</a>
*
* @version $Revision: 1.1 $
*
* DISCLAIMER: HttpClient developers DO NOT actively support this component.
* The component is provided as a reference material, which may be inappropriate
* to be used without additional customization.
*/
public final class ContentType {
/** Mime Type mappings 'liberated' from Tomcat4.1.18/conf/web.xml*/
public static final String[][] MIME_TYPE_MAPPINGS = { { "abs", "audio/x-mpeg" }, {
"ai", "application/postscript" }, {
"aif", "audio/x-aiff" }, {
"aifc", "audio/x-aiff" }, {
"aiff", "audio/x-aiff" }, {
"aim", "application/x-aim" }, {
"art", "image/x-jg" }, {
"asf", "video/x-ms-asf" }, {
"asx", "video/x-ms-asf" }, {
"au", "audio/basic" }, {
"avi", "video/x-msvideo" }, {
"avx", "video/x-rad-screenplay" }, {
"bcpio", "application/x-bcpio" }, {
"bin", "application/octet-stream" }, {
"bmp", "image/bmp" }, {
"body", "text/html" }, {
"cdf", "application/x-cdf" }, {
"cer", "application/x-x509-ca-cert" }, {
"class", "application/java" }, {
"cpio", "application/x-cpio" }, {
"csh", "application/x-csh" }, {
"css", "text/css" }, {
"dib", "image/bmp" }, {
"doc", "application/msword" }, {
"dtd", "text/plain" }, {
"dv", "video/x-dv" }, {
"dvi", "application/x-dvi" }, {
"eps", "application/postscript" }, {
"etx", "text/x-setext" }, {
"exe", "application/octet-stream" }, {
"gif", "image/gif" }, {
"gtar", "application/x-gtar" }, {
"gz", "application/x-gzip" }, {
"hdf", "application/x-hdf" }, {
"hqx", "application/mac-binhex40" }, {
"htc", "text/x-component" }, {
"htm", "text/html" }, {
"html", "text/html" }, {
"hqx", "application/mac-binhex40" }, {
"ief", "image/ief" }, {
"jad", "text/vnd.sun.j2me.app-descriptor" }, {
"jar", "application/java-archive" }, {
"java", "text/plain" }, {
"jnlp", "application/x-java-jnlp-file" }, {
"jpe", "image/jpeg" }, {
"jpeg", "image/jpeg" }, {
"jpg", "image/jpeg" }, {
"js", "text/javascript" }, {
"jsf", "text/plain" }, {
"jspf", "text/plain" }, {
"kar", "audio/x-midi" }, {
"latex", "application/x-latex" }, {
"m3u", "audio/x-mpegurl" }, {
"mac", "image/x-macpaint" }, {
"man", "application/x-troff-man" }, {
"me", "application/x-troff-me" }, {
"mid", "audio/x-midi" }, {
"midi", "audio/x-midi" }, {
"mif", "application/x-mif" }, {
"mov", "video/quicktime" }, {
"movie", "video/x-sgi-movie" }, {
"mp1", "audio/x-mpeg" }, {
"mp2", "audio/x-mpeg" }, {
"mp3", "audio/x-mpeg" }, {
"mpa", "audio/x-mpeg" }, {
"mpe", "video/mpeg" }, {
"mpeg", "video/mpeg" }, {
"mpega", "audio/x-mpeg" }, {
"mpg", "video/mpeg" }, {
"mpv2", "video/mpeg2" }, {
"ms", "application/x-wais-source" }, {
"nc", "application/x-netcdf" }, {
"oda", "application/oda" }, {
"pbm", "image/x-portable-bitmap" }, {
"pct", "image/pict" }, {
"pdf", "application/pdf" }, {
"pgm", "image/x-portable-graymap" }, {
"pic", "image/pict" }, {
"pict", "image/pict" }, {
"pls", "audio/x-scpls" }, {
"png", "image/png" }, {
"pnm", "image/x-portable-anymap" }, {
"pnt", "image/x-macpaint" }, {
"ppm", "image/x-portable-pixmap" }, {
"ps", "application/postscript" }, {
"psd", "image/x-photoshop" }, {
"qt", "video/quicktime" }, {
"qti", "image/x-quicktime" }, {
"qtif", "image/x-quicktime" }, {
"ras", "image/x-cmu-raster" }, {
"rgb", "image/x-rgb" }, {
"rm", "application/vnd.rn-realmedia" }, {
"roff", "application/x-troff" }, {
"rtf", "application/rtf" }, {
"rtx", "text/richtext" }, {
"sh", "application/x-sh" }, {
"shar", "application/x-shar" }, {
"smf", "audio/x-midi" }, {
"snd", "audio/basic" }, {
"src", "application/x-wais-source" }, {
"sv4cpio", "application/x-sv4cpio" }, {
"sv4crc", "application/x-sv4crc" }, {
"swf", "application/x-shockwave-flash" }, {
"t", "application/x-troff" }, {
"tar", "application/x-tar" }, {
"tcl", "application/x-tcl" }, {
"tex", "application/x-tex" }, {
"texi", "application/x-texinfo" }, {
"texinfo", "application/x-texinfo" }, {
"tif", "image/tiff" }, {
"tiff", "image/tiff" }, {
"tr", "application/x-troff" }, {
"tsv", "text/tab-separated-values" }, {
"txt", "text/plain" }, {
"ulw", "audio/basic" }, {
"ustar", "application/x-ustar" }, {
"xbm", "image/x-xbitmap" }, {
"xml", "text/xml" }, {
"xpm", "image/x-xpixmap" }, {
"xsl", "text/xml" }, {
"xwd", "image/x-xwindowdump" }, {
"wav", "audio/x-wav" }, {
"svg", "image/svg+xml" }, {
"svgz", "image/svg+xml" }, {
"wbmp", "image/vnd.wap.wbmp" }, {
"wml", "text/vnd.wap.wml" }, {
"wmlc", "application/vnd.wap.wmlc" }, {
"wmls", "text/vnd.wap.wmlscript" }, {
"wmlscriptc", "application/vnd.wap.wmlscriptc" }, {
"wrl", "x-world/x-vrml" }, {
"Z", "application/x-compress" }, {
"z", "application/x-compress" }, {
"zip", "application/zip" }
};
/** The constructor is intentionally private as the class only provides static methods.
*/
private ContentType() {
}
/**
* Get the content type based on the extension of the file name<br>
*
* @param fileName for which the content type is to be determined.
*
* @return the content type for the file or null if no mapping was
* possible.
*/
public static String get(String fileName) {
String contentType = null;
if (fileName != null) {
int extensionIndex = fileName.lastIndexOf('.');
if (extensionIndex != -1) {
if (extensionIndex + 1 < fileName.length()) {
String extension = fileName.substring(extensionIndex + 1);
for (int i = 0; i < MIME_TYPE_MAPPINGS.length; i++) {
if (extension.equals(MIME_TYPE_MAPPINGS[i][0])) {
contentType = MIME_TYPE_MAPPINGS[i][1];
break;
}
}
}
}
}
return contentType;
}
/**
* Get the content type based on the extension of the file name<br>
*
* @param file for which the content type is to be determined.
*
* @return the content type for the file or null if no mapping was
* possible.
*
* @throws IOException if the construction of the canonical path for
* the file fails.
*/
public static String get(File file) {
String contentType = null;
if (file != null) {
contentType = get(file.getName());
}
return contentType;
}
}
1.1 jakarta-commons/httpclient/src/contrib/org/apache/commons/httpclient/contrib/methods/multipart/ContentTypeFilePart.java
Index: ContentTypeFilePart.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/httpclient/src/contrib/org/apache/commons/httpclient/contrib/methods/multipart/ContentTypeFilePart.java,v 1.1 2003/06/26 10:27:56 adrian Exp $
* $Revision: 1.1 $
* $Date: 2003/06/26 10:27:56 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.commons.httpclient.contrib.methods.multipart;
import java.io.File;
import java.io.FileNotFoundException;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.PartSource;
/** A simple extension to {@link FilePart} that automatically determines the content type
* of the file.
*
* @author <a href="mailto:adrian@intencha.com">Adrian Sutton</a>
* @version $Revision $
*
* DISCLAIMER: HttpClient developers DO NOT actively support this component.
* The component is provided as a reference material, which may be inappropriate
* to be used without additional customization.
*/
public class ContentTypeFilePart extends FilePart {
/**
* ContentTypeFilePart constructor.
*
* @param name the name of the part
* @param partSource the source for this part
* @param charset the charset encoding for this part.
*/
public ContentTypeFilePart(String name, PartSource partSource, String charset) {
super(name, partSource, ContentType.get(partSource.getFileName()), charset);
}
/**
* ContentTypeFilePart constructor.
*
* @param name the name of the part
* @param partSource the source for this part
*/
public ContentTypeFilePart(String name, PartSource partSource) {
this(name, partSource, null);
}
/**
* ContentTypeFilePart constructor.
*
* @param name the name of the part
* @param file the file to post
* @throws FileNotFoundException if the file does not exist
*/
public ContentTypeFilePart(String name, File file) throws FileNotFoundException {
this(name, file, null);
}
/**
* ContentTypeFilePart constructor.
*
* @param name the name of the part
* @param file the file to post
* @param charset the charset encoding for the file
* @throws FileNotFoundException
*/
public ContentTypeFilePart(String name, File file, String charset)
throws FileNotFoundException {
super(name, file, ContentType.get(file), charset);
}
/**
* ContentTypeFilePart constructor.
*
* @param name the name of the part
* @param fileName the file name
* @param file the file to post
* @throws FileNotFoundException if the file does not exist
*/
public ContentTypeFilePart(String name, String fileName, File file)
throws FileNotFoundException {
super(name, fileName, file, ContentType.get(fileName), null);
}
/**
* ContentTypeFilePart constructor.
*
* @param name the name of the part
* @param fileName the file name
* @param file the file to post
* @param charset the charset encoding for the file
* @throws FileNotFoundException if the file does not exist
*/
public ContentTypeFilePart(String name, String fileName, File file,
String charset) throws FileNotFoundException {
super(name, fileName, file, ContentType.get(file), charset);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org