001: package com.sun.portal.desktop.util;
002:
003: import java.util.Collections;
004: import java.util.Iterator;
005: import java.util.Set;
006: import java.util.Map;
007: import java.util.HashMap;
008: import java.util.StringTokenizer;
009:
010: /**
011: * This class is a utility class which does the parsing of
012: * the pathInfo from request into a map of maps. Each key is mapped
013: * into a map containing the key/value pairs.
014: * This class is mainly used by DesktopRequest.java and AuthlessSessionContext.java
015: * DesktopRequest.java uses this class by calling parse() method by passing in
016: * the pathinfo to parse the arguments encoded into the PathInfo.
017: * desktop arguments are encoded into the pathinfo using the key "desktop.args".
018: * AuthlessSessionContext.java uses this class by calling parse() method by passing in
019: * the pathinfo to parse the authless client properties encoded into the PathInfo.
020: * Client Properties will be encoded into the PathInfo using the key "desktop.authless".
021: * pathinfo passed into the parse method will be something like
022: * /desktop.args=<arg1=val1&arg2=val2...>/desktop.authless=<arg1=val1&arg2=val2...>
023: */
024:
025: public class PIParser {
026:
027: public static final String DESKTOP_ARGS = "desktop.args";
028: public static final String DESKTOP_AUTHLESS = "desktop.authless";
029: public static final String SEPARATOR = "$";
030:
031: public static Map parse(String piString) {
032: String pi = null;
033: if (piString != null && piString.length() > 1) {
034: // remove the leading "/"
035: pi = piString.substring(1);
036: } else {
037: return Collections.EMPTY_MAP;
038: }
039: int delimIndex = pi.indexOf(SEPARATOR);
040: Map pathInfoMap = Collections.synchronizedMap(new HashMap());
041: if (delimIndex != -1) {
042: StringTokenizer st = new StringTokenizer(pi, SEPARATOR);
043: while (st.hasMoreTokens()) {
044: String keyValue = st.nextToken();
045: int index = keyValue.indexOf("=");
046: if (index != -1) {
047: String key = keyValue.substring(0, index);
048: if (!pathInfoMap.containsKey(key)
049: && (key.equals(DESKTOP_ARGS) || key
050: .equals(DESKTOP_AUTHLESS))) {
051: String value = keyValue.substring(index + 1);
052: Map valueMap = decodeKeyValueString(value);
053: pathInfoMap.put(key, valueMap);
054: }
055: }
056: }
057: } else {
058: // single key
059: int index = pi.indexOf("=");
060: if (index != -1) {
061: String key = pi.substring(0, index);
062: if (key.equals(DESKTOP_ARGS)
063: || key.equals(DESKTOP_AUTHLESS)) {
064: String value = pi.substring(index + 1);
065: Map valueMap = decodeKeyValueString(value);
066: pathInfoMap.put(key, valueMap);
067: }
068: }
069: }
070: return pathInfoMap;
071: }
072:
073: public static String getPathInfoString(Map piMap) {
074: if (piMap == null) {
075: return null;
076: }
077: StringBuffer piBuffer;
078: synchronized (piMap) {
079: Set keys = piMap.keySet();
080: piBuffer = new StringBuffer();
081: for (Iterator i = keys.iterator(); i.hasNext();) {
082: String key = (String) i.next();
083: Map value = (Map) piMap.get(key);
084: String encodedPathInfoString = encodeKeyValueMap(value);
085: piBuffer.append(key).append("=").append(
086: encodedPathInfoString);
087: if (i.hasNext()) {
088: piBuffer.append(SEPARATOR);
089: }
090: }
091: }
092: return piBuffer.toString();
093: }
094:
095: public static Map decodeKeyValueString(String s) {
096: if (s == null) {
097: return null;
098: }
099:
100: Map cp = null;
101:
102: s = new String(Base64.decode(s.getBytes()));
103:
104: StringTokenizer t = new StringTokenizer(s, "&");
105: while (t.hasMoreTokens()) {
106: String p = t.nextToken();
107: Target property = new Target(p, '=', Target.INDEX_FIRST);
108:
109: if (property.getName() != null) {
110: if (cp == null) {
111: cp = Collections.synchronizedMap(new HashMap());
112: }
113: String name = property.getName();
114: String value = property.getValue();
115: //dac.debugError("DesktopRequest.decodeKeyvalueString(): decode name=" + name + ", value=" + newVal);
116: cp.put(name, value);
117: }
118: }
119:
120: return cp;
121: }
122:
123: public static String encodeKeyValueMap(Map pathInfo) {
124: if (pathInfo == null) {
125: return null;
126: }
127: StringBuffer piBuffer = new StringBuffer();
128: synchronized (pathInfo) {
129: // build pathInfo string
130: for (Iterator i = pathInfo.keySet().iterator(); i.hasNext();) {
131: String key = (String) i.next();
132: String value = (String) pathInfo.get(key);
133: if (value != null) {
134: piBuffer.append(key).append("=").append(value);
135: if (i.hasNext()) {
136: piBuffer.append("&");
137: }
138: }
139: }
140: }
141: // get Base64 encoded string
142: String encodedPathInfoString = Base64.encode(piBuffer
143: .toString());
144: return encodedPathInfoString;
145:
146: }
147:
148: }
|