You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ro...@apache.org on 2007/09/08 16:54:06 UTC

svn commit: r573849 - in /jakarta/httpcomponents/httpcore/trunk/module-main/src: main/java/org/apache/http/ main/java/org/apache/http/impl/ main/java/org/apache/http/protocol/ test/java/org/apache/http/

Author: rolandw
Date: Sat Sep  8 07:54:05 2007
New Revision: 573849

URL: http://svn.apache.org/viewvc?rev=573849&view=rev
Log:
HTTPCORE-107: HttpVersion gets a base class

Added:
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/ProtocolVersion.java   (with props)
Modified:
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/HttpVersion.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/DefaultConnectionReuseStrategy.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestExecutor.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/RequestExpectContinue.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/ResponseContent.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/TestHttpVersion.java

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/HttpVersion.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/HttpVersion.java?rev=573849&r1=573848&r2=573849&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/HttpVersion.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/HttpVersion.java Sat Sep  8 07:54:05 2007
@@ -41,16 +41,14 @@
  * 
  * @version $Revision$ $Date$
  */
-public final class HttpVersion implements Comparable, Serializable {
+public final class HttpVersion extends ProtocolVersion
+    implements Serializable {
 
-    static final long serialVersionUID = -3164547215216382904L;
-    
-    /** Major version number of the HTTP protocol */
-    private int major = 0;
-
-    /** Minor version number of the HTTP protocol */
-    private int minor = 0;
+    private static final long serialVersionUID = 8693840424276433600L;
 
+    /** The protocol name. */
+    public static final String HTTP = "HTTP";
+    
     /** HTTP protocol version 0.9 */
     public static final HttpVersion HTTP_0_9 = new HttpVersion(0, 9);  
 
@@ -59,6 +57,7 @@
 
     /** HTTP protocol version 1.1 */
     public static final HttpVersion HTTP_1_1 = new HttpVersion(1, 1);  
+
     
     /**
      * Create an HTTP protocol version designator.
@@ -69,120 +68,7 @@
      * @throws IllegalArgumentException if either major or minor version number is negative
      */
     public HttpVersion(int major, int minor) {
-        if (major < 0) {
-            throw new IllegalArgumentException("HTTP major version number may not be negative");
-        }
-        this.major = major;
-        if (minor < 0) {
-            throw new IllegalArgumentException("HTTP minor version number may not be negative");
-        }
-        this.minor = minor;
-    }
-    
-    /**
-     * Returns the major version number of the HTTP protocol.
-     * 
-     * @return the major version number.
-     */
-    public int getMajor() {
-        return major;
-    }
-
-    /**
-     * Returns the minor version number of the HTTP protocol.
-     * 
-     * @return the minor version number.
-     */
-    public int getMinor() {
-        return minor;
-    }
-
-    /**
-     * @see java.lang.Object#hashCode()
-     */
-    public int hashCode() {
-        return this.major * 100000 + this.minor;
-    }
-        
-    /**
-     * @see java.lang.Object#equals(java.lang.Object)
-     */
-    public boolean equals(Object obj) {
-        if (this == obj) {
-            return true;
-        }
-        if (!(obj instanceof HttpVersion)) {
-            return false;
-        }
-        return equals((HttpVersion)obj);  
-    }
-
-    /**
-     * Compares this HTTP protocol version with another one.
-     * 
-     * @param anotherVer the version to be compared with.
-     *  
-     * @return a negative integer, zero, or a positive integer as this version is less than, 
-     *    equal to, or greater than the specified version.     
-     */
-    public int compareTo(HttpVersion anotherVer) {
-        if (anotherVer == null) {
-            throw new IllegalArgumentException("Version parameter may not be null"); 
-        }
-        int delta = getMajor() - anotherVer.getMajor();
-        if (delta == 0) {
-            delta = getMinor() - anotherVer.getMinor();
-        }
-        return delta;
-    }
-
-    /**
-     * @see java.lang.Comparable#compareTo(java.lang.Object)
-     */
-    public int compareTo(Object o) {
-        return compareTo((HttpVersion)o);
-    }
-
-    /**
-     * Test if the HTTP protocol version is equal to the given number.
-     * 
-     * @return <tt>true</tt> if HTTP protocol version is given to the given number, 
-     *         <tt>false</tt> otherwise.
-     */
-    public boolean equals(HttpVersion version) {
-        return compareTo(version) == 0;  
-    }
-
-    /**
-     * Test if the HTTP protocol version is greater or equal to the given number.
-     * 
-     * @return <tt>true</tt> if HTTP protocol version is greater or equal given to the 
-     *         given number, <tt>false</tt> otherwise.
-     */
-    public boolean greaterEquals(HttpVersion version) {
-        return compareTo(version) >= 0;
-    }
-
-    /**
-     * Test if the HTTP protocol version is less or equal to the given number.
-     * 
-     * @return <tt>true</tt> if HTTP protocol version is less or equal to given to the 
-     *         given number, <tt>false</tt> otherwise.
-     */
-    public boolean lessEquals(HttpVersion version) {
-        return compareTo(version) <= 0;
-    }
-
-    /**
-     * @see java.lang.Object#toString()
-     */
-    public String toString() {
-        CharArrayBuffer buffer = new CharArrayBuffer(16);
-        buffer.append("HTTP/"); 
-        buffer.append(Integer.toString(this.major)); 
-        buffer.append('.'); 
-        buffer.append(Integer.toString(this.minor)); 
-        return buffer.toString();
+        super(HTTP, major, minor);
     }
 
 }

Added: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/ProtocolVersion.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/ProtocolVersion.java?rev=573849&view=auto
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/ProtocolVersion.java (added)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/ProtocolVersion.java Sat Sep  8 07:54:05 2007
@@ -0,0 +1,257 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.http;
+
+import java.io.Serializable;
+import org.apache.http.util.CharArrayBuffer;
+
+
+/**
+ * Represents a protocol version, as specified in RFC 2616.
+ * RFC 2616 specifies only HTTP versions, like "HTTP/1.1" and "HTTP/1.0".
+ * RFC 3261 specifies a message format that is identical to HTTP except
+ * for the protocol name. It defines a protocol version "SIP/2.0".
+ * There are some nitty-gritty differences between the interpretation
+ * of versions in HTTP and SIP. In those cases, HTTP takes precedence.
+ * <p>
+ * This class defines a protocol version as a combination of
+ * protocol name, major version number, and minor version number.
+ * Note that {@link #equals} and {@link #hashCode} are defined as
+ * final here, they cannot be overridden in derived classes.
+ * 
+ * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
+ * @author <a href="mailto:rolandw at apache.org">Roland Weber</a>
+ * 
+ * @version $Revision$
+ */
+public class ProtocolVersion implements Serializable {
+
+    private static final long serialVersionUID = 8287599752106796338L;
+
+
+    /** Name of the protocol. */
+    protected final String protocol;
+
+    /** Major version number of the protocol */
+    protected final int major;
+
+    /** Minor version number of the protocol */
+    protected final int minor;
+
+    
+    /**
+     * Create a protocol version designator.
+     *
+     * @param protocol   the name of the protocol, for example "HTTP"
+     * @param major      the major version number of the protocol
+     * @param minor      the minor version number of the protocol
+     */
+    public ProtocolVersion(String protocol, int major, int minor) {
+        if (protocol == null) {
+            throw new IllegalArgumentException
+                ("Protocol name must not be null.");
+        }
+        if (major < 0) {
+            throw new IllegalArgumentException
+                ("Protocol major version number must not be negative.");
+        }
+        if (minor < 0) {
+            throw new IllegalArgumentException
+                ("Protocol minor version number may not be negative");
+        }
+        this.protocol = protocol;
+        this.major = major;
+        this.minor = minor;
+    }
+
+    /**
+     * Returns the name of the protocol.
+     * 
+     * @return the protocol name
+     */
+    public final String getProtocol() {
+        return protocol;
+    }
+
+    /**
+     * Returns the major version number of the protocol.
+     * 
+     * @return the major version number.
+     */
+    public final int getMajor() {
+        return major;
+    }
+
+    /**
+     * Returns the minor version number of the HTTP protocol.
+     * 
+     * @return the minor version number.
+     */
+    public final int getMinor() {
+        return minor;
+    }
+
+
+    /**
+     * Obtains a hash code consistent with {@link #equals}.
+     *
+     * @return  the hashcode of this protocol version
+     */
+    public final int hashCode() {
+        return this.protocol.hashCode() ^ (this.major * 100000) ^ this.minor;
+    }
+
+        
+    /**
+     * Checks equality of this protocol version with an object.
+     * The object is equal if it is a protocl version with the same
+     * protocol name, major version number, and minor version number.
+     * The specific class of the object is <i>not</i> relevant,
+     * instances of derived classes with identical attributes are
+     * equal to instances of the base class and vice versa.
+     *
+     * @param obj       the object to compare with
+     *
+     * @return  <code>true</code> if the argument is the same protocol version,
+     *          <code>false</code> otherwise
+     */
+    public final boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!(obj instanceof ProtocolVersion)) {
+            return false;
+        }
+        ProtocolVersion that = (ProtocolVersion) obj;
+
+        return ((this.protocol.equals(that.protocol)) &&
+                (this.major == that.major) &&
+                (this.minor == that.minor));
+    }
+
+
+    /**
+     * Checks whether this protocol can be compared to another one.
+     * Only protocol versions with the same protocol name can be
+     * {@link #compareToVersion compared}.
+     *
+     * @param that      the protocol version to consider
+     *
+     * @return  <code>true</code> if {@link #compareToVersion compareToVersion}
+     *          can be called with the argument, <code>false</code> otherwise
+     */
+    public boolean isComparable(ProtocolVersion that) {
+        return (that != null) && this.protocol.equals(that.protocol);
+    }
+
+
+    /**
+     * Compares this protocol version with another one.
+     * Only protocol versions with the same protocol name can be compared.
+     * This method does <i>not</i> define a total ordering, as it would be
+     * required for {@link java.lang.Comparable}.
+     *
+     * @param that      the protocl version to compare with
+     *  
+     * @return   a negative integer, zero, or a positive integer
+     *           as this version is less than, equal to, or greater than
+     *           the argument version.
+     *
+     * @throws IllegalArgumentException
+     *         if the argument has a different protocol name than this object,
+     *         or if the argument is <code>null</code>
+     */
+    public int compareToVersion(ProtocolVersion that) {
+        if (that == null) {
+            throw new IllegalArgumentException
+                ("Protocol version must not be null."); 
+        }
+        if (!this.protocol.equals(that.protocol)) {
+            throw new IllegalArgumentException
+                ("Versions for different protocols cannot be compared. " +
+                 this + " " + that);
+        }
+
+        int delta = getMajor() - that.getMajor();
+        if (delta == 0) {
+            delta = getMinor() - that.getMinor();
+        }
+        return delta;
+    }
+
+
+    /**
+     * Tests if this protocol version is greater or equal to the given one.
+     *
+     * @param version   the version against which to check this version
+     *
+     * @return  <code>true</code> if this protocol version is
+     *          {@link #isComparable comparable} to the argument
+     *          and {@link #compareToVersion compares} as greater or equal,
+     *          <code>false</code> otherwise
+     */
+    public final boolean greaterEquals(ProtocolVersion version) {
+        return isComparable(version) && (compareToVersion(version) >= 0);
+    }
+
+
+    /**
+     * Tests if this protocol version is less or equal to the given one.
+     *
+     * @param version   the version against which to check this version
+     *
+     * @return  <code>true</code> if this protocol version is
+     *          {@link #isComparable comparable} to the argument
+     *          and {@link #compareToVersion compares} as less or equal,
+     *          <code>false</code> otherwise
+     */
+    public final boolean lessEquals(ProtocolVersion version) {
+        return isComparable(version) && (compareToVersion(version) <= 0);
+    }
+
+
+    /**
+     * Converts this protocol version to a string.
+     *
+     * @return  a protocol version string, like "HTTP/1.1"
+     */
+    public String toString() {
+        CharArrayBuffer buffer = new CharArrayBuffer(16);
+        buffer.append(this.protocol); 
+        buffer.append('/'); 
+        buffer.append(Integer.toString(this.major)); 
+        buffer.append('.'); 
+        buffer.append(Integer.toString(this.minor)); 
+        return buffer.toString();
+    }
+
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/ProtocolVersion.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/ProtocolVersion.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/ProtocolVersion.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/DefaultConnectionReuseStrategy.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/DefaultConnectionReuseStrategy.java?rev=573849&r1=573848&r2=573849&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/DefaultConnectionReuseStrategy.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/DefaultConnectionReuseStrategy.java Sat Sep  8 07:54:05 2007
@@ -99,7 +99,7 @@
             }
         }
         // Resorting to protocol version default close connection policy
-        return ver.greaterEquals(HttpVersion.HTTP_1_1);
+        return !ver.lessEquals(HttpVersion.HTTP_1_0);
     }
             
 }

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestExecutor.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestExecutor.java?rev=573849&r1=573848&r2=573849&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestExecutor.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpRequestExecutor.java Sat Sep  8 07:54:05 2007
@@ -205,7 +205,7 @@
             boolean sendentity = true;
             final HttpVersion ver = request.getRequestLine().getHttpVersion();
             if (((HttpEntityEnclosingRequest) request).expectContinue() &&
-                ver.greaterEquals(HttpVersion.HTTP_1_1)) {
+                !ver.lessEquals(HttpVersion.HTTP_1_0)) {
 
                 conn.flush();
                 // As suggested by RFC 2616 section 8.2.3, we don't wait for a

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/RequestExpectContinue.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/RequestExpectContinue.java?rev=573849&r1=573848&r2=573849&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/RequestExpectContinue.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/RequestExpectContinue.java Sat Sep  8 07:54:05 2007
@@ -67,7 +67,7 @@
             if (entity != null && entity.getContentLength() != 0) { 
                 HttpVersion ver = request.getRequestLine().getHttpVersion();
                 if (HttpProtocolParams.useExpectContinue(request.getParams()) 
-                        && ver.greaterEquals(HttpVersion.HTTP_1_1)) {
+                        && !ver.lessEquals(HttpVersion.HTTP_1_0)) {
                     request.addHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE);
                 }
             }

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/ResponseContent.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/ResponseContent.java?rev=573849&r1=573848&r2=573849&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/ResponseContent.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/ResponseContent.java Sat Sep  8 07:54:05 2007
@@ -72,7 +72,7 @@
         HttpEntity entity = response.getEntity();
         if (entity != null) {
             long len = entity.getContentLength();
-            if (entity.isChunked() && ver.greaterEquals(HttpVersion.HTTP_1_1)) {
+            if (entity.isChunked() && !ver.lessEquals(HttpVersion.HTTP_1_0)) {
                 response.addHeader(HTTP.TRANSFER_ENCODING, HTTP.CHUNK_CODING);
             } else if (len >= 0) {
                 response.addHeader(HTTP.CONTENT_LEN, Long.toString(entity.getContentLength()));

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/TestHttpVersion.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/TestHttpVersion.java?rev=573849&r1=573848&r2=573849&view=diff
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/TestHttpVersion.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/TestHttpVersion.java Sat Sep  8 07:54:05 2007
@@ -83,16 +83,28 @@
 
         assertFalse(ver1.equals(new Float(1.1)));
         
-        try {
-            ver1.equals(null);
-            fail("IllegalArgumentException should have been thrown");
-        } catch (IllegalArgumentException e) {
-        }
-
         assertTrue((new HttpVersion(0, 9)).equals(HttpVersion.HTTP_0_9));
         assertTrue((new HttpVersion(1, 0)).equals(HttpVersion.HTTP_1_0));
         assertTrue((new HttpVersion(1, 1)).equals(HttpVersion.HTTP_1_1));
         assertFalse((new HttpVersion(1, 1)).equals(HttpVersion.HTTP_1_0));
+
+        assertTrue
+            ((new ProtocolVersion("HTTP", 0, 9)).equals(HttpVersion.HTTP_0_9));
+        assertTrue
+            ((new ProtocolVersion("HTTP", 1, 0)).equals(HttpVersion.HTTP_1_0));
+        assertTrue
+            ((new ProtocolVersion("HTTP", 1, 1)).equals(HttpVersion.HTTP_1_1));
+        assertFalse
+            ((new ProtocolVersion("http", 1, 1)).equals(HttpVersion.HTTP_1_1));
+
+        assertTrue
+            (HttpVersion.HTTP_0_9.equals(new ProtocolVersion("HTTP", 0, 9)));
+        assertTrue
+            (HttpVersion.HTTP_1_0.equals(new ProtocolVersion("HTTP", 1, 0)));
+        assertTrue
+            (HttpVersion.HTTP_1_1.equals(new ProtocolVersion("HTTP", 1, 1)));
+        assertFalse
+            (HttpVersion.HTTP_1_1.equals(new ProtocolVersion("http", 1, 1)));
     }
 
     public void testHttpVersionComparison() {
@@ -100,9 +112,9 @@
         assertTrue(HttpVersion.HTTP_0_9.greaterEquals(HttpVersion.HTTP_0_9));
         assertFalse(HttpVersion.HTTP_0_9.greaterEquals(HttpVersion.HTTP_1_0));
         
-        assertTrue(HttpVersion.HTTP_1_0.compareTo((Object)HttpVersion.HTTP_1_1) < 0);
-        assertTrue(HttpVersion.HTTP_1_0.compareTo((Object)HttpVersion.HTTP_0_9) > 0);
-        assertTrue(HttpVersion.HTTP_1_0.compareTo((Object)HttpVersion.HTTP_1_0) == 0);
+        assertTrue(HttpVersion.HTTP_1_0.compareToVersion(HttpVersion.HTTP_1_1) < 0);
+        assertTrue(HttpVersion.HTTP_1_0.compareToVersion(HttpVersion.HTTP_0_9) > 0);
+        assertTrue(HttpVersion.HTTP_1_0.compareToVersion(HttpVersion.HTTP_1_0) == 0);
    }
     
 }