001: /**********************************************************************************
002: *
003: * Copyright (c) 2003, 2004 The Regents of the University of Michigan, Trustees of Indiana University,
004: * Board of Trustees of the Leland Stanford, Jr., University, and The MIT Corporation
005: *
006: * Licensed under the Educational Community License Version 1.0 (the "License");
007: * By obtaining, using and/or copying this Original Work, you agree that you have read,
008: * understand, and will comply with the terms and conditions of the Educational Community License.
009: * You may obtain a copy of the License at:
010: *
011: * http://cvs.sakaiproject.org/licenses/license_1_0.html
012: *
013: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
014: * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
015: * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
016: * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
017: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
018: *
019: **********************************************************************************/package edu.indiana.lib.twinpeaks.util;
020:
021: import java.io.*;
022: import java.net.*;
023: import java.util.*;
024:
025: /**
026: * Represent a single cookie
027: */
028: public class CookieData {
029:
030: private static org.apache.commons.logging.Log _log = LogUtils
031: .getLog(CookieData.class);
032: /**
033: * Null (unset) cookie age
034: */
035: public static final int NULL_AGE = -1;
036: /**
037: * Expired cookie
038: */
039: public static final int EXPIRED_AGE = 0;
040:
041: private String key;
042: private String value;
043:
044: private String path;
045: private String domain;
046: private String version;
047:
048: private String expires;
049: private int maxAge;
050: private boolean secure;
051:
052: private CookieData() {
053: }
054:
055: /**
056: * Constructor
057: * @param url URL associated with this cookie
058: * @param key Cookie name
059: * @param value Cookie value
060: */
061: public CookieData(URL url, String key, String value) {
062: int slash = url.getFile().lastIndexOf("/");
063: /*
064: * Save cookie name and content
065: */
066: this .key = key;
067: this .value = value;
068: /*
069: * Domain defaults to hostname, path to the "directory" portion of
070: * the request, minus all text from the rightmost "/" character to
071: * the end of the string...
072: */
073: this .path = slash < 1 ? "" : url.getFile().substring(0, slash);
074: this .domain = url.getHost();
075:
076: this .version = null;
077: this .expires = null;
078: this .maxAge = NULL_AGE;
079:
080: this .secure = false;
081: }
082:
083: /**
084: * Get cookie name
085: * @return The cooke name
086: */
087: public String getName() {
088: return this .key;
089: }
090:
091: /**
092: * Get cookie value (the cookie "text")
093: * @return The value
094: */
095: public String getValue() {
096: return this .value;
097: }
098:
099: /**
100: * Save the path
101: */
102: public void setPath(String path) {
103: this .path = path;
104: }
105:
106: /**
107: * Get the path
108: * @return The cooke path attribute value (null if none)
109: */
110: public String getPath() {
111: return this .path;
112: }
113:
114: /**
115: * Save the expiration date
116: */
117: public void setExpires(String expires) {
118: this .expires = expires;
119: }
120:
121: /**
122: * Get the expiration date
123: * @return The expires attribute value (null if none)
124: */
125: public String getExpires() {
126: return this .expires;
127: }
128:
129: /**
130: * Save the domain
131: */
132: public void setDomain(String domain) {
133: this .domain = domain;
134: }
135:
136: /**
137: * Get the domain
138: * @return The domain attribute value (null if none)
139: */
140: public String getDomain() {
141: return this .domain;
142: }
143:
144: /**
145: * Save the version
146: */
147: public void setVersion(String version) {
148: this .version = version;
149: }
150:
151: /**
152: * Get the cookie version
153: * @return The version (null if none)
154: */
155: public String getVersion() {
156: return this .version;
157: }
158:
159: /**
160: * Set the maximum age for this cookie
161: */
162: public void setMaxAge(String maxAge) {
163: try {
164: this .maxAge = Integer.parseInt(maxAge);
165: } catch (NumberFormatException ignore) {
166: }
167: }
168:
169: /**
170: * Get the maximum age for this cookie
171: */
172: public int getMaxAge() {
173: return this .maxAge;
174: }
175:
176: /**
177: * Save security setting (true if cookie to be sent only via HTTPS)
178: */
179: public void setSecure(boolean secure) {
180: this .secure = secure;
181: }
182:
183: public boolean getSecure() {
184: return this .secure;
185: }
186:
187: /**
188: * Equal strings?
189: * @param a String one
190: * @param b Stringtwo
191: * @return true if both are null, or String "equals" is true
192: */
193: private boolean stringEquals(String a, String b) {
194: if ((a == null) && (b == null)) {
195: return true;
196: }
197:
198: if ((a == null) || (b == null)) {
199: return false;
200: }
201:
202: return a.equals(b);
203: }
204:
205: /**
206: * Equal cookies?
207: * @param cookie for comparison
208: * @return true if cookie name, path, and domain are all equal
209: */
210: public boolean equals(CookieData cookie) {
211: if (!key.equals(cookie.getName())) {
212: return false;
213: }
214:
215: return stringEquals(path, cookie.getPath())
216: && stringEquals(domain, cookie.getDomain());
217: }
218: }
|