Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
    /*
  Copyright 2007-2009 Selenium committers
  
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
       http://www.apache.org/licenses/LICENSE-2.0
  
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
  */
 
 package io.selendroid.server.model;
 
 import java.util.Date;
 
 public class Cookie {
     private final String name;
     private final String value;
     private final String path;
     private final String domain;
     private final Date expiry;
     private final boolean isSecure;

    
Creates an insecure cookie with no domain specified.

Parameters:
name The name of the cookie; may not be null or an empty string.
value The cookie value; may not be null.
path The path the cookie is visible to. If left blank or set to null, will be set to "/".
expiry The cookie's expiration date; may be null.
See also:
Cookie(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.util.Date)
 
     public Cookie(String nameString valueString pathDate expiry) {
         this(namevaluenullpathexpiry);
     }

    
Creates an insecure cookie.

Parameters:
name The name of the cookie; may not be null or an empty string.
value The cookie value; may not be null.
domain The domain the cookie is visible to.
path The path the cookie is visible to. If left blank or set to null, will be set to "/".
expiry The cookie's expiration date; may be null.
See also:
Cookie(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.util.Date,boolean)
 
     public Cookie(String nameString valueString domainString pathDate expiry) {
         this(namevaluedomainpathexpiryfalse);
     }

    
Creates a cookie.

Parameters:
name The name of the cookie; may not be null or an empty string.
value The cookie value; may not be null.
domain The domain the cookie is visible to.
path The path the cookie is visible to. If left blank or set to null, will be set to "/".
expiry The cookie's expiration date; may be null.
isSecure Whether this cookie requires a secure connection.
 
     public Cookie(String nameString valueString domainString pathDate expiry,
                   boolean isSecure) {
         this. = name;
         this. = value;
         this. = path == null || "".equals(path) ? "/" : path;
 
         this. = stripPort(domain);
         this. = isSecure;
 
         if (expiry != null) {
             // Expiration date is specified in seconds since (UTC) epoch time, so truncate the date.
             this. = new Date(expiry.getTime() / 1000 * 1000);
         } else {
             this. = null;
         }
 
         validate();
     }

    
Create a cookie for the default path with the given name and value with no expiry set.

Parameters:
name The cookie's name
value The cookie's value
 
     public Cookie(String nameString value) {
         this(namevalue"/"null);
     }

    
Create a cookie.

Parameters:
name The cookie's name
value The cookie's value
path The path the cookie is for
    public Cookie(String nameString valueString path) {
        this(namevaluepathnull);
    }
    public String getName() {
        return ;
    }
    public String getValue() {
        return ;
    }
    public String getDomain() {
        return ;
    }
    public String getPath() {
        return ;
    }
    public boolean isSecure() {
        return ;
    }
    public Date getExpiry() {
        return ;
    }
    private static String stripPort(String domain) {
        return (domain == null) ? null : domain.split(":")[0];
    }
    protected void validate() {
        if ( == null || "".equals() ||  == null ||  == null) {
            throw new IllegalArgumentException("Required attributes are not set or " +
                    "any non-null attribute set to null");
        }
        if (.indexOf(';') != -1) {
            throw new IllegalArgumentException(
                    "Cookie names cannot contain a ';': " + );
        }
        if ( != null && .contains(":")) {
            throw new IllegalArgumentException("Domain should not contain a port: " + );
        }
    }
    @Override
    public String toString() {
        return  + "=" + 
                + ( == null ? ""
                : "; expires=" + new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z")
                .format())
                + ("".equals() ? "" : "; path=" + )
                + ( == null ? "" : "; domain=" + )
                + ( ? ";secure;" : "");
    }

    
Two cookies are equal if the name and value match
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Cookie)) {
            return false;
        }
        Cookie cookie = (Cookieo;
        if (!.equals(cookie.name)) {
            return false;
        }
        return !( != null ? !.equals(cookie.value) : cookie.value != null);
    }
    @Override
    public int hashCode() {
        return .hashCode();
    }
    public static class Builder {
        private final String name;
        private final String value;
        private String path;
        private String domain;
        private Date expiry;
        private boolean secure;
        public Builder(String nameString value) {
            this. = name;
            this. = value;
        }
        public Builder domain(String host) {
            this. = stripPort(host);
            return this;
        }
        public Builder path(String path) {
            this. = path;
            return this;
        }
        public Builder expiresOn(Date expiry) {
            this. = expiry;
            return this;
        }
        public Builder isSecure(boolean secure) {
            this. = secure;
            return this;
        }
        public Cookie build() {
            return new Cookie();
        }
    }
New to GrepCode? Check out our FAQ X