001: /*
002: * Copyright 2005-2007 Noelios Consulting.
003: *
004: * The contents of this file are subject to the terms of the Common Development
005: * and Distribution License (the "License"). You may not use this file except in
006: * compliance with the License.
007: *
008: * You can obtain a copy of the license at
009: * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
010: * language governing permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL HEADER in each file and
013: * include the License file at http://www.opensource.org/licenses/cddl1.txt If
014: * applicable, add the following below this CDDL HEADER, with the fields
015: * enclosed by brackets "[]" replaced with your own identifying information:
016: * Portions Copyright [yyyy] [name of copyright owner]
017: */
018:
019: package org.restlet.data;
020:
021: import org.restlet.util.Engine;
022:
023: /**
024: * Cookie provided by a client.
025: *
026: * @author Jerome Louvel (contact@noelios.com)
027: */
028: public class Cookie extends Parameter {
029: /** The version number. */
030: private int version;
031:
032: /** The validity path. */
033: private String path;
034:
035: /** The domain name. */
036: private String domain;
037:
038: /**
039: * Constructor.
040: */
041: public Cookie() {
042: this (0, null, null, null, null);
043: }
044:
045: /**
046: * Constructor.
047: *
048: * @param version
049: * The version number.
050: * @param name
051: * The name.
052: * @param value
053: * The value.
054: */
055: public Cookie(int version, String name, String value) {
056: this (version, name, value, null, null);
057: }
058:
059: /**
060: * Constructor.
061: *
062: * @param version
063: * The version number.
064: * @param name
065: * The name.
066: * @param value
067: * The value.
068: * @param path
069: * The validity path.
070: * @param domain
071: * The domain name.
072: */
073: public Cookie(int version, String name, String value, String path,
074: String domain) {
075: super (name, value);
076: this .version = version;
077: this .path = path;
078: this .domain = domain;
079: }
080:
081: /**
082: * Constructor.
083: *
084: * @param name
085: * The name.
086: * @param value
087: * The value.
088: */
089: public Cookie(String name, String value) {
090: this (0, name, value, null, null);
091: }
092:
093: /** {@inheritDoc} */
094: @Override
095: public boolean equals(Object obj) {
096: boolean result = (obj == this );
097:
098: // if obj == this no need to go further
099: if (!result) {
100: // test for equality at Parameter level i.e. name and value.
101: if (super .equals(obj)) {
102: // if obj isn't a cookie or is null don't evaluate further
103: if ((obj instanceof Cookie) && obj != null) {
104: Cookie that = (Cookie) obj;
105: result = (this .version == that.version);
106: if (result) // if versions are equal test domains
107: {
108: if (!(this .domain == null)) // compare domains
109: // taking care of nulls
110: {
111: result = (this .domain.equals(that.domain));
112: } else {
113: result = (that.domain == null);
114: }
115:
116: if (result) // if domains are equal test the paths
117: {
118: if (!(this .path == null)) // compare paths taking
119: // care of nulls
120: {
121: result = (this .path.equals(that.path));
122: } else {
123: result = (that.path == null);
124: }
125: }
126: }
127: }
128: }
129: }
130:
131: return result;
132: }
133:
134: /**
135: * Returns the domain name.
136: *
137: * @return The domain name.
138: */
139: public String getDomain() {
140: return this .domain;
141: }
142:
143: /**
144: * Returns the validity path.
145: *
146: * @return The validity path.
147: */
148: public String getPath() {
149: return this .path;
150: }
151:
152: /**
153: * Returns the cookie specification version.
154: *
155: * @return The cookie specification version.
156: */
157: public int getVersion() {
158: return this .version;
159: }
160:
161: /** {@inheritDoc} */
162: @Override
163: public int hashCode() {
164: return Engine.hashCode(super .hashCode(), getVersion(),
165: getPath(), getDomain());
166: }
167:
168: /**
169: * Sets the domain name.
170: *
171: * @param domain
172: * The domain name.
173: */
174: public void setDomain(String domain) {
175: this .domain = domain;
176: }
177:
178: /**
179: * Sets the validity path.
180: *
181: * @param path
182: * The validity path.
183: */
184: public void setPath(String path) {
185: this .path = path;
186: }
187:
188: /**
189: * Sets the cookie specification version.
190: *
191: * @param version
192: * The cookie specification version.
193: */
194: public void setVersion(int version) {
195: this.version = version;
196: }
197:
198: }
|