001 /*
002 * The contents of this file are subject to the terms
003 * of the Common Development and Distribution License
004 * (the "License"). You may not use this file except
005 * in compliance with the License.
006 *
007 * You can obtain a copy of the license at
008 * http://www.opensource.org/licenses/cddl1.php
009 * See the License for the specific language governing
010 * permissions and limitations under the License.
011 */
012
013 /*
014 * Cookie.java
015 *
016 * Created on March 12, 2007, 5:01 PM
017 *
018 */
019
020 package javax.ws.rs.core;
021
022 import javax.ws.rs.ext.RuntimeDelegate;
023 import javax.ws.rs.ext.RuntimeDelegate.HeaderDelegate;
024
025 /**
026 * Represents the value of a HTTP cookie, transferred in a request.
027 * RFC 2109 specifies the legal characters for name,
028 * value, path and domain. The default version of 1 corresponds to RFC 2109.
029 * @see <a href="http://www.ietf.org/rfc/rfc2109.txt">IETF RFC 2109</a>
030 */
031 public class Cookie {
032
033 /**
034 * Cookies using the default version correspond to RFC 2109.
035 */
036 public static final int DEFAULT_VERSION = 1;
037
038 private static final HeaderDelegate<Cookie> delegate =
039 RuntimeDelegate.getInstance().createHeaderDelegate(Cookie.class);
040
041 private String name;
042 private String value;
043 private int version;
044 private String path;
045 private String domain;
046
047 /**
048 * Create a new instance.
049 * @param name the name of the cookie
050 * @param value the value of the cookie
051 * @param path the URI path for which the cookie is valid
052 * @param domain the host domain for which the cookie is valid
053 * @param version the version of the specification to which the cookie complies
054 * @throws IllegalArgumentException if name is null
055 */
056 public Cookie(String name, String value, String path, String domain, int version) {
057 if (name == null)
058 throw new IllegalArgumentException("name==null");
059 this.name = name;
060 this.value = value;
061 this.version = version;
062 this.domain = domain;
063 this.path = path;
064 }
065
066 /**
067 * Create a new instance.
068 * @param name the name of the cookie
069 * @param value the value of the cookie
070 * @param path the URI path for which the cookie is valid
071 * @param domain the host domain for which the cookie is valid
072 * @throws IllegalArgumentException if name is null
073 */
074 public Cookie(String name, String value, String path, String domain) {
075 this(name, value, path, domain, DEFAULT_VERSION);
076 }
077
078 /**
079 * Create a new instance.
080 * @param name the name of the cookie
081 * @param value the value of the cookie
082 * @throws IllegalArgumentException if name is null
083 */
084 public Cookie(String name, String value) {
085 this(name, value, null, null);
086 }
087
088 /**
089 * Creates a new instance of Cookie by parsing the supplied string.
090 * @param value the cookie string
091 * @return the newly created Cookie
092 * @throws IllegalArgumentException if the supplied string cannot be parsed
093 * or is null
094 */
095 public static Cookie valueOf(String value) throws IllegalArgumentException {
096 return delegate.fromString(value);
097 }
098
099 /**
100 * Get the name of the cookie
101 * @return the name
102 */
103 public String getName() {
104 return name;
105 }
106
107 /**
108 * Get the value of the cookie
109 * @return the value
110 */
111 public String getValue() {
112 return value;
113 }
114
115 /**
116 * Get the version of the cookie
117 * @return the version
118 */
119 public int getVersion() {
120 return version;
121 }
122
123 /**
124 * Get the domain of the cookie
125 * @return the domain
126 */
127 public String getDomain() {
128 return domain;
129 }
130
131 /**
132 * Get the path of the cookie
133 * @return the path
134 */
135 public String getPath() {
136 return path;
137 }
138
139 /**
140 * Convert the cookie to a string suitable for use as the value of the
141 * corresponding HTTP header.
142 * @return a stringified cookie
143 */
144 @Override
145 public String toString() {
146 return delegate.toString(this);
147 }
148
149 /**
150 * Generate a hashcode by hashing all of the cookies properties
151 * @return the hashcode
152 */
153 @Override
154 public int hashCode() {
155 int hash = 7;
156 hash = 97 * hash + (this.name != null ? this.name.hashCode() : 0);
157 hash = 97 * hash + (this.value != null ? this.value.hashCode() : 0);
158 hash = 97 * hash + this.version;
159 hash = 97 * hash + (this.path != null ? this.path.hashCode() : 0);
160 hash = 97 * hash + (this.domain != null ? this.domain.hashCode() : 0);
161 return hash;
162 }
163
164 /**
165 * Compare for equality
166 * @param obj the object to compare to
167 * @return true if the object is a {@code Cookie} with the same value for
168 * all properties, false otherwise.
169 */
170 @Override
171 public boolean equals(Object obj) {
172 if (obj == null) {
173 return false;
174 }
175 if (getClass() != obj.getClass()) {
176 return false;
177 }
178 final Cookie other = (Cookie) obj;
179 if (this.name != other.name && (this.name == null || !this.name.equals(other.name))) {
180 return false;
181 }
182 if (this.value != other.value && (this.value == null || !this.value.equals(other.value))) {
183 return false;
184 }
185 if (this.version != other.version) {
186 return false;
187 }
188 if (this.path != other.path && (this.path == null || !this.path.equals(other.path))) {
189 return false;
190 }
191 if (this.domain != other.domain && (this.domain == null || !this.domain.equals(other.domain))) {
192 return false;
193 }
194 return true;
195 }
196
197
198 }