You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ol...@apache.org on 2006/04/30 14:39:53 UTC
svn commit: r398327 - in
/jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src:
java/org/apache/commons/httpclient/cookie/RFC2965Spec.java
test/org/apache/commons/httpclient/cookie/TestCookieRFC2965Spec.java
Author: olegk
Date: Sun Apr 30 05:39:51 2006
New Revision: 398327
URL: http://svn.apache.org/viewcvs?rev=398327&view=rev
Log:
Refactored handling of the 'Discard', 'Comment', and 'CommentURL' cookie attributes
Modified:
jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java
jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/test/org/apache/commons/httpclient/cookie/TestCookieRFC2965Spec.java
Modified: jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java?rev=398327&r1=398326&r2=398327&view=diff
==============================================================================
--- jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java (original)
+++ jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java Sun Apr 30 05:39:51 2006
@@ -95,6 +95,9 @@
registerAttribHandler(Cookie2.PORT, new Cookie2PortAttributeHandler());
registerAttribHandler(Cookie2.MAXAGE, new Cookie2MaxageAttributeHandler());
registerAttribHandler(Cookie2.SECURE, new CookieSecureAttributeHandler());
+ registerAttribHandler(Cookie2.COMMENT, new CookieCommentAttributeHandler());
+ registerAttribHandler(Cookie2.COMMENTURL, new CookieCommentUrlAttributeHandler());
+ registerAttribHandler(Cookie2.DISCARD, new CookieDiscardAttributeHandler());
registerAttribHandler(Cookie2.VERSION, new Cookie2VersionAttributeHandler());
}
@@ -279,11 +282,11 @@
*
* @param attribute {@link org.apache.commons.httpclient.NameValuePair} cookie attribute from the
* <tt>Set-Cookie2</tt> header.
- * @param cookieParam {@link org.apache.commons.httpclient.Cookie} to be updated
+ * @param cookie {@link org.apache.commons.httpclient.Cookie} to be updated
* @throws MalformedCookieException if an exception occurs during parsing
*/
public void parseAttribute(
- final NameValuePair attribute, final Cookie cookieParam)
+ final NameValuePair attribute, final Cookie cookie)
throws MalformedCookieException {
if (attribute == null) {
throw new IllegalArgumentException("Attribute may not be null.");
@@ -291,35 +294,20 @@
if (attribute.getName() == null) {
throw new IllegalArgumentException("Attribute Name may not be null.");
}
- if (cookieParam == null) {
+ if (cookie == null) {
throw new IllegalArgumentException("Cookie may not be null.");
}
- if (!(cookieParam instanceof Cookie2)) {
- throw new IllegalArgumentException("Expected Cookie2 cookies.");
- }
- Cookie2 cookie = (Cookie2) cookieParam;
final String paramName = attribute.getName().toLowerCase();
final String paramValue = attribute.getValue();
CookieAttributeHandler handler = findAttribHandler(paramName);
- if (handler != null) {
- handler.parse(cookie, paramValue);
- }
-
- // handle other cookie attributes
- if (paramName.equals(Cookie2.COMMENT)) {
- if (cookie.getComment() == null)
- cookie.setComment(paramValue);
- } else if (paramName.equals(Cookie2.COMMENTURL)) {
- if (cookie.getCommentURL() == null)
- cookie.setCommentURL(paramValue);
- } else if (paramName.equals(Cookie2.DISCARD)) {
- cookie.setDiscard(true);
- } else {
+ if (handler == null) {
// ignore unknown attribute-value pairs
if (LOG.isDebugEnabled())
LOG.debug("Unrecognized cookie attribute: " +
attribute.toString());
+ } else {
+ handler.parse(cookie, paramValue);
}
}
@@ -331,7 +319,7 @@
* @param path the path from which the {@link org.apache.commons.httpclient.Cookie} was received
* @param secure <tt>true</tt> when the {@link org.apache.commons.httpclient.Cookie} was received using a
* secure connection
- * @param cookieParam The cookie to validate
+ * @param cookie The cookie to validate
* @throws MalformedCookieException if an exception occurs during
* validation
*/
@@ -367,7 +355,6 @@
* @param port the port to which the request is being submitted (ignored)
* @param path the path to which the request is being submitted
* @param secure <tt>true</tt> if the request is using a secure connection
- * @param cookieParam {@link Cookie} to be matched
* @return true if the cookie matches the criterium
*/
public boolean match(String host, int port, String path,
@@ -428,26 +415,26 @@
/**
* Return a string suitable for sending in a <tt>"Cookie"</tt> header as
* defined in RFC 2965
- * @param cookieParam a {@link org.apache.commons.httpclient.Cookie} to be formatted as string
+ * @param cookie a {@link org.apache.commons.httpclient.Cookie} to be formatted as string
* @return a string suitable for sending in a <tt>"Cookie"</tt> header.
*/
- public String formatCookie(Cookie cookieParam) {
+ public String formatCookie(final Cookie cookie) {
LOG.trace("enter RFC2965Spec.formatCookie(Cookie)");
- if (cookieParam == null) {
+ if (cookie == null) {
throw new IllegalArgumentException("Cookie may not be null");
}
- if (cookieParam instanceof Cookie2) {
+ if (cookie instanceof Cookie2) {
/* format cookie2 cookie */
- Cookie2 cookie = (Cookie2) cookieParam;
+ Cookie2 cookie2 = (Cookie2) cookie;
final StringBuffer buffer = new StringBuffer();
this.formatter.format(buffer, new NameValuePair("$Version", "1"));
buffer.append("; ");
- doFormatCookie2(cookie, buffer);
+ doFormatCookie2(cookie2, buffer);
return buffer.toString();
} else {
// old-style cookies are formatted according to the old rules
- return this.rfc2109.formatCookie(cookieParam);
+ return this.rfc2109.formatCookie(cookie);
}
}
@@ -937,13 +924,13 @@
/**
* validate cookie max-age attribute.
*/
- public void validate(final Cookie cookieParam, final CookieOrigin origin) {
+ public void validate(final Cookie cookie, final CookieOrigin origin) {
}
/**
* @see CookieAttributeHandler#match(org.apache.commons.httpclient.Cookie, String)
*/
- public boolean match(final Cookie cookieParam, final CookieOrigin origin) {
+ public boolean match(final Cookie cookie, final CookieOrigin origin) {
return true;
}
@@ -976,7 +963,76 @@
}
- /**
+ /**
+ * <tt>"Commant"</tt> cookie attribute handler for RFC 2965 cookie spec.
+ */
+ private class CookieCommentAttributeHandler
+ implements CookieAttributeHandler {
+
+ public void parse(final Cookie cookie, final String comment)
+ throws MalformedCookieException {
+ cookie.setComment(comment);
+ }
+
+ public void validate(final Cookie cookie, final CookieOrigin origin)
+ throws MalformedCookieException {
+ }
+
+ public boolean match(final Cookie cookie, final CookieOrigin origin) {
+ return true;
+ }
+
+ }
+
+ /**
+ * <tt>"CommantURL"</tt> cookie attribute handler for RFC 2965 cookie spec.
+ */
+ private class CookieCommentUrlAttributeHandler
+ implements CookieAttributeHandler {
+
+ public void parse(final Cookie cookie, final String commenturl)
+ throws MalformedCookieException {
+ if (cookie instanceof Cookie2) {
+ Cookie2 cookie2 = (Cookie2) cookie;
+ cookie2.setCommentURL(commenturl);
+ }
+ }
+
+ public void validate(final Cookie cookie, final CookieOrigin origin)
+ throws MalformedCookieException {
+ }
+
+ public boolean match(final Cookie cookie, final CookieOrigin origin) {
+ return true;
+ }
+
+ }
+
+ /**
+ * <tt>"Discard"</tt> cookie attribute handler for RFC 2965 cookie spec.
+ */
+ private class CookieDiscardAttributeHandler
+ implements CookieAttributeHandler {
+
+ public void parse(final Cookie cookie, final String commenturl)
+ throws MalformedCookieException {
+ if (cookie instanceof Cookie2) {
+ Cookie2 cookie2 = (Cookie2) cookie;
+ cookie2.setDiscard(true);
+ }
+ }
+
+ public void validate(final Cookie cookie, final CookieOrigin origin)
+ throws MalformedCookieException {
+ }
+
+ public boolean match(final Cookie cookie, final CookieOrigin origin) {
+ return true;
+ }
+
+ }
+
+ /**
* <tt>"Version"</tt> cookie attribute handler for RFC 2965 cookie spec.
*/
private class Cookie2VersionAttributeHandler
@@ -1035,7 +1091,7 @@
}
}
- public boolean match(final Cookie cookieParam, final CookieOrigin origin) {
+ public boolean match(final Cookie cookie, final CookieOrigin origin) {
return true;
}
Modified: jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/test/org/apache/commons/httpclient/cookie/TestCookieRFC2965Spec.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/test/org/apache/commons/httpclient/cookie/TestCookieRFC2965Spec.java?rev=398327&r1=398326&r2=398327&view=diff
==============================================================================
--- jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/test/org/apache/commons/httpclient/cookie/TestCookieRFC2965Spec.java (original)
+++ jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/test/org/apache/commons/httpclient/cookie/TestCookieRFC2965Spec.java Sun Apr 30 05:39:51 2006
@@ -400,10 +400,12 @@
assertFalse(cookie.isPersistent());
// Discard is OPTIONAL, default behavior is dictated by max-age
- header = new Header("Set-Cookie2", "name=value;Version=1");
+ header = new Header("Set-Cookie2", "name=value;Max-age=36000;Version=1");
parsed = cookiespec.parse("www.domain.com", 80, "/", false, header);
assertNotNull(parsed);
assertEquals(1, parsed.length);
+ cookie = (Cookie2) parsed[0];
+ assertTrue(cookie.isPersistent());
}
/**
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org