Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | * ==================================================================== |
michael@0 | 3 | * Licensed to the Apache Software Foundation (ASF) under one |
michael@0 | 4 | * or more contributor license agreements. See the NOTICE file |
michael@0 | 5 | * distributed with this work for additional information |
michael@0 | 6 | * regarding copyright ownership. The ASF licenses this file |
michael@0 | 7 | * to you under the Apache License, Version 2.0 (the |
michael@0 | 8 | * "License"); you may not use this file except in compliance |
michael@0 | 9 | * with the License. You may obtain a copy of the License at |
michael@0 | 10 | * |
michael@0 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 12 | * |
michael@0 | 13 | * Unless required by applicable law or agreed to in writing, |
michael@0 | 14 | * software distributed under the License is distributed on an |
michael@0 | 15 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
michael@0 | 16 | * KIND, either express or implied. See the License for the |
michael@0 | 17 | * specific language governing permissions and limitations |
michael@0 | 18 | * under the License. |
michael@0 | 19 | * ==================================================================== |
michael@0 | 20 | * |
michael@0 | 21 | * This software consists of voluntary contributions made by many |
michael@0 | 22 | * individuals on behalf of the Apache Software Foundation. For more |
michael@0 | 23 | * information on the Apache Software Foundation, please see |
michael@0 | 24 | * <http://www.apache.org/>. |
michael@0 | 25 | * |
michael@0 | 26 | */ |
michael@0 | 27 | |
michael@0 | 28 | package ch.boye.httpclientandroidlib.impl.cookie; |
michael@0 | 29 | |
michael@0 | 30 | import java.io.Serializable; |
michael@0 | 31 | import java.util.Date; |
michael@0 | 32 | import java.util.HashMap; |
michael@0 | 33 | import java.util.Locale; |
michael@0 | 34 | import java.util.Map; |
michael@0 | 35 | |
michael@0 | 36 | import ch.boye.httpclientandroidlib.annotation.NotThreadSafe; |
michael@0 | 37 | |
michael@0 | 38 | import ch.boye.httpclientandroidlib.cookie.ClientCookie; |
michael@0 | 39 | import ch.boye.httpclientandroidlib.cookie.SetCookie; |
michael@0 | 40 | |
michael@0 | 41 | /** |
michael@0 | 42 | * Default implementation of {@link SetCookie}. |
michael@0 | 43 | * |
michael@0 | 44 | * @since 4.0 |
michael@0 | 45 | */ |
michael@0 | 46 | @NotThreadSafe |
michael@0 | 47 | public class BasicClientCookie implements SetCookie, ClientCookie, Cloneable, Serializable { |
michael@0 | 48 | |
michael@0 | 49 | private static final long serialVersionUID = -3869795591041535538L; |
michael@0 | 50 | |
michael@0 | 51 | /** |
michael@0 | 52 | * Default Constructor taking a name and a value. The value may be null. |
michael@0 | 53 | * |
michael@0 | 54 | * @param name The name. |
michael@0 | 55 | * @param value The value. |
michael@0 | 56 | */ |
michael@0 | 57 | public BasicClientCookie(final String name, final String value) { |
michael@0 | 58 | super(); |
michael@0 | 59 | if (name == null) { |
michael@0 | 60 | throw new IllegalArgumentException("Name may not be null"); |
michael@0 | 61 | } |
michael@0 | 62 | this.name = name; |
michael@0 | 63 | this.attribs = new HashMap<String, String>(); |
michael@0 | 64 | this.value = value; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | /** |
michael@0 | 68 | * Returns the name. |
michael@0 | 69 | * |
michael@0 | 70 | * @return String name The name |
michael@0 | 71 | */ |
michael@0 | 72 | public String getName() { |
michael@0 | 73 | return this.name; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | /** |
michael@0 | 77 | * Returns the value. |
michael@0 | 78 | * |
michael@0 | 79 | * @return String value The current value. |
michael@0 | 80 | */ |
michael@0 | 81 | public String getValue() { |
michael@0 | 82 | return this.value; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | /** |
michael@0 | 86 | * Sets the value |
michael@0 | 87 | * |
michael@0 | 88 | * @param value |
michael@0 | 89 | */ |
michael@0 | 90 | public void setValue(final String value) { |
michael@0 | 91 | this.value = value; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | /** |
michael@0 | 95 | * Returns the comment describing the purpose of this cookie, or |
michael@0 | 96 | * <tt>null</tt> if no such comment has been defined. |
michael@0 | 97 | * |
michael@0 | 98 | * @return comment |
michael@0 | 99 | * |
michael@0 | 100 | * @see #setComment(String) |
michael@0 | 101 | */ |
michael@0 | 102 | public String getComment() { |
michael@0 | 103 | return cookieComment; |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | /** |
michael@0 | 107 | * If a user agent (web browser) presents this cookie to a user, the |
michael@0 | 108 | * cookie's purpose will be described using this comment. |
michael@0 | 109 | * |
michael@0 | 110 | * @param comment |
michael@0 | 111 | * |
michael@0 | 112 | * @see #getComment() |
michael@0 | 113 | */ |
michael@0 | 114 | public void setComment(String comment) { |
michael@0 | 115 | cookieComment = comment; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | |
michael@0 | 119 | /** |
michael@0 | 120 | * Returns null. Cookies prior to RFC2965 do not set this attribute |
michael@0 | 121 | */ |
michael@0 | 122 | public String getCommentURL() { |
michael@0 | 123 | return null; |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | |
michael@0 | 127 | /** |
michael@0 | 128 | * Returns the expiration {@link Date} of the cookie, or <tt>null</tt> |
michael@0 | 129 | * if none exists. |
michael@0 | 130 | * <p><strong>Note:</strong> the object returned by this method is |
michael@0 | 131 | * considered immutable. Changing it (e.g. using setTime()) could result |
michael@0 | 132 | * in undefined behaviour. Do so at your peril. </p> |
michael@0 | 133 | * @return Expiration {@link Date}, or <tt>null</tt>. |
michael@0 | 134 | * |
michael@0 | 135 | * @see #setExpiryDate(java.util.Date) |
michael@0 | 136 | * |
michael@0 | 137 | */ |
michael@0 | 138 | public Date getExpiryDate() { |
michael@0 | 139 | return cookieExpiryDate; |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | /** |
michael@0 | 143 | * Sets expiration date. |
michael@0 | 144 | * <p><strong>Note:</strong> the object returned by this method is considered |
michael@0 | 145 | * immutable. Changing it (e.g. using setTime()) could result in undefined |
michael@0 | 146 | * behaviour. Do so at your peril.</p> |
michael@0 | 147 | * |
michael@0 | 148 | * @param expiryDate the {@link Date} after which this cookie is no longer valid. |
michael@0 | 149 | * |
michael@0 | 150 | * @see #getExpiryDate |
michael@0 | 151 | * |
michael@0 | 152 | */ |
michael@0 | 153 | public void setExpiryDate (Date expiryDate) { |
michael@0 | 154 | cookieExpiryDate = expiryDate; |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | |
michael@0 | 158 | /** |
michael@0 | 159 | * Returns <tt>false</tt> if the cookie should be discarded at the end |
michael@0 | 160 | * of the "session"; <tt>true</tt> otherwise. |
michael@0 | 161 | * |
michael@0 | 162 | * @return <tt>false</tt> if the cookie should be discarded at the end |
michael@0 | 163 | * of the "session"; <tt>true</tt> otherwise |
michael@0 | 164 | */ |
michael@0 | 165 | public boolean isPersistent() { |
michael@0 | 166 | return (null != cookieExpiryDate); |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | |
michael@0 | 170 | /** |
michael@0 | 171 | * Returns domain attribute of the cookie. |
michael@0 | 172 | * |
michael@0 | 173 | * @return the value of the domain attribute |
michael@0 | 174 | * |
michael@0 | 175 | * @see #setDomain(java.lang.String) |
michael@0 | 176 | */ |
michael@0 | 177 | public String getDomain() { |
michael@0 | 178 | return cookieDomain; |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | /** |
michael@0 | 182 | * Sets the domain attribute. |
michael@0 | 183 | * |
michael@0 | 184 | * @param domain The value of the domain attribute |
michael@0 | 185 | * |
michael@0 | 186 | * @see #getDomain |
michael@0 | 187 | */ |
michael@0 | 188 | public void setDomain(String domain) { |
michael@0 | 189 | if (domain != null) { |
michael@0 | 190 | cookieDomain = domain.toLowerCase(Locale.ENGLISH); |
michael@0 | 191 | } else { |
michael@0 | 192 | cookieDomain = null; |
michael@0 | 193 | } |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | |
michael@0 | 197 | /** |
michael@0 | 198 | * Returns the path attribute of the cookie |
michael@0 | 199 | * |
michael@0 | 200 | * @return The value of the path attribute. |
michael@0 | 201 | * |
michael@0 | 202 | * @see #setPath(java.lang.String) |
michael@0 | 203 | */ |
michael@0 | 204 | public String getPath() { |
michael@0 | 205 | return cookiePath; |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | /** |
michael@0 | 209 | * Sets the path attribute. |
michael@0 | 210 | * |
michael@0 | 211 | * @param path The value of the path attribute |
michael@0 | 212 | * |
michael@0 | 213 | * @see #getPath |
michael@0 | 214 | * |
michael@0 | 215 | */ |
michael@0 | 216 | public void setPath(String path) { |
michael@0 | 217 | cookiePath = path; |
michael@0 | 218 | } |
michael@0 | 219 | |
michael@0 | 220 | /** |
michael@0 | 221 | * @return <code>true</code> if this cookie should only be sent over secure connections. |
michael@0 | 222 | * @see #setSecure(boolean) |
michael@0 | 223 | */ |
michael@0 | 224 | public boolean isSecure() { |
michael@0 | 225 | return isSecure; |
michael@0 | 226 | } |
michael@0 | 227 | |
michael@0 | 228 | /** |
michael@0 | 229 | * Sets the secure attribute of the cookie. |
michael@0 | 230 | * <p> |
michael@0 | 231 | * When <tt>true</tt> the cookie should only be sent |
michael@0 | 232 | * using a secure protocol (https). This should only be set when |
michael@0 | 233 | * the cookie's originating server used a secure protocol to set the |
michael@0 | 234 | * cookie's value. |
michael@0 | 235 | * |
michael@0 | 236 | * @param secure The value of the secure attribute |
michael@0 | 237 | * |
michael@0 | 238 | * @see #isSecure() |
michael@0 | 239 | */ |
michael@0 | 240 | public void setSecure (boolean secure) { |
michael@0 | 241 | isSecure = secure; |
michael@0 | 242 | } |
michael@0 | 243 | |
michael@0 | 244 | |
michael@0 | 245 | /** |
michael@0 | 246 | * Returns null. Cookies prior to RFC2965 do not set this attribute |
michael@0 | 247 | */ |
michael@0 | 248 | public int[] getPorts() { |
michael@0 | 249 | return null; |
michael@0 | 250 | } |
michael@0 | 251 | |
michael@0 | 252 | |
michael@0 | 253 | /** |
michael@0 | 254 | * Returns the version of the cookie specification to which this |
michael@0 | 255 | * cookie conforms. |
michael@0 | 256 | * |
michael@0 | 257 | * @return the version of the cookie. |
michael@0 | 258 | * |
michael@0 | 259 | * @see #setVersion(int) |
michael@0 | 260 | * |
michael@0 | 261 | */ |
michael@0 | 262 | public int getVersion() { |
michael@0 | 263 | return cookieVersion; |
michael@0 | 264 | } |
michael@0 | 265 | |
michael@0 | 266 | /** |
michael@0 | 267 | * Sets the version of the cookie specification to which this |
michael@0 | 268 | * cookie conforms. |
michael@0 | 269 | * |
michael@0 | 270 | * @param version the version of the cookie. |
michael@0 | 271 | * |
michael@0 | 272 | * @see #getVersion |
michael@0 | 273 | */ |
michael@0 | 274 | public void setVersion(int version) { |
michael@0 | 275 | cookieVersion = version; |
michael@0 | 276 | } |
michael@0 | 277 | |
michael@0 | 278 | /** |
michael@0 | 279 | * Returns true if this cookie has expired. |
michael@0 | 280 | * @param date Current time |
michael@0 | 281 | * |
michael@0 | 282 | * @return <tt>true</tt> if the cookie has expired. |
michael@0 | 283 | */ |
michael@0 | 284 | public boolean isExpired(final Date date) { |
michael@0 | 285 | if (date == null) { |
michael@0 | 286 | throw new IllegalArgumentException("Date may not be null"); |
michael@0 | 287 | } |
michael@0 | 288 | return (cookieExpiryDate != null |
michael@0 | 289 | && cookieExpiryDate.getTime() <= date.getTime()); |
michael@0 | 290 | } |
michael@0 | 291 | |
michael@0 | 292 | public void setAttribute(final String name, final String value) { |
michael@0 | 293 | this.attribs.put(name, value); |
michael@0 | 294 | } |
michael@0 | 295 | |
michael@0 | 296 | public String getAttribute(final String name) { |
michael@0 | 297 | return this.attribs.get(name); |
michael@0 | 298 | } |
michael@0 | 299 | |
michael@0 | 300 | public boolean containsAttribute(final String name) { |
michael@0 | 301 | return this.attribs.get(name) != null; |
michael@0 | 302 | } |
michael@0 | 303 | |
michael@0 | 304 | @Override |
michael@0 | 305 | public Object clone() throws CloneNotSupportedException { |
michael@0 | 306 | BasicClientCookie clone = (BasicClientCookie) super.clone(); |
michael@0 | 307 | clone.attribs = new HashMap<String, String>(this.attribs); |
michael@0 | 308 | return clone; |
michael@0 | 309 | } |
michael@0 | 310 | |
michael@0 | 311 | @Override |
michael@0 | 312 | public String toString() { |
michael@0 | 313 | StringBuilder buffer = new StringBuilder(); |
michael@0 | 314 | buffer.append("[version: "); |
michael@0 | 315 | buffer.append(Integer.toString(this.cookieVersion)); |
michael@0 | 316 | buffer.append("]"); |
michael@0 | 317 | buffer.append("[name: "); |
michael@0 | 318 | buffer.append(this.name); |
michael@0 | 319 | buffer.append("]"); |
michael@0 | 320 | buffer.append("[value: "); |
michael@0 | 321 | buffer.append(this.value); |
michael@0 | 322 | buffer.append("]"); |
michael@0 | 323 | buffer.append("[domain: "); |
michael@0 | 324 | buffer.append(this.cookieDomain); |
michael@0 | 325 | buffer.append("]"); |
michael@0 | 326 | buffer.append("[path: "); |
michael@0 | 327 | buffer.append(this.cookiePath); |
michael@0 | 328 | buffer.append("]"); |
michael@0 | 329 | buffer.append("[expiry: "); |
michael@0 | 330 | buffer.append(this.cookieExpiryDate); |
michael@0 | 331 | buffer.append("]"); |
michael@0 | 332 | return buffer.toString(); |
michael@0 | 333 | } |
michael@0 | 334 | |
michael@0 | 335 | // ----------------------------------------------------- Instance Variables |
michael@0 | 336 | |
michael@0 | 337 | /** Cookie name */ |
michael@0 | 338 | private final String name; |
michael@0 | 339 | |
michael@0 | 340 | /** Cookie attributes as specified by the origin server */ |
michael@0 | 341 | private Map<String, String> attribs; |
michael@0 | 342 | |
michael@0 | 343 | /** Cookie value */ |
michael@0 | 344 | private String value; |
michael@0 | 345 | |
michael@0 | 346 | /** Comment attribute. */ |
michael@0 | 347 | private String cookieComment; |
michael@0 | 348 | |
michael@0 | 349 | /** Domain attribute. */ |
michael@0 | 350 | private String cookieDomain; |
michael@0 | 351 | |
michael@0 | 352 | /** Expiration {@link Date}. */ |
michael@0 | 353 | private Date cookieExpiryDate; |
michael@0 | 354 | |
michael@0 | 355 | /** Path attribute. */ |
michael@0 | 356 | private String cookiePath; |
michael@0 | 357 | |
michael@0 | 358 | /** My secure flag. */ |
michael@0 | 359 | private boolean isSecure; |
michael@0 | 360 | |
michael@0 | 361 | /** The version of the cookie specification I was created from. */ |
michael@0 | 362 | private int cookieVersion; |
michael@0 | 363 | |
michael@0 | 364 | } |
michael@0 | 365 |