001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.transport.http.client;
038:
039: import java.net.URL;
040: import java.util.Date;
041: import java.util.StringTokenizer;
042:
043: /**
044: * An object which represents an HTTP cookie. Can be constructed by
045: * parsing a string from the set-cookie: header.
046: *
047: * Syntax: Set-Cookie: NAME=VALUE; expires=DATE;
048: * path=PATH; domain=DOMAIN_NAME; secure
049: *
050: * All but the first field are optional.
051: *
052: * @author WS Development Team
053: */
054: public class HttpCookie {
055:
056: private Date expirationDate = null;
057: private String nameAndValue;
058: private String path;
059: private String domain;
060: private boolean isSecure = false;
061:
062: public HttpCookie(String cookieString) {
063: parseCookieString(cookieString);
064: }
065:
066: //
067: // Constructor for use by the bean
068: //
069: public HttpCookie(Date expirationDate, String nameAndValue,
070: String path, String domain, boolean isSecure) {
071:
072: this .expirationDate = expirationDate;
073: this .nameAndValue = nameAndValue;
074: this .path = path;
075: this .domain = stripPort(domain);
076: this .isSecure = isSecure;
077: }
078:
079: public HttpCookie(URL url, String cookieString) {
080: parseCookieString(cookieString);
081: applyDefaults(url);
082: }
083:
084: /**
085: * Fills in default values for domain, path, etc. from the URL
086: * after creation of the cookie.
087: */
088: private void applyDefaults(URL url) {
089:
090: if (domain == null) {
091: domain = url.getHost();
092:
093: // REMIND: record the port
094: }
095:
096: if (path == null) {
097: path = url.getFile();
098:
099: // The documentation for cookies say that the path is
100: // by default, the path of the document, not the filename of the
101: // document. This could be read as not including that document
102: // name itself, just its path (this is how NetScape inteprets it)
103: // so amputate the document name!
104: int last = path.lastIndexOf("/");
105:
106: if (last > -1) {
107: path = path.substring(0, last);
108: }
109: }
110: }
111:
112: private String stripPort(String domainName) {
113:
114: int index = domainName.indexOf(':');
115:
116: if (index == -1) {
117: return domainName;
118: }
119:
120: return domainName.substring(0, index);
121: }
122:
123: /**
124: * Parse the given string into its individual components, recording them
125: * in the member variables of this object.
126: */
127: private void parseCookieString(String cookieString) {
128:
129: StringTokenizer tokens = new StringTokenizer(cookieString, ";");
130:
131: if (!tokens.hasMoreTokens()) {
132:
133: // REMIND: make this robust against parse errors
134: }
135:
136: nameAndValue = tokens.nextToken().trim();
137:
138: while (tokens.hasMoreTokens()) {
139: String token = tokens.nextToken().trim();
140:
141: if (token.equalsIgnoreCase("secure")) {
142: isSecure = true;
143: } else {
144: int equIndex = token.indexOf("=");
145:
146: if (equIndex < 0) {
147: continue;
148:
149: // REMIND: malformed cookie
150: }
151:
152: String attr = token.substring(0, equIndex);
153: String val = token.substring(equIndex + 1);
154:
155: if (attr.equalsIgnoreCase("path")) {
156: path = val;
157: } else if (attr.equalsIgnoreCase("domain")) {
158: if (val.indexOf(".") == 0) {
159:
160: // spec seems to allow for setting the domain in
161: // the form 'domain=.eng.sun.com'. We want to
162: // trim off the leading '.' so we can allow for
163: // both leading dot and non leading dot forms
164: // without duplicate storage.
165: domain = stripPort(val.substring(1));
166: } else {
167: domain = stripPort(val);
168: }
169: } else if (attr.equalsIgnoreCase("expires")) {
170: expirationDate = parseExpireDate(val);
171: } else {
172:
173: // unknown attribute -- do nothing
174: }
175: }
176: }
177: }
178:
179: //======================================================================
180: //
181: // Accessor functions
182: //
183: public String getNameValue() {
184: return nameAndValue;
185: }
186:
187: /**
188: * Returns just the name part of the cookie
189: */
190: public String getName() {
191:
192: int index = nameAndValue.indexOf("=");
193:
194: return nameAndValue.substring(0, index);
195: }
196:
197: /**
198: * Returns the domain of the cookie as it was presented
199: */
200: public String getDomain() {
201:
202: // REMIND: add port here if appropriate
203: return domain;
204: }
205:
206: public String getPath() {
207: return path;
208: }
209:
210: public Date getExpirationDate() {
211: return expirationDate;
212: }
213:
214: boolean hasExpired() {
215:
216: if (expirationDate == null) {
217: return false;
218: }
219:
220: return (expirationDate.getTime() <= System.currentTimeMillis());
221: }
222:
223: /**
224: * Returns true if the cookie has an expiration date (meaning it's
225: * persistent), and if the date nas not expired;
226: */
227: boolean isSaveable() {
228: return (expirationDate != null)
229: && (expirationDate.getTime() > System
230: .currentTimeMillis());
231: }
232:
233: public boolean isSecure() {
234: return isSecure;
235: }
236:
237: private Date parseExpireDate(String dateString) {
238:
239: // format is wdy, DD-Mon-yyyy HH:mm:ss GMT
240: RfcDateParser parser = new RfcDateParser(dateString);
241: Date theDate = parser.getDate();
242:
243: return theDate;
244: }
245:
246: public String toString() {
247:
248: String result = nameAndValue;
249:
250: if (expirationDate != null) {
251: result += "; expires=" + expirationDate;
252: }
253:
254: if (path != null) {
255: result += "; path=" + path;
256: }
257:
258: if (domain != null) {
259: result += "; domain=" + domain;
260: }
261:
262: if (isSecure) {
263: result += "; secure";
264: }
265:
266: return result;
267: }
268: }
|