Class CookieParser
- java.lang.Object
-
- org.simpleframework.common.parse.Parser
-
- org.simpleframework.http.parse.CookieParser
-
- All Implemented Interfaces:
java.lang.Iterable<Cookie>
public class CookieParser extends Parser implements java.lang.Iterable<Cookie>
CookieParser is used to parse the cookie header. The cookie header is one of the headers that is used by the HTTP state management mechanism. The Cookie header is the header that is sent from the client to the server in response to a Set-Cookie header. The syntax of the Cookie header as taken from RFC 2109, HTTP State Management Mechanism.cookie = "Cookie:" cookie-version 1*((";" | ",") cookie-value) cookie-value = NAME "=" VALUE [";" path] [";" domain] cookie-version = "$Version" "=" value NAME = attr VALUE = value path = "$Path" "=" value domain = "$Domain" "=" valueThe cookie header may consist of several cookies. Each cookie can be extracted from the header by examining the it syntax of the cookie header. The syntax of the cookie header is defined in RFC 2109.Each cookie has a
$Versionattribute followed by multiple cookies. Each contains a name and a value, followed by an optional$Pathand$Domainattribute. This will parse a given cookie header and return each cookie extracted as aCookieobject.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classCookieParser.SequenceThis is used to represent anIteratorthat will iterate over the available cookies within the provided source text.private classCookieParser.TokenThis is a token object that is used to store the offset and length of a region of chars in theCookieParser.bufarray.
-
Field Summary
Fields Modifier and Type Field Description private CookieParser.TokendomainUsed to store the$Domainvalues.private booleanfinishedDetermines when theParserhas finished.private CookieParser.TokennameUsed to store the name of theCookie.private booleanparsedUsed so theParserdoes not parse twice.private CookieParser.TokenpathUsed to store the$Pathvalues.private CookieParser.TokenvalueUsed to store the value of theCookie.private intversionVersion of theCookiebeing parsed.
-
Constructor Summary
Constructors Constructor Description CookieParser()Create aCookieParserthat contains no cookies.CookieParser(java.lang.String header)This is primarily a convineance constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidcookie()This is used to parse aCookiefrom the buffer that contains theCookievalues.private voiddata()This initializes the value token and extracts the value of thisCookie.private voiddomain()Initializes the domain token and extracts the$Domainof thisCookie.private CookiegetCookie()Creates theCookiefrom the token objects.private CookiegetCookie(java.lang.String name, java.lang.String value)Creates theCookiefrom the token objects.protected voidinit()Resets the cookie and the buffer variables for thisCookieParser.java.util.Iterator<Cookie>iterator()This is used to acquire the cookie values from the provided the provided source text.private voidname()This initializes the name token and extracts the name of thisCookie.protected voidparse()This will extract the nextCookiefrom the buffer.private voidpath()This initializes the path token and extracts the$Pathof thisCookie.voidreset()This is used so that the collection ofCookiescan be reiterated.protected booleanskip(java.lang.String text)This is used to skip an arbitraryStringwithin thecharbuf.private booleanterminal(char ch)This is used to determine if a given iso8859-1 character is a terminal character.private voidvalue()Used to extract everything found after theNAME '='within aCookie.private voidversion()This extracts the$Versionof thisCookie.-
Methods inherited from class org.simpleframework.common.parse.Parser
digit, ensureCapacity, parse, space, toLower
-
-
-
-
Field Detail
-
finished
private boolean finished
Determines when theParserhas finished.
-
parsed
private boolean parsed
Used so theParserdoes not parse twice.
-
version
private int version
Version of theCookiebeing parsed.
-
name
private CookieParser.Token name
Used to store the name of theCookie.
-
value
private CookieParser.Token value
Used to store the value of theCookie.
-
path
private CookieParser.Token path
Used to store the$Pathvalues.
-
domain
private CookieParser.Token domain
Used to store the$Domainvalues.
-
-
Constructor Detail
-
CookieParser
public CookieParser()
Create aCookieParserthat contains no cookies. the instance will returnfalsefor thehasNextmethod. cookies may be parsed using this instance by using theparsemethod.
-
CookieParser
public CookieParser(java.lang.String header)
This is primarily a convineance constructor. This will parse theStringgiven to extract the cookies. This could be achived by calling the default no-arg constructor and then using the instance to invoke theparsemethod on thatString.- Parameters:
header- aStringcontaining a cookie value
-
-
Method Detail
-
init
protected void init()
Resets the cookie and the buffer variables for thisCookieParser. It is used to set the state of the parser to start parsing a new cookie.
-
parse
protected void parse()
This will extract the nextCookiefrom the buffer. If all the characters in the buffer have already been examined then this method will simply do nothing. Otherwise this will parse the remainder of the buffer and (if it follows RFC 2109) produce aCookie.
-
skip
protected boolean skip(java.lang.String text)
This is used to skip an arbitraryStringwithin thecharbuf. It checks the length of theStringfirst to ensure that it will not go out of bounds. A comparison is then made with the buffers contents and theStringif the reigon in the buffer matched theStringthen the offset within the buffer is increased by theString's length so that it has effectively skipped it.This
skipmethod will ignore all of the whitespace text. This will also skip trailing spaces within the the input text and all spaces within the source text. For example if the input was the string "s omete xt" and the source was "some text to skip" then the result of a skip ignoring spaces would be "to skip" in the source string, as the trailing spaces are also eaten by this.
-
iterator
public java.util.Iterator<Cookie> iterator()
This is used to acquire the cookie values from the provided the provided source text. This allows the cookie parser to be used within a for each loop to parse out the values of a cookie one by one so that they may be used or stored.- Specified by:
iteratorin interfacejava.lang.Iterable<Cookie>- Returns:
- this returns an iterator for extracting cookie value
-
reset
public void reset()
This is used so that the collection ofCookiescan be reiterated. This allows the collection to be reused. Theresetmethod will invoke the super classesinitmethod. This will reinitialize thisParserso the cookie will be reparsed.
-
getCookie
private Cookie getCookie()
Creates theCookiefrom the token objects. It is assumed that theCookieStringhas been parsed when this is called. This should only be used after theparsemethod has been called.If there is no
$Domainor$Pathwithin theCookieStringthen thegetDomainandgetPathare null.- Returns:
- the
Cookiethat was just parsed
-
getCookie
private Cookie getCookie(java.lang.String name, java.lang.String value)
Creates theCookiefrom the token objects. It is assumed that theCookieStringhas been parsed when this is called. This should only be used after theparsemethod has been called.If there is no
$Domainor$Pathwithin theCookieStringthen thegetDomainandgetPathare null.- Parameters:
name- the name that theCookiecontainsvalue- the value that theCookiecontains- Returns:
- the
Cookiethat was just parsed
-
cookie
private void cookie()
This is used to parse aCookiefrom the buffer that contains theCookievalues. This will first try to remove any trailing value after the version/prevCookieonce this is removed it will extract the name/value pair from theCookie. The name and value of theCookiewill be saved by the name and value tokens.
-
name
private void name()
This initializes the name token and extracts the name of thisCookie. The offset and length of the name will be saved in the name token. This will read allchar's upto but excluding the first '='charencountered from theoffwithin the buffer.
-
value
private void value()
Used to extract everything found after theNAME '='within aCookie. This extracts theCookievalue the$Pathand$Domainattributes if they exist (i.e.$Pathand$Domainare optional in a cookie see RFC 2109).The path method reads the terminal found before it as does the
domainmethod that is ";$Path" is read as the first part of the path method. This is because if there is no path the parser should not read data it does not know belongs to a specific part of theCookie.
-
data
private void data()
This initializes the value token and extracts the value of thisCookie. The offset and length of the value will be saved in the value token. This will read allchar's upto but excluding the first terminal char encountered from the off within the buffer, or if the value is a literal it will read a literal from the buffer (literal is any data between quotes except if the quote is prefixed with a backward slash character that is '\').
-
path
private void path()
This initializes the path token and extracts the$Pathof thisCookie. The offset and length of the path will be saved in the path token. This will read allchar's up to but excluding the first terminalcharencountered from theoffwithin the buffer, or if the value is a literal it will read a literal from the buffer (literal is any data between quotes except if the quote is prefixed with a backward slash character, that is '\').This reads the terminal before the
$Pathso that if there is no$Pathfor theCookiethen the character before it will not be read needlessly.
-
domain
private void domain()
Initializes the domain token and extracts the$Domainof thisCookie. The offset and length of the domain will be saved in the path token. This will read all characters up to but excluding the first terminalcharencountered from the off within the buffer, or if the value is a literal it will read a literal from the buffer (literal is any data between quotes except if the quote is prefixed with a backward slash character, that is '\').This reads the terminal before the
$Domainso that if there is no$Domainfor theCookiethen the character before it will not be read needlessly.
-
version
private void version()
This extracts the$Versionof thisCookie. The version is parsed and converted into a decimal int from the digit characters that make up a version.This will read all digit
char's up to but excluding the first non digitcharthat it encounters from the offset within the buffer, or if the value is a literal it will read a literal from the buffer (literal is any data between quotes except if the quote is prefixed with a backward slash character i.e. '\').
-
terminal
private boolean terminal(char ch)
This is used to determine if a given iso8859-1 character is a terminal character. That is either the ';' or ',' characters. Although the RFC 2109 says the terminal can be either a comma, it is not used by any browsers.- Parameters:
ch- the character that is to be compared- Returns:
- true if this is a semicolon character
-
-