1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/cookie/BasicClientCookie.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,365 @@ 1.4 +/* 1.5 + * ==================================================================== 1.6 + * Licensed to the Apache Software Foundation (ASF) under one 1.7 + * or more contributor license agreements. See the NOTICE file 1.8 + * distributed with this work for additional information 1.9 + * regarding copyright ownership. The ASF licenses this file 1.10 + * to you under the Apache License, Version 2.0 (the 1.11 + * "License"); you may not use this file except in compliance 1.12 + * with the License. You may obtain a copy of the License at 1.13 + * 1.14 + * http://www.apache.org/licenses/LICENSE-2.0 1.15 + * 1.16 + * Unless required by applicable law or agreed to in writing, 1.17 + * software distributed under the License is distributed on an 1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1.19 + * KIND, either express or implied. See the License for the 1.20 + * specific language governing permissions and limitations 1.21 + * under the License. 1.22 + * ==================================================================== 1.23 + * 1.24 + * This software consists of voluntary contributions made by many 1.25 + * individuals on behalf of the Apache Software Foundation. For more 1.26 + * information on the Apache Software Foundation, please see 1.27 + * <http://www.apache.org/>. 1.28 + * 1.29 + */ 1.30 + 1.31 +package ch.boye.httpclientandroidlib.impl.cookie; 1.32 + 1.33 +import java.io.Serializable; 1.34 +import java.util.Date; 1.35 +import java.util.HashMap; 1.36 +import java.util.Locale; 1.37 +import java.util.Map; 1.38 + 1.39 +import ch.boye.httpclientandroidlib.annotation.NotThreadSafe; 1.40 + 1.41 +import ch.boye.httpclientandroidlib.cookie.ClientCookie; 1.42 +import ch.boye.httpclientandroidlib.cookie.SetCookie; 1.43 + 1.44 +/** 1.45 + * Default implementation of {@link SetCookie}. 1.46 + * 1.47 + * @since 4.0 1.48 + */ 1.49 +@NotThreadSafe 1.50 +public class BasicClientCookie implements SetCookie, ClientCookie, Cloneable, Serializable { 1.51 + 1.52 + private static final long serialVersionUID = -3869795591041535538L; 1.53 + 1.54 + /** 1.55 + * Default Constructor taking a name and a value. The value may be null. 1.56 + * 1.57 + * @param name The name. 1.58 + * @param value The value. 1.59 + */ 1.60 + public BasicClientCookie(final String name, final String value) { 1.61 + super(); 1.62 + if (name == null) { 1.63 + throw new IllegalArgumentException("Name may not be null"); 1.64 + } 1.65 + this.name = name; 1.66 + this.attribs = new HashMap<String, String>(); 1.67 + this.value = value; 1.68 + } 1.69 + 1.70 + /** 1.71 + * Returns the name. 1.72 + * 1.73 + * @return String name The name 1.74 + */ 1.75 + public String getName() { 1.76 + return this.name; 1.77 + } 1.78 + 1.79 + /** 1.80 + * Returns the value. 1.81 + * 1.82 + * @return String value The current value. 1.83 + */ 1.84 + public String getValue() { 1.85 + return this.value; 1.86 + } 1.87 + 1.88 + /** 1.89 + * Sets the value 1.90 + * 1.91 + * @param value 1.92 + */ 1.93 + public void setValue(final String value) { 1.94 + this.value = value; 1.95 + } 1.96 + 1.97 + /** 1.98 + * Returns the comment describing the purpose of this cookie, or 1.99 + * <tt>null</tt> if no such comment has been defined. 1.100 + * 1.101 + * @return comment 1.102 + * 1.103 + * @see #setComment(String) 1.104 + */ 1.105 + public String getComment() { 1.106 + return cookieComment; 1.107 + } 1.108 + 1.109 + /** 1.110 + * If a user agent (web browser) presents this cookie to a user, the 1.111 + * cookie's purpose will be described using this comment. 1.112 + * 1.113 + * @param comment 1.114 + * 1.115 + * @see #getComment() 1.116 + */ 1.117 + public void setComment(String comment) { 1.118 + cookieComment = comment; 1.119 + } 1.120 + 1.121 + 1.122 + /** 1.123 + * Returns null. Cookies prior to RFC2965 do not set this attribute 1.124 + */ 1.125 + public String getCommentURL() { 1.126 + return null; 1.127 + } 1.128 + 1.129 + 1.130 + /** 1.131 + * Returns the expiration {@link Date} of the cookie, or <tt>null</tt> 1.132 + * if none exists. 1.133 + * <p><strong>Note:</strong> the object returned by this method is 1.134 + * considered immutable. Changing it (e.g. using setTime()) could result 1.135 + * in undefined behaviour. Do so at your peril. </p> 1.136 + * @return Expiration {@link Date}, or <tt>null</tt>. 1.137 + * 1.138 + * @see #setExpiryDate(java.util.Date) 1.139 + * 1.140 + */ 1.141 + public Date getExpiryDate() { 1.142 + return cookieExpiryDate; 1.143 + } 1.144 + 1.145 + /** 1.146 + * Sets expiration date. 1.147 + * <p><strong>Note:</strong> the object returned by this method is considered 1.148 + * immutable. Changing it (e.g. using setTime()) could result in undefined 1.149 + * behaviour. Do so at your peril.</p> 1.150 + * 1.151 + * @param expiryDate the {@link Date} after which this cookie is no longer valid. 1.152 + * 1.153 + * @see #getExpiryDate 1.154 + * 1.155 + */ 1.156 + public void setExpiryDate (Date expiryDate) { 1.157 + cookieExpiryDate = expiryDate; 1.158 + } 1.159 + 1.160 + 1.161 + /** 1.162 + * Returns <tt>false</tt> if the cookie should be discarded at the end 1.163 + * of the "session"; <tt>true</tt> otherwise. 1.164 + * 1.165 + * @return <tt>false</tt> if the cookie should be discarded at the end 1.166 + * of the "session"; <tt>true</tt> otherwise 1.167 + */ 1.168 + public boolean isPersistent() { 1.169 + return (null != cookieExpiryDate); 1.170 + } 1.171 + 1.172 + 1.173 + /** 1.174 + * Returns domain attribute of the cookie. 1.175 + * 1.176 + * @return the value of the domain attribute 1.177 + * 1.178 + * @see #setDomain(java.lang.String) 1.179 + */ 1.180 + public String getDomain() { 1.181 + return cookieDomain; 1.182 + } 1.183 + 1.184 + /** 1.185 + * Sets the domain attribute. 1.186 + * 1.187 + * @param domain The value of the domain attribute 1.188 + * 1.189 + * @see #getDomain 1.190 + */ 1.191 + public void setDomain(String domain) { 1.192 + if (domain != null) { 1.193 + cookieDomain = domain.toLowerCase(Locale.ENGLISH); 1.194 + } else { 1.195 + cookieDomain = null; 1.196 + } 1.197 + } 1.198 + 1.199 + 1.200 + /** 1.201 + * Returns the path attribute of the cookie 1.202 + * 1.203 + * @return The value of the path attribute. 1.204 + * 1.205 + * @see #setPath(java.lang.String) 1.206 + */ 1.207 + public String getPath() { 1.208 + return cookiePath; 1.209 + } 1.210 + 1.211 + /** 1.212 + * Sets the path attribute. 1.213 + * 1.214 + * @param path The value of the path attribute 1.215 + * 1.216 + * @see #getPath 1.217 + * 1.218 + */ 1.219 + public void setPath(String path) { 1.220 + cookiePath = path; 1.221 + } 1.222 + 1.223 + /** 1.224 + * @return <code>true</code> if this cookie should only be sent over secure connections. 1.225 + * @see #setSecure(boolean) 1.226 + */ 1.227 + public boolean isSecure() { 1.228 + return isSecure; 1.229 + } 1.230 + 1.231 + /** 1.232 + * Sets the secure attribute of the cookie. 1.233 + * <p> 1.234 + * When <tt>true</tt> the cookie should only be sent 1.235 + * using a secure protocol (https). This should only be set when 1.236 + * the cookie's originating server used a secure protocol to set the 1.237 + * cookie's value. 1.238 + * 1.239 + * @param secure The value of the secure attribute 1.240 + * 1.241 + * @see #isSecure() 1.242 + */ 1.243 + public void setSecure (boolean secure) { 1.244 + isSecure = secure; 1.245 + } 1.246 + 1.247 + 1.248 + /** 1.249 + * Returns null. Cookies prior to RFC2965 do not set this attribute 1.250 + */ 1.251 + public int[] getPorts() { 1.252 + return null; 1.253 + } 1.254 + 1.255 + 1.256 + /** 1.257 + * Returns the version of the cookie specification to which this 1.258 + * cookie conforms. 1.259 + * 1.260 + * @return the version of the cookie. 1.261 + * 1.262 + * @see #setVersion(int) 1.263 + * 1.264 + */ 1.265 + public int getVersion() { 1.266 + return cookieVersion; 1.267 + } 1.268 + 1.269 + /** 1.270 + * Sets the version of the cookie specification to which this 1.271 + * cookie conforms. 1.272 + * 1.273 + * @param version the version of the cookie. 1.274 + * 1.275 + * @see #getVersion 1.276 + */ 1.277 + public void setVersion(int version) { 1.278 + cookieVersion = version; 1.279 + } 1.280 + 1.281 + /** 1.282 + * Returns true if this cookie has expired. 1.283 + * @param date Current time 1.284 + * 1.285 + * @return <tt>true</tt> if the cookie has expired. 1.286 + */ 1.287 + public boolean isExpired(final Date date) { 1.288 + if (date == null) { 1.289 + throw new IllegalArgumentException("Date may not be null"); 1.290 + } 1.291 + return (cookieExpiryDate != null 1.292 + && cookieExpiryDate.getTime() <= date.getTime()); 1.293 + } 1.294 + 1.295 + public void setAttribute(final String name, final String value) { 1.296 + this.attribs.put(name, value); 1.297 + } 1.298 + 1.299 + public String getAttribute(final String name) { 1.300 + return this.attribs.get(name); 1.301 + } 1.302 + 1.303 + public boolean containsAttribute(final String name) { 1.304 + return this.attribs.get(name) != null; 1.305 + } 1.306 + 1.307 + @Override 1.308 + public Object clone() throws CloneNotSupportedException { 1.309 + BasicClientCookie clone = (BasicClientCookie) super.clone(); 1.310 + clone.attribs = new HashMap<String, String>(this.attribs); 1.311 + return clone; 1.312 + } 1.313 + 1.314 + @Override 1.315 + public String toString() { 1.316 + StringBuilder buffer = new StringBuilder(); 1.317 + buffer.append("[version: "); 1.318 + buffer.append(Integer.toString(this.cookieVersion)); 1.319 + buffer.append("]"); 1.320 + buffer.append("[name: "); 1.321 + buffer.append(this.name); 1.322 + buffer.append("]"); 1.323 + buffer.append("[value: "); 1.324 + buffer.append(this.value); 1.325 + buffer.append("]"); 1.326 + buffer.append("[domain: "); 1.327 + buffer.append(this.cookieDomain); 1.328 + buffer.append("]"); 1.329 + buffer.append("[path: "); 1.330 + buffer.append(this.cookiePath); 1.331 + buffer.append("]"); 1.332 + buffer.append("[expiry: "); 1.333 + buffer.append(this.cookieExpiryDate); 1.334 + buffer.append("]"); 1.335 + return buffer.toString(); 1.336 + } 1.337 + 1.338 + // ----------------------------------------------------- Instance Variables 1.339 + 1.340 + /** Cookie name */ 1.341 + private final String name; 1.342 + 1.343 + /** Cookie attributes as specified by the origin server */ 1.344 + private Map<String, String> attribs; 1.345 + 1.346 + /** Cookie value */ 1.347 + private String value; 1.348 + 1.349 + /** Comment attribute. */ 1.350 + private String cookieComment; 1.351 + 1.352 + /** Domain attribute. */ 1.353 + private String cookieDomain; 1.354 + 1.355 + /** Expiration {@link Date}. */ 1.356 + private Date cookieExpiryDate; 1.357 + 1.358 + /** Path attribute. */ 1.359 + private String cookiePath; 1.360 + 1.361 + /** My secure flag. */ 1.362 + private boolean isSecure; 1.363 + 1.364 + /** The version of the cookie specification I was created from. */ 1.365 + private int cookieVersion; 1.366 + 1.367 +} 1.368 +