commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r280413 - in /jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie: CookieAttributeHandler.java CookieSource.java RFC2965Spec.java
Date Mon, 12 Sep 2005 20:37:21 GMT
Author: olegk
Date: Mon Sep 12 13:37:16 2005
New Revision: 280413

URL: http://svn.apache.org/viewcvs?rev=280413&view=rev
Log:
CookieAttributeHandler interface modified to enable polymorphic invocation of various cookie
attribute handles when validating and matching cookies. /Contributed by Oleg Kalnichevski
/Reviewed by Samit Jain

Added:
    jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/CookieSource.java
  (with props)
Modified:
    jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/CookieAttributeHandler.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/CookieAttributeHandler.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/CookieAttributeHandler.java?rev=280413&r1=280412&r2=280413&view=diff
==============================================================================
--- jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/CookieAttributeHandler.java
(original)
+++ jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/CookieAttributeHandler.java
Mon Sep 12 13:37:16 2005
@@ -1,3 +1,31 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ *  Copyright 1999-2004 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
 package org.apache.commons.httpclient.cookie;
 
 import org.apache.commons.httpclient.Cookie;
@@ -22,17 +50,17 @@
    * @param cookie {@link org.apache.commons.httpclient.Cookie} to be updated
    * @param value cookie attribute value from the cookie response header
    */
-  public void parse(Cookie cookie, String value)
+  void parse(Cookie cookie, String value)
           throws MalformedCookieException;
 
   /**
    * Peforms cookie validation for the given attribute value.
    *
    * @param cookie {@link org.apache.commons.httpclient.Cookie} to validate
-   * @param value specific property of the host the cookie was received from.
+   * @param source the cookie source to validate against
    * @throws MalformedCookieException if cookie validation fails for this attribute
    */
-  public void validate(Cookie cookie, String value)
+  void validate(Cookie cookie, CookieSource source)
           throws MalformedCookieException;
 
   /**
@@ -40,10 +68,10 @@
    * submitted) with the corresponding cookie attribute.
    *
    * @param cookie {@link org.apache.commons.httpclient.Cookie} to match
-   * @param value specific property of the destination host
+   * @param source the cookie source to match against
    * @return <tt>true</tt> if the match is successful; <tt>false</tt>
otherwise
    */
-  public boolean match(Cookie cookie, String value);
+  boolean match(Cookie cookie, CookieSource source);
 
   /**
    * Format the cookie attribute suitable for sending in a cookie request header and
@@ -53,5 +81,6 @@
    * @param cookie {@link org.apache.commons.httpclient.Cookie} to get the attribute
    *        from.
    */
-  public void format(StringBuffer buffer, Cookie cookie);
+  void format(StringBuffer buffer, Cookie cookie);
+  
 }

Added: jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/CookieSource.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/CookieSource.java?rev=280413&view=auto
==============================================================================
--- jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/CookieSource.java
(added)
+++ jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/CookieSource.java
Mon Sep 12 13:37:16 2005
@@ -0,0 +1,77 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ *  Copyright 1999-2004 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+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;
+	}
+	
+}

Propchange: jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/CookieSource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/CookieSource.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/CookieSource.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

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=280413&r1=280412&r2=280413&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
Mon Sep 12 13:37:16 2005
@@ -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);
+        }
+        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;
+        	}
         }
-
-        /* 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;
         // 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:
-     * <ul>
-     *   <li>Host name must not be null or blank.</li>
-     *   <li>Path must not be null.</li>
-     *   <li>Port must be &gt;0.</li>
-     * </ul>
-     *
-     * @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 (host.trim().equals("")) {
-                throw new IllegalArgumentException(
-                        "Host of origin may not be blank");
+            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: " +
                                                    "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 (host.trim().equals("")) {
-                throw new IllegalArgumentException(
-                        "Destination Host may not be blank");
+            if (source == null) {
+                throw new IllegalArgumentException("Cookie source may not be null");
             }
-            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 (port < 0) {
-                throw new IllegalArgumentException("Invalid port of host.");
+            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(
@@ -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 (port < 0) {
-                throw new IllegalArgumentException("Invalid port of destination: " + value);
+            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");
@@ -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;
         }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message