jdk/src/share/classes/java/net/HttpCookie.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 480 c309ca1d3a86
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.NoSuchElementException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.text.SimpleDateFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.TimeZone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.lang.NullPointerException;  // for javadoc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * An HttpCookie object represents an http cookie, which carries state
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * information between server and user agent. Cookie is widely adopted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * to create stateful sessions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>There are 3 http cookie specifications:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *   Netscape draft<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *   RFC 2109 - <a href="http://www.ietf.org/rfc/rfc2109.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <i>http://www.ietf.org/rfc/rfc2109.txt</i></a><br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *   RFC 2965 - <a href="http://www.ietf.org/rfc/rfc2965.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <i>http://www.ietf.org/rfc/rfc2965.txt</i></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>HttpCookie class can accept all these 3 forms of syntax.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author Edward Wang
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public final class HttpCookie implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /* ---------------- Fields -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // The value of the cookie itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private String name;        // NAME= ... "$Name" style is reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private String value;       // value of NAME
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // Attributes encoded in the header's cookie fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private String comment;     // Comment=VALUE ... describes cookie's use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private String commentURL;  // CommentURL="http URL" ... describes cookie's use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private boolean toDiscard;  // Discard ... discard cookie unconditionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private String domain;      // Domain=VALUE ... domain that sees cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private long maxAge = MAX_AGE_UNSPECIFIED;  // Max-Age=VALUE ... cookies auto-expire
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private String path;        // Path=VALUE ... URLs that see the cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private String portlist;    // Port[="portlist"] ... the port cookie may be returned to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private boolean secure;     // Secure ... e.g. use SSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private int version = 1;    // Version=1 ... RFC 2965 style
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // Hold the creation time (in seconds) of the http cookie for later
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    // expiration calculation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private long whenCreated = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // Since the positive and zero max-age have their meanings,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    // this value serves as a hint as 'not specify max-age'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private final static long MAX_AGE_UNSPECIFIED = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    // date format used by Netscape's cookie draft
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private final static String NETSCAPE_COOKIE_DATE_FORMAT = "EEE',' dd-MMM-yyyy HH:mm:ss 'GMT'";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    // constant strings represent set-cookie header token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private final static String SET_COOKIE = "set-cookie:";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private final static String SET_COOKIE2 = "set-cookie2:";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /* ---------------- Ctors -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Constructs a cookie with a specified name and value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * <p>The name must conform to RFC 2965. That means it can contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * only ASCII alphanumeric characters and cannot contain commas,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * semicolons, or white space or begin with a $ character. The cookie's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * name cannot be changed after creation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * <p>The value can be anything the server chooses to send. Its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * value is probably of interest only to the server. The cookie's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * value can be changed after creation with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * <code>setValue</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * <p>By default, cookies are created according to the RFC 2965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * cookie specification. The version can be changed with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * <code>setVersion</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param name                      a <code>String</code> specifying the name of the cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @param value                     a <code>String</code> specifying the value of the cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @throws IllegalArgumentException if the cookie name contains illegal characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *                                  or it is one of the tokens reserved for use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *                                  by the cookie protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @throws NullPointerException     if <tt>name</tt> is <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @see #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @see #setVersion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public HttpCookie(String name, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        name = name.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (name.length() == 0 || !isToken(name) || isReserved(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            throw new IllegalArgumentException("Illegal cookie name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        toDiscard = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        secure = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        whenCreated = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Constructs cookies from set-cookie or set-cookie2 header string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * RFC 2965 section 3.2.2 set-cookie2 syntax indicates that one header line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * may contain more than one cookie definitions, so this is a static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * utility method instead of another constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @param header    a <tt>String</tt> specifying the set-cookie header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *                  The header should start with "set-cookie", or "set-cookie2"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *                  token; or it should have no leading token at all.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @return          a List of cookie parsed from header line string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @throws IllegalArgumentException if header string violates the cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *                                  specification's syntax, or the cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *                                  name contains llegal characters, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *                                  the cookie name is one of the tokens
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *                                  reserved for use by the cookie protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @throws NullPointerException     if the header string is <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public static List<HttpCookie> parse(String header) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        int version = guessCookieVersion(header);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        // if header start with set-cookie or set-cookie2, strip it off
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (startsWithIgnoreCase(header, SET_COOKIE2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            header = header.substring(SET_COOKIE2.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        } else if (startsWithIgnoreCase(header, SET_COOKIE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            header = header.substring(SET_COOKIE.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        List<HttpCookie> cookies = new java.util.ArrayList<HttpCookie>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        // The Netscape cookie may have a comma in its expires attribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        // while the comma is the delimiter in rfc 2965/2109 cookie header string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        // so the parse logic is slightly different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (version == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            // Netscape draft cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            HttpCookie cookie = parseInternal(header);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            cookie.setVersion(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            cookies.add(cookie);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            // rfc2965/2109 cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            // if header string contains more than one cookie,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            // it'll separate them with comma
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            List<String> cookieStrings = splitMultiCookies(header);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            for (String cookieStr : cookieStrings) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                HttpCookie cookie = parseInternal(cookieStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                cookie.setVersion(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                cookies.add(cookie);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return cookies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /* ---------------- Public operations -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Reports whether this http cookie has expired or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @return  <tt>true</tt> to indicate this http cookie has expired;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *          otherwise, <tt>false</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public boolean hasExpired() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if (maxAge == 0) return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        // if not specify max-age, this cookie should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        // discarded when user agent is to be closed, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        // it is not expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (maxAge == MAX_AGE_UNSPECIFIED) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        long deltaSecond = (System.currentTimeMillis() - whenCreated) / 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (deltaSecond > maxAge)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Specifies a comment that describes a cookie's purpose.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * The comment is useful if the browser presents the cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * to the user. Comments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * are not supported by Netscape Version 0 cookies.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @param purpose           a <code>String</code> specifying the comment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *                          to display to the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @see #getComment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    public void setComment(String purpose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        comment = purpose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Returns the comment describing the purpose of this cookie, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * <code>null</code> if the cookie has no comment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @return                  a <code>String</code> containing the comment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *                          or <code>null</code> if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @see #setComment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public String getComment() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return comment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Specifies a comment url that describes a cookie's purpose.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * The comment url is useful if the browser presents the cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * to the user. Comment url is RFC 2965 only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @param purpose           a <code>String</code> specifying the comment url
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *                          to display to the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @see #getCommentURL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public void setCommentURL(String purpose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        commentURL = purpose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Returns the comment url describing the purpose of this cookie, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * <code>null</code> if the cookie has no comment url.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @return                  a <code>String</code> containing the comment url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *                          or <code>null</code> if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @see #setCommentURL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    public String getCommentURL() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        return commentURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Specify whether user agent should discard the cookie unconditionally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * This is RFC 2965 only attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @param discard   <tt>true</tt> indicates to discard cookie unconditionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @see #getDiscard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public void setDiscard(boolean discard) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        toDiscard = discard;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Return the discard attribute of the cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @return  a <tt>boolean</tt> to represent this cookie's discard attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @see #setDiscard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    public boolean getDiscard() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return toDiscard;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * Specify the portlist of the cookie, which restricts the port(s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * to which a cookie may be sent back in a Cookie header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @param ports     a <tt>String</tt> specify the port list, which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *                  comma seperated series of digits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @see #getPortlist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    public void setPortlist(String ports) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        portlist = ports;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * Return the port list attribute of the cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @return  a <tt>String</tt> contains the port list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *          or <tt>null</tt> if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @see #setPortlist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    public String getPortlist() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        return portlist;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * Specifies the domain within which this cookie should be presented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * <p>The form of the domain name is specified by RFC 2965. A domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * name begins with a dot (<code>.foo.com</code>) and means that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * the cookie is visible to servers in a specified Domain Name System
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * (DNS) zone (for example, <code>www.foo.com</code>, but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * <code>a.b.foo.com</code>). By default, cookies are only returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * to the server that sent them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @param pattern           a <code>String</code> containing the domain name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *                          within which this cookie is visible;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *                          form is according to RFC 2965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @see #getDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    public void setDomain(String pattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        if (pattern != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            domain = pattern.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            domain = pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * Returns the domain name set for this cookie. The form of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * the domain name is set by RFC 2965.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @return                  a <code>String</code> containing the domain name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @see #setDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    public String getDomain() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        return domain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * Sets the maximum age of the cookie in seconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * <p>A positive value indicates that the cookie will expire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * after that many seconds have passed. Note that the value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * the <i>maximum</i> age when the cookie will expire, not the cookie's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * current age.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * <p>A negative value means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * that the cookie is not stored persistently and will be deleted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * when the Web browser exits. A zero value causes the cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * to be deleted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @param expiry            an integer specifying the maximum age of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *                          cookie in seconds; if zero, the cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *                          should be discarded immediately;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *                          otherwise, the cookie's max age is unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @see #getMaxAge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    public void setMaxAge(long expiry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        maxAge = expiry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * Returns the maximum age of the cookie, specified in seconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * By default, <code>-1</code> indicating the cookie will persist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * until browser shutdown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @return                  an integer specifying the maximum age of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *                          cookie in seconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @see #setMaxAge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    public long getMaxAge() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        return maxAge;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * Specifies a path for the cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * to which the client should return the cookie.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * <p>The cookie is visible to all the pages in the directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * you specify, and all the pages in that directory's subdirectories.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * A cookie's path must include the servlet that set the cookie,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * for example, <i>/catalog</i>, which makes the cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * visible to all directories on the server under <i>/catalog</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * <p>Consult RFC 2965 (available on the Internet) for more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * information on setting path names for cookies.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * @param uri               a <code>String</code> specifying a path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @see #getPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    public void setPath(String uri) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        path = uri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * Returns the path on the server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * to which the browser returns this cookie. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * cookie is visible to all subpaths on the server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * @return          a <code>String</code> specifying a path that contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *                  a servlet name, for example, <i>/catalog</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @see #setPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    public String getPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        return path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * Indicates to the browser whether the cookie should only be sent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * using a secure protocol, such as HTTPS or SSL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * <p>The default value is <code>false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * @param flag      if <code>true</code>, sends the cookie from the browser
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *                  to the server using only when using a secure protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *                  if <code>false</code>, sent on any protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * @see #getSecure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    public void setSecure(boolean flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        secure = flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * Returns <code>true</code> if the browser is sending cookies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * only over a secure protocol, or <code>false</code> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * browser can send cookies using any protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * @return          <code>true</code> if the browser can use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     *                  any standard protocol; otherwise, <code>false</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @see #setSecure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    public boolean getSecure() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        return secure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * Returns the name of the cookie. The name cannot be changed after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * creation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * @return          a <code>String</code> specifying the cookie's name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * Assigns a new value to a cookie after the cookie is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * If you use a binary value, you may want to use BASE64 encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * <p>With Version 0 cookies, values should not contain white
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * space, brackets, parentheses, equals signs, commas,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * double quotes, slashes, question marks, at signs, colons,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * and semicolons. Empty values may not behave the same way
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * on all browsers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * @param newValue          a <code>String</code> specifying the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @see #getValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    public void setValue(String newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        value = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * Returns the value of the cookie.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @return                  a <code>String</code> containing the cookie's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *                          present value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * @see #setValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    public String getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * Returns the version of the protocol this cookie complies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * with. Version 1 complies with RFC 2965/2109,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * and version 0 complies with the original
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * cookie specification drafted by Netscape. Cookies provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * by a browser use and identify the browser's cookie version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * @return                  0 if the cookie complies with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     *                          original Netscape specification; 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     *                          if the cookie complies with RFC 2965/2109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * @see #setVersion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    public int getVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        return version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * Sets the version of the cookie protocol this cookie complies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * with. Version 0 complies with the original Netscape cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * specification. Version 1 complies with RFC 2965/2109.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * @param v                 0 if the cookie should comply with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *                          the original Netscape specification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     *                          1 if the cookie should comply with RFC 2965/2109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * @throws IllegalArgumentException if <tt>v</tt> is neither 0 nor 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * @see #getVersion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    public void setVersion(int v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        if (v != 0 && v != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            throw new IllegalArgumentException("cookie version should be 0 or 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        version = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * The utility method to check whether a host name is in a domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * <p>This concept is described in the cookie specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * To understand the concept, some terminologies need to be defined first:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * effective host name = hostname if host name contains dot<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;or = hostname.local if not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * <p>Host A's name domain-matches host B's if:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * <blockquote><ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     *   <li>their host name strings string-compare equal; or</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     *   <li>A is a HDN string and has the form NB, where N is a non-empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     *   name string, B has the form .B', and B' is a HDN string.  (So,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     *   x.y.com domain-matches .Y.com but not Y.com.)</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * </ul></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * <p>A host isn't in a domain (RFC 2965 sec. 3.3.2) if:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * <blockquote><ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     *   <li>The value for the Domain attribute contains no embedded dots,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     *   and the value is not .local.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     *   <li>The effective host name that derives from the request-host does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *   not domain-match the Domain attribute.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     *   <li>The request-host is a HDN (not IP address) and has the form HD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     *   where D is the value of the Domain attribute, and H is a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     *   that contains one or more dots.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * </ul></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * <p>Examples:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * <blockquote><ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     *   <li>A Set-Cookie2 from request-host y.x.foo.com for Domain=.foo.com
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     *   would be rejected, because H is y.x and contains a dot.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     *   <li>A Set-Cookie2 from request-host x.foo.com for Domain=.foo.com
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *   would be accepted.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     *   <li>A Set-Cookie2 with Domain=.com or Domain=.com., will always be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     *   rejected, because there is no embedded dot.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     *   <li>A Set-Cookie2 with Domain=ajax.com will be accepted, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     *   value for Domain will be taken to be .ajax.com, because a dot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     *   gets prepended to the value.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     *   <li>A Set-Cookie2 from request-host example for Domain=.local will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     *   be accepted, because the effective host name for the request-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     *   host is example.local, and example.local domain-matches .local.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * </ul></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * @param domain    the domain name to check host name with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * @param host      the host name in question
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * @return          <tt>true</tt> if they domain-matches; <tt>false</tt> if not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    public static boolean domainMatches(String domain, String host) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        if (domain == null || host == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        // if there's no embedded dot in domain and domain is not .local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        boolean isLocalDomain = ".local".equalsIgnoreCase(domain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        int embeddedDotInDomain = domain.indexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        if (embeddedDotInDomain == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            embeddedDotInDomain = domain.indexOf('.', 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        if (!isLocalDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            && (embeddedDotInDomain == -1 || embeddedDotInDomain == domain.length() - 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        // if the host name contains no dot and the domain name is .local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        int firstDotInHost = host.indexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        if (firstDotInHost == -1 && isLocalDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        int domainLength = domain.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        int lengthDiff = host.length() - domainLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        if (lengthDiff == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            // if the host name and the domain name are just string-compare euqal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            return host.equalsIgnoreCase(domain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        else if (lengthDiff > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            // need to check H & D component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            String H = host.substring(0, lengthDiff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            String D = host.substring(lengthDiff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            return (H.indexOf('.') == -1 && D.equalsIgnoreCase(domain));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        else if (lengthDiff == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            // if domain is actually .host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            return (domain.charAt(0) == '.' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                        host.equalsIgnoreCase(domain.substring(1)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * Constructs a cookie header string representation of this cookie,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * which is in the format defined by corresponding cookie specification,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * but without the leading "Cookie:" token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * @return  a string form of the cookie. The string has the defined format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        if (getVersion() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            return toRFC2965HeaderString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            return toNetscapeHeaderString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * Test the equality of two http cookies.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * <p> The result is <tt>true</tt> only if two cookies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * come from same domain (case-insensitive),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * have same name (case-insensitive),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * and have same path (case-sensitive).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * @return          <tt>true</tt> if 2 http cookies equal to each other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     *                  otherwise, <tt>false</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        if (!(obj instanceof HttpCookie))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        HttpCookie other = (HttpCookie)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        // One http cookie equals to another cookie (RFC 2965 sec. 3.3.3) if:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        //   1. they come from same domain (case-insensitive),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        //   2. have same name (case-insensitive),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        //   3. and have same path (case-sensitive).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        return equalsIgnoreCase(getName(), other.getName()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
               equalsIgnoreCase(getDomain(), other.getDomain()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
               equals(getPath(), other.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * Return hash code of this http cookie. The result is the sum of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * hash code value of three significant components of this cookie:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * name, domain, and path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * That is, the hash code is the value of the expression:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * getName().toLowerCase().hashCode()<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * + getDomain().toLowerCase().hashCode()<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * + getPath().hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * @return          this http cookie's hash code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        int h1 = name.toLowerCase().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        int h2 = (domain!=null) ? domain.toLowerCase().hashCode() : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        int h3 = (path!=null) ? path.hashCode() : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        return h1 + h2 + h3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * Create and return a copy of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * @return          a clone of this http cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            return super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            throw new RuntimeException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    /* ---------------- Private operations -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    // Note -- disabled for now to allow full Netscape compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    // from RFC 2068, token special case characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    // private static final String tspecials = "()<>@,;:\\\"/[]?={} \t";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    private static final String tspecials = ",;";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * Tests a string and returns true if the string counts as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * @param value             the <code>String</code> to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * @return                  <code>true</code> if the <code>String</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     *                          a token; <code>false</code> if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    private static boolean isToken(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        int len = value.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            char c = value.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            if (c < 0x20 || c >= 0x7f || tspecials.indexOf(c) != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * @param name      the name to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * @return          <tt>true</tt> if the name is reserved by cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     *                  specification, <tt>false</tt> if it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    private static boolean isReserved(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        if (name.equalsIgnoreCase("Comment")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            || name.equalsIgnoreCase("CommentURL")      // rfc2965 only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            || name.equalsIgnoreCase("Discard")         // rfc2965 only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            || name.equalsIgnoreCase("Domain")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            || name.equalsIgnoreCase("Expires")         // netscape draft only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            || name.equalsIgnoreCase("Max-Age")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            || name.equalsIgnoreCase("Path")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            || name.equalsIgnoreCase("Port")            // rfc2965 only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            || name.equalsIgnoreCase("Secure")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            || name.equalsIgnoreCase("Version")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            || name.charAt(0) == '$')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * Parse header string to cookie object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * @param header    header string; should contain only one NAME=VALUE pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * @return          an HttpCookie being extracted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * @throws IllegalArgumentException if header string violates the cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     *                                  specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    private static HttpCookie parseInternal(String header)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        HttpCookie cookie = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        String namevaluePair = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        StringTokenizer tokenizer = new StringTokenizer(header, ";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        // there should always have at least on name-value pair;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        // it's cookie's name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            namevaluePair = tokenizer.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            int index = namevaluePair.indexOf('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            if (index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                String name = namevaluePair.substring(0, index).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                String value = namevaluePair.substring(index + 1).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                cookie = new HttpCookie(name, stripOffSurroundingQuote(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                // no "=" in name-value pair; it's an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                throw new IllegalArgumentException("Invalid cookie name-value pair");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        } catch (NoSuchElementException ignored) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            throw new IllegalArgumentException("Empty cookie header string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        // remaining name-value pairs are cookie's attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        while (tokenizer.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            namevaluePair = tokenizer.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            int index = namevaluePair.indexOf('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            String name, value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            if (index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                name = namevaluePair.substring(0, index).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                value = namevaluePair.substring(index + 1).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                name = namevaluePair.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            // assign attribute to cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            assignAttribute(cookie, name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        return cookie;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * assign cookie attribute value to attribute name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * use a map to simulate method dispatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    static interface CookieAttributeAssignor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            public void assign(HttpCookie cookie, String attrName, String attrValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    static java.util.Map<String, CookieAttributeAssignor> assignors = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        assignors = new java.util.HashMap<String, CookieAttributeAssignor>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        assignors.put("comment", new CookieAttributeAssignor(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                public void assign(HttpCookie cookie, String attrName, String attrValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                    if (cookie.getComment() == null) cookie.setComment(attrValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        assignors.put("commenturl", new CookieAttributeAssignor(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                public void assign(HttpCookie cookie, String attrName, String attrValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                    if (cookie.getCommentURL() == null) cookie.setCommentURL(attrValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        assignors.put("discard", new CookieAttributeAssignor(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                public void assign(HttpCookie cookie, String attrName, String attrValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                    cookie.setDiscard(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        assignors.put("domain", new CookieAttributeAssignor(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                public void assign(HttpCookie cookie, String attrName, String attrValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                    if (cookie.getDomain() == null) cookie.setDomain(attrValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        assignors.put("max-age", new CookieAttributeAssignor(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                public void assign(HttpCookie cookie, String attrName, String attrValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                        long maxage = Long.parseLong(attrValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                        if (cookie.getMaxAge() == MAX_AGE_UNSPECIFIED) cookie.setMaxAge(maxage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                    } catch (NumberFormatException ignored) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                        throw new IllegalArgumentException("Illegal cookie max-age attribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        assignors.put("path", new CookieAttributeAssignor(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                public void assign(HttpCookie cookie, String attrName, String attrValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                    if (cookie.getPath() == null) cookie.setPath(attrValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        assignors.put("port", new CookieAttributeAssignor(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                public void assign(HttpCookie cookie, String attrName, String attrValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                    if (cookie.getPortlist() == null) cookie.setPortlist(attrValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        assignors.put("secure", new CookieAttributeAssignor(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                public void assign(HttpCookie cookie, String attrName, String attrValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                    cookie.setSecure(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        assignors.put("version", new CookieAttributeAssignor(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                public void assign(HttpCookie cookie, String attrName, String attrValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                        int version = Integer.parseInt(attrValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                        cookie.setVersion(version);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                    } catch (NumberFormatException ignored) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                        throw new IllegalArgumentException("Illegal cookie version attribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        assignors.put("expires", new CookieAttributeAssignor(){ // Netscape only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                public void assign(HttpCookie cookie, String attrName, String attrValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                    if (cookie.getMaxAge() == MAX_AGE_UNSPECIFIED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                        cookie.setMaxAge(cookie.expiryDate2DeltaSeconds(attrValue));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    private static void assignAttribute(HttpCookie cookie,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                                       String attrName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                                       String attrValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        // strip off the surrounding "-sign if there's any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        attrValue = stripOffSurroundingQuote(attrValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        CookieAttributeAssignor assignor = assignors.get(attrName.toLowerCase());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        if (assignor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            assignor.assign(cookie, attrName, attrValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            // must be an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            throw new IllegalArgumentException("Illegal cookie attribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * Constructs a string representation of this cookie. The string format is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * as Netscape spec, but without leading "Cookie:" token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
    private String toNetscapeHeaderString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        sb.append(getName() + "=" + getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * Constructs a string representation of this cookie. The string format is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     * as RFC 2965/2109, but without leading "Cookie:" token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    private String toRFC2965HeaderString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        sb.append(getName()).append("=\"").append(getValue()).append('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        if (getPath() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            sb.append(";$Path=\"").append(getPath()).append('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        if (getDomain() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
            sb.append(";$Domain=\"").append(getDomain()).append('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        if (getPortlist() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            sb.append(";$Port=\"").append(getPortlist()).append('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * @param dateString        a date string in format of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     *                          "EEE',' dd-MMM-yyyy HH:mm:ss 'GMT'",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     *                          which defined in Netscape cookie spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * @return                  delta seconds between this cookie's creation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     *                          time and the time specified by dateString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    private long expiryDate2DeltaSeconds(String dateString) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        SimpleDateFormat df = new SimpleDateFormat(NETSCAPE_COOKIE_DATE_FORMAT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        df.setTimeZone(TimeZone.getTimeZone("GMT"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            Date date = df.parse(dateString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            return (date.getTime() - whenCreated) / 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * try to guess the cookie version through set-cookie header string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    private static int guessCookieVersion(String header) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        int version = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        header = header.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        if (header.indexOf("expires=") != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            // only netscape cookie using 'expires'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            version = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        } else if (header.indexOf("version=") != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            // version is mandatory for rfc 2965/2109 cookie
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            version = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        } else if (header.indexOf("max-age") != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            // rfc 2965/2109 use 'max-age'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            version = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        } else if (startsWithIgnoreCase(header, SET_COOKIE2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            // only rfc 2965 cookie starts with 'set-cookie2'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            version = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        return version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    private static String stripOffSurroundingQuote(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        if (str != null && str.length() > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            str.charAt(0) == '"' && str.charAt(str.length() - 1) == '"') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            return str.substring(1, str.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
    private static boolean equalsIgnoreCase(String s, String t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        if (s == t) return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        if ((s != null) && (t != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            return s.equalsIgnoreCase(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    private static boolean equals(String s, String t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        if (s == t) return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        if ((s != null) && (t != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            return s.equals(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    private static boolean startsWithIgnoreCase(String s, String start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        if (s == null || start == null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        if (s.length() >= start.length() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                start.equalsIgnoreCase(s.substring(0, start.length()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * Split cookie header string according to rfc 2965:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     *   1) split where it is a comma;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     *   2) but not the comma surrounding by double-quotes, which is the comma
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     *      inside port list or embeded URIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * @param header            the cookie header string to split
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * @return                  list of strings; never null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
    private static List<String> splitMultiCookies(String header) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        List<String> cookies = new java.util.ArrayList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        int quoteCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        int p, q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        for (p = 0, q = 0; p < header.length(); p++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
            char c = header.charAt(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            if (c == '"') quoteCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            if (c == ',' && (quoteCount % 2 == 0)) {      // it is comma and not surrounding by double-quotes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                cookies.add(header.substring(q, p));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                q = p + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        cookies.add(header.substring(q));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        return cookies;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
}