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 setting provided by a server.
025: *
026: * @author Jerome Louvel (contact@noelios.com)
027: */
028: public final class CookieSetting extends Cookie {
029: /** The user's comment. */
030: private String comment;
031:
032: /**
033: * The maximum age in seconds. Use 0 to discard an existing cookie.
034: */
035: private int maxAge;
036:
037: /** Indicates if cookie should only be transmitted by secure means. */
038: private boolean secure;
039:
040: /**
041: * Default constructor.
042: */
043: public CookieSetting() {
044: this (0, null, null, null, null);
045: }
046:
047: /**
048: * Preferred constructor.
049: *
050: * @param version
051: * The cookie's version.
052: * @param name
053: * The cookie's name.
054: * @param value
055: * The cookie's value.
056: */
057: public CookieSetting(int version, String name, String value) {
058: this (version, name, value, null, null);
059: }
060:
061: /**
062: * Preferred constructor.
063: *
064: * @param version
065: * The cookie's version.
066: * @param name
067: * The cookie's name.
068: * @param value
069: * The cookie's value.
070: * @param path
071: * The cookie's path.
072: * @param domain
073: * The cookie's domain name.
074: */
075: public CookieSetting(int version, String name, String value,
076: String path, String domain) {
077: super (version, name, value, path, domain);
078: this .comment = null;
079: this .maxAge = -1;
080: this .secure = false;
081: }
082:
083: /**
084: * Preferred constructor.
085: *
086: * @param name
087: * The cookie's name.
088: * @param value
089: * The cookie's value.
090: */
091: public CookieSetting(String name, String value) {
092: this (0, name, value, null, null);
093: }
094:
095: /** {@inheritDoc} */
096: @Override
097: public boolean equals(Object obj) {
098: boolean result = (obj == this );
099:
100: // if obj == this no need to go further
101: if (!result) {
102: // test for equality at Cookie level i.e. name and value.
103: if (super .equals(obj)) {
104: // if obj isn't a cookie setting or is null don't evaluate
105: // further
106: if ((obj instanceof CookieSetting) && obj != null) {
107: CookieSetting that = (CookieSetting) obj;
108: result = (this .maxAge == that.maxAge)
109: && (this .secure == that.secure);
110:
111: if (result) // if "maxAge" and "secure" properties are equal
112: // test comments
113: {
114: if (!(this .comment == null)) // compare comments
115: // taking care of nulls
116: {
117: result = (this .comment.equals(that.comment));
118: } else {
119: result = (that.comment == null);
120: }
121: }
122: }
123: }
124: }
125:
126: return result;
127: }
128:
129: /**
130: * Returns the comment for the user.
131: *
132: * @return The comment for the user.
133: */
134: public String getComment() {
135: return this .comment;
136: }
137:
138: /**
139: * Returns the description of this REST element.
140: *
141: * @return The description of this REST element.
142: */
143: public String getDescription() {
144: return "Cookie setting";
145: }
146:
147: /**
148: * Returns the maximum age in seconds.<br/> Use 0 to immediately discard an
149: * existing cookie.<br/> Use -1 to discard the cookie at the end of the
150: * session (default).
151: *
152: * @return The maximum age in seconds.
153: */
154: public int getMaxAge() {
155: return this .maxAge;
156: }
157:
158: /** {@inheritDoc} */
159: @Override
160: public int hashCode() {
161: return Engine.hashCode(super .hashCode(), getComment(),
162: getMaxAge(), isSecure());
163: }
164:
165: /**
166: * Indicates if cookie should only be transmitted by secure means.
167: *
168: * @return True if cookie should only be transmitted by secure means.
169: */
170: public boolean isSecure() {
171: return this .secure;
172: }
173:
174: /**
175: * Sets the comment for the user.
176: *
177: * @param comment
178: * The comment for the user.
179: */
180: public void setComment(String comment) {
181: this .comment = comment;
182: }
183:
184: /**
185: * Sets the maximum age in seconds.<br/> Use 0 to immediately discard an
186: * existing cookie.<br/> Use -1 to discard the cookie at the end of the
187: * session (default).
188: *
189: * @param maxAge
190: * The maximum age in seconds.
191: */
192: public void setMaxAge(int maxAge) {
193: this .maxAge = maxAge;
194: }
195:
196: /**
197: * Indicates if cookie should only be transmitted by secure means.
198: *
199: * @param secure
200: * True if cookie should only be transmitted by secure means.
201: */
202: public void setSecure(boolean secure) {
203: this.secure = secure;
204: }
205:
206: }
|