001: /*
002: * Copyright 2003-2006 Rick Knowles <winstone-devel at lists sourceforge net>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: */
007: package javax.servlet.http;
008:
009: /**
010: * Cookie model value object
011: *
012: * @author <a href="mailto:rick_knowles@hotmail.com">Rick Knowles</a>
013: */
014: public class Cookie implements Cloneable {
015: private String name;
016: private String value;
017: private String comment;
018: private String domain;
019: private String path;
020: private boolean secure;
021: private int maxAge;
022: private int version;
023:
024: public Cookie(String name, String value) {
025: setName(name);
026: setValue(value);
027: setMaxAge(-1);
028: }
029:
030: public Object clone() {
031: Cookie clone = new Cookie(this .name, this .value);
032: clone.setComment(this .comment);
033: clone.setDomain(this .domain);
034: clone.setMaxAge(this .maxAge);
035: clone.setSecure(this .secure);
036: clone.setVersion(this .version);
037: clone.setPath(this .path);
038: return clone;
039: }
040:
041: public String getComment() {
042: return this .comment;
043: }
044:
045: public String getDomain() {
046: return this .domain;
047: }
048:
049: public int getMaxAge() {
050: return this .maxAge;
051: }
052:
053: public String getName() {
054: return this .name;
055: }
056:
057: public String getPath() {
058: return this .path;
059: }
060:
061: public boolean getSecure() {
062: return this .secure;
063: }
064:
065: public String getValue() {
066: return this .value;
067: }
068:
069: public int getVersion() {
070: return this .version;
071: }
072:
073: private void setName(String name) {
074: if (name == null) {
075: throw new IllegalArgumentException("Cookie name was null");
076: } else if (name.indexOf(";") != -1) {
077: throw new IllegalArgumentException(
078: "Cookie name contains a semicolon");
079: } else if (name.indexOf(",") != -1) {
080: throw new IllegalArgumentException(
081: "Cookie name contains a comma");
082: } else if (name.startsWith("$")) {
083: throw new IllegalArgumentException(
084: "Cookie name starts with $");
085: } else {
086: // Check for white space, comma, semicolon
087: for (int n = 0; n < name.length(); n++) {
088: char c = name.charAt(n);
089: if (c <= 0x20 || c >= 0x7f) {
090: throw new IllegalArgumentException(
091: "Cookie name contains whitespace or "
092: + "non-alphanumeric char: "
093: + name.charAt(n) + " in " + name);
094: }
095: }
096: this .name = name;
097: }
098: }
099:
100: public void setComment(String purpose) {
101: this .comment = purpose;
102: }
103:
104: public void setDomain(String pattern) {
105: this .domain = pattern;
106: }
107:
108: public void setMaxAge(int expiry) {
109: this .maxAge = expiry;
110: }
111:
112: public void setPath(String uri) {
113: this .path = uri;
114: }
115:
116: public void setSecure(boolean flag) {
117: this .secure = flag;
118: }
119:
120: public void setValue(String newValue) {
121: this .value = newValue;
122: }
123:
124: public void setVersion(int v) {
125: this .version = v;
126: }
127:
128: public String toString() {
129: return "[Cookie: name=" + this .name + " value=" + this .value
130: + " version=" + this .version + " path=" + this .path
131: + " domain=" + this .domain + " comment=" + this .comment
132: + " maxAge=" + this .maxAge + " secure=" + this .secure
133: + "]";
134: }
135: }
|