Index: src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java
===================================================================
--- src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java (revision 265534)
+++ src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java (working copy)
@@ -161,7 +161,21 @@
+ "String, int, String, boolean, String)");
// before we do anything, lets check validity of arguments
- validateArgs(host, port, path);
+ if (host == null) {
+ throw new IllegalArgumentException(
+ "Host of origin may not be null");
+ }
+ if (host.trim().equals("")) {
+ throw new IllegalArgumentException(
+ "Host of origin may not be blank");
+ }
+ if (port < 0) {
+ throw new IllegalArgumentException("Invalid port: " + port);
+ }
+ if (path == null) {
+ throw new IllegalArgumentException(
+ "Path of origin may not be null.");
+ }
if (header == null) {
throw new IllegalArgumentException("Header may not be null.");
}
@@ -276,39 +290,23 @@
* validation
*/
public void validate(String host, int port, String path,
- boolean secure, final Cookie cookieParam)
+ boolean secure, final Cookie cookie)
throws MalformedCookieException {
LOG.trace("enter RFC2965Spec.validate(String, int, String, "
+ "boolean, Cookie)");
- // before we do anything, lets check validity of arguments
- validateArgs(host, port, path);
-
- if (!(cookieParam instanceof Cookie2)) {
+ if (!(cookie instanceof Cookie2)) {
// old-style cookies are validated according to the old rules
CookieSpec rfc2109Spec = CookiePolicy.getCookieSpec(CookiePolicy.RFC_2109);
- rfc2109Spec.validate(host, port, path, secure, cookieParam);
+ rfc2109Spec.validate(host, port, path, secure, cookie);
return;
}
-
- /* validate cookie2 cookies */
- Cookie2 cookie = (Cookie2) cookieParam;
- // validate cookie name
- CookieAttributeHandler handler = getAttributeHandler(Cookie2.COOKIE_NAME_KEY);
- handler.validate(cookie, null);
- // validate cookie path attribute
- handler = getAttributeHandler(Cookie2.PATH);
- handler.validate(cookie, path);
- // validate cookie domain attribute
- handler = getAttributeHandler(Cookie2.DOMAIN);
- handler.validate(cookie, host);
- // validate cookie port attribute
- handler = getAttributeHandler(Cookie2.PORT);
- handler.validate(cookie, String.valueOf(port));
- // validate cookie version attribute
- handler = getAttributeHandler(Cookie2.VERSION);
- handler.validate(cookie, null);
+ CookieSource cookiesource = new CookieSource(host, port, path, secure);
+ for (Iterator i = attributeHandlerMap.values().iterator(); i.hasNext(); ) {
+ CookieAttributeHandler handler = (CookieAttributeHandler) i.next();
+ handler.validate(cookie, cookiesource);
+ }
}
/**
@@ -322,44 +320,36 @@
* @return true if the cookie matches the criterium
*/
public boolean match(String host, int port, String path,
- boolean secure, final Cookie cookieParam) {
+ boolean secure, final Cookie cookie) {
LOG.trace("enter RFC2965.match("
+ "String, int, String, boolean, Cookie");
- // before we do anything, lets check validity of arguments
- validateArgs(host, port, path);
- if (cookieParam == null) {
+ if (cookie == null) {
throw new IllegalArgumentException("Cookie may not be null");
}
- if (!(cookieParam instanceof Cookie2)) {
+ if (!(cookie instanceof Cookie2)) {
// old-style cookies are matched according to the old rules
CookieSpec rfc2109Spec = CookiePolicy.getCookieSpec(CookiePolicy.RFC_2109);
- return rfc2109Spec.match(host, port, path, secure, cookieParam);
+ return rfc2109Spec.match(host, port, path, secure, cookie);
}
-
- /* match cookie2 cookies */
- Cookie2 cookie = (Cookie2) cookieParam;
- // match cookie path attribute
- CookieAttributeHandler handler = getAttributeHandler(Cookie2.PATH);
- if (!handler.match(cookie, path))
- return false;
- // match cookie domain attribute
- handler = getAttributeHandler(Cookie2.DOMAIN);
- if (!handler.match(cookie, host))
- return false;
- // match cookie port attribute
- handler = getAttributeHandler(Cookie2.PORT);
- if (!handler.match(cookie, String.valueOf(port)))
- return false;
+ CookieSource cookiesource = new CookieSource(host, port, path, secure);
+ for (Iterator i = attributeHandlerMap.values().iterator(); i.hasNext(); ) {
+ CookieAttributeHandler handler = (CookieAttributeHandler) i.next();
+ if (!handler.match(cookie, cookiesource)) {
+ return false;
+ }
+ }
// check if cookie has expired
- if (cookie.isPersistent() && cookie.isExpired())
- return false;
+ if (cookie.isPersistent() && cookie.isExpired()) {
+ return false;
+ }
// finally make sure that if cookie Secure attribute is set, then this
// request is made using a secure connection
- if (cookie.getSecure())
- return secure;
+ if (cookie.getSecure()) {
+ return secure;
+ }
// if we get to this stage, we have a match
return true;
}
@@ -503,37 +493,6 @@
}
/**
- * Validates host, port, path parameters. Refactored out since it is reqd by
- * many methods. Validation rules are:
- *
- * - Host name must not be null or blank.
- * - Path must not be null.
- * - Port must be >0.
- *
- *
- * @param host host name where cookie was received from or being sent to.
- * @param port port of host where cookie was received from or being sent to.
- * @param path path on host where cookie was received from or being sent to.
- */
- private void validateArgs(String host, int port, String path) {
- if (host == null) {
- throw new IllegalArgumentException(
- "Host of origin may not be null");
- }
- if (host.trim().equals("")) {
- throw new IllegalArgumentException(
- "Host of origin may not be blank");
- }
- if (port < 0) {
- throw new IllegalArgumentException("Invalid port: " + port);
- }
- if (path == null) {
- throw new IllegalArgumentException(
- "Path of origin may not be null.");
- }
- }
-
- /**
* Gets attribute handler {@link CookieAttributeHandler} for the
* given attribute.
*
@@ -661,12 +620,16 @@
* prefix of the request-URI (case-sensitive matching).
* @see CookieAttributeHandler#validate(org.apache.commons.httpclient.Cookie, String)
*/
- public void validate(Cookie cookieParam, String path)
+ public void validate(final Cookie cookieParam, final CookieSource source)
throws MalformedCookieException {
if (cookieParam == null) {
throw new IllegalArgumentException("Cookie may not be null");
}
+ if (source == null) {
+ throw new IllegalArgumentException("Cookie source may not be null");
+ }
Cookie2 cookie = getCookie2Cookie(cookieParam);
+ String path = source.getPath();
if (path == null) {
throw new IllegalArgumentException(
"Path of origin host may not be null.");
@@ -692,15 +655,15 @@
* prefix of the request-URI (case-sensitive matching).
* @see CookieAttributeHandler#match(org.apache.commons.httpclient.Cookie, String)
*/
- public boolean match(Cookie cookieParam, String path) {
+ public boolean match(final Cookie cookieParam, final CookieSource source) {
if (cookieParam == null) {
throw new IllegalArgumentException("Cookie may not be null");
}
- Cookie2 cookie = getCookie2Cookie(cookieParam);
- if (path == null) {
- throw new IllegalArgumentException(
- "Path of destination host may not be null.");
+ if (source == null) {
+ throw new IllegalArgumentException("Cookie source may not be null");
}
+ Cookie2 cookie = getCookie2Cookie(cookieParam);
+ String path = source.getPath();
if (cookie.getPath() == null) {
LOG.warn("Invalid cookie state: path attribute is null.");
return false;
@@ -770,25 +733,20 @@
* Validate cookie domain attribute.
* @see CookieAttributeHandler#validate(org.apache.commons.httpclient.Cookie, String)
*/
- public void validate(Cookie cookieParam, String host)
+ public void validate(final Cookie cookieParam, final CookieSource source)
throws MalformedCookieException {
if (cookieParam == null) {
throw new IllegalArgumentException("Cookie may not be null");
}
- Cookie2 cookie = getCookie2Cookie(cookieParam);
- if (host == null) {
- throw new IllegalArgumentException(
- "Host of origin may not be null");
+ if (source == null) {
+ throw new IllegalArgumentException("Cookie source may not be null");
}
- if (host.trim().equals("")) {
- throw new IllegalArgumentException(
- "Host of origin may not be blank");
- }
+ Cookie2 cookie = getCookie2Cookie(cookieParam);
+ String host = source.getHost().toLowerCase();
if (cookie.getDomain() == null) {
throw new MalformedCookieException("Invalid cookie state: " +
"domain not specified");
}
- host = host.toLowerCase();
String cookieDomain = cookie.getDomain().toLowerCase();
if (cookie.isDomainAttributeSpecified()) {
@@ -843,24 +801,15 @@
* Match cookie domain attribute.
* @see CookieAttributeHandler#match(org.apache.commons.httpclient.Cookie, String)
*/
- public boolean match(Cookie cookieParam, String host) {
+ public boolean match(final Cookie cookieParam, final CookieSource source) {
if (cookieParam == null) {
throw new IllegalArgumentException("Cookie may not be null");
}
- Cookie2 cookie = getCookie2Cookie(cookieParam);
- if (host == null) {
- throw new IllegalArgumentException(
- "Destination Host may not be null");
+ if (source == null) {
+ throw new IllegalArgumentException("Cookie source may not be null");
}
- if (host.trim().equals("")) {
- throw new IllegalArgumentException(
- "Destination Host may not be blank");
- }
- if (cookie.getDomain() == null) {
- LOG.warn("Invalid cookie state: domain not specified");
- return false;
- }
- host = host.toLowerCase();
+ Cookie2 cookie = getCookie2Cookie(cookieParam);
+ String host = source.getHost().toLowerCase();
String cookieDomain = cookie.getDomain();
String effectiveHost = getEffectiveHost(host);
@@ -932,22 +881,16 @@
* in header, the request port must be in cookie's port list.
* @see CookieAttributeHandler#validate(org.apache.commons.httpclient.Cookie, String)
*/
- public void validate(Cookie cookieParam, String value)
+ public void validate(final Cookie cookieParam, final CookieSource source)
throws MalformedCookieException {
if (cookieParam == null) {
throw new IllegalArgumentException("Cookie may not be null");
}
- Cookie2 cookie = getCookie2Cookie(cookieParam);
- int port = -1;
- try {
- port = Integer.parseInt(value);
- } catch (NumberFormatException e) {
- port = -1;
+ if (source == null) {
+ throw new IllegalArgumentException("Cookie source may not be null");
}
- if (port < 0) {
- throw new IllegalArgumentException("Invalid port of host.");
- }
-
+ Cookie2 cookie = getCookie2Cookie(cookieParam);
+ int port = source.getPort();
if (cookie.isPortAttributeSpecified()) {
if (!portMatch(port, cookie.getPorts())) {
throw new MalformedCookieException(
@@ -963,21 +906,15 @@
* must be in the cookie's port list.
* @see CookieAttributeHandler#match(org.apache.commons.httpclient.Cookie, String)
*/
- public boolean match(Cookie cookieParam, String value) {
+ public boolean match(final Cookie cookieParam, final CookieSource source) {
if (cookieParam == null) {
throw new IllegalArgumentException("Cookie may not be null");
}
- Cookie2 cookie = getCookie2Cookie(cookieParam);
- int port = -1;
- try {
- port = Integer.parseInt(value);
- } catch (NumberFormatException e) {
- port = -1;
+ if (source == null) {
+ throw new IllegalArgumentException("Cookie source may not be null");
}
- if (port < 0) {
- throw new IllegalArgumentException("Invalid port of destination: " + value);
- }
-
+ Cookie2 cookie = getCookie2Cookie(cookieParam);
+ int port = source.getPort();
if (cookie.isPortAttributeSpecified()) {
if (cookie.getPorts() == null) {
LOG.warn("Invalid cookie state: port not specified");
@@ -1027,7 +964,7 @@
* validate cookie name.
* @see CookieAttributeHandler#validate(org.apache.commons.httpclient.Cookie, String)
*/
- public void validate(Cookie cookieParam, String value)
+ public void validate(final Cookie cookieParam, final CookieSource source)
throws MalformedCookieException {
if (cookieParam == null) {
throw new IllegalArgumentException("Cookie may not be null");
@@ -1044,7 +981,7 @@
/**
* @see CookieAttributeHandler#match(org.apache.commons.httpclient.Cookie, String)
*/
- public boolean match(Cookie cookie, String value) {
+ public boolean match(final Cookie cookieParam, final CookieSource source) {
return true;
}
@@ -1105,13 +1042,13 @@
* validate cookie max-age attribute.
* @see CookieAttributeHandler#validate(org.apache.commons.httpclient.Cookie, String)
*/
- public void validate(Cookie cookie, String value) {
+ public void validate(final Cookie cookieParam, final CookieSource source) {
}
/**
* @see CookieAttributeHandler#match(org.apache.commons.httpclient.Cookie, String)
*/
- public boolean match(Cookie cookie, String value) {
+ public boolean match(final Cookie cookieParam, final CookieSource source) {
return true;
}
@@ -1161,7 +1098,7 @@
* validate cookie version attribute. Version attribute is REQUIRED.
* @see CookieAttributeHandler#validate(org.apache.commons.httpclient.Cookie, String)
*/
- public void validate(Cookie cookieParam, String value)
+ public void validate(final Cookie cookieParam, final CookieSource source)
throws MalformedCookieException {
if (cookieParam == null) {
throw new IllegalArgumentException("Cookie may not be null");
@@ -1182,7 +1119,7 @@
/**
* @see CookieAttributeHandler#match(org.apache.commons.httpclient.Cookie, String)
*/
- public boolean match(Cookie cookie, String value) {
+ public boolean match(final Cookie cookieParam, final CookieSource source) {
return true;
}
Index: src/java/org/apache/commons/httpclient/cookie/CookieAttributeHandler.java
===================================================================
--- src/java/org/apache/commons/httpclient/cookie/CookieAttributeHandler.java (revision 264149)
+++ src/java/org/apache/commons/httpclient/cookie/CookieAttributeHandler.java (working copy)
@@ -32,7 +32,7 @@
* @param value specific property of the host the cookie was received from.
* @throws MalformedCookieException if cookie validation fails for this attribute
*/
- public void validate(Cookie cookie, String value)
+ public void validate(Cookie cookie, CookieSource source)
throws MalformedCookieException;
/**
@@ -43,7 +43,7 @@
* @param value specific property of the destination host
* @return true if the match is successful; false otherwise
*/
- public boolean match(Cookie cookie, String value);
+ public boolean match(Cookie cookie, CookieSource source);
/**
* Format the cookie attribute suitable for sending in a cookie request header and
@@ -54,4 +54,5 @@
* from.
*/
public void format(StringBuffer buffer, Cookie cookie);
+
}
Index: src/java/org/apache/commons/httpclient/cookie/CookieSource.java
===================================================================
--- /dev/null 2005-09-11 00:10:14.895388648 +0200
+++ src/java/org/apache/commons/httpclient/cookie/CookieSource.java 2005-09-11 10:41:03.000000000 +0200
@@ -0,0 +1,49 @@
+package org.apache.commons.httpclient.cookie;
+
+public final class CookieSource {
+
+ private final String host;
+ private final int port;
+ private final String path;
+ private final boolean secure;
+
+ public CookieSource(final String host, int port, final String path, boolean secure) {
+ super();
+ if (host == null) {
+ throw new IllegalArgumentException(
+ "Host of origin may not be null");
+ }
+ if (host.trim().equals("")) {
+ throw new IllegalArgumentException(
+ "Host of origin may not be blank");
+ }
+ if (port < 0) {
+ throw new IllegalArgumentException("Invalid port: " + port);
+ }
+ if (path == null) {
+ throw new IllegalArgumentException(
+ "Path of origin may not be null.");
+ }
+ this.host = host;
+ this.port = port;
+ this.path = path;
+ this.secure = secure;
+ }
+
+ public String getHost() {
+ return this.host;
+ }
+
+ public String getPath() {
+ return this.path;
+ }
+
+ public int getPort() {
+ return this.port;
+ }
+
+ public boolean isSecure() {
+ return this.secure;
+ }
+
+}