001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.safehaus.asyncweb.common;
021:
022: /**
023: * Default cookie implementation.
024: * Content built using <code>Bytes</code> is cached as a <code>LazyDecodedString</code>
025: * until the content item is needed.
026: * <code>DefaultCookie</code> tracks changes to content items - allowing changed cookies
027: * to be written automatically when a response is written back to the client
028: *
029: * @author irvingd
030: *
031: */
032: public class DefaultCookie implements MutableCookie {
033:
034: private static final long serialVersionUID = 5713305385740914300L;
035:
036: private final String name;
037: private String value;
038: private String domain;
039: private String path;
040: private String comment;
041: private boolean secure;
042: private int version = 0;
043: private int maxAge = -1;
044:
045: /**
046: * Constructor used to allow users to create new cookies to be sent
047: * back to the client.
048: *
049: * @param name the name of the new cookie.
050: */
051: public DefaultCookie(String name) {
052: if (name == null) {
053: throw new NullPointerException("name");
054: }
055:
056: this .name = name;
057: }
058:
059: public String getComment() {
060: return comment;
061: }
062:
063: public void setComment(String comment) {
064: this .comment = comment;
065: }
066:
067: public String getDomain() {
068: return domain;
069: }
070:
071: public void setDomain(String domain) {
072: this .domain = domain;
073: }
074:
075: public int getMaxAge() {
076: return maxAge;
077: }
078:
079: public void setMaxAge(int maxAge) {
080: this .maxAge = maxAge;
081: }
082:
083: public String getPath() {
084: return path;
085: }
086:
087: public void setPath(String path) {
088: this .path = path;
089: }
090:
091: public boolean isSecure() {
092: return secure;
093: }
094:
095: public void setSecure(boolean secure) {
096: this .secure = secure;
097: }
098:
099: public String getValue() {
100: return value;
101: }
102:
103: public void setValue(String value) {
104: this .value = value;
105: }
106:
107: public int getVersion() {
108: return version;
109: }
110:
111: public void setVersion(int version) {
112: this .version = version;
113: }
114:
115: public String getName() {
116: return name;
117: }
118:
119: public int hashCode() {
120: return getName().hashCode();
121: }
122:
123: public boolean equals(Object o) {
124: if (o == this ) {
125: return true;
126: }
127:
128: if (!(o instanceof Cookie)) {
129: return false;
130: }
131:
132: Cookie that = (Cookie) o;
133: if (!name.equals(that.getName())) {
134: return false;
135: }
136:
137: if (path == null) {
138: if (that.getPath() != null) {
139: return false;
140: }
141: } else if (!path.equals(that.getPath())) {
142: return false;
143: }
144:
145: if (domain == null) {
146: if (that.getDomain() != null) {
147: return false;
148: }
149: } else if (!domain.equals(that.getDomain())) {
150: return false;
151: }
152:
153: return true;
154: }
155:
156: public int compareTo(Cookie o) {
157: int answer;
158:
159: // Compare the name first.
160: answer = name.compareTo(o.getName());
161: if (answer != 0) {
162: return answer;
163: }
164:
165: // and then path
166: if (path == null) {
167: if (o.getPath() != null) {
168: answer = -1;
169: } else {
170: answer = 0;
171: }
172: } else {
173: answer = path.compareTo(o.getPath());
174: }
175:
176: if (answer != 0) {
177: return answer;
178: }
179:
180: // and then domain
181: if (domain == null) {
182: if (o.getDomain() != null) {
183: answer = -1;
184: } else {
185: answer = 0;
186: }
187: } else {
188: answer = domain.compareTo(o.getDomain());
189: }
190:
191: return answer;
192: }
193:
194: public String toString() {
195: return "name=" + getName() + " value=" + getValue()
196: + " domain=" + getDomain() + " path=" + getPath()
197: + " maxAge=" + getMaxAge() + " secure=" + isSecure();
198: }
199: }
|