1 package org.apache.turbine.services.mimetype; 2 3 18 19 import java.io.File ; 20 import java.io.IOException ; 21 22 import java.util.Locale ; 23 24 import org.apache.commons.configuration.Configuration; 25 26 import org.apache.turbine.services.InitializationException; 27 import org.apache.turbine.services.TurbineBaseService; 28 import org.apache.turbine.services.mimetype.util.CharSetMap; 29 import org.apache.turbine.services.mimetype.util.MimeType; 30 import org.apache.turbine.services.mimetype.util.MimeTypeMap; 31 import org.apache.turbine.services.servlet.TurbineServlet; 32 33 53 public class TurbineMimeTypeService 54 extends TurbineBaseService 55 implements MimeTypeService 56 { 57 60 public static final String MIME_TYPES = "mime.types"; 61 62 65 public static final String CHARSETS = "charsets"; 66 67 70 private MimeTypeMap mimeTypeMap; 71 72 75 private CharSetMap charSetMap; 76 77 80 public TurbineMimeTypeService() 81 { 82 } 83 84 89 public void init() 90 throws InitializationException 91 { 92 String path = null; 93 Configuration conf = getConfiguration(); 94 if (conf != null) 95 { 96 path = conf.getString(MIME_TYPES); 97 if (path != null) 98 { 99 path = TurbineServlet.getRealPath(path); 100 } 101 } 102 if (path != null) 103 { 104 try 105 { 106 mimeTypeMap = new MimeTypeMap(path); 107 } 108 catch (IOException x) 109 { 110 throw new InitializationException(path, x); 111 } 112 } 113 else 114 { 115 mimeTypeMap = new MimeTypeMap(); 116 } 117 118 if (conf != null) 119 { 120 path = conf.getString(CHARSETS); 121 if (path != null) 122 { 123 path = TurbineServlet.getRealPath(path); 124 } 125 } 126 if (path != null) 127 { 128 try 129 { 130 charSetMap = new CharSetMap(path); 131 } 132 catch (IOException x) 133 { 134 throw new InitializationException(path, x); 135 } 136 } 137 else 138 { 139 charSetMap = new CharSetMap(); 140 } 141 setInit(true); 142 } 143 144 151 public void setContentType(String spec) 152 { 153 mimeTypeMap.setContentType(spec); 154 } 155 156 162 public String getContentType(File file) 163 { 164 return mimeTypeMap.getContentType(file); 165 } 166 167 173 public String getContentType(String name) 174 { 175 return mimeTypeMap.getContentType(name); 176 } 177 178 185 public String getContentType(String ext, 186 String def) 187 { 188 return mimeTypeMap.getContentType(ext, def); 189 } 190 191 197 public MimeType getMimeContentType(File file) 198 { 199 return mimeTypeMap.getMimeContentType(file); 200 } 201 202 208 public MimeType getMimeContentType(String name) 209 { 210 return mimeTypeMap.getMimeContentType(name); 211 } 212 213 220 public MimeType getMimeContentType(String ext, 221 String def) 222 { 223 return mimeTypeMap.getMimeContentType(ext, def); 224 } 225 226 233 public String getDefaultExtension(String type) 234 { 235 return mimeTypeMap.getDefaultExtension(type); 236 } 237 238 245 public String getDefaultExtension(MimeType mime) 246 { 247 return mimeTypeMap.getDefaultExtension(mime); 248 } 249 250 256 public void setCharSet(String key, 257 String charset) 258 { 259 charSetMap.setCharSet(key, charset); 260 } 261 262 270 public String getCharSet(Locale locale) 271 { 272 return charSetMap.getCharSet(locale); 273 } 274 275 291 public String getCharSet(Locale locale, 292 String variant) 293 { 294 return charSetMap.getCharSet(locale, variant); 295 } 296 297 303 public String getCharSet(String key) 304 { 305 return charSetMap.getCharSet(key); 306 } 307 308 315 public String getCharSet(String key, 316 String def) 317 { 318 return charSetMap.getCharSet(key, def); 319 } 320 } 321 | Popular Tags |