View Javadoc
1 package org.apache.turbine.services.mimetype; 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.IOException; 59 import java.util.Locale; 60 61 import org.apache.turbine.services.TurbineBaseService; 62 import org.apache.turbine.services.servlet.TurbineServlet; 63 import org.apache.turbine.services.InitializationException; 64 import org.apache.turbine.services.mimetype.util.MimeType; 65 import org.apache.turbine.services.mimetype.util.MimeTypeMap; 66 import org.apache.turbine.services.mimetype.util.CharSetMap; 67 import org.apache.commons.configuration.Configuration; 68 69 70 /*** 71 * The MimeType Service maintains mappings between MIME types and 72 * the corresponding file name extensions, and between locales and 73 * character encodings. 74 * 75 * <p>The MIME type mappings can be defined in MIME type files 76 * located in user's home directory, Java home directory or 77 * the current class jar. The default mapping file is defined 78 * with the mime.type.file property. In addition, the service maintains 79 * a set of most common mappings. 80 * 81 * <p>The charset mappings can be defined in property files 82 * located in user's home directory, Java home directory or 83 * the current class jar. The default mapping file is defined 84 * with the charset.file property. In addition, the service maintains 85 * a set of most common mappings. 86 * 87 * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a> 88 * @version $Id: TurbineMimeTypeService.java,v 1.3 2002/04/16 22:07:08 kschrader Exp $ 89 */ 90 public class TurbineMimeTypeService 91 extends TurbineBaseService 92 implements MimeTypeService 93 { 94 /*** 95 * The MIME type file property. 96 */ 97 public static final String MIME_TYPES = "mime.types"; 98 99 /*** 100 * The charset file property. 101 */ 102 public static final String CHARSETS = "charsets"; 103 104 /*** 105 * The MIME type map used by the service. 106 */ 107 private MimeTypeMap mimeTypeMap; 108 109 /*** 110 * The charset map used by the service. 111 */ 112 private CharSetMap charSetMap; 113 114 /*** 115 * Constructs a new service. 116 */ 117 public TurbineMimeTypeService() 118 { 119 } 120 121 /*** 122 * Initializes the service. 123 * 124 * @throws InitializationException if initialization fails. 125 */ 126 public void init() 127 throws InitializationException 128 { 129 String path = null; 130 Configuration conf = getConfiguration(); 131 if (conf != null) 132 { 133 path = conf.getString(MIME_TYPES); 134 if (path != null) 135 { 136 path = TurbineServlet.getRealPath(path); 137 } 138 } 139 if (path != null) 140 { 141 try 142 { 143 mimeTypeMap = new MimeTypeMap(path); 144 } 145 catch (IOException x) 146 { 147 throw new InitializationException(path,x); 148 } 149 } 150 else 151 { 152 mimeTypeMap = new MimeTypeMap(); 153 } 154 155 if (conf != null) 156 { 157 path = conf.getString(CHARSETS); 158 if (path != null) 159 { 160 path = TurbineServlet.getRealPath(path); 161 } 162 } 163 if (path != null) 164 { 165 try 166 { 167 charSetMap = new CharSetMap(path); 168 } 169 catch (IOException x) 170 { 171 throw new InitializationException(path,x); 172 } 173 } 174 else 175 { 176 charSetMap = new CharSetMap(); 177 } 178 setInit(true); 179 } 180 181 /*** 182 * Sets a MIME content type mapping to extensions to the map. 183 * The extension is specified by a MIME type name followed 184 * by a list of file name extensions separated by a whitespace. 185 * 186 * @param spec a MIME type extension specification to add. 187 */ 188 public void setContentType(String spec) 189 { 190 mimeTypeMap.setContentType(spec); 191 } 192 193 /*** 194 * Gets the MIME content type for a file as a string. 195 * 196 * @param file the file. 197 * @return the MIME type string. 198 */ 199 public String getContentType(File file) 200 { 201 return mimeTypeMap.getContentType(file); 202 } 203 204 /*** 205 * Gets the MIME content type for a named file as a string. 206 * 207 * @param name the name of the file. 208 * @return the MIME type string. 209 */ 210 public String getContentType(String name) 211 { 212 return mimeTypeMap.getContentType(name); 213 } 214 215 /*** 216 * Gets the MIME content type for a file name extension as a string. 217 * 218 * @param ext the file name extension. 219 * @param def the default type if none is found. 220 * @return the MIME type string. 221 */ 222 public String getContentType(String ext, 223 String def) 224 { 225 return mimeTypeMap.getContentType(ext,def); 226 } 227 228 /*** 229 * Gets the MIME content type for a file. 230 * 231 * @param file the file. 232 * @return the MIME type. 233 */ 234 public MimeType getMimeContentType(File file) 235 { 236 return mimeTypeMap.getMimeContentType(file); 237 } 238 239 /*** 240 * Gets the MIME content type for a named file. 241 * 242 * @param name the name of the file. 243 * @return the MIME type. 244 */ 245 public MimeType getMimeContentType(String name) 246 { 247 return mimeTypeMap.getMimeContentType(name); 248 } 249 250 /*** 251 * Gets the MIME content type for a file name extension. 252 * 253 * @param ext the file name extension. 254 * @param def the default type if none is found. 255 * @return the MIME type. 256 */ 257 public MimeType getMimeContentType(String ext, 258 String def) 259 { 260 return mimeTypeMap.getMimeContentType(ext,def); 261 } 262 263 /*** 264 * Gets the default file name extension for a MIME type. 265 * Note that the mappers are called in the reverse order. 266 * 267 * @param mime the MIME type as a string. 268 * @return the file name extension or null. 269 */ 270 public String getDefaultExtension(String type) 271 { 272 return mimeTypeMap.getDefaultExtension(type); 273 } 274 275 /*** 276 * Gets the default file name extension for a MIME type. 277 * Note that the mappers are called in the reverse order. 278 * 279 * @param mime the MIME type. 280 * @return the file name extension or null. 281 */ 282 public String getDefaultExtension(MimeType mime) 283 { 284 return mimeTypeMap.getDefaultExtension(mime); 285 } 286 287 /*** 288 * Sets a locale-charset mapping. 289 * 290 * @param key the key for the charset. 291 * @param charset the corresponding charset. 292 */ 293 public void setCharSet(String key, 294 String charset) 295 { 296 charSetMap.setCharSet(key,charset); 297 } 298 299 /*** 300 * Gets the charset for a locale. First a locale specific charset 301 * is searched for, then a country specific one and lastly a language 302 * specific one. If none is found, the default charset is returned. 303 * 304 * @param locale the locale. 305 * @return the charset. 306 */ 307 public String getCharSet(Locale locale) 308 { 309 return charSetMap.getCharSet(locale); 310 } 311 312 /*** 313 * Gets the charset for a locale with a variant. The search 314 * is performed in the following order: 315 * "lang"_"country"_"variant"="charset", 316 * _"counry"_"variant"="charset", 317 * "lang"__"variant"="charset", 318 * __"variant"="charset", 319 * "lang"_"country"="charset", 320 * _"country"="charset", 321 * "lang"="charset". 322 * If nothing of the above is found, the default charset is returned. 323 * 324 * @param locale the locale. 325 * @param variant a variant field. 326 * @return the charset. 327 */ 328 public String getCharSet(Locale locale, 329 String variant) 330 { 331 return charSetMap.getCharSet(locale,variant); 332 } 333 334 /*** 335 * Gets the charset for a specified key. 336 * 337 * @param key the key for the charset. 338 * @return the found charset or the default one. 339 */ 340 public String getCharSet(String key) 341 { 342 return charSetMap.getCharSet(key); 343 } 344 345 /*** 346 * Gets the charset for a specified key. 347 * 348 * @param key the key for the charset. 349 * @param def the default charset if none is found. 350 * @return the found charset or the given default. 351 */ 352 public String getCharSet(String key, 353 String def) 354 { 355 return charSetMap.getCharSet(key,def); 356 } 357 }

This page was automatically generated by Maven