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