Methods Summary |
---|
public static com.macfaq.http.Cookie | bake(java.lang.String header, java.net.URI uri)
try {
String[] attributes = header.split(";");
String nameValue = attributes[0];
Cookie cookie = new Cookie();
cookie.uri = uri;
cookie.name = nameValue.substring(0, nameValue.indexOf('="));
cookie.value = nameValue.substring(nameValue.indexOf('=")+1);
cookie.path = "/";
cookie.domain = uri.getHost();
if (attributes[attributes.length-1].trim().equals("secure")) {
cookie.secure = true;
}
for (int i=1; i < attributes.length; i++) {
nameValue = attributes[i].trim();
int equals = nameValue.indexOf('=");
if (equals == -1) continue;
String attributeName = nameValue.substring(0, equals);
String attributeValue = nameValue.substring(equals+1);
if (attributeName.equalsIgnoreCase("domain")) {
String uriDomain = uri.getHost();
if (uriDomain.equals(attributeValue)) {
cookie.domain = attributeValue;
}
else {
if (!attributeValue.startsWith(".")) {
attributeValue = "." + attributeValue;
}
uriDomain = uriDomain.substring(uriDomain.indexOf('."));
if (!uriDomain.equals(attributeValue)) {
throw new CookieException(
"Server tried to set cookie in another domain");
}
cookie.domain = attributeValue;
}
}
else if (attributeName.equalsIgnoreCase("path")) {
cookie.path = attributeValue;
}
else if (attributeName.equalsIgnoreCase("expires")) {
cookie.expires = expiresFormat.parse(attributeValue);
}
else if (attributeName.equalsIgnoreCase("Version")) {
if (!"1".equals(attributeValue)) {
throw new CookieException("Unexpected version " + attributeValue);
}
cookie.version = attributeValue;
}
}
return cookie;
}
catch (Exception ex) {
// ParseException, StringIndexOutOfBoundsException etc.
throw new CookieException(ex);
}
|
public java.lang.String | getName()
return name;
|
public java.net.URI | getURI()
return uri;
|
public java.lang.String | getVersion()
return version;
|
public boolean | isExpired()
if (expires == null) return false;
Date now = new Date();
return now.after(expires);
|
public boolean | isSecure()
return secure;
|
public boolean | matches(java.net.URI u)
if (isExpired()) return false;
String path = u.getPath();
if (path == null) path = "/";
if (path.startsWith(this.path)) return true;
return false;
|
public java.lang.String | toExternalForm()
StringBuffer result = new StringBuffer(name);
result.append("=");
result.append(value);
if ("1".equals(version)) {
result.append(" Version=1");
}
return result.toString();
|