FileDocCategorySizeDatePackage
ServerCookie.javaAPI DocApache Tomcat 6.0.1410728Fri Jul 20 04:20:30 BST 2007org.apache.tomcat.util.http

ServerCookie

public class ServerCookie extends Object implements Serializable
Server-side cookie representation. Allows recycling and uses MessageBytes as low-level representation ( and thus the byte-> char conversion can be delayed until we know the charset ). Tomcat.core uses this recyclable object to represent cookies, and the facade will convert it to the external representation.

Fields Summary
private static org.apache.juli.logging.Log
log
private org.apache.tomcat.util.buf.MessageBytes
name
private org.apache.tomcat.util.buf.MessageBytes
value
private org.apache.tomcat.util.buf.MessageBytes
comment
private org.apache.tomcat.util.buf.MessageBytes
domain
private int
maxAge
private org.apache.tomcat.util.buf.MessageBytes
path
private boolean
secure
private int
version
private static final String
tspecials
private static final String
tspecials2
private static final String
ancientDate
static final int
dbg
Constructors Summary
public ServerCookie()

        // ;Version=1

    //XXX CommentURL, Port -> use notes ?
    
      

    
Methods Summary
public static voidappendCookieValue(java.lang.StringBuffer buf, int version, java.lang.String name, java.lang.String value, java.lang.String path, java.lang.String domain, java.lang.String comment, int maxAge, boolean isSecure)


         
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                            
    
        // this part is the same for all cookies
        buf.append( name );
        buf.append("=");
        maybeQuote2(version, buf, value);

        // XXX Netscape cookie: "; "
         // add version 1 specific information
        if (version == 1) {
            // Version=1 ... required
            buf.append ("; Version=1");

            // Comment=comment
            if ( comment!=null ) {
                buf.append ("; Comment=");
                maybeQuote (version, buf, comment);
            }
        }
        
        // add domain information, if present

        if (domain!=null) {
            buf.append("; Domain=");
            maybeQuote (version, buf, domain);
        }

        // Max-Age=secs/Discard ... or use old "Expires" format
        if (maxAge >= 0) {
            if (version == 0) {
                // XXX XXX XXX We need to send both, for
                // interoperatibility (long word )
                buf.append ("; Expires=");
                // Wdy, DD-Mon-YY HH:MM:SS GMT ( Expires netscape format )
                // To expire we need to set the time back in future
                // ( pfrieden@dChain.com )
                if (maxAge == 0)
                    buf.append( ancientDate );
                else
                    DateTool.formatOldCookie
                        (new Date( System.currentTimeMillis() +
                                   maxAge *1000L), buf,
                         new FieldPosition(0));

            } else {
                buf.append ("; Max-Age=");
                buf.append (maxAge);
            }
        }

        // Path=path
        if (path!=null) {
            buf.append ("; Path=");
            maybeQuote (version, buf, path);
        }

        // Secure
        if (isSecure) {
          buf.append ("; Secure");
        }
        
        
    
public static booleancheckName(java.lang.String name)

        if (!isToken(name)
                || name.equalsIgnoreCase("Comment")        // rfc2019
                || name.equalsIgnoreCase("Discard")        // 2019++
                || name.equalsIgnoreCase("Domain")
                || name.equalsIgnoreCase("Expires")        // (old cookies)
                || name.equalsIgnoreCase("Max-Age")        // rfc2019
                || name.equalsIgnoreCase("Path")
                || name.equalsIgnoreCase("Secure")
                || name.equalsIgnoreCase("Version")
            ) {
            return false;
        }
        return true;
    
private static java.lang.StringescapeDoubleQuotes(java.lang.String s)
Escapes any double quotes in the given string.

param
s the input string
return
The (possibly) escaped string


        if (s == null || s.length() == 0 || s.indexOf('"") == -1) {
            return s;
        }

        StringBuffer b = new StringBuffer();
        char p = s.charAt(0);
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '"" && p != '\\")
                b.append('\\").append('"");
            else
                b.append(c);
            p = c;
        }

        return b.toString();
    
public org.apache.tomcat.util.buf.MessageBytesgetComment()

        return comment;
    
public java.lang.StringgetCookieHeaderName()
Return the header name to set the cookie, based on cookie version

        return getCookieHeaderName(version);
    
public static java.lang.StringgetCookieHeaderName(int version)
Return the header name to set the cookie, based on cookie version

        if( dbg>0 ) log( (version==1) ? "Set-Cookie2" : "Set-Cookie");
        if (version == 1) {
            // RFC2109
            return "Set-Cookie";
            // XXX RFC2965 is not standard yet, and Set-Cookie2
            // is not supported by Netscape 4, 6, IE 3, 5 .
            // It is supported by Lynx, and there is hope 
            //            return "Set-Cookie2";
        } else {
            // Old Netscape
            return "Set-Cookie";
        }
    
public org.apache.tomcat.util.buf.MessageBytesgetDomain()

        return domain;
    
public intgetMaxAge()

        return maxAge;
    
public org.apache.tomcat.util.buf.MessageBytesgetName()

        return name;
    
public org.apache.tomcat.util.buf.MessageBytesgetPath()

        return path;
    
public booleangetSecure()

        return secure;
    
public org.apache.tomcat.util.buf.MessageBytesgetValue()

        return value;
    
public intgetVersion()

        return version;
    
public static booleanisToken(java.lang.String value)


    /*
     * Tests a string and returns true if the string counts as a
     * reserved token in the Java language.
     *
     * @param value the <code>String</code> to be tested
     *
     * @return      <code>true</code> if the <code>String</code> is a reserved
     *              token; <code>false</code> if it is not
     */
         
        if( value==null) return true;
        int len = value.length();

        for (int i = 0; i < len; i++) {
            char c = value.charAt(i);

            if (c < 0x20 || c >= 0x7f || tspecials.indexOf(c) != -1)
                return false;
        }
        return true;
    
public static booleanisToken2(java.lang.String value)

        if( value==null) return true;
        int len = value.length();

        for (int i = 0; i < len; i++) {
            char c = value.charAt(i);

            if (c < 0x20 || c >= 0x7f || tspecials2.indexOf(c) != -1)
                return false;
        }
        return true;
    
public static voidlog(java.lang.String s)

          
        if (log.isDebugEnabled())
            log.debug("ServerCookie: " + s);
    
public static voidmaybeQuote(int version, java.lang.StringBuffer buf, java.lang.String value)

        // special case - a \n or \r  shouldn't happen in any case
        if (isToken(value)) {
            buf.append(value);
        } else {
            buf.append('"");
            buf.append(escapeDoubleQuotes(value));
            buf.append('"");
        }
    
public static voidmaybeQuote2(int version, java.lang.StringBuffer buf, java.lang.String value)

        // special case - a \n or \r  shouldn't happen in any case
        if (isToken2(value)) {
            buf.append(value);
        } else {
            buf.append('"");
            buf.append(escapeDoubleQuotes(value));
            buf.append('"");
        }
    
public voidrecycle()

        path.recycle();
            name.recycle();
            value.recycle();
            comment.recycle();
            maxAge=-1;
            path.recycle();
        domain.recycle();
            version=0;
            secure=false;
    
public voidsetMaxAge(int expiry)

        maxAge = expiry;
    
public voidsetSecure(boolean flag)

        secure = flag;
    
public voidsetVersion(int v)

        version = v;
    
public java.lang.StringtoString()

        return "Cookie " + getName() + "=" + getValue() + " ; "
            + getVersion() + " " + getPath() + " " + getDomain();