~testplan-team/testplan/source-collection

« back to all changes in this revision

Viewing changes to commons-httpclient-3.1/src/contrib/org/apache/commons/httpclient/contrib/methods/multipart/ContentType.java

  • Committer: edA-qa mort-ora-y
  • Date: 2009-01-28 15:36:51 UTC
  • Revision ID: eda-qa@disemia.com-20090128153651-u4uzd035zyjea16j
adding httpclient

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * ====================================================================
 
3
 *
 
4
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 
5
 *  contributor license agreements.  See the NOTICE file distributed with
 
6
 *  this work for additional information regarding copyright ownership.
 
7
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 
8
 *  (the "License"); you may not use this file except in compliance with
 
9
 *  the License.  You may obtain a copy of the License at
 
10
 *
 
11
 *      http://www.apache.org/licenses/LICENSE-2.0
 
12
 *
 
13
 *  Unless required by applicable law or agreed to in writing, software
 
14
 *  distributed under the License is distributed on an "AS IS" BASIS,
 
15
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
16
 *  See the License for the specific language governing permissions and
 
17
 *  limitations under the License.
 
18
 * ====================================================================
 
19
 *
 
20
 * This software consists of voluntary contributions made by many
 
21
 * individuals on behalf of the Apache Software Foundation.  For more
 
22
 * information on the Apache Software Foundation, please see
 
23
 * <http://www.apache.org/>.
 
24
 *
 
25
 * [Additional notices, if required by prior licensing conditions]
 
26
 *
 
27
 */
 
28
 
 
29
package org.apache.commons.httpclient.contrib.methods.multipart;
 
30
 
 
31
import java.io.File;
 
32
 
 
33
/**
 
34
 * This class provides mappings from file name extensions to content types.
 
35
 *
 
36
 * @author <a href="mailto:emdevlin@charter.net">Eric Devlin</a>
 
37
 * @author <a href="mailto:adrian@intencha.com">Adrian Sutton</a>
 
38
 * 
 
39
 * @version $Revision: 480424 $
 
40
 * 
 
41
 * DISCLAIMER: HttpClient developers DO NOT actively support this component.
 
42
 * The component is provided as a reference material, which may be inappropriate
 
43
 * to be used without additional customization.
 
44
 */
 
45
 
 
46
public final class ContentType {
 
47
 
 
48
    /** Mime Type mappings 'liberated' from Tomcat4.1.18/conf/web.xml*/
 
49
    public static final String[][] MIME_TYPE_MAPPINGS = { { "abs", "audio/x-mpeg" }, {
 
50
            "ai", "application/postscript" }, {
 
51
            "aif", "audio/x-aiff" }, {
 
52
            "aifc", "audio/x-aiff" }, {
 
53
            "aiff", "audio/x-aiff" }, {
 
54
            "aim", "application/x-aim" }, {
 
55
            "art", "image/x-jg" }, {
 
56
            "asf", "video/x-ms-asf" }, {
 
57
            "asx", "video/x-ms-asf" }, {
 
58
            "au", "audio/basic" }, {
 
59
            "avi", "video/x-msvideo" }, {
 
60
            "avx", "video/x-rad-screenplay" }, {
 
61
            "bcpio", "application/x-bcpio" }, {
 
62
            "bin", "application/octet-stream" }, {
 
63
            "bmp", "image/bmp" }, {
 
64
            "body", "text/html" }, {
 
65
            "cdf", "application/x-cdf" }, {
 
66
            "cer", "application/x-x509-ca-cert" }, {
 
67
            "class", "application/java" }, {
 
68
            "cpio", "application/x-cpio" }, {
 
69
            "csh", "application/x-csh" }, {
 
70
            "css", "text/css" }, {
 
71
            "dib", "image/bmp" }, {
 
72
            "doc", "application/msword" }, {
 
73
            "dtd", "text/plain" }, {
 
74
            "dv", "video/x-dv" }, {
 
75
            "dvi", "application/x-dvi" }, {
 
76
            "eps", "application/postscript" }, {
 
77
            "etx", "text/x-setext" }, {
 
78
            "exe", "application/octet-stream" }, {
 
79
            "gif", "image/gif" }, {
 
80
            "gtar", "application/x-gtar" }, {
 
81
            "gz", "application/x-gzip" }, {
 
82
            "hdf", "application/x-hdf" }, {
 
83
            "hqx", "application/mac-binhex40" }, {
 
84
            "htc", "text/x-component" }, {
 
85
            "htm", "text/html" }, {
 
86
            "html", "text/html" }, {
 
87
            "hqx", "application/mac-binhex40" }, {
 
88
            "ief", "image/ief" }, {
 
89
            "jad", "text/vnd.sun.j2me.app-descriptor" }, {
 
90
            "jar", "application/java-archive" }, {
 
91
            "java", "text/plain" }, {
 
92
            "jnlp", "application/x-java-jnlp-file" }, {
 
93
            "jpe", "image/jpeg" }, {
 
94
            "jpeg", "image/jpeg" }, {
 
95
            "jpg", "image/jpeg" }, {
 
96
            "js", "text/javascript" }, {
 
97
            "jsf", "text/plain" }, {
 
98
            "jspf", "text/plain" }, {
 
99
            "kar", "audio/x-midi" }, {
 
100
            "latex", "application/x-latex" }, {
 
101
            "m3u", "audio/x-mpegurl" }, {
 
102
            "mac", "image/x-macpaint" }, {
 
103
            "man", "application/x-troff-man" }, {
 
104
            "me", "application/x-troff-me" }, {
 
105
            "mid", "audio/x-midi" }, {
 
106
            "midi", "audio/x-midi" }, {
 
107
            "mif", "application/x-mif" }, {
 
108
            "mov", "video/quicktime" }, {
 
109
            "movie", "video/x-sgi-movie" }, {
 
110
            "mp1", "audio/x-mpeg" }, {
 
111
            "mp2", "audio/x-mpeg" }, {
 
112
            "mp3", "audio/x-mpeg" }, {
 
113
            "mpa", "audio/x-mpeg" }, {
 
114
            "mpe", "video/mpeg" }, {
 
115
            "mpeg", "video/mpeg" }, {
 
116
            "mpega", "audio/x-mpeg" }, {
 
117
            "mpg", "video/mpeg" }, {
 
118
            "mpv2", "video/mpeg2" }, {
 
119
            "ms", "application/x-wais-source" }, {
 
120
            "nc", "application/x-netcdf" }, {
 
121
            "oda", "application/oda" }, {
 
122
            "pbm", "image/x-portable-bitmap" }, {
 
123
            "pct", "image/pict" }, {
 
124
            "pdf", "application/pdf" }, {
 
125
            "pgm", "image/x-portable-graymap" }, {
 
126
            "pic", "image/pict" }, {
 
127
            "pict", "image/pict" }, {
 
128
            "pls", "audio/x-scpls" }, {
 
129
            "png", "image/png" }, {
 
130
            "pnm", "image/x-portable-anymap" }, {
 
131
            "pnt", "image/x-macpaint" }, {
 
132
            "ppm", "image/x-portable-pixmap" }, {
 
133
            "ps", "application/postscript" }, {
 
134
            "psd", "image/x-photoshop" }, {
 
135
            "qt", "video/quicktime" }, {
 
136
            "qti", "image/x-quicktime" }, {
 
137
            "qtif", "image/x-quicktime" }, {
 
138
            "ras", "image/x-cmu-raster" }, {
 
139
            "rgb", "image/x-rgb" }, {
 
140
            "rm", "application/vnd.rn-realmedia" }, {
 
141
            "roff", "application/x-troff" }, {
 
142
            "rtf", "application/rtf" }, {
 
143
            "rtx", "text/richtext" }, {
 
144
            "sh", "application/x-sh" }, {
 
145
            "shar", "application/x-shar" }, {
 
146
            "smf", "audio/x-midi" }, {
 
147
            "snd", "audio/basic" }, {
 
148
            "src", "application/x-wais-source" }, {
 
149
            "sv4cpio", "application/x-sv4cpio" }, {
 
150
            "sv4crc", "application/x-sv4crc" }, {
 
151
            "swf", "application/x-shockwave-flash" }, {
 
152
            "t", "application/x-troff" }, {
 
153
            "tar", "application/x-tar" }, {
 
154
            "tcl", "application/x-tcl" }, {
 
155
            "tex", "application/x-tex" }, {
 
156
            "texi", "application/x-texinfo" }, {
 
157
            "texinfo", "application/x-texinfo" }, {
 
158
            "tif", "image/tiff" }, {
 
159
            "tiff", "image/tiff" }, {
 
160
            "tr", "application/x-troff" }, {
 
161
            "tsv", "text/tab-separated-values" }, {
 
162
            "txt", "text/plain" }, {
 
163
            "ulw", "audio/basic" }, {
 
164
            "ustar", "application/x-ustar" }, {
 
165
            "xbm", "image/x-xbitmap" }, {
 
166
            "xml", "text/xml" }, {
 
167
            "xpm", "image/x-xpixmap" }, {
 
168
            "xsl", "text/xml" }, {
 
169
            "xwd", "image/x-xwindowdump" }, {
 
170
            "wav", "audio/x-wav" }, {
 
171
            "svg", "image/svg+xml" }, {
 
172
            "svgz", "image/svg+xml" }, {
 
173
            "wbmp", "image/vnd.wap.wbmp" }, {
 
174
            "wml", "text/vnd.wap.wml" }, {
 
175
            "wmlc", "application/vnd.wap.wmlc" }, {
 
176
            "wmls", "text/vnd.wap.wmlscript" }, {
 
177
            "wmlscriptc", "application/vnd.wap.wmlscriptc" }, {
 
178
            "wrl", "x-world/x-vrml" }, {
 
179
            "Z", "application/x-compress" }, {
 
180
            "z", "application/x-compress" }, {
 
181
            "zip", "application/zip" }
 
182
    };
 
183
 
 
184
    /** The constructor is intentionally private as the class only provides static methods.
 
185
     */
 
186
    private ContentType() {
 
187
    }
 
188
 
 
189
    /**
 
190
     * Get the content type based on the extension of the file name&lt;br&gt;
 
191
     *
 
192
     * @param fileName for which the content type is to be determined.
 
193
     *
 
194
     * @return the content type for the file or null if no mapping was
 
195
     * possible.
 
196
     */
 
197
    public static String get(String fileName) {
 
198
        String contentType = null;
 
199
 
 
200
        if (fileName != null) {
 
201
            int extensionIndex = fileName.lastIndexOf('.');
 
202
            if (extensionIndex != -1) {
 
203
                if (extensionIndex + 1 < fileName.length()) {
 
204
                    String extension = fileName.substring(extensionIndex + 1);
 
205
                    for (int i = 0; i < MIME_TYPE_MAPPINGS.length; i++) {
 
206
                        if (extension.equals(MIME_TYPE_MAPPINGS[i][0])) {
 
207
                            contentType = MIME_TYPE_MAPPINGS[i][1];
 
208
                            break;
 
209
                        }
 
210
                    }
 
211
                }
 
212
            }
 
213
        }
 
214
 
 
215
        return contentType;
 
216
    }
 
217
 
 
218
    /**
 
219
     * Get the content type based on the extension of the file name&lt;br&gt;
 
220
     *
 
221
     * @param file for which the content type is to be determined.
 
222
     *
 
223
     * @return the content type for the file or null if no mapping was
 
224
     * possible.
 
225
     *
 
226
     * @throws IOException if the construction of the canonical path for 
 
227
     * the file fails.
 
228
     */
 
229
    public static String get(File file) {
 
230
        String contentType = null;
 
231
 
 
232
        if (file != null) {
 
233
            contentType = get(file.getName());
 
234
        }
 
235
 
 
236
        return contentType;
 
237
    }
 
238
}