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/28 23:46:53 UTC
svn commit: r398025 - in
/jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie:
Cookie2.java RFC2965Spec.java
Author: olegk
Date: Fri Apr 28 14:46:53 2006
New Revision: 398025
URL: http://svn.apache.org/viewcvs?rev=398025&view=rev
Log:
Various (rather minor but numerous) code cleaups
Modified:
jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/Cookie2.java
jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java
Modified: jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/Cookie2.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/Cookie2.java?rev=398025&r1=398024&r2=398025&view=diff
==============================================================================
--- jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/Cookie2.java (original)
+++ jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/Cookie2.java Fri Apr 28 14:46:53 2006
@@ -55,8 +55,6 @@
public static final String COMMENTURL = "commenturl";
public static final String DISCARD = "discard";
- public static final String COOKIE_NAME_KEY = "cookieName";
-
/**
* Default constructor. Creates a blank cookie
*/
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=398025&r1=398024&r2=398025&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 Fri Apr 28 14:46:53 2006
@@ -90,7 +90,6 @@
this.attribHandlerList = new ArrayList(10);
this.rfc2109 = new RFC2109Spec();
- registerAttribHandler(Cookie2.COOKIE_NAME_KEY, new Cookie2NameAttributeHandler());
registerAttribHandler(Cookie2.PATH, new Cookie2PathAttributeHandler());
registerAttribHandler(Cookie2.DOMAIN, new Cookie2DomainAttributeHandler());
registerAttribHandler(Cookie2.PORT, new Cookie2PortAttributeHandler());
@@ -344,15 +343,21 @@
LOG.trace("enter RFC2965Spec.validate(String, int, String, "
+ "boolean, Cookie)");
- if (!(cookie instanceof Cookie2)) {
+ if (cookie instanceof Cookie2) {
+ if (cookie.getName().indexOf(' ') != -1) {
+ throw new MalformedCookieException("Cookie name may not contain blanks");
+ }
+ if (cookie.getName().startsWith("$")) {
+ throw new MalformedCookieException("Cookie name may not start with $");
+ }
+ CookieSource cookiesource = new CookieSource(host, port, path, secure);
+ for (Iterator i = getAttribHandlerIterator(); i.hasNext(); ) {
+ CookieAttributeHandler handler = (CookieAttributeHandler) i.next();
+ handler.validate(cookie, cookiesource);
+ }
+ } else {
// old-style cookies are validated according to the old rules
this.rfc2109.validate(host, port, path, secure, cookie);
- return;
- }
- CookieSource cookiesource = new CookieSource(host, port, path, secure);
- for (Iterator i = getAttribHandlerIterator(); i.hasNext(); ) {
- CookieAttributeHandler handler = (CookieAttributeHandler) i.next();
- handler.validate(cookie, cookiesource);
}
}
@@ -371,33 +376,32 @@
LOG.trace("enter RFC2965.match("
+ "String, int, String, boolean, Cookie");
-
if (cookie == null) {
throw new IllegalArgumentException("Cookie may not be null");
}
-
- if (!(cookie instanceof Cookie2)) {
+ if (cookie instanceof Cookie2) {
+ CookieSource cookiesource = new CookieSource(host, port, path, secure);
+ for (Iterator i = getAttribHandlerIterator(); 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;
+ }
+ // 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 we get to this stage, we have a match
+ return true;
+ } else {
// old-style cookies are matched according to the old rules
return this.rfc2109.match(host, port, path, secure, cookie);
}
- CookieSource cookiesource = new CookieSource(host, port, path, secure);
- for (Iterator i = getAttribHandlerIterator(); 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;
- }
- // 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 we get to this stage, we have a match
- return true;
}
private void doFormatCookie2(final Cookie2 cookie, final StringBuffer buffer) {
@@ -600,18 +604,6 @@
}
/**
- * Casts the given {@link Cookie} cookie to {@link Cookie2} cookie.
- * @param cookieParam {@link Cookie}
- * @return {@link Cookie2}
- */
- private Cookie2 getCookie2Cookie(Cookie cookieParam) {
- if (!(cookieParam instanceof Cookie2)) {
- throw new IllegalArgumentException("Expected Cookie2 cookie.");
- }
- return ((Cookie2) cookieParam);
- }
-
- /**
* <tt>"Path"</tt> attribute handler for RFC 2965 cookie spec.
*/
private class Cookie2PathAttributeHandler
@@ -621,12 +613,11 @@
* Parse cookie path attribute.
* @see CookieAttributeHandler#parse(org.apache.commons.httpclient.Cookie, String)
*/
- public void parse(Cookie cookieParam, String path)
+ public void parse(final Cookie cookie, final String path)
throws MalformedCookieException {
- if (cookieParam == null) {
+ if (cookie == null) {
throw new IllegalArgumentException("Cookie may not be null");
}
- Cookie2 cookie = getCookie2Cookie(cookieParam);
if (!cookie.isPathAttributeSpecified()) {
if (path == null) {
throw new MalformedCookieException(
@@ -646,15 +637,14 @@
* prefix of the request-URI (case-sensitive matching).
* @see CookieAttributeHandler#validate(org.apache.commons.httpclient.Cookie, String)
*/
- public void validate(final Cookie cookieParam, final CookieSource source)
+ public void validate(final Cookie cookie, final CookieSource source)
throws MalformedCookieException {
- if (cookieParam == null) {
+ if (cookie == 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(
@@ -673,7 +663,6 @@
"Illegal path attribute \"" + cookie.getPath()
+ "\". Path of origin: \"" + path + "\"");
}
-
}
/**
@@ -681,14 +670,13 @@
* prefix of the request-URI (case-sensitive matching).
* @see CookieAttributeHandler#match(org.apache.commons.httpclient.Cookie, String)
*/
- public boolean match(final Cookie cookieParam, final CookieSource source) {
- if (cookieParam == null) {
+ public boolean match(final Cookie cookie, final CookieSource source) {
+ if (cookie == 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 (cookie.getPath() == null) {
LOG.warn("Invalid cookie state: path attribute is null.");
@@ -715,12 +703,11 @@
* Parse cookie domain attribute.
* @see CookieAttributeHandler#parse(org.apache.commons.httpclient.Cookie, String)
*/
- public void parse(Cookie cookieParam, String domain)
+ public void parse(final Cookie cookie, String domain)
throws MalformedCookieException {
- if (cookieParam == null) {
+ if (cookie == null) {
throw new IllegalArgumentException("Cookie may not be null");
}
- Cookie2 cookie = getCookie2Cookie(cookieParam);
if (!cookie.isDomainAttributeSpecified()) {
//TODO (jain): how do we handle the case when domain is specified and equals host?
if (domain == null) {
@@ -744,15 +731,14 @@
* Validate cookie domain attribute.
* @see CookieAttributeHandler#validate(org.apache.commons.httpclient.Cookie, String)
*/
- public void validate(final Cookie cookieParam, final CookieSource source)
+ public void validate(final Cookie cookie, final CookieSource source)
throws MalformedCookieException {
- if (cookieParam == null) {
+ if (cookie == 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 host = source.getHost().toLowerCase();
if (cookie.getDomain() == null) {
throw new MalformedCookieException("Invalid cookie state: " +
@@ -812,14 +798,13 @@
* Match cookie domain attribute.
* @see CookieAttributeHandler#match(org.apache.commons.httpclient.Cookie, String)
*/
- public boolean match(final Cookie cookieParam, final CookieSource source) {
- if (cookieParam == null) {
+ public boolean match(final Cookie cookie, final CookieSource source) {
+ if (cookie == 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 host = source.getHost().toLowerCase();
String cookieDomain = cookie.getDomain();
String effectiveHost = getEffectiveHost(host);
@@ -851,24 +836,26 @@
* Parse cookie port attribute.
* @see CookieAttributeHandler#parse(org.apache.commons.httpclient.Cookie, String)
*/
- public void parse(Cookie cookieParam, String portValue)
+ public void parse(final Cookie cookie, final String portValue)
throws MalformedCookieException {
- if (cookieParam == null) {
+ if (cookie == null) {
throw new IllegalArgumentException("Cookie may not be null");
}
- Cookie2 cookie = getCookie2Cookie(cookieParam);
- if (!cookie.isPortAttributeSpecified()) {
- if ((portValue == null) || (portValue.trim().equals(""))) {
- // If the Port attribute is present but has no value, the
- // cookie can only be sent to the request-port.
- // Since the default port list contains only request-port, we don't
- // need to do anything here.
- cookie.setPortAttributeBlank(true);
- } else {
- int[] ports = parsePortAttribute(portValue);
- cookie.setPorts(ports);
+ if (cookie instanceof Cookie2) {
+ Cookie2 cookie2 = (Cookie2) cookie;
+ if (!cookie2.isPortAttributeSpecified()) {
+ if ((portValue == null) || (portValue.trim().equals(""))) {
+ // If the Port attribute is present but has no value, the
+ // cookie can only be sent to the request-port.
+ // Since the default port list contains only request-port, we don't
+ // need to do anything here.
+ cookie2.setPortAttributeBlank(true);
+ } else {
+ int[] ports = parsePortAttribute(portValue);
+ cookie2.setPorts(ports);
+ }
+ cookie2.setPortAttributeSpecified(true);
}
- cookie.setPortAttributeSpecified(true);
}
}
@@ -877,21 +864,23 @@
* in header, the request port must be in cookie's port list.
* @see CookieAttributeHandler#validate(org.apache.commons.httpclient.Cookie, String)
*/
- public void validate(final Cookie cookieParam, final CookieSource source)
+ public void validate(final Cookie cookie, final CookieSource source)
throws MalformedCookieException {
- if (cookieParam == null) {
+ if (cookie == 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);
- int port = source.getPort();
- if (cookie.isPortAttributeSpecified()) {
- if (!portMatch(port, cookie.getPorts())) {
- throw new MalformedCookieException(
- "Port attribute violates RFC 2965: "
- + "Request port not found in cookie's port list.");
+ if (cookie instanceof Cookie2) {
+ Cookie2 cookie2 = (Cookie2) cookie;
+ int port = source.getPort();
+ if (cookie2.isPortAttributeSpecified()) {
+ if (!portMatch(port, cookie2.getPorts())) {
+ throw new MalformedCookieException(
+ "Port attribute violates RFC 2965: "
+ + "Request port not found in cookie's port list.");
+ }
}
}
}
@@ -902,71 +891,33 @@
* must be in the cookie's port list.
* @see CookieAttributeHandler#match(org.apache.commons.httpclient.Cookie, String)
*/
- public boolean match(final Cookie cookieParam, final CookieSource source) {
- if (cookieParam == null) {
+ public boolean match(final Cookie cookie, final CookieSource source) {
+ if (cookie == 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);
- int port = source.getPort();
- if (cookie.isPortAttributeSpecified()) {
- if (cookie.getPorts() == null) {
- LOG.warn("Invalid cookie state: port not specified");
- return false;
- }
- if (!portMatch(port, cookie.getPorts())) {
- return false;
+ if (cookie instanceof Cookie2) {
+ Cookie2 cookie2 = (Cookie2) cookie;
+ int port = source.getPort();
+ if (cookie2.isPortAttributeSpecified()) {
+ if (cookie2.getPorts() == null) {
+ LOG.warn("Invalid cookie state: port not specified");
+ return false;
+ }
+ if (!portMatch(port, cookie2.getPorts())) {
+ return false;
+ }
}
+ return true;
+ } else {
+ return false;
}
- return true;
}
-
}
/**
- * <tt>"Name"</tt> cookie attribute handler for RFC 2965 cookie spec.
- */
- private class Cookie2NameAttributeHandler
- implements CookieAttributeHandler {
-
- /**
- * Parse cookie name.
- * @see CookieAttributeHandler#parse(org.apache.commons.httpclient.Cookie, String)
- */
- public void parse(Cookie cookie, String value)
- throws MalformedCookieException {
- }
-
- /**
- * validate cookie name.
- * @see CookieAttributeHandler#validate(org.apache.commons.httpclient.Cookie, String)
- */
- 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 (cookie.getName().indexOf(' ') != -1) {
- throw new MalformedCookieException("Cookie name may not contain blanks");
- }
- if (cookie.getName().startsWith("$")) {
- throw new MalformedCookieException("Cookie name may not start with $");
- }
- }
-
- /**
- * @see CookieAttributeHandler#match(org.apache.commons.httpclient.Cookie, String)
- */
- public boolean match(final Cookie cookieParam, final CookieSource source) {
- return true;
- }
-
- }
-
- /**
* <tt>"Max-age"</tt> cookie attribute handler for RFC 2965 cookie spec.
*/
private class Cookie2MaxageAttributeHandler
@@ -976,12 +927,11 @@
* Parse cookie max-age attribute.
* @see CookieAttributeHandler#parse(org.apache.commons.httpclient.Cookie, String)
*/
- public void parse(Cookie cookieParam, String value)
+ public void parse(final Cookie cookie, final String value)
throws MalformedCookieException {
- if (cookieParam == null) {
+ if (cookie == null) {
throw new IllegalArgumentException("Cookie may not be null");
}
- Cookie2 cookie = getCookie2Cookie(cookieParam);
if (cookie.getExpiryDate() == null) {
if (value == null) {
throw new MalformedCookieException(
@@ -1027,28 +977,30 @@
* Parse cookie version attribute.
* @see CookieAttributeHandler#parse(org.apache.commons.httpclient.Cookie, String)
*/
- public void parse(Cookie cookieParam, String value)
+ public void parse(final Cookie cookie, final String value)
throws MalformedCookieException {
- if (cookieParam == null) {
+ if (cookie == null) {
throw new IllegalArgumentException("Cookie may not be null");
}
- Cookie2 cookie = getCookie2Cookie(cookieParam);
- if (!cookie.isVersionAttributeSpecified()) {
- if (value == null) {
- throw new MalformedCookieException(
- "Missing value for version attribute");
- }
- int version = -1;
- try {
- version = Integer.parseInt(value);
- } catch (NumberFormatException e) {
- version = -1;
- }
- if (version < 0) {
- throw new MalformedCookieException("Invalid cookie version.");
+ if (cookie instanceof Cookie2) {
+ Cookie2 cookie2 = (Cookie2) cookie;
+ if (!cookie2.isVersionAttributeSpecified()) {
+ if (value == null) {
+ throw new MalformedCookieException(
+ "Missing value for version attribute");
+ }
+ int version = -1;
+ try {
+ version = Integer.parseInt(value);
+ } catch (NumberFormatException e) {
+ version = -1;
+ }
+ if (version < 0) {
+ throw new MalformedCookieException("Invalid cookie version.");
+ }
+ cookie2.setVersion(Integer.parseInt(value));
+ cookie2.setVersionAttributeSpecified(true);
}
- cookie.setVersion(Integer.parseInt(value));
- cookie.setVersionAttributeSpecified(true);
}
}
@@ -1056,21 +1008,23 @@
* validate cookie version attribute. Version attribute is REQUIRED.
* @see CookieAttributeHandler#validate(org.apache.commons.httpclient.Cookie, String)
*/
- public void validate(final Cookie cookieParam, final CookieSource source)
+ public void validate(final Cookie cookie, final CookieSource source)
throws MalformedCookieException {
- if (cookieParam == null) {
- throw new IllegalArgumentException("Cookie may not be null");
- }
- Cookie2 cookie = getCookie2Cookie(cookieParam);
- if (!cookie.isVersionAttributeSpecified()) {
- throw new MalformedCookieException(
- "Violates RFC 2965. Version attribute is required.");
+ if (cookie == null) {
+ throw new IllegalArgumentException("Cookie may not be null");
}
- //TODO (jain): other versions for set-cookie2 ?
- if (cookie.getVersion() != 1) {
- throw new MalformedCookieException(
- "Violates RFC 2965. Invalid value for Version attribute." +
- "Must be \"1\".");
+ if (cookie instanceof Cookie2) {
+ Cookie2 cookie2 = (Cookie2) cookie;
+ if (!cookie2.isVersionAttributeSpecified()) {
+ throw new MalformedCookieException(
+ "Violates RFC 2965. Version attribute is required.");
+ }
+ //TODO (jain): other versions for set-cookie2 ?
+ if (cookie2.getVersion() != 1) {
+ throw new MalformedCookieException(
+ "Violates RFC 2965. Invalid value for Version attribute." +
+ "Must be \"1\".");
+ }
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org