Index: module-main/src/test/java/org/apache/http/TestHttpVersion.java =================================================================== --- module-main/src/test/java/org/apache/http/TestHttpVersion.java (revision 571807) +++ module-main/src/test/java/org/apache/http/TestHttpVersion.java (working copy) @@ -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() { Index: module-main/src/main/java/org/apache/http/ProtocolVersion.java =================================================================== --- module-main/src/main/java/org/apache/http/ProtocolVersion.java (revision 0) +++ module-main/src/main/java/org/apache/http/ProtocolVersion.java (revision 0) @@ -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 + * . + * + */ + +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. + *

+ * 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 Oleg Kalnichevski + * @author Roland Weber + * + * @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 not 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 true if the argument is the same protocol version, + * false 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 true if {@link #compareToVersion compareToVersion} + * can be called with the argument, false 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 not 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 null + */ + 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 true if this protocol version is + * {@link #isComparable comparable} to the argument + * and {@link #compareToVersion compares} as greater or equal, + * false 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 true if this protocol version is + * {@link #isComparable comparable} to the argument + * and {@link #compareToVersion compares} as less or equal, + * false 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(); + } + +} Property changes on: module-main/src/main/java/org/apache/http/ProtocolVersion.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Date Author Id Revision HeadURL Name: svn:eol-style + native Index: module-main/src/main/java/org/apache/http/impl/DefaultConnectionReuseStrategy.java =================================================================== --- module-main/src/main/java/org/apache/http/impl/DefaultConnectionReuseStrategy.java (revision 571807) +++ module-main/src/main/java/org/apache/http/impl/DefaultConnectionReuseStrategy.java (working copy) @@ -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); } } Index: module-main/src/main/java/org/apache/http/protocol/RequestExpectContinue.java =================================================================== --- module-main/src/main/java/org/apache/http/protocol/RequestExpectContinue.java (revision 571807) +++ module-main/src/main/java/org/apache/http/protocol/RequestExpectContinue.java (working copy) @@ -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); } } Index: module-main/src/main/java/org/apache/http/protocol/HttpRequestExecutor.java =================================================================== --- module-main/src/main/java/org/apache/http/protocol/HttpRequestExecutor.java (revision 571807) +++ module-main/src/main/java/org/apache/http/protocol/HttpRequestExecutor.java (working copy) @@ -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 Index: module-main/src/main/java/org/apache/http/protocol/ResponseContent.java =================================================================== --- module-main/src/main/java/org/apache/http/protocol/ResponseContent.java (revision 571807) +++ module-main/src/main/java/org/apache/http/protocol/ResponseContent.java (working copy) @@ -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())); Index: module-main/src/main/java/org/apache/http/HttpVersion.java =================================================================== --- module-main/src/main/java/org/apache/http/HttpVersion.java (revision 571807) +++ module-main/src/main/java/org/apache/http/HttpVersion.java (working copy) @@ -41,16 +41,14 @@ * * @version $Revision$ $Date$ */ -public final class HttpVersion implements Comparable, Serializable { +public final class HttpVersion extends ProtocolVersion + implements Comparable, Serializable { - static final long serialVersionUID = -3164547215216382904L; - - /** Major version number of the HTTP protocol */ - private int major = 0; + //@@@ static final long serialVersionUID = -3164547215216382904L; - /** Minor version number of the HTTP protocol */ - private int minor = 0; - + /** 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,26 @@ * @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; + super(HTTP, major, 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() + * Compares this HTTP version to another one. + * This method explicitly compares only {@link HttpVersion}, + * not generic {@link ProtocolVersion} even if the + * protocol name would allow the comparison. It defines + * a total ordering, as required by {@link Comparable}. + * + * @param o the object to compare with + * + * @return negative, zero, or positive if this version is + * less than, equal to, or greater than the argument version + * + * @throws ClassCastException if the argument is not comparable */ - public int hashCode() { - return this.major * 100000 + this.minor; + public int compareTo(Object o) throws ClassCastException { + return super.compareToVersion((HttpVersion)o); } - - /** - * @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 true if HTTP protocol version is given to the given number, - * false 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 true if HTTP protocol version is greater or equal given to the - * given number, false 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 true if HTTP protocol version is less or equal to given to the - * given number, false 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(); - } - }