FileDocCategorySizeDatePackage
CookieTokenizer.javaAPI DocGlassfish v2 API6835Fri May 04 22:34:10 BST 2007com.sun.enterprise.web.util

CookieTokenizer

public class CookieTokenizer extends Object
Parse a Cookie: header into individual tokens according to RFC 2109.

Fields Summary
private static final int
MAX_COOKIE_TOKENS
Upper bound on the number of cookie tokens to accept. The limit is based on the 4 different attributes (4.3.4) and the 20 cookie minimum (6.3) given in RFC 2109 multiplied by 2 to accomodate the 2 tokens in each name=value pair ("JSESSIONID=1234" is 2 tokens).
private String[]
tokens
Array of cookie tokens. Even indices contain name tokens while odd indices contain value tokens (or null).
private int
numTokens
Number of cookie tokens currently in the tokens[] array.
Constructors Summary
Methods Summary
public intgetNumTokens()
Return the number of cookie tokens parsed from the Cookie: header.

        return numTokens;
    
private intparseNameValue(java.lang.String cookies, int beginIndex)
Parse a name=value pair from the Cookie: header.

param
cookies The Cookie: header to parse
param
beginIndex The index in cookies to begin parsing from, inclusive


                                   
          
        int length = cookies.length();
        int index = beginIndex;

        while (index < length) {
            switch (cookies.charAt(index)) {
            case ';":
            case ',":
                // Found end of name token without value
                tokens[numTokens] = cookies.substring(beginIndex, index).trim();
                if (tokens[numTokens].length() > 0) {
                    numTokens++;
                    tokens[numTokens] = null;
                    numTokens++;
                }
                return index + 1;

            case '=":
                // Found end of name token with value
                tokens[numTokens] = cookies.substring(beginIndex, index).trim();
                numTokens++;
                return parseValue(cookies, index + 1);

            case '"":
                // Skip past quoted span
                do index++; while (cookies.charAt(index) != '"");
                break;
            }

            index++;
        }

        if (index > beginIndex) {
            // Found end of name token without value
            tokens[numTokens] = cookies.substring(beginIndex, index).trim();
            if (tokens[numTokens].length() > 0) {
                numTokens++;
                tokens[numTokens] = null;
                numTokens++;
            }
        }

        return index;
    
private intparseValue(java.lang.String cookies, int beginIndex)
Parse the value token from a name=value pair.

param
cookies The Cookie: header to parse
param
beginIndex The index in cookies to begin parsing from, inclusive

        int length = cookies.length();
        int index = beginIndex;

        while (index < length) {
            switch (cookies.charAt(index)) {
            case ';":
            case ',":
                // Found end of value token
                tokens[numTokens] = cookies.substring(beginIndex, index).trim();
                numTokens++;
                return index + 1;

            case '"":
                // Skip past quoted span
                do index++; while (cookies.charAt(index) != '"");
                break;
            }

            index++;
        }

        // Found end of value token
        tokens[numTokens] = cookies.substring(beginIndex, index).trim();
        numTokens++;

        return index;
    
public java.lang.StringtokenAt(int index)
Returns a given cookie token from the Cookie: header.

param
index The index of the cookie token to return

        return tokens[index];
    
public inttokenize(java.lang.String cookies)
Parse the name=value tokens from a Cookie: header.

param
cookies The Cookie: header to parse

        numTokens = 0;

        if (cookies != null) {
            try {
                // Advance through cookies, parsing name=value pairs
                int length = cookies.length();
                int index = 0;
                while (index < length)
                    index = parseNameValue(cookies, index);
            }
            catch (ArrayIndexOutOfBoundsException e) {
                // Filled up the tokens[] array
            }
            catch (IndexOutOfBoundsException e) {
                // Walked off the end of the cookies header
            }
        }

        return numTokens;