01: /*
02: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
03: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
04: */
05: package com.sun.portal.rewriter.util.i18n;
06:
07: import com.sun.portal.rewriter.util.ConfigManager;
08: import com.sun.portal.rewriter.util.collections.ListMap;
09:
10: import java.net.URLConnection;
11: import java.util.Map;
12:
13: public final class MIMEHelper {
14: public static final String UNKNOWN_MIME = "content/unknown";
15: private static final Map extensionMAP = new ListMap();
16:
17: static {
18: extensionMAP.put("", UNKNOWN_MIME);
19: }//static block
20:
21: public static void init(String aResourceLocation) {
22: if (aResourceLocation != null) {
23: extensionMAP.putAll(ConfigManager
24: .readProps(aResourceLocation));
25: }
26: }//init()
27:
28: /**
29: * A useful utility routine that tries to guess the content-type
30: * of an object based upon its extension.
31: */
32: private static String guessContentTypeFromURLString(String fname) {
33: String ext = "";
34: int i = fname.lastIndexOf('#');
35:
36: if (i != -1) {
37: fname = fname.substring(0, i - 1);
38: }
39:
40: i = fname.lastIndexOf('.');
41: i = Math.max(i, fname.lastIndexOf('/'));
42: i = Math.max(i, fname.lastIndexOf('?'));
43:
44: if (i != -1 && fname.charAt(i) == '.') {
45: if (++i < fname.length()) {
46: ext = fname.substring(i).toLowerCase();
47: }
48: }
49:
50: return guessContentTypeFromFileName("." + ext);
51: }//guessContentTypeFromURLString()
52:
53: public static String guessContentTypeFromFileName(final String fname) {
54: String result = URLConnection.getFileNameMap()
55: .getContentTypeFor(fname);
56:
57: String ext = "";
58: final int i = fname.lastIndexOf('.');
59:
60: if (i != -1) {
61: ext = fname.substring(i + 1).trim().toLowerCase();
62: }
63:
64: //standard java URLConnection returns wron MIME for xml extions
65: //it returns as application/xml rather than text/xml
66: if (result != null && !ext.equals("xml")) {
67: return result;
68: }
69:
70: result = (String) extensionMAP.get(ext);
71: if (result == null) {
72: result = (String) extensionMAP.get("");
73: }
74:
75: return result;
76: }//guessContentTypeFromFileName()
77: }//class MIMEHelper
|