Mercurial Hosting > luan
comparison src/org/eclipse/jetty/http/MimeTypes.java @ 1020:6be43ef1eb96
HttpHeaderValues uses StringCache
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 31 Oct 2016 22:24:41 -0600 |
parents | 8e9db0bbf4f9 |
children | 27f3dc761452 |
comparison
equal
deleted
inserted
replaced
1019:f126d30e04a4 | 1020:6be43ef1eb96 |
---|---|
37 /** | 37 /** |
38 * | 38 * |
39 */ | 39 */ |
40 public class MimeTypes | 40 public class MimeTypes |
41 { | 41 { |
42 private static final Logger LOG = LoggerFactory.getLogger(MimeTypes.class); | 42 private static final Logger LOG = LoggerFactory.getLogger(MimeTypes.class); |
43 | 43 |
44 public final static String | 44 public final static String |
45 FORM_ENCODED="application/x-www-form-urlencoded", | 45 FORM_ENCODED="application/x-www-form-urlencoded", |
46 MESSAGE_HTTP="message/http", | 46 MESSAGE_HTTP="message/http", |
47 MULTIPART_BYTERANGES="multipart/byteranges", | 47 MULTIPART_BYTERANGES="multipart/byteranges", |
48 | 48 |
49 TEXT_HTML="text/html", | 49 TEXT_HTML="text/html", |
50 TEXT_PLAIN="text/plain", | 50 TEXT_PLAIN="text/plain", |
51 TEXT_XML="text/xml", | 51 TEXT_XML="text/xml", |
52 TEXT_JSON="text/json", | 52 TEXT_JSON="text/json", |
53 | 53 |
54 TEXT_HTML_8859_1="text/html;charset=ISO-8859-1", | 54 TEXT_HTML_8859_1="text/html;charset=ISO-8859-1", |
55 TEXT_PLAIN_8859_1="text/plain;charset=ISO-8859-1", | 55 TEXT_PLAIN_8859_1="text/plain;charset=ISO-8859-1", |
56 TEXT_XML_8859_1="text/xml;charset=ISO-8859-1", | 56 TEXT_XML_8859_1="text/xml;charset=ISO-8859-1", |
57 | 57 |
58 TEXT_HTML_UTF_8="text/html;charset=UTF-8", | 58 TEXT_HTML_UTF_8="text/html;charset=UTF-8", |
59 TEXT_PLAIN_UTF_8="text/plain;charset=UTF-8", | 59 TEXT_PLAIN_UTF_8="text/plain;charset=UTF-8", |
60 TEXT_XML_UTF_8="text/xml;charset=UTF-8", | 60 TEXT_XML_UTF_8="text/xml;charset=UTF-8", |
61 TEXT_JSON_UTF_8="text/json;charset=UTF-8"; | 61 TEXT_JSON_UTF_8="text/json;charset=UTF-8"; |
62 | 62 |
63 private final static String | 63 private final static String |
64 TEXT_HTML__8859_1="text/html; charset=ISO-8859-1", | 64 TEXT_HTML__8859_1="text/html; charset=ISO-8859-1", |
65 TEXT_PLAIN__8859_1="text/plain; charset=ISO-8859-1", | 65 TEXT_PLAIN__8859_1="text/plain; charset=ISO-8859-1", |
66 TEXT_XML__8859_1="text/xml; charset=ISO-8859-1", | 66 TEXT_XML__8859_1="text/xml; charset=ISO-8859-1", |
67 TEXT_HTML__UTF_8="text/html; charset=UTF-8", | 67 TEXT_HTML__UTF_8="text/html; charset=UTF-8", |
68 TEXT_PLAIN__UTF_8="text/plain; charset=UTF-8", | 68 TEXT_PLAIN__UTF_8="text/plain; charset=UTF-8", |
69 TEXT_XML__UTF_8="text/xml; charset=UTF-8", | 69 TEXT_XML__UTF_8="text/xml; charset=UTF-8", |
70 TEXT_JSON__UTF_8="text/json; charset=UTF-8"; | 70 TEXT_JSON__UTF_8="text/json; charset=UTF-8"; |
71 | 71 |
72 private final static int | 72 private final static int |
73 FORM_ENCODED_ORDINAL=1, | 73 FORM_ENCODED_ORDINAL=1, |
74 MESSAGE_HTTP_ORDINAL=2, | 74 MESSAGE_HTTP_ORDINAL=2, |
75 MULTIPART_BYTERANGES_ORDINAL=3, | 75 MULTIPART_BYTERANGES_ORDINAL=3, |
76 | 76 |
77 TEXT_HTML_ORDINAL=4, | 77 TEXT_HTML_ORDINAL=4, |
78 TEXT_PLAIN_ORDINAL=5, | 78 TEXT_PLAIN_ORDINAL=5, |
79 TEXT_XML_ORDINAL=6, | 79 TEXT_XML_ORDINAL=6, |
80 TEXT_JSON_ORDINAL=7, | 80 TEXT_JSON_ORDINAL=7, |
81 | 81 |
82 TEXT_HTML_8859_1_ORDINAL=8, | 82 TEXT_HTML_8859_1_ORDINAL=8, |
83 TEXT_PLAIN_8859_1_ORDINAL=9, | 83 TEXT_PLAIN_8859_1_ORDINAL=9, |
84 TEXT_XML_8859_1_ORDINAL=10, | 84 TEXT_XML_8859_1_ORDINAL=10, |
85 | 85 |
86 TEXT_HTML_UTF_8_ORDINAL=11, | 86 TEXT_HTML_UTF_8_ORDINAL=11, |
87 TEXT_PLAIN_UTF_8_ORDINAL=12, | 87 TEXT_PLAIN_UTF_8_ORDINAL=12, |
88 TEXT_XML_UTF_8_ORDINAL=13, | 88 TEXT_XML_UTF_8_ORDINAL=13, |
89 TEXT_JSON_UTF_8_ORDINAL=14; | 89 TEXT_JSON_UTF_8_ORDINAL=14; |
90 | 90 |
91 private static int __index=15; | 91 private static int __index=15; |
92 | 92 |
93 public final static BufferCache CACHE = new BufferCache(); | 93 public final static BufferCache CACHE = new BufferCache(); |
94 | 94 |
95 public final static CachedBuffer | 95 public final static CachedBuffer |
96 FORM_ENCODED_BUFFER=CACHE.add(FORM_ENCODED,FORM_ENCODED_ORDINAL), | 96 FORM_ENCODED_BUFFER=CACHE.add(FORM_ENCODED,FORM_ENCODED_ORDINAL), |
97 MESSAGE_HTTP_BUFFER=CACHE.add(MESSAGE_HTTP, MESSAGE_HTTP_ORDINAL), | 97 MESSAGE_HTTP_BUFFER=CACHE.add(MESSAGE_HTTP, MESSAGE_HTTP_ORDINAL), |
98 MULTIPART_BYTERANGES_BUFFER=CACHE.add(MULTIPART_BYTERANGES,MULTIPART_BYTERANGES_ORDINAL), | 98 MULTIPART_BYTERANGES_BUFFER=CACHE.add(MULTIPART_BYTERANGES,MULTIPART_BYTERANGES_ORDINAL), |
99 | 99 |
100 TEXT_HTML_BUFFER=CACHE.add(TEXT_HTML,TEXT_HTML_ORDINAL), | 100 TEXT_HTML_BUFFER=CACHE.add(TEXT_HTML,TEXT_HTML_ORDINAL), |
101 TEXT_PLAIN_BUFFER=CACHE.add(TEXT_PLAIN,TEXT_PLAIN_ORDINAL), | 101 TEXT_PLAIN_BUFFER=CACHE.add(TEXT_PLAIN,TEXT_PLAIN_ORDINAL), |
102 TEXT_XML_BUFFER=CACHE.add(TEXT_XML,TEXT_XML_ORDINAL), | 102 TEXT_XML_BUFFER=CACHE.add(TEXT_XML,TEXT_XML_ORDINAL), |
103 TEXT_JSON_BUFFER=CACHE.add(TEXT_JSON,TEXT_JSON_ORDINAL), | 103 TEXT_JSON_BUFFER=CACHE.add(TEXT_JSON,TEXT_JSON_ORDINAL), |
104 | 104 |
105 TEXT_HTML_8859_1_BUFFER=CACHE.add(TEXT_HTML_8859_1,TEXT_HTML_8859_1_ORDINAL), | 105 TEXT_HTML_8859_1_BUFFER=CACHE.add(TEXT_HTML_8859_1,TEXT_HTML_8859_1_ORDINAL), |
106 TEXT_PLAIN_8859_1_BUFFER=CACHE.add(TEXT_PLAIN_8859_1,TEXT_PLAIN_8859_1_ORDINAL), | 106 TEXT_PLAIN_8859_1_BUFFER=CACHE.add(TEXT_PLAIN_8859_1,TEXT_PLAIN_8859_1_ORDINAL), |
107 TEXT_XML_8859_1_BUFFER=CACHE.add(TEXT_XML_8859_1,TEXT_XML_8859_1_ORDINAL), | 107 TEXT_XML_8859_1_BUFFER=CACHE.add(TEXT_XML_8859_1,TEXT_XML_8859_1_ORDINAL), |
108 | 108 |
109 TEXT_HTML_UTF_8_BUFFER=CACHE.add(TEXT_HTML_UTF_8,TEXT_HTML_UTF_8_ORDINAL), | 109 TEXT_HTML_UTF_8_BUFFER=CACHE.add(TEXT_HTML_UTF_8,TEXT_HTML_UTF_8_ORDINAL), |
110 TEXT_PLAIN_UTF_8_BUFFER=CACHE.add(TEXT_PLAIN_UTF_8,TEXT_PLAIN_UTF_8_ORDINAL), | 110 TEXT_PLAIN_UTF_8_BUFFER=CACHE.add(TEXT_PLAIN_UTF_8,TEXT_PLAIN_UTF_8_ORDINAL), |
111 TEXT_XML_UTF_8_BUFFER=CACHE.add(TEXT_XML_UTF_8,TEXT_XML_UTF_8_ORDINAL), | 111 TEXT_XML_UTF_8_BUFFER=CACHE.add(TEXT_XML_UTF_8,TEXT_XML_UTF_8_ORDINAL), |
112 TEXT_JSON_UTF_8_BUFFER=CACHE.add(TEXT_JSON_UTF_8,TEXT_JSON_UTF_8_ORDINAL), | 112 TEXT_JSON_UTF_8_BUFFER=CACHE.add(TEXT_JSON_UTF_8,TEXT_JSON_UTF_8_ORDINAL), |
113 | 113 |
114 TEXT_HTML__8859_1_BUFFER=CACHE.add(TEXT_HTML__8859_1,TEXT_HTML_8859_1_ORDINAL), | 114 TEXT_HTML__8859_1_BUFFER=CACHE.add(TEXT_HTML__8859_1,TEXT_HTML_8859_1_ORDINAL), |
115 TEXT_PLAIN__8859_1_BUFFER=CACHE.add(TEXT_PLAIN__8859_1,TEXT_PLAIN_8859_1_ORDINAL), | 115 TEXT_PLAIN__8859_1_BUFFER=CACHE.add(TEXT_PLAIN__8859_1,TEXT_PLAIN_8859_1_ORDINAL), |
116 TEXT_XML__8859_1_BUFFER=CACHE.add(TEXT_XML__8859_1,TEXT_XML_8859_1_ORDINAL), | 116 TEXT_XML__8859_1_BUFFER=CACHE.add(TEXT_XML__8859_1,TEXT_XML_8859_1_ORDINAL), |
117 | 117 |
118 TEXT_HTML__UTF_8_BUFFER=CACHE.add(TEXT_HTML__UTF_8,TEXT_HTML_UTF_8_ORDINAL), | 118 TEXT_HTML__UTF_8_BUFFER=CACHE.add(TEXT_HTML__UTF_8,TEXT_HTML_UTF_8_ORDINAL), |
119 TEXT_PLAIN__UTF_8_BUFFER=CACHE.add(TEXT_PLAIN__UTF_8,TEXT_PLAIN_UTF_8_ORDINAL), | 119 TEXT_PLAIN__UTF_8_BUFFER=CACHE.add(TEXT_PLAIN__UTF_8,TEXT_PLAIN_UTF_8_ORDINAL), |
120 TEXT_XML__UTF_8_BUFFER=CACHE.add(TEXT_XML__UTF_8,TEXT_XML_UTF_8_ORDINAL), | 120 TEXT_XML__UTF_8_BUFFER=CACHE.add(TEXT_XML__UTF_8,TEXT_XML_UTF_8_ORDINAL), |
121 TEXT_JSON__UTF_8_BUFFER=CACHE.add(TEXT_JSON__UTF_8,TEXT_JSON_UTF_8_ORDINAL); | 121 TEXT_JSON__UTF_8_BUFFER=CACHE.add(TEXT_JSON__UTF_8,TEXT_JSON_UTF_8_ORDINAL); |
122 | 122 |
123 | 123 |
124 /* ------------------------------------------------------------ */ | 124 /* ------------------------------------------------------------ */ |
125 /* ------------------------------------------------------------ */ | 125 /* ------------------------------------------------------------ */ |
126 private final static Map __dftMimeMap = new HashMap(); | 126 private final static Map __dftMimeMap = new HashMap(); |
127 private final static Map __encodings = new HashMap(); | 127 private final static Map __encodings = new HashMap(); |
128 static | 128 static |
129 { | 129 { |
130 try | 130 try |
131 { | 131 { |
132 ResourceBundle mime = ResourceBundle.getBundle("org/eclipse/jetty/http/mime"); | 132 ResourceBundle mime = ResourceBundle.getBundle("org/eclipse/jetty/http/mime"); |
133 Enumeration i = mime.getKeys(); | 133 Enumeration i = mime.getKeys(); |
134 while(i.hasMoreElements()) | 134 while(i.hasMoreElements()) |
135 { | 135 { |
136 String ext = (String)i.nextElement(); | 136 String ext = (String)i.nextElement(); |
137 String m = mime.getString(ext); | 137 String m = mime.getString(ext); |
138 __dftMimeMap.put(StringUtil.asciiToLowerCase(ext),normalizeMimeType(m)); | 138 __dftMimeMap.put(StringUtil.asciiToLowerCase(ext),normalizeMimeType(m)); |
139 } | 139 } |
140 } | 140 } |
141 catch(MissingResourceException e) | 141 catch(MissingResourceException e) |
142 { | 142 { |
143 LOG.warn(e.toString()); | 143 LOG.warn(e.toString()); |
144 LOG.debug("",e); | 144 LOG.debug("",e); |
145 } | 145 } |
146 | 146 |
147 try | 147 try |
148 { | 148 { |
149 ResourceBundle encoding = ResourceBundle.getBundle("org/eclipse/jetty/http/encoding"); | 149 ResourceBundle encoding = ResourceBundle.getBundle("org/eclipse/jetty/http/encoding"); |
150 Enumeration i = encoding.getKeys(); | 150 Enumeration i = encoding.getKeys(); |
151 while(i.hasMoreElements()) | 151 while(i.hasMoreElements()) |
152 { | 152 { |
153 Buffer type = normalizeMimeType((String)i.nextElement()); | 153 Buffer type = normalizeMimeType((String)i.nextElement()); |
154 __encodings.put(type,encoding.getString(type.toString())); | 154 __encodings.put(type,encoding.getString(type.toString())); |
155 } | 155 } |
156 } | 156 } |
157 catch(MissingResourceException e) | 157 catch(MissingResourceException e) |
158 { | 158 { |
159 LOG.warn(e.toString()); | 159 LOG.warn(e.toString()); |
160 LOG.debug("",e); | 160 LOG.debug("",e); |
161 } | 161 } |
162 | 162 |
163 | 163 |
164 TEXT_HTML_BUFFER.setAssociate("ISO-8859-1",TEXT_HTML_8859_1_BUFFER); | 164 TEXT_HTML_BUFFER.setAssociate("ISO-8859-1",TEXT_HTML_8859_1_BUFFER); |
165 TEXT_HTML_BUFFER.setAssociate("ISO_8859_1",TEXT_HTML_8859_1_BUFFER); | 165 TEXT_HTML_BUFFER.setAssociate("ISO_8859_1",TEXT_HTML_8859_1_BUFFER); |
166 TEXT_HTML_BUFFER.setAssociate("iso-8859-1",TEXT_HTML_8859_1_BUFFER); | 166 TEXT_HTML_BUFFER.setAssociate("iso-8859-1",TEXT_HTML_8859_1_BUFFER); |
167 TEXT_PLAIN_BUFFER.setAssociate("ISO-8859-1",TEXT_PLAIN_8859_1_BUFFER); | 167 TEXT_PLAIN_BUFFER.setAssociate("ISO-8859-1",TEXT_PLAIN_8859_1_BUFFER); |
168 TEXT_PLAIN_BUFFER.setAssociate("ISO_8859_1",TEXT_PLAIN_8859_1_BUFFER); | 168 TEXT_PLAIN_BUFFER.setAssociate("ISO_8859_1",TEXT_PLAIN_8859_1_BUFFER); |
169 TEXT_PLAIN_BUFFER.setAssociate("iso-8859-1",TEXT_PLAIN_8859_1_BUFFER); | 169 TEXT_PLAIN_BUFFER.setAssociate("iso-8859-1",TEXT_PLAIN_8859_1_BUFFER); |
170 TEXT_XML_BUFFER.setAssociate("ISO-8859-1",TEXT_XML_8859_1_BUFFER); | 170 TEXT_XML_BUFFER.setAssociate("ISO-8859-1",TEXT_XML_8859_1_BUFFER); |
171 TEXT_XML_BUFFER.setAssociate("ISO_8859_1",TEXT_XML_8859_1_BUFFER); | 171 TEXT_XML_BUFFER.setAssociate("ISO_8859_1",TEXT_XML_8859_1_BUFFER); |
172 TEXT_XML_BUFFER.setAssociate("iso-8859-1",TEXT_XML_8859_1_BUFFER); | 172 TEXT_XML_BUFFER.setAssociate("iso-8859-1",TEXT_XML_8859_1_BUFFER); |
173 | 173 |
174 TEXT_HTML_BUFFER.setAssociate("UTF-8",TEXT_HTML_UTF_8_BUFFER); | 174 TEXT_HTML_BUFFER.setAssociate("UTF-8",TEXT_HTML_UTF_8_BUFFER); |
175 TEXT_HTML_BUFFER.setAssociate("UTF8",TEXT_HTML_UTF_8_BUFFER); | 175 TEXT_HTML_BUFFER.setAssociate("UTF8",TEXT_HTML_UTF_8_BUFFER); |
176 TEXT_HTML_BUFFER.setAssociate("utf8",TEXT_HTML_UTF_8_BUFFER); | 176 TEXT_HTML_BUFFER.setAssociate("utf8",TEXT_HTML_UTF_8_BUFFER); |
177 TEXT_HTML_BUFFER.setAssociate("utf-8",TEXT_HTML_UTF_8_BUFFER); | 177 TEXT_HTML_BUFFER.setAssociate("utf-8",TEXT_HTML_UTF_8_BUFFER); |
178 TEXT_PLAIN_BUFFER.setAssociate("UTF-8",TEXT_PLAIN_UTF_8_BUFFER); | 178 TEXT_PLAIN_BUFFER.setAssociate("UTF-8",TEXT_PLAIN_UTF_8_BUFFER); |
179 TEXT_PLAIN_BUFFER.setAssociate("UTF8",TEXT_PLAIN_UTF_8_BUFFER); | 179 TEXT_PLAIN_BUFFER.setAssociate("UTF8",TEXT_PLAIN_UTF_8_BUFFER); |
180 TEXT_PLAIN_BUFFER.setAssociate("utf8",TEXT_PLAIN_UTF_8_BUFFER); | 180 TEXT_PLAIN_BUFFER.setAssociate("utf8",TEXT_PLAIN_UTF_8_BUFFER); |
181 TEXT_PLAIN_BUFFER.setAssociate("utf-8",TEXT_PLAIN_UTF_8_BUFFER); | 181 TEXT_PLAIN_BUFFER.setAssociate("utf-8",TEXT_PLAIN_UTF_8_BUFFER); |
182 TEXT_XML_BUFFER.setAssociate("UTF-8",TEXT_XML_UTF_8_BUFFER); | 182 TEXT_XML_BUFFER.setAssociate("UTF-8",TEXT_XML_UTF_8_BUFFER); |
183 TEXT_XML_BUFFER.setAssociate("UTF8",TEXT_XML_UTF_8_BUFFER); | 183 TEXT_XML_BUFFER.setAssociate("UTF8",TEXT_XML_UTF_8_BUFFER); |
184 TEXT_XML_BUFFER.setAssociate("utf8",TEXT_XML_UTF_8_BUFFER); | 184 TEXT_XML_BUFFER.setAssociate("utf8",TEXT_XML_UTF_8_BUFFER); |
185 TEXT_XML_BUFFER.setAssociate("utf-8",TEXT_XML_UTF_8_BUFFER); | 185 TEXT_XML_BUFFER.setAssociate("utf-8",TEXT_XML_UTF_8_BUFFER); |
186 TEXT_JSON_BUFFER.setAssociate("UTF-8",TEXT_JSON_UTF_8_BUFFER); | 186 TEXT_JSON_BUFFER.setAssociate("UTF-8",TEXT_JSON_UTF_8_BUFFER); |
187 TEXT_JSON_BUFFER.setAssociate("UTF8",TEXT_JSON_UTF_8_BUFFER); | 187 TEXT_JSON_BUFFER.setAssociate("UTF8",TEXT_JSON_UTF_8_BUFFER); |
188 TEXT_JSON_BUFFER.setAssociate("utf8",TEXT_JSON_UTF_8_BUFFER); | 188 TEXT_JSON_BUFFER.setAssociate("utf8",TEXT_JSON_UTF_8_BUFFER); |
189 TEXT_JSON_BUFFER.setAssociate("utf-8",TEXT_JSON_UTF_8_BUFFER); | 189 TEXT_JSON_BUFFER.setAssociate("utf-8",TEXT_JSON_UTF_8_BUFFER); |
190 } | 190 } |
191 | 191 |
192 | 192 |
193 /* ------------------------------------------------------------ */ | 193 /* ------------------------------------------------------------ */ |
194 private Map _mimeMap; | 194 private Map _mimeMap; |
195 | 195 |
196 /* ------------------------------------------------------------ */ | 196 /* ------------------------------------------------------------ */ |
197 /** Constructor. | 197 /** Constructor. |
198 */ | 198 */ |
199 public MimeTypes() | 199 public MimeTypes() |
200 { | 200 { |
201 } | 201 } |
202 | 202 |
203 /* ------------------------------------------------------------ */ | 203 /* ------------------------------------------------------------ */ |
204 public synchronized Map getMimeMap() | 204 public synchronized Map getMimeMap() |
205 { | 205 { |
206 return _mimeMap; | 206 return _mimeMap; |
207 } | 207 } |
208 | 208 |
209 /* ------------------------------------------------------------ */ | 209 /* ------------------------------------------------------------ */ |
210 /** | 210 /** |
211 * @param mimeMap A Map of file extension to mime-type. | 211 * @param mimeMap A Map of file extension to mime-type. |
212 */ | 212 */ |
213 public void setMimeMap(Map mimeMap) | 213 public void setMimeMap(Map mimeMap) |
214 { | 214 { |
215 if (mimeMap==null) | 215 if (mimeMap==null) |
216 { | 216 { |
217 _mimeMap=null; | 217 _mimeMap=null; |
218 return; | 218 return; |
219 } | 219 } |
220 | 220 |
221 Map m=new HashMap(); | 221 Map m=new HashMap(); |
222 Iterator i=mimeMap.entrySet().iterator(); | 222 Iterator i=mimeMap.entrySet().iterator(); |
223 while (i.hasNext()) | 223 while (i.hasNext()) |
224 { | 224 { |
225 Map.Entry entry = (Map.Entry)i.next(); | 225 Map.Entry entry = (Map.Entry)i.next(); |
226 m.put(entry.getKey(),normalizeMimeType(entry.getValue().toString())); | 226 m.put(entry.getKey(),normalizeMimeType(entry.getValue().toString())); |
227 } | 227 } |
228 _mimeMap=m; | 228 _mimeMap=m; |
229 } | 229 } |
230 | 230 |
231 /* ------------------------------------------------------------ */ | 231 /* ------------------------------------------------------------ */ |
232 /** Get the MIME type by filename extension. | 232 /** Get the MIME type by filename extension. |
233 * @param filename A file name | 233 * @param filename A file name |
234 * @return MIME type matching the longest dot extension of the | 234 * @return MIME type matching the longest dot extension of the |
235 * file name. | 235 * file name. |
236 */ | 236 */ |
237 public Buffer getMimeByExtension(String filename) | 237 public Buffer getMimeByExtension(String filename) |
238 { | 238 { |
239 Buffer type=null; | 239 Buffer type=null; |
240 | 240 |
241 if (filename!=null) | 241 if (filename!=null) |
242 { | 242 { |
243 int i=-1; | 243 int i=-1; |
244 while(type==null) | 244 while(type==null) |
245 { | 245 { |
246 i=filename.indexOf(".",i+1); | 246 i=filename.indexOf(".",i+1); |
247 | 247 |
248 if (i<0 || i>=filename.length()) | 248 if (i<0 || i>=filename.length()) |
249 break; | 249 break; |
250 | 250 |
251 String ext=StringUtil.asciiToLowerCase(filename.substring(i+1)); | 251 String ext=StringUtil.asciiToLowerCase(filename.substring(i+1)); |
252 if (_mimeMap!=null) | 252 if (_mimeMap!=null) |
253 type = (Buffer)_mimeMap.get(ext); | 253 type = (Buffer)_mimeMap.get(ext); |
254 if (type==null) | 254 if (type==null) |
255 type=(Buffer)__dftMimeMap.get(ext); | 255 type=(Buffer)__dftMimeMap.get(ext); |
256 } | 256 } |
257 } | 257 } |
258 | 258 |
259 if (type==null) | 259 if (type==null) |
260 { | 260 { |
261 if (_mimeMap!=null) | 261 if (_mimeMap!=null) |
262 type=(Buffer)_mimeMap.get("*"); | 262 type=(Buffer)_mimeMap.get("*"); |
263 if (type==null) | 263 if (type==null) |
264 type=(Buffer)__dftMimeMap.get("*"); | 264 type=(Buffer)__dftMimeMap.get("*"); |
265 } | 265 } |
266 | 266 |
267 return type; | 267 return type; |
268 } | 268 } |
269 | 269 |
270 /* ------------------------------------------------------------ */ | 270 /* ------------------------------------------------------------ */ |
271 /** Set a mime mapping | 271 /** Set a mime mapping |
272 * @param extension | 272 * @param extension |
273 * @param type | 273 * @param type |
274 */ | 274 */ |
275 public void addMimeMapping(String extension,String type) | 275 public void addMimeMapping(String extension,String type) |
276 { | 276 { |
277 if (_mimeMap==null) | 277 if (_mimeMap==null) |
278 _mimeMap=new HashMap(); | 278 _mimeMap=new HashMap(); |
279 | 279 |
280 _mimeMap.put(StringUtil.asciiToLowerCase(extension),normalizeMimeType(type)); | 280 _mimeMap.put(StringUtil.asciiToLowerCase(extension),normalizeMimeType(type)); |
281 } | 281 } |
282 | 282 |
283 /* ------------------------------------------------------------ */ | 283 /* ------------------------------------------------------------ */ |
284 private static synchronized Buffer normalizeMimeType(String type) | 284 private static synchronized Buffer normalizeMimeType(String type) |
285 { | 285 { |
286 Buffer b =CACHE.get(type); | 286 Buffer b =CACHE.get(type); |
287 if (b==null) | 287 if (b==null) |
288 b=CACHE.add(type,__index++); | 288 b=CACHE.add(type,__index++); |
289 return b; | 289 return b; |
290 } | 290 } |
291 | 291 |
292 /* ------------------------------------------------------------ */ | 292 /* ------------------------------------------------------------ */ |
293 public static String getCharsetFromContentType(Buffer value) | 293 public static String getCharsetFromContentType(Buffer value) |
294 { | 294 { |
295 if (value instanceof CachedBuffer) | 295 if (value instanceof CachedBuffer) |
296 { | 296 { |
297 switch(((CachedBuffer)value).getOrdinal()) | 297 switch(((CachedBuffer)value).getOrdinal()) |
298 { | 298 { |
299 case TEXT_HTML_8859_1_ORDINAL: | 299 case TEXT_HTML_8859_1_ORDINAL: |
300 case TEXT_PLAIN_8859_1_ORDINAL: | 300 case TEXT_PLAIN_8859_1_ORDINAL: |
301 case TEXT_XML_8859_1_ORDINAL: | 301 case TEXT_XML_8859_1_ORDINAL: |
302 return StringUtil.__ISO_8859_1; | 302 return StringUtil.__ISO_8859_1; |
303 | 303 |
304 case TEXT_HTML_UTF_8_ORDINAL: | 304 case TEXT_HTML_UTF_8_ORDINAL: |
305 case TEXT_PLAIN_UTF_8_ORDINAL: | 305 case TEXT_PLAIN_UTF_8_ORDINAL: |
306 case TEXT_XML_UTF_8_ORDINAL: | 306 case TEXT_XML_UTF_8_ORDINAL: |
307 case TEXT_JSON_UTF_8_ORDINAL: | 307 case TEXT_JSON_UTF_8_ORDINAL: |
308 return StringUtil.__UTF8; | 308 return StringUtil.__UTF8; |
309 } | 309 } |
310 } | 310 } |
311 | 311 |
312 int i=value.getIndex(); | 312 int i=value.getIndex(); |
313 int end=value.putIndex(); | 313 int end=value.putIndex(); |
314 int state=0; | 314 int state=0; |
315 int start=0; | 315 int start=0; |
316 boolean quote=false; | 316 boolean quote=false; |
317 for (;i<end;i++) | 317 for (;i<end;i++) |
318 { | 318 { |
319 byte b = value.peek(i); | 319 byte b = value.peek(i); |
320 | 320 |
321 if (quote && state!=10) | 321 if (quote && state!=10) |
322 { | 322 { |
323 if ('"'==b) | 323 if ('"'==b) |
324 quote=false; | 324 quote=false; |
325 continue; | 325 continue; |
326 } | 326 } |
327 | 327 |
328 switch(state) | 328 switch(state) |
329 { | 329 { |
330 case 0: | 330 case 0: |
331 if ('"'==b) | 331 if ('"'==b) |
332 { | 332 { |
333 quote=true; | 333 quote=true; |
334 break; | 334 break; |
335 } | 335 } |
336 if (';'==b) | 336 if (';'==b) |
337 state=1; | 337 state=1; |
338 break; | 338 break; |
339 | 339 |
340 case 1: if ('c'==b) state=2; else if (' '!=b) state=0; break; | 340 case 1: if ('c'==b) state=2; else if (' '!=b) state=0; break; |
341 case 2: if ('h'==b) state=3; else state=0;break; | 341 case 2: if ('h'==b) state=3; else state=0;break; |
342 case 3: if ('a'==b) state=4; else state=0;break; | 342 case 3: if ('a'==b) state=4; else state=0;break; |
343 case 4: if ('r'==b) state=5; else state=0;break; | 343 case 4: if ('r'==b) state=5; else state=0;break; |
344 case 5: if ('s'==b) state=6; else state=0;break; | 344 case 5: if ('s'==b) state=6; else state=0;break; |
345 case 6: if ('e'==b) state=7; else state=0;break; | 345 case 6: if ('e'==b) state=7; else state=0;break; |
346 case 7: if ('t'==b) state=8; else state=0;break; | 346 case 7: if ('t'==b) state=8; else state=0;break; |
347 | 347 |
348 case 8: if ('='==b) state=9; else if (' '!=b) state=0; break; | 348 case 8: if ('='==b) state=9; else if (' '!=b) state=0; break; |
349 | 349 |
350 case 9: | 350 case 9: |
351 if (' '==b) | 351 if (' '==b) |
352 break; | 352 break; |
353 if ('"'==b) | 353 if ('"'==b) |
354 { | 354 { |
355 quote=true; | 355 quote=true; |
356 start=i+1; | 356 start=i+1; |
357 state=10; | 357 state=10; |
358 break; | 358 break; |
359 } | 359 } |
360 start=i; | 360 start=i; |
361 state=10; | 361 state=10; |
362 break; | 362 break; |
363 | 363 |
364 case 10: | 364 case 10: |
365 if (!quote && (';'==b || ' '==b )|| | 365 if (!quote && (';'==b || ' '==b )|| |
366 (quote && '"'==b )) | 366 (quote && '"'==b )) |
367 return CACHE.lookup(value.peek(start,i-start)).toString(StringUtil.__UTF8); | 367 return CACHE.lookup(value.peek(start,i-start)).toString(StringUtil.__UTF8); |
368 } | 368 } |
369 } | 369 } |
370 | 370 |
371 if (state==10) | 371 if (state==10) |
372 return CACHE.lookup(value.peek(start,i-start)).toString(StringUtil.__UTF8); | 372 return CACHE.lookup(value.peek(start,i-start)).toString(StringUtil.__UTF8); |
373 | 373 |
374 return (String)__encodings.get(value); | 374 return (String)__encodings.get(value); |
375 } | 375 } |
376 } | 376 } |