001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/HttpVersion.java,v 1.6 2004/12/20 19:52:50 olegk Exp $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient;
032:
033: /**
034: * <p>HTTP version, as specified in RFC 2616.</p>
035: * <p>
036: * HTTP uses a "<major>.<minor>" numbering scheme to indicate
037: * versions of the protocol. The protocol versioning policy is intended to
038: * allow the sender to indicate the format of a message and its capacity for
039: * understanding further HTTP communication, rather than the features
040: * obtained via that communication. No change is made to the version
041: * number for the addition of message components which do not affect
042: * communication behavior or which only add to extensible field values.
043: * The <minor> number is incremented when the changes made to the
044: * protocol add features which do not change the general message parsing
045: * algorithm, but which may add to the message semantics and imply
046: * additional capabilities of the sender. The <major> number is
047: * incremented when the format of a message within the protocol is
048: * changed. See RFC 2145 [36] for a fuller explanation.
049: * </p>
050: * <p>
051: * The version of an HTTP message is indicated by an HTTP-Version field
052: * in the first line of the message.
053: * </p>
054: * <pre>
055: * HTTP-Version = "HTTP" "/" 1*DIGIT "." 1*DIGIT
056: * </pre>
057: * <p>
058: * Note that the major and minor numbers MUST be treated as separate
059: * integers and that each MAY be incremented higher than a single digit.
060: * Thus, HTTP/2.4 is a lower version than HTTP/2.13, which in turn is
061: * lower than HTTP/12.3. Leading zeros MUST be ignored by recipients and
062: * MUST NOT be sent.
063: * </p>
064: *
065: * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
066: *
067: * @version $Revision: 480424 $ $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
068: *
069: * @since 3.0
070: */
071: public class HttpVersion implements Comparable {
072:
073: /** Major version number of the HTTP protocol */
074: private int major = 0;
075:
076: /** Minor version number of the HTTP protocol */
077: private int minor = 0;
078:
079: /** HTTP protocol version 0.9 */
080: public static final HttpVersion HTTP_0_9 = new HttpVersion(0, 9);
081:
082: /** HTTP protocol version 1.0 */
083: public static final HttpVersion HTTP_1_0 = new HttpVersion(1, 0);
084:
085: /** HTTP protocol version 1.1 */
086: public static final HttpVersion HTTP_1_1 = new HttpVersion(1, 1);
087:
088: /**
089: * Create an HTTP protocol version designator.
090: *
091: * @param major the major version number of the HTTP protocol
092: * @param minor the minor version number of the HTTP protocol
093: *
094: * @throws IllegalArgumentException if either major or minor version number is negative
095: */
096: public HttpVersion(int major, int minor) {
097: if (major < 0) {
098: throw new IllegalArgumentException(
099: "HTTP major version number may not be negative");
100: }
101: this .major = major;
102: if (minor < 0) {
103: throw new IllegalArgumentException(
104: "HTTP minor version number may not be negative");
105: }
106: this .minor = minor;
107: }
108:
109: /**
110: * Returns the major version number of the HTTP protocol.
111: *
112: * @return the major version number.
113: */
114: public int getMajor() {
115: return major;
116: }
117:
118: /**
119: * Returns the minor version number of the HTTP protocol.
120: *
121: * @return the minor version number.
122: */
123: public int getMinor() {
124: return minor;
125: }
126:
127: /**
128: * @see java.lang.Object#hashCode()
129: */
130: public int hashCode() {
131: return this .major * 100000 + this .minor;
132: }
133:
134: /**
135: * @see java.lang.Object#equals(java.lang.Object)
136: */
137: public boolean equals(Object obj) {
138: if (this == obj) {
139: return true;
140: }
141: if (!(obj instanceof HttpVersion)) {
142: return false;
143: }
144: return equals((HttpVersion) obj);
145: }
146:
147: /**
148: * Compares this HTTP protocol version with another one.
149: *
150: * @param anotherVer the version to be compared with.
151: *
152: * @return a negative integer, zero, or a positive integer as this version is less than,
153: * equal to, or greater than the specified version.
154: */
155: public int compareTo(HttpVersion anotherVer) {
156: if (anotherVer == null) {
157: throw new IllegalArgumentException(
158: "Version parameter may not be null");
159: }
160: int delta = getMajor() - anotherVer.getMajor();
161: if (delta == 0) {
162: delta = getMinor() - anotherVer.getMinor();
163: }
164: return delta;
165: }
166:
167: /**
168: * @see java.lang.Comparable#compareTo(java.lang.Object)
169: */
170: public int compareTo(Object o) {
171: return compareTo((HttpVersion) o);
172: }
173:
174: /**
175: * Test if the HTTP protocol version is equal to the given number.
176: *
177: * @return <tt>true</tt> if HTTP protocol version is given to the given number,
178: * <tt>false</tt> otherwise.
179: */
180: public boolean equals(HttpVersion version) {
181: return compareTo(version) == 0;
182: }
183:
184: /**
185: * Test if the HTTP protocol version is greater or equal to the given number.
186: *
187: * @return <tt>true</tt> if HTTP protocol version is greater or equal given to the
188: * given number, <tt>false</tt> otherwise.
189: */
190: public boolean greaterEquals(HttpVersion version) {
191: return compareTo(version) >= 0;
192: }
193:
194: /**
195: * Test if the HTTP protocol version is less or equal to the given number.
196: *
197: * @return <tt>true</tt> if HTTP protocol version is less or equal to given to the
198: * given number, <tt>false</tt> otherwise.
199: */
200: public boolean lessEquals(HttpVersion version) {
201: return compareTo(version) <= 0;
202: }
203:
204: /**
205: * @see java.lang.Object#toString()
206: */
207: public String toString() {
208: StringBuffer buffer = new StringBuffer();
209: buffer.append("HTTP/");
210: buffer.append(this .major);
211: buffer.append('.');
212: buffer.append(this .minor);
213: return buffer.toString();
214: }
215:
216: /**
217: * Parses the textual representation of the given HTTP protocol version.
218: *
219: * @return HTTP protocol version.
220: *
221: * @throws ProtocolException if the string is not a valid HTTP protocol version.
222: */
223: public static HttpVersion parse(final String s)
224: throws ProtocolException {
225: if (s == null) {
226: throw new IllegalArgumentException("String may not be null");
227: }
228: if (!s.startsWith("HTTP/")) {
229: throw new ProtocolException("Invalid HTTP version string: "
230: + s);
231: }
232: int major, minor;
233:
234: int i1 = "HTTP/".length();
235: int i2 = s.indexOf(".", i1);
236: if (i2 == -1) {
237: throw new ProtocolException("Invalid HTTP version number: "
238: + s);
239: }
240: try {
241: major = Integer.parseInt(s.substring(i1, i2));
242: } catch (NumberFormatException e) {
243: throw new ProtocolException(
244: "Invalid HTTP major version number: " + s);
245: }
246: i1 = i2 + 1;
247: i2 = s.length();
248: try {
249: minor = Integer.parseInt(s.substring(i1, i2));
250: } catch (NumberFormatException e) {
251: throw new ProtocolException(
252: "Invalid HTTP minor version number: " + s);
253: }
254: return new HttpVersion(major, minor);
255: }
256:
257: }
|