Author: olegk
Date: Fri Apr 28 12:27:19 2006
New Revision: 397988

URL: http://svn.apache.org/viewcvs?rev=397988&view=rev
Log:
Simplied cookie formatting. CookieAttributeHandler#format() provided no real 
benefit 

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=397988&r1=397987&r2=397988&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
 Fri Apr 28 12:27:19 2006
@@ -73,14 +73,4 @@
    */
   boolean match(Cookie cookie, CookieSource source);
 
-  /**
-   * Format the cookie attribute suitable for sending in a cookie request 
header and
-   * append it to the given buffer.
-   *
-   * @param buffer the string buffer to use for output
-   * @param cookie [EMAIL PROTECTED] org.apache.commons.httpclient.Cookie} to 
get the attribute
-   *        from.
-   */
-  void format(StringBuffer buffer, Cookie 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=397988&r1=397987&r2=397988&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 12:27:19 2006
@@ -400,6 +400,34 @@
         return true;
     }
 
+    private void doFormatCookie2(final Cookie2 cookie, final StringBuffer 
buffer) {
+        String name = cookie.getName();
+        String value = cookie.getValue();
+        if (value == null) {
+            value = "";
+        }
+        this.formatter.format(buffer, new NameValuePair(name, value));
+        // format domain attribute
+        if (cookie.getDomain() != null && cookie.isDomainAttributeSpecified()) 
{
+            buffer.append("; ");
+            this.formatter.format(buffer, new NameValuePair("$Domain", 
cookie.getDomain()));
+        }
+        // format path attribute
+        if ((cookie.getPath() != null) && (cookie.isPathAttributeSpecified())) 
{
+            buffer.append("; ");
+            this.formatter.format(buffer, new NameValuePair("$Path", 
cookie.getPath()));
+        }
+        // format port attribute
+        if (cookie.isPortAttributeSpecified()) {
+            String portValue = "";
+            if (!cookie.isPortAttributeBlank()) {
+                portValue = createPortAttribute(cookie.getPorts());
+            }
+            buffer.append("; ");
+            this.formatter.format(buffer, new NameValuePair("$Port", 
portValue));
+        }
+    }
+    
     /**
      * Return a string suitable for sending in a <tt>"Cookie"</tt> header as
      * defined in RFC 2965
@@ -412,20 +440,18 @@
         if (cookieParam == null) {
             throw new IllegalArgumentException("Cookie may not be null");
         }
-        if (!(cookieParam instanceof Cookie2)) {
+        if (cookieParam instanceof Cookie2) {
+            /* format cookie2 cookie */
+            Cookie2 cookie = (Cookie2) cookieParam;
+            final StringBuffer buffer = new StringBuffer();
+            this.formatter.format(buffer, new NameValuePair("$Version", "1"));
+            buffer.append("; ");
+            doFormatCookie2(cookie, buffer);
+            return buffer.toString();
+        } else {
             // old-style cookies are formatted according to the old rules
             return this.rfc2109.formatCookie(cookieParam);
         }
-
-        /* format cookie2 cookie */
-        Cookie2 cookie = (Cookie2) cookieParam;
-        final StringBuffer buffer = new StringBuffer();
-        // format cookie version
-        CookieAttributeHandler handler = getAttribHandler(Cookie2.VERSION);
-        handler.format(buffer, cookie);
-        // format cookie attributes
-        formatCookieAttributes(buffer, cookie);
-        return buffer.toString();
     }
 
     /**
@@ -449,48 +475,24 @@
                 break;
             }
         }
-        // TODO(jain): check this logic?
         if (hasOldStyleCookie) {
             // delegate old-style cookie formatting to rfc2109Spec
             return this.rfc2109.formatCookies(cookies);
         }
-
         /* format cookie2 cookies */
         final StringBuffer buffer = new StringBuffer();
         // format cookie version
-        CookieAttributeHandler handler = getAttribHandler(Cookie2.VERSION);
-        handler.format(buffer, null);
-
+        this.formatter.format(buffer, new NameValuePair("$Version", "1"));
         for (int i = 0; i < cookies.length; i++) {
+            buffer.append("; ");
             Cookie2 cookie = (Cookie2) cookies[i];
             // format cookie attributes
-            formatCookieAttributes(buffer, cookie);
+            doFormatCookie2(cookie, buffer);
         }
         return buffer.toString();
     }
 
     /**
-     * Return a string suitable for sending in a <tt>"Cookie"</tt> header
-     * as defined in RFC 2965.
-     * @param buffer The string buffer to use for output
-     * @param cookie The [EMAIL PROTECTED] Cookie2} to be formatted as string
-     */
-    private void formatCookieAttributes(final StringBuffer buffer, final 
Cookie2 cookie) {
-        // format cookie name and value
-        CookieAttributeHandler handler = 
getAttribHandler(Cookie2.COOKIE_NAME_KEY);
-        handler.format(buffer, cookie);
-        // format domain attribute
-        handler = getAttribHandler(Cookie2.DOMAIN);
-        handler.format(buffer, cookie);
-        // format path attribute
-        handler = getAttribHandler(Cookie2.PATH);
-        handler.format(buffer, cookie);
-        // format port attribute
-        handler = getAttribHandler(Cookie2.PORT);
-        handler.format(buffer, cookie);
-    }
-
-    /**
      * Retrieves valid Port attribute value for the given ports array.
      * e.g. "8000,8001,8002"
      *
@@ -701,21 +703,6 @@
             }
             return true;
         }
-
-        /**
-         * Format cookie Path attribute.
-         * @see CookieAttributeHandler#format(StringBuffer, 
org.apache.commons.httpclient.Cookie)
-         */
-        public void format(StringBuffer buffer, Cookie cookieParam) {
-            if (cookieParam == null) {
-                throw new IllegalArgumentException("Cookie may not be null");
-            }
-            Cookie2 cookie = getCookie2Cookie(cookieParam);
-            if ((cookie.getPath() != null) && 
(cookie.isPathAttributeSpecified())) {
-                buffer.append("; ");
-                formatter.format(buffer, new NameValuePair("$Path", 
cookie.getPath()));
-            }
-        }
     }
 
     /**
@@ -852,21 +839,6 @@
             return true;
         }
 
-        /**
-         * Format cookie domain attribute.
-         * @see CookieAttributeHandler#format(StringBuffer, 
org.apache.commons.httpclient.Cookie)
-         */
-        public void format(StringBuffer buffer, Cookie cookieParam) {
-            if (cookieParam == null) {
-                throw new IllegalArgumentException("Cookie may not be null");
-            }
-            Cookie2 cookie = getCookie2Cookie(cookieParam);
-            if (cookie.getDomain() != null
-                && cookie.isDomainAttributeSpecified()) {
-                buffer.append("; ");
-                formatter.format(buffer, new NameValuePair("$Domain", 
cookie.getDomain()));
-            }
-        }
     }
 
     /**
@@ -951,23 +923,6 @@
             return true;
         }
 
-        /**
-         * @see CookieAttributeHandler#format(StringBuffer, 
org.apache.commons.httpclient.Cookie)
-         */
-        public void format(StringBuffer buffer, Cookie cookieParam) {
-            if (cookieParam == null) {
-                throw new IllegalArgumentException("Cookie may not be null");
-            }
-            Cookie2 cookie = getCookie2Cookie(cookieParam);
-            if (cookie.isPortAttributeSpecified()) {
-                String portValue = "";
-                if (!cookie.isPortAttributeBlank()) {
-                    portValue = createPortAttribute(cookie.getPorts());
-                }
-                buffer.append("; ");
-                formatter.format(buffer, new NameValuePair("$Port", 
portValue));
-            }
-        }
     }
 
   /**
@@ -1009,22 +964,6 @@
           return true;
       }
 
-      /**
-       * @see CookieAttributeHandler#format(StringBuffer, 
org.apache.commons.httpclient.Cookie)
-       */
-      public void format(StringBuffer buffer, Cookie cookieParam) {
-          if (cookieParam == null) {
-              throw new IllegalArgumentException("Cookie may not be null");
-          }
-          Cookie2 cookie = getCookie2Cookie(cookieParam);
-          // format cookie name and value
-          String value = cookie.getValue();
-          if (value == null) {
-              value = "";
-          }
-          buffer.append("; ");
-          formatter.format(buffer, new NameValuePair(cookie.getName(), value));
-      }
   }
 
   /**
@@ -1076,11 +1015,6 @@
           return true;
       }
 
-      /**
-       * @see CookieAttributeHandler#format(StringBuffer, 
org.apache.commons.httpclient.Cookie)
-       */
-      public void format(StringBuffer buffer, Cookie cookie) {
-      }
   }
 
     /**
@@ -1147,14 +1081,6 @@
             return true;
         }
 
-        /**
-         * @see CookieAttributeHandler#format(StringBuffer, 
org.apache.commons.httpclient.Cookie)
-         */
-        public void format(StringBuffer buffer, Cookie cookie) {
-            formatter.format(buffer, new NameValuePair("$Version", "1"));
-        }
-        
-        
     }
 
     public int getVersion() {
@@ -1168,5 +1094,6 @@
                 Integer.toString(getVersion())));
         return new Header("Cookie2", buffer.toString(), true);
     }
+    
 }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to