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