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     * NewCookie.java
015     *
016     * Created on March 12, 2007, 5:08 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     * Used to create a new HTTP cookie, transferred in a response.
027     * @see <a href="http://www.ietf.org/rfc/rfc2109.txt">IETF RFC 2109</a>
028     */
029    public class NewCookie extends Cookie {
030        
031        /**
032         * Specifies that the cookie expires with the current application/browser session.
033         */
034        public static final int DEFAULT_MAX_AGE = -1;
035        
036        private static final HeaderDelegate<NewCookie> delegate = 
037                RuntimeDelegate.getInstance().createHeaderDelegate(NewCookie.class);
038    
039        private String comment = null;
040        private int maxAge = DEFAULT_MAX_AGE;
041        private boolean secure = false;
042        
043        /**
044         * Create a new instance.
045         * @param name the name of the cookie
046         * @param value the value of the cookie
047         * @throws IllegalArgumentException if name is null
048         */
049        public NewCookie(String name, String value) {
050            super(name, value);
051        }
052        
053        /**
054         * Create a new instance.
055         * @param name the name of the cookie
056         * @param value the value of the cookie
057         * @param path the URI path for which the cookie is valid
058         * @param domain the host domain for which the cookie is valid
059         * @param comment the comment
060         * @param maxAge the maximum age of the cookie in seconds
061         * @param secure specifies whether the cookie will only be sent over a secure connection
062         * @throws IllegalArgumentException if name is null
063         */
064        public NewCookie(String name, String value, String path, String domain, String comment, int maxAge, boolean secure) {
065            super(name, value, path, domain);
066            this.comment = comment;
067            this.maxAge = maxAge;
068            this.secure = secure;
069        }
070        
071        /**
072         * Create a new instance.
073         * @param name the name of the cookie
074         * @param value the value of the cookie
075         * @param path the URI path for which the cookie is valid
076         * @param domain the host domain for which the cookie is valid
077         * @param version the version of the specification to which the cookie complies
078         * @param comment the comment
079         * @param maxAge the maximum age of the cookie in seconds
080         * @param secure specifies whether the cookie will only be sent over a secure connection
081         * @throws IllegalArgumentException if name is null
082         */
083        public NewCookie(String name, String value, String path, String domain, int version, String comment, int maxAge, boolean secure) {
084            super(name, value, path, domain, version);
085            this.comment = comment;
086            this.maxAge = maxAge;
087            this.secure = secure;
088        }
089        
090        /**
091         * Create a new instance copying the information in the supplied cookie.
092         * @param cookie the cookie to clone
093         * @throws IllegalArgumentException if cookie is null
094         */
095        public NewCookie(Cookie cookie) {
096            super(cookie==null ? null : cookie.getName(), 
097                  cookie==null ? null : cookie.getValue(),
098                  cookie==null ? null : cookie.getPath(),
099                  cookie==null ? null : cookie.getDomain(),
100                  cookie==null ? Cookie.DEFAULT_VERSION : cookie.getVersion());
101        }
102    
103        /**
104         * Create a new instance supplementing the information in the supplied cookie.
105         * @param cookie the cookie to clone
106         * @param comment the comment
107         * @param maxAge the maximum age of the cookie in seconds
108         * @param secure specifies whether the cookie will only be sent over a secure connection
109         * @throws IllegalArgumentException if cookie is null
110         */
111        public NewCookie(Cookie cookie, String comment, int maxAge, boolean secure) {
112            this(cookie);
113            this.comment = comment;
114            this.maxAge = maxAge;
115            this.secure = secure;
116        }
117    
118        /**
119         * Creates a new instance of NewCookie by parsing the supplied string.
120         * @param value the cookie string
121         * @return the newly created NewCookie
122         * @throws IllegalArgumentException if the supplied string cannot be parsed
123         * or is null
124         */
125        public static NewCookie valueOf(String value) throws IllegalArgumentException {
126            return delegate.fromString(value);
127        }
128        
129        /**
130         * Get the comment associated with the cookie.
131         * @return the comment or null if none set
132         */
133        public String getComment() {
134            return comment;
135        }
136    
137        /**
138         * Get the maximum age of the the cookie in seconds. Cookies older than
139         * the maximum age are discarded. A cookie can be unset by sending a new
140         * cookie with maximum age of 0 since it will overwrite any existing cookie
141         * and then be immediately discarded. The default value of -1 indicates that the cookie
142         * will be discarded at the end of the browser/application session.
143         * @return the maximum age in seconds
144         */
145        public int getMaxAge() {
146            return maxAge;
147        }
148    
149        /**
150         * Whether the cookie will only be sent over a secure connection. Defaults
151         * to false.
152         * @return true if the cookie will only be sent over a secure connection,
153         * false otherwise.
154         */
155        public boolean isSecure() {
156            return secure;
157        }
158        
159        /**
160         * Obtain a new instance of a {@link Cookie} with the same name, value, path,
161         * domain and version as this {@code NewCookie}. This method can be used to
162         * obtain an object that can be compared for equality with another {@code Cookie};
163         * since a {@code Cookie} will never compare equal to a {@code NewCookie}.
164         * @return a {@link Cookie}
165         */
166        public Cookie toCookie() {
167            return new Cookie(this.getName(),this.getValue(), this.getPath(), 
168                    this.getDomain(), this.getVersion());
169        }
170    
171        /**
172         * Convert the cookie to a string suitable for use as the value of the
173         * corresponding HTTP header.
174         * @return a stringified cookie
175         */
176        @Override
177        public String toString() {
178            return delegate.toString(this);
179        }
180    
181        /**
182         * Generate a hashcode by hashing all of the properties
183         * @return the hashcode
184         */
185        @Override
186        public int hashCode() {
187            int hash = super.hashCode();
188            hash = 59 * hash + (this.comment != null ? this.comment.hashCode() : 0);
189            hash = 59 * hash + this.maxAge;
190            hash = 59 * hash + (this.secure ? 1 : 0);
191            return hash;
192        }
193    
194        /**
195         * Compare for equality. Use {@link #toCookie()} to compare a 
196         * {@code NewCookie} to a {@code Cookie} considering only the common
197         * properties.
198         * @param obj
199         * @return true if the object is a {@code NewCookie} with the same value for
200         * all properties, false otherwise.
201         */
202        @Override
203        public boolean equals(Object obj) {
204            if (obj == null) {
205                return false;
206            }
207            if (getClass() != obj.getClass()) {
208                return false;
209            }
210            final NewCookie other = (NewCookie) obj;
211            if (this.getName() != other.getName() && (this.getName() == null || !this.getName().equals(other.getName()))) {
212                return false;
213            }
214            if (this.getValue() != other.getValue() && (this.getValue() == null || !this.getValue().equals(other.getValue()))) {
215                return false;
216            }
217            if (this.getVersion() != other.getVersion()) {
218                return false;
219            }
220            if (this.getPath() != other.getPath() && (this.getPath() == null || !this.getPath().equals(other.getPath()))) {
221                return false;
222            }
223            if (this.getDomain() != other.getDomain() && (this.getDomain() == null || !this.getDomain().equals(other.getDomain()))) {
224                return false;
225            }
226            if (this.comment != other.comment && (this.comment == null || !this.comment.equals(other.comment))) {
227                return false;
228            }
229            if (this.maxAge != other.maxAge) {
230                return false;
231            }
232            if (this.secure != other.secure) {
233                return false;
234            }
235            return true;
236        }
237        
238    }