001: package com.sun.portal.rewriter.util.uri;
002:
003: import com.sun.portal.rewriter.util.StringHelper;
004:
005: import java.util.HashMap;
006: import java.util.Iterator;
007: import java.util.Map;
008: import java.util.StringTokenizer;
009:
010: public class URIHelper {
011: // The names of all the protocol strings usable in URLs
012: public static final String COFFEE_SCHEME = "coffee";
013: public static final String DATA_SCHEME = "data";
014: public static final String FILE_SCHEME = "file";
015: public static final String FTP_SCHEME = "ftp";
016: public static final String GOPHER_SCHEME = "gopher";
017: public static final String HNEWS_SCHEME = "hnews";
018: public static final String HTTP_SCHEME = "http";
019: public static final String HTTP_NG_SCHEME = "http-ng";
020: public static final String HTTPS_SCHEME = "https";
021: public static final String IMAP_SCHEME = "imap";
022: public static final String IRC_SCHEME = "irc";
023: public static final String JAR_SCHEME = "jar";
024: public static final String LDAP_SCHEME = "ldap";
025: public static final String MAILTO_SCHEME = "mailto:";
026: public static final String NEWS_SCHEME = "news";
027: public static final String NFS_SCHEME = "nfs";
028: public static final String NNTP_SCHEME = "nntp";
029: public static final String POP_SCHEME = "pop";
030: public static final String PROSPERO_SCHEME = "prospero";
031: public static final String RWHOIS_SCHEME = "rwhois";
032: public static final String SHTTP_SCHEME = "shttp";
033: public static final String SMTP_SCHEME = "smtp";
034: public static final String SNEWS_SCHEME = "snews";
035: public static final String TELNET_SCHEME = "telnet";
036: public static final String URN_SCHEME = "urn";
037: public static final String VEMMI_SCHEME = "vemmi";
038: public static final String VIDEOTEX_SCHEME = "videotex";
039: public static final String WAIS_SCHEME = "wais";
040: public static final String WHOIS_SCHEME = "whois";
041: public static final String WHOIS_PLUS_SCHEME = "whois++";
042: public static final String Z3950R_SCHEME = "z39.50r";
043: public static final String Z3950S_SCHEME = "z39.50s";
044:
045: // The default port numbers
046: public static final int COFFEE_PORT = 80;
047: public static final int FTP_PORT = 21;
048: public static final int GOPHER_PORT = 70;
049: public static final int HNEWS_PORT = 80;
050: public static final int HTTP_PORT = 80;
051: public static final int HTTPS_PORT = 443;
052: public static final int HTTP_NG_PORT = 80;
053: public static final int IMAP_PORT = 143;
054: public static final int IRC_PORT = 194;
055: public static final int LDAP_PORT = 389;
056: public static final int NNTP_PORT = 119;
057: public static final int NEWS_PORT = 119;
058: public static final int NFS_PORT = 2049;
059: public static final int POP_PORT = 110;
060: public static final int PROSPERO_PORT = 1525;
061: public static final int RWHOIS_PORT = 4321;
062: public static final int SHTTP_PORT = 80;
063: public static final int SMTP_PORT = 25;
064: public static final int SNEWS_PORT = 563;
065: public static final int TELNET_PORT = 23;
066: public static final int VEMMI_PORT = 575;
067: public static final int VIDEOTEX_PORT = 516;
068: public static final int WAIS_PORT = 210;
069: public static final int WHOIS_PORT = 43;
070: public static final int WHOIS_PLUS_PORT = 63;
071: public static final int Z3950R_PORT = 210;
072: public static final int Z3950S_PORT = 210;
073:
074: private static final HashMap protocolPorts;
075:
076: /**
077: * Static initialiser that is used to create the protocol/port lookup
078: * table.
079: */
080: static {
081: protocolPorts = new HashMap(30);
082:
083: protocolPorts.put(HTTP_SCHEME, new Integer(HTTP_PORT));
084: protocolPorts.put(SHTTP_SCHEME, new Integer(SHTTP_PORT));
085: protocolPorts.put(HTTPS_SCHEME, new Integer(HTTPS_PORT));
086: protocolPorts.put(HTTP_NG_SCHEME, new Integer(HTTP_NG_PORT));
087: protocolPorts.put(COFFEE_SCHEME, new Integer(COFFEE_PORT));
088: protocolPorts.put(FTP_SCHEME, new Integer(FTP_PORT));
089: protocolPorts.put(TELNET_SCHEME, new Integer(TELNET_PORT));
090: protocolPorts.put(NNTP_SCHEME, new Integer(NNTP_PORT));
091: protocolPorts.put(NEWS_SCHEME, new Integer(NEWS_PORT));
092: protocolPorts.put(SNEWS_SCHEME, new Integer(SNEWS_PORT));
093: protocolPorts.put(HNEWS_SCHEME, new Integer(HNEWS_PORT));
094: protocolPorts.put(SMTP_SCHEME, new Integer(SMTP_PORT));
095: protocolPorts.put(GOPHER_SCHEME, new Integer(GOPHER_PORT));
096: protocolPorts.put(WAIS_SCHEME, new Integer(WAIS_PORT));
097: protocolPorts.put(WHOIS_SCHEME, new Integer(WHOIS_PORT));
098: protocolPorts.put(WHOIS_PLUS_SCHEME, new Integer(
099: WHOIS_PLUS_PORT));
100: protocolPorts.put(RWHOIS_SCHEME, new Integer(RWHOIS_PORT));
101: protocolPorts.put(IMAP_SCHEME, new Integer(IMAP_PORT));
102: protocolPorts.put(POP_SCHEME, new Integer(POP_PORT));
103: protocolPorts.put(PROSPERO_SCHEME, new Integer(PROSPERO_PORT));
104: protocolPorts.put(IRC_SCHEME, new Integer(IRC_PORT));
105: protocolPorts.put(LDAP_SCHEME, new Integer(LDAP_PORT));
106: protocolPorts.put(Z3950R_SCHEME, new Integer(Z3950R_PORT));
107: protocolPorts.put(Z3950S_SCHEME, new Integer(Z3950S_PORT));
108: protocolPorts.put(VEMMI_SCHEME, new Integer(VEMMI_PORT));
109: protocolPorts.put(VIDEOTEX_SCHEME, new Integer(VIDEOTEX_PORT));
110: protocolPorts.put(NFS_SCHEME, new Integer(NFS_PORT));
111: }//static block
112:
113: public static int getDefaultPort(final String aProtocol) {
114: int result = 0;
115: final Integer val = (Integer) protocolPorts.get(aProtocol
116: .toLowerCase());
117:
118: if (val != null) {
119: result = val.intValue();
120: }
121:
122: return result;
123: }//getDefaultPort()
124:
125: /**
126: * Similar Method present in Java Servlet API - HttpUtils class
127: */
128: public static Map parseQueryString(final String aQuery) {
129: final Map hashMap = new HashMap();
130: if (StringHelper.normalize(aQuery).length() == 0) {
131: return hashMap;
132: }
133:
134: StringBuffer stringbuffer = new StringBuffer(aQuery.length());
135: String lParamValueArray[];
136: String lParamName;
137:
138: StringTokenizer stringtokenizer = new StringTokenizer(aQuery,
139: "&");
140: while (stringtokenizer.hasMoreTokens()) {
141: String bToken = stringtokenizer.nextToken();
142: int i = bToken.indexOf('=');
143: if (i == -1) {
144: //TODO:This fix just ignoring the invalid params
145: //This could create a problem if one is trying to recreate the
146: //query string from the map..(as on today we are not using this way
147: //so just fixing it like this)
148: continue;//BugNo:4809850
149: //throw new IllegalArgumentException();
150: }
151:
152: lParamName = parseName(bToken.substring(0, i), stringbuffer);
153: String bParamValue = parseName(bToken.substring(i + 1,
154: bToken.length()), stringbuffer);
155: if (hashMap.containsKey(lParamName)) {
156: String as1[] = (String[]) hashMap.get(lParamName);
157: lParamValueArray = new String[as1.length + 1];
158: for (int j = 0; j < as1.length; j++) {
159: lParamValueArray[j] = as1[j];
160: }
161:
162: lParamValueArray[as1.length] = bParamValue;
163: } else {
164: lParamValueArray = new String[1];
165: lParamValueArray[0] = bParamValue;
166: }
167:
168: hashMap.put(lParamName, lParamValueArray);
169: }//while loop
170:
171: return hashMap;
172: }//parseQueryString()
173:
174: private static String parseName(final String aContent,
175: final StringBuffer aBuffer) {
176: aBuffer.setLength(0);
177: for (int i = 0; i < aContent.length(); i++) {
178: char c = aContent.charAt(i);
179: switch (c) {
180: case 43: // '+'
181: {
182: aBuffer.append(' ');
183: break;
184: }
185:
186: case 37: // '%'
187: {
188: try {
189: aBuffer.append((char) Integer.parseInt(aContent
190: .substring(i + 1, i + 3), 16));
191: i += 2;
192: break;
193: } catch (NumberFormatException _ex) {
194: throw new IllegalArgumentException();
195: } catch (StringIndexOutOfBoundsException _ex) {
196: String s1 = aContent.substring(i);
197: aBuffer.append(s1);
198: if (s1.length() == 2) {
199: i++;
200: }
201: }
202: break;
203: }
204:
205: default: {
206: aBuffer.append(c);
207: break;
208: }
209: }//switch/case
210: }//for loop
211:
212: return aBuffer.toString();
213: }//parseName()
214:
215: static String deparameterize(final String uri, final Map parameters) {
216: final int i = uri.lastIndexOf('?');
217: if (i == -1) {
218: return uri;
219: }
220:
221: String[] params = StringHelper.tokenize(uri.substring(i + 1),
222: "&");
223: for (int j = 0; j < params.length; j++) {
224: String p = params[j];
225: int k = p.indexOf('=');
226: if (k == -1) {
227: break;
228: }
229:
230: String name = p.substring(0, k);
231: String value = p.substring(k + 1);
232: parameters.put(name, value);
233: }
234: return uri.substring(0, i);
235: }//deparameterize()
236:
237: public static String parameterize(String aURI, final Map aParams) {
238: if (aURI == null || aURI.length() == 0 || aParams == null
239: || aParams.size() == 0) {
240: return aURI;
241: }
242: aURI = aURI.trim();
243: StringBuffer buffer = new StringBuffer(aURI);
244: int i = aURI.lastIndexOf('?');
245: if (i == -1) {
246: buffer.append('?');
247: } else {
248: buffer.append('&');
249: }
250:
251: for (Iterator iterator = aParams.entrySet().iterator(); iterator
252: .hasNext();) {
253: Map.Entry entry = (Map.Entry) iterator.next();
254: buffer.append(entry.getKey());
255: buffer.append('=');
256: buffer.append(entry.getValue());
257: if (iterator.hasNext()) {
258: buffer.append('&');
259: }
260: }
261:
262: return buffer.toString();
263: }//parameterize()
264:
265: public static boolean needsToAppendDirectorySeperator(
266: final String aString) {
267: if (aString == null) {
268: return false;
269: }
270:
271: boolean mayBeDirectory = false;
272:
273: if (aString.endsWith(".")) {
274: mayBeDirectory = true;
275:
276: char c;
277: for (int i = aString.length() - 2; i >= 0; i--) {
278: c = aString.charAt(i);
279:
280: if (c == '/') {
281: break;
282: }
283:
284: if (c != '.') {
285: mayBeDirectory = false;
286: break;
287: }
288: }
289: }
290:
291: return mayBeDirectory;
292: }//needsToAppendDirectorySeperator()
293: }//class URIHelper
|