001: /*
002: * CoadunationLib: The coaduntion implementation library.
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * CookieWrapper.java
020: *
021: * This class is responsible for wrapping the access to the cookie information
022: * containied within, and for parsing cookie headers.
023: */
024:
025: // package path
026: package com.rift.coad.lib.httpd;
027:
028: // logging import
029: import org.apache.log4j.Logger;
030:
031: // java imports
032: import java.net.InetAddress;
033: import java.util.Date;
034: import java.util.Map;
035: import java.util.HashMap;
036: import java.util.StringTokenizer;
037: import java.util.TimeZone;
038: import java.text.SimpleDateFormat;
039:
040: // coadunation imports
041: import com.rift.coad.lib.configuration.Configuration;
042: import com.rift.coad.lib.configuration.ConfigurationFactory;
043:
044: /**
045: * This class is responsible for wrapping the access to the cookie information
046: * containied within, and for parsing cookie headers.
047: *
048: * @author Brett Chaldecott
049: */
050: public class CookieWrapper {
051:
052: // class constants
053: private final static String PATH = "Path";
054: private final static String DOMAIN = "Domain";
055: public final static String VERSION = "Version";
056: public final static String DOLLAR = "$";
057: private final static String VERSION_NUMBER = "1";
058: private final static String COOKIE_HOST = "cookie_host";
059:
060: // private member variables
061: private Logger log = Logger
062: .getLogger(CookieWrapper.class.getName());
063: private String name = null;
064: private String value = null;
065: private String domain = null;
066: private String path = null;
067:
068: /**
069: * The constructor of the class responsible for processing the raw source.
070: *
071: * @param rawSrouce The raw source to process.
072: * @exception HttpdException
073: */
074: public CookieWrapper(String rawSource) throws HttpdException {
075: // check for valid formatting of cookie
076: if (rawSource.contains("=") == false) {
077: throw new HttpdException("Incorrectly formated cookie ["
078: + rawSource + "]");
079: }
080: name = rawSource.substring(0, rawSource.indexOf('=')).trim()
081: .toLowerCase();
082: value = stripInvertedCommas(
083: rawSource.substring(rawSource.indexOf('=') + 1).trim())
084: .trim();
085: }
086:
087: /**
088: *
089: * Creates a new instance of CookieWrapper
090: *
091: *
092: * @param name The name associated with this cookie.
093: * @param value The value wrapped within.
094: * @exception HttpdException
095: */
096: public CookieWrapper(String name, String value)
097: throws HttpdException {
098: try {
099: Configuration config = ConfigurationFactory.getInstance()
100: .getConfig(CookieWrapper.class);
101: this .name = name.toLowerCase().trim();
102: this .value = value;
103: } catch (Exception ex) {
104: throw new HttpdException(
105: "Failed to init the cookie wrapper : "
106: + ex.getMessage(), ex);
107: }
108: }
109:
110: /**
111: * This method returns the name of this cookie.
112: *
113: * @return The string containing the name of this cookie.
114: */
115: public String getName() {
116: return name;
117: }
118:
119: /**
120: * This method returns the value contained within.
121: *
122: * @return The string containing the value of the cookie.
123: */
124: public String getValue() {
125: return value;
126: }
127:
128: /**
129: * This method sets the value of the cookie.
130: *
131: * @param value The value to store in the cookie.
132: */
133: public void setValue(String value) {
134: this .value = value;
135: }
136:
137: /**
138: * This method returns the domain name.
139: *
140: * @return The string containing the domain name.
141: */
142: public String getDomain() {
143: return domain;
144: }
145:
146: /**
147: * This method sets the domain key value.
148: *
149: * @param domain The new domain to set.
150: */
151: public void setDomain(String domain) {
152: this .domain = domain;
153: }
154:
155: /**
156: * This method returns the path under which the cookie will be available.
157: *
158: * @return The path the cookie can be used on.
159: */
160: public String getPath() {
161: return path;
162: }
163:
164: /**
165: * This method sets the path.
166: *
167: * @param path The path to the object.
168: */
169: public void setPath(String path) {
170: this .path = path;
171: }
172:
173: /**
174: * This method generates a cookie string that can be used by Set-Cookie.
175: *
176: * @return A string that can be used as a Set cookie header.
177: */
178: public String getSetCookieString() {
179: StringBuffer cookieBuffer = new StringBuffer();
180: cookieBuffer.append(name).append("=").append(value).append(";")
181: .append(VERSION).append("=").append(VERSION_NUMBER);
182: if (domain != null) {
183: cookieBuffer.append(";").append(DOMAIN).append("=").append(
184: domain);
185: }
186: if (path != null) {
187: cookieBuffer.append(";").append(PATH).append("=").append(
188: path);
189: }
190: return cookieBuffer.toString();
191: }
192:
193: /**
194: * This method strips the inverted commas off a string.
195: *
196: * @return The stripped string.
197: * @param value The string to strip the values off of.
198: */
199: private String stripInvertedCommas(String value) {
200: int beginPos = value.indexOf('"');
201: if (beginPos == -1) {
202: return value;
203: }
204: int endPos = value.indexOf('"', beginPos);
205: return value.substring(beginPos, endPos);
206: }
207: }
|