View Javadoc
1 package org.apache.turbine.services.mimetype.util; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import java.io.File; 58 import java.io.InputStream; 59 import java.io.IOException; 60 61 /*** 62 * This class maintains a set of mappers defining mappings 63 * between MIME types and the corresponding file name extensions. 64 * The mappings are defined as lines formed by a MIME type name 65 * followed by a list of extensions separated by a whitespace. 66 * The definitions can be listed in MIME type files located in user's 67 * home directory, Java home directory or the current class jar. 68 * In addition, this class maintains static default mappings 69 * and constructors support application specific mappings. 70 * 71 * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a> 72 * @version $Id: MimeTypeMap.java,v 1.2 2001/09/09 01:31:29 fedor Exp $ 73 */ 74 public class MimeTypeMap 75 { 76 /*** 77 * The default MIME type when nothing else is applicable. 78 */ 79 public static final MimeType DEFAULT_MIMETYPE = 80 MimeType.APPLICATION_OCTET_STREAM; 81 82 /*** 83 * The default MIME type as a string. 84 */ 85 public static final String DEFAULT_TYPE = DEFAULT_MIMETYPE.toString(); 86 87 /*** 88 * The name for MIME type mapper resources. 89 */ 90 public static final String MIMETYPE_RESOURCE = "mime.types"; 91 92 /*** 93 * Common MIME type extensions. 94 */ 95 public static final String EXT_HTML = "html"; 96 public static final String EXT_HTM = "htm"; 97 public static final String EXT_WML = "wml"; 98 public static final String EXT_HDML = "hdml"; 99 public static final String EXT_HDM = "hdm"; 100 public static final String EXT_CHTML = "chtml"; 101 public static final String EXT_TEXT = "txt"; 102 public static final String EXT_GIF = "gif"; 103 public static final String EXT_JPEG = "jpeg"; 104 public static final String EXT_JPG = "jpg"; 105 public static final String EXT_WBMP = "wbmp"; 106 107 /*** 108 * Priorities of available mappers. 109 */ 110 private static final int MAP_PROG = 0; 111 private static final int MAP_HOME = 1; 112 private static final int MAP_SYS = 2; 113 private static final int MAP_JAR = 3; 114 private static final int MAP_COM = 4; 115 116 /*** 117 * A common MIME type mapper. 118 */ 119 private static MimeTypeMapper commonMapper = new MimeTypeMapper(); 120 static 121 { 122 commonMapper.setContentType( 123 MimeType.TEXT_HTML.toString() + " " + EXT_HTML + " " + EXT_HTM); 124 commonMapper.setContentType( 125 MimeType.TEXT_WML.toString() + " " + EXT_WML); 126 commonMapper.setContentType( 127 MimeType.TEXT_HDML.toString() + " " + EXT_HDML + " " + EXT_HDM); 128 commonMapper.setContentType( 129 MimeType.TEXT_CHTML.toString() + " " + EXT_CHTML); 130 commonMapper.setContentType( 131 MimeType.TEXT_PLAIN.toString() + " " + EXT_TEXT); 132 commonMapper.setContentType( 133 MimeType.IMAGE_GIF.toString() + " " + EXT_GIF); 134 commonMapper.setContentType( 135 MimeType.IMAGE_JPEG.toString() + " " + EXT_JPEG + " " + EXT_JPG); 136 commonMapper.setContentType( 137 MimeType.IMAGE_WBMP.toString() + " " + EXT_WBMP); 138 } 139 140 /*** 141 * An array of available MIME type mappers. 142 */ 143 private MimeTypeMapper mappers[] = new MimeTypeMapper[5]; 144 145 /*** 146 * Loads mappings from a file path. 147 * 148 * @param path a file path. 149 * @return the mappings. 150 * @throws IOException for an incorrect file. 151 */ 152 protected static MimeTypeMapper loadPath(String path) 153 throws IOException 154 { 155 return new MimeTypeMapper(path); 156 } 157 158 /*** 159 * Loads mappings from a resource. 160 * 161 * @param name a resource name. 162 * @return the mappings. 163 */ 164 protected static MimeTypeMapper loadResource(String name) 165 { 166 InputStream input = MimeTypeMap.class.getResourceAsStream(name); 167 if (input != null) 168 { 169 try 170 { 171 return new MimeTypeMapper(input); 172 } 173 catch (IOException x) 174 { 175 return null; 176 } 177 } 178 else 179 { 180 return null; 181 } 182 } 183 184 /*** 185 * Constructs a new MIME type map with default mappers. 186 */ 187 public MimeTypeMap() 188 { 189 String path; 190 try 191 { 192 // Check whether the user directory contains mappings. 193 path = System.getProperty("user.home"); 194 if (path != null) 195 { 196 path = path + File.separator + MIMETYPE_RESOURCE; 197 mappers[MAP_HOME] = loadPath(path); 198 } 199 } 200 catch (Exception x) 201 { 202 } 203 204 try 205 { 206 // Check whether the system directory contains mappings. 207 path = System.getProperty("java.home") + 208 File.separator + "lib" + File.separator + MIMETYPE_RESOURCE; 209 mappers[MAP_SYS] = loadPath(path); 210 } 211 catch (Exception x) 212 { 213 } 214 215 // Check whether the current class jar contains mappings. 216 mappers[MAP_JAR] = loadResource("/META-INF/" + MIMETYPE_RESOURCE); 217 218 // Set the common mapper to have the lowest priority. 219 mappers[MAP_COM] = commonMapper; 220 } 221 222 /*** 223 * Contructs a MIME type map read from a stream. 224 * 225 * @param input an input stream. 226 * @throws IOException for an incorrect stream. 227 */ 228 public MimeTypeMap(InputStream input) 229 throws IOException 230 { 231 this(); 232 mappers[MAP_PROG] = new MimeTypeMapper(input); 233 } 234 235 /*** 236 * Contructs a MIME type map read from a file. 237 * 238 * @param path an input file. 239 * @throws IOException for an incorrect input file. 240 */ 241 public MimeTypeMap(File file) 242 throws IOException 243 { 244 this(); 245 mappers[MAP_PROG] = new MimeTypeMapper(file); 246 } 247 248 /*** 249 * Contructs a MIME type map read from a file path. 250 * 251 * @param path an input file path. 252 * @throws IOException for an incorrect input file. 253 */ 254 public MimeTypeMap(String path) 255 throws IOException 256 { 257 this(); 258 mappers[MAP_PROG] = new MimeTypeMapper(path); 259 } 260 261 /*** 262 * Sets a MIME content type mapping to extensions. 263 * 264 * @param spec a MIME type extension specification to set. 265 */ 266 public synchronized void setContentType(String spec) 267 { 268 if (mappers[MAP_PROG] == null) 269 { 270 mappers[MAP_PROG] = new MimeTypeMapper(); 271 } 272 mappers[MAP_PROG].setContentType(spec); 273 } 274 275 /*** 276 * Gets the MIME content type for a file as a string. 277 * 278 * @param file the file. 279 * @return the MIME type string. 280 */ 281 public String getContentType(File file) 282 { 283 return getContentType(file.getName()); 284 } 285 286 /*** 287 * Gets the MIME content type for a named file as a string. 288 * 289 * @param name the name of the file. 290 * @return the MIME type string. 291 */ 292 public String getContentType(String name) 293 { 294 int i = name.lastIndexOf('.'); 295 if (i >= 0) 296 { 297 String ext = name.substring(i + 1); 298 return ext.length() > 0 ? 299 getContentType(ext,DEFAULT_TYPE) : DEFAULT_TYPE; 300 } 301 else 302 { 303 return DEFAULT_TYPE; 304 } 305 } 306 307 /*** 308 * Gets the MIME content type for a file name extension as a string. 309 * 310 * @param ext the file name extension. 311 * @param def the default type if none is found. 312 * @return the MIME type string. 313 */ 314 public String getContentType(String ext, 315 String def) 316 { 317 int i = ext.lastIndexOf('.'); 318 if (i >= 0) 319 { 320 ext = ext.substring(i + 1); 321 } 322 323 String mime; 324 MimeTypeMapper mapper; 325 for (i = 0; i < mappers.length; i++) 326 { 327 mapper = mappers[i]; 328 if (mapper != null) 329 { 330 mime = mapper.getContentType(ext); 331 if (mime != null) 332 { 333 return mime; 334 } 335 } 336 } 337 return def; 338 } 339 340 /*** 341 * Gets the MIME content type for a file. 342 * 343 * @param file the file. 344 * @return the MIME type. 345 */ 346 public MimeType getMimeContentType(File file) 347 { 348 try 349 { 350 return new MimeType(getContentType(file)); 351 } 352 catch (Exception x) 353 { 354 return DEFAULT_MIMETYPE; 355 } 356 } 357 358 /*** 359 * Gets the MIME content type for a named file. 360 * 361 * @param name the name of the file. 362 * @return the MIME type. 363 */ 364 public MimeType getMimeContentType(String name) 365 { 366 try 367 { 368 return new MimeType(getContentType(name)); 369 } 370 catch (Exception x) 371 { 372 return DEFAULT_MIMETYPE; 373 } 374 } 375 376 /*** 377 * Gets the MIME content type for a file name extension. 378 * 379 * @param ext the file name extension. 380 * @param def the default type if none is found. 381 * @return the MIME type. 382 */ 383 public MimeType getMimeContentType(String ext, 384 String def) 385 { 386 try 387 { 388 return new MimeType(getContentType(ext,def)); 389 } 390 catch (Exception x) 391 { 392 return DEFAULT_MIMETYPE; 393 } 394 } 395 396 /*** 397 * Gets the default file name extension for a MIME type. 398 * Note that the mappers are called in the reverse order. 399 * 400 * @param type the MIME type as a string. 401 * @return the file name extension or null. 402 */ 403 public String getDefaultExtension(String type) 404 { 405 String ext; 406 MimeTypeMapper mapper; 407 int i = type.indexOf(';'); 408 if (i >= 0) 409 { 410 type = type.substring(0,i); 411 } 412 type = type.trim(); 413 for (i = mappers.length - 1; i >= 0; i--) 414 { 415 mapper = mappers[i]; 416 if (mapper != null) 417 { 418 ext = mapper.getExtension(type); 419 if (ext != null) 420 { 421 return ext; 422 } 423 } 424 } 425 return null; 426 } 427 428 /*** 429 * Gets the default file name extension for a MIME type. 430 * Note that the mappers are called in the reverse order. 431 * 432 * @param mime the MIME type. 433 * @return the file name extension or null. 434 */ 435 public String getDefaultExtension(MimeType mime) 436 { 437 return getDefaultExtension(mime.getTypes()); 438 } 439 440 /*** 441 * Sets a common MIME content type mapping to extensions. 442 * 443 * @param spec a MIME type extension specification to set. 444 */ 445 protected synchronized void setCommonContentType(String spec) 446 { 447 mappers[MAP_COM].setContentType(spec); 448 } 449 }

This page was automatically generated by Maven