001: package com.sun.portal.proxylet.client.common.server;
002:
003: import java.util.StringTokenizer;
004: import java.util.Iterator;
005: import java.util.Vector;
006: import java.util.Hashtable;
007: import java.util.Enumeration;
008:
009: /**
010: * Cookie
011: *
012: * Class to represent a cookie.
013: */
014:
015: public class Cookie {
016: /**
017: * Constant that indicate the format of the input string
018: */
019: public static String SET_COOKIE_FORMAT = "SET_COOKIE_FORMAT";
020:
021: /**
022: * Constant that indicate the format of the input string
023: */
024: public static String APPLETPARAM_COOKIE_FORMAT = "APPLETPARAM_COOKIE_FORMAT";
025:
026: /**
027: * name of the cookie
028: */
029: private String name;
030:
031: /**
032: * encoded information of the cookie
033: */
034: private String value;
035:
036: /**
037: * Domain the cookie is set for
038: */
039: private String domain;
040:
041: /**
042: * Path the cookie is set for
043: */
044: private String pathPattern;
045:
046: /**
047: * Original cookie string
048: */
049: private String rawCookie;
050:
051: /**
052: * Contains the list of cookies
053: */
054: private Hashtable cookieList = new Hashtable();
055:
056: /**
057: * Pattern of the cookie
058: */
059: private String pattern;
060:
061: /**
062: * Constructs an empty cookie object
063: */
064: public Cookie() {
065: }
066:
067: public Cookie(String cookie, String pattern) {
068: rawCookie = cookie;
069: this .pattern = pattern;
070: format();
071: }
072:
073: public void format() {
074: if (rawCookie == null)
075: return;
076:
077: if (pattern.equals(SET_COOKIE_FORMAT)) {
078: parseCookie();
079: } else if (pattern.equals(APPLETPARAM_COOKIE_FORMAT)) {
080: parseMulCookie();
081: }
082: }
083:
084: private void parseMulCookie() {
085: // Cookie is of the format name=value;name=value;...
086: StringTokenizer st = new StringTokenizer(rawCookie, ";");
087: String elem = null, val = null;
088: while (st.hasMoreTokens()) {
089: String nvpair = (String) st.nextToken();
090: int index = nvpair.indexOf('=');
091: if (index != -1) {
092: elem = nvpair.substring(0, index);
093: val = nvpair.substring(index + 1, nvpair.length());
094: cookieList.put(elem, val);
095: }
096: }
097: }
098:
099: private void parseCookie() {
100: // Cookie is of the format name=value;path=value;domain=value;
101: StringTokenizer st = new StringTokenizer(rawCookie, ";");
102: while (st.hasMoreTokens()) {
103: String nvpair = (String) st.nextToken();
104: int index = nvpair.indexOf('=');
105: String elem = nvpair.substring(0, index);
106: String val = nvpair.substring(index + 1, nvpair.length());
107:
108: if (elem.equals("Domain"))
109: domain = val;
110: else if (elem.equals("Path"))
111: pathPattern = val;
112: else {
113: name = elem;
114: value = val;
115: }
116: }
117: }
118:
119: public String getCookieHeaderField() {
120: return (getName() + "=" + getValue());
121: }
122:
123: public boolean mayBeSent(String host, String path) {
124:
125: if (getDomain() != null
126: && (host.equalsIgnoreCase(getDomain()) || host
127: .endsWith(getDomain()))) {
128: // least match
129: if (pathPattern == null)
130: return true;
131: if (path.startsWith(pathPattern)) {
132: return true;
133: }
134: }
135: return false;
136: }
137:
138: public static Cookie getCookieByName(String cookieName,
139: Vector cookieMulLine) {
140: String ctemp = "";
141: Cookie ccookie = null;
142: Cookie retcookie = null;
143:
144: if (cookieMulLine != null && !cookieMulLine.isEmpty()) {
145: Iterator icookieMulLine = cookieMulLine.iterator();
146: while (icookieMulLine.hasNext()) {
147: String cookieLine = (String) icookieMulLine.next();
148: ccookie = new Cookie(cookieLine, SET_COOKIE_FORMAT);
149: if (ccookie.getName().equals(cookieName)) {
150: retcookie = ccookie;
151: break;
152: }
153: }
154: }
155: return retcookie;
156:
157: }
158:
159: public String getDomain() {
160: return domain;
161: }
162:
163: public String getPath() {
164: return pathPattern;
165: }
166:
167: public String getName() {
168: if (pattern.equals(SET_COOKIE_FORMAT))
169: return name;
170: else {
171: return "iPlanetDirectoryPro";
172: }
173: }
174:
175: public String getValue() {
176: if (pattern.equals(SET_COOKIE_FORMAT))
177: return value;
178: else {
179: Enumeration vals = cookieList.elements();
180: String value = null;
181: for (Enumeration e = cookieList.keys(); e.hasMoreElements();) {
182: String temp = ((String) e.nextElement()).trim();
183: value = ((String) vals.nextElement()).trim();
184: if (temp.equals("iPlanetDirectoryPro")) {
185: break;
186: }
187:
188: }
189:
190: return value;
191: }
192: }
193:
194: public String getOriginalCookie() {
195: return rawCookie;
196: }
197:
198: }
|