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: public class CookieUtils {
026:
027: private static org.apache.commons.logging.Log _log = LogUtils
028: .getLog(CookieUtils.class);
029:
030: private CookieUtils() {
031: }
032:
033: /**
034: * Get a new (empty) CookieData list
035: * return An empty ArrayList
036: */
037: public static List newCookieList() {
038: return new ArrayList();
039: }
040:
041: /**
042: * Parse an HTTP cookie
043: * @param value Cookie value
044: * @return A CookieData object representing this cookie
045: */
046: public static CookieData parseCookie(URL url, String value) {
047: String[] cookieFields;
048: CookieData cookie;
049:
050: cookieFields = value.split(";\\s*");
051: cookie = makeCookieData(url, cookieFields[0]);
052:
053: for (int j = 1; j < cookieFields.length; j++) {
054: if ("secure".equalsIgnoreCase(cookieFields[j])) {
055: cookie.setSecure(true);
056: continue;
057: }
058:
059: if (cookieFields[j].indexOf('=') > 0) {
060: String[] field = cookieFields[j].split("=");
061:
062: if ("expires".equalsIgnoreCase(field[0])) {
063: cookie.setExpires(field[1]);
064: } else if ("domain".equalsIgnoreCase(field[0])) {
065: cookie.setDomain(field[1]);
066: } else if ("path".equalsIgnoreCase(field[0])) {
067: cookie.setPath(field[1]);
068: } else if ("version".equalsIgnoreCase(field[0])) {
069: cookie.setVersion(field[1]);
070: } else if ("max-age".equalsIgnoreCase(field[0])) {
071: cookie.setMaxAge(field[1]);
072: }
073: }
074: }
075: return cookie;
076: }
077:
078: /**
079: * Build a CookieData object from cookieName=cookieValue text
080: * @param url URL this cookie belongs to
081: * @param cookieText cookieName[=cookieValue] text
082: * @return A new CookieData object
083: */
084: private static CookieData makeCookieData(URL url, String cookieText) {
085:
086: for (int i = 0; i < cookieText.length(); i++) {
087:
088: if (cookieText.charAt(i) == '=') {
089: String name = cookieText.substring(0, i);
090: String value = "";
091:
092: if (i + 1 <= cookieText.length()) {
093: value = cookieText.substring(i + 1);
094: }
095: return new CookieData(url, name, value);
096: }
097: }
098: return new CookieData(url, cookieText, "");
099: }
100:
101: /**
102: * Maintain a list of CookieData objects (add, replace, or delete a cookie)
103: * @param cookieList CookieData list
104: * @param cookie A CookieData object
105: */
106: public static void storeCookie(List cookieList, CookieData cookie) {
107: int size = cookieList.size();
108:
109: for (int i = 0; i < size; i++) {
110: CookieData cd = (CookieData) cookieList.get(i);
111:
112: if (cookie.equals(cd)) {
113: if (cookie.getMaxAge() == 0) {
114: cookieList.remove(i);
115: return;
116: }
117: cookieList.set(i, cookie);
118: return;
119: }
120: }
121: if (cookie.getMaxAge() != 0) {
122: cookieList.add(cookie);
123: }
124: }
125:
126: /**
127: * Does the cookie domain match the URL?
128: * @param urlString URL String to match
129: * @param cookie CookieData object (the cookie)
130: * @return true if the cookie domain matches the URL
131: */
132: public static boolean inDomain(String urlString, CookieData cookie) {
133: URL url;
134:
135: try {
136: url = new URL(urlString);
137: } catch (MalformedURLException exception) {
138: return false;
139: }
140: return inDomain(url, cookie);
141: }
142:
143: /**
144: * Does the cookie domain match the URL?
145: * @param url URL to match
146: * @param cookie CookieData object (the cookie)
147: * @return true if the cookie domain matches the URL
148: */
149: public static boolean inDomain(URL url, CookieData cookie) {
150: String domain = cookie.getDomain();
151:
152: return url.getHost().toLowerCase().endsWith(
153: domain.toLowerCase());
154: }
155:
156: /**
157: * Does the cookie path match the URL "file"?
158: * @param urlString String URL to match
159: * @param cookie CookieData object (the cookie)
160: * @return true if the cookie domain matches the URL
161: */
162: public static boolean inPath(String urlString, CookieData cookie) {
163: URL url;
164:
165: try {
166: url = new URL(urlString);
167: } catch (MalformedURLException exception) {
168: return false;
169: }
170: return inPath(url, cookie);
171: }
172:
173: /**
174: * Does the cookie path match the URL "file"?
175: * @param url URL to match
176: * @param cookie CookieData object (the cookie)
177: * @return true if the cookie domain matches the URL
178: */
179: public static boolean inPath(URL url, CookieData cookie) {
180: return url.getFile().startsWith(cookie.getPath());
181: }
182:
183: /**
184: * Find all stored cookies which associated with this server
185: * @param cookieList List of stored cookies (CookieData objects)
186: * @param url URL representing the request to lookup (server)
187: * @return A List of associated cookies
188: */
189: public static List findCookiesForServer(List cookieList, URL url) {
190: Iterator iterator = cookieList.iterator();
191: ArrayList list = new ArrayList();
192:
193: while (iterator.hasNext()) {
194: CookieData cookie = (CookieData) iterator.next();
195:
196: if ((inDomain(url, cookie)) && (inPath(url, cookie))) {
197: list.add(cookie);
198: }
199: }
200: return list;
201: }
202:
203: /**
204: * Find cookies associated with this server (by name)
205: * @param cookieList List of stored cookies (CookieData objects)
206: * @param url URL representing the request to lookup (server)
207: * @param name Cookie name
208: * @param exact true for exact name match, false to match on name prefix
209: * @return A List of associated cookies
210: */
211: public static List getCookies(List cookieList, URL url,
212: String name, boolean exact) {
213:
214: List serverCookies = findCookiesForServer(cookieList, url);
215: Iterator iterator = serverCookies.iterator();
216: ArrayList list = new ArrayList();
217:
218: while (iterator.hasNext()) {
219: CookieData cookie = (CookieData) iterator.next();
220:
221: if (exact) {
222: if (cookie.getName().equals(name)) {
223: list.add(cookie);
224: }
225: continue;
226: }
227:
228: if (cookie.getName().startsWith(name)) {
229: list.add(cookie);
230: }
231: }
232: return list;
233: }
234:
235: /**
236: * Find cookies associated with this server (exact name match)
237: * @param cookieList List of stored cookies (CookieData objects)
238: * @param url URL representing the request to lookup (server)
239: * @param name Cookie name
240: * @return A List of associated cookies
241: */
242: public static List getCookiesByName(List cookieList, URL url,
243: String name) {
244: return getCookies(cookieList, url, name, true);
245: }
246:
247: /**
248: * Find cookies associated with this server (match on name "prefix")
249: * @param cookieList List of stored cookies (CookieData objects)
250: * @param url URL representing the request to lookup (server)
251: * @param name Cookie name
252: * @return A List of associated cookies
253: */
254: public static List getCookiesByPrefix(List cookieList, URL url,
255: String name) {
256: return getCookies(cookieList, url, name, false);
257: }
258: }
|