View Javadoc

1   /*
2    * $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 $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 1999-2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   */
29  
30  package org.apache.commons.httpclient;
31  
32  /***
33   *  <p>HTTP version, as specified in RFC 2616.</p>
34   *  <p>
35   *  HTTP uses a "&lt;major&gt;.&lt;minor&gt;" numbering scheme to indicate
36   *  versions of the protocol. The protocol versioning policy is intended to
37   *  allow the sender to indicate the format of a message and its capacity for
38   *  understanding further HTTP communication, rather than the features
39   *  obtained via that communication. No change is made to the version
40   *  number for the addition of message components which do not affect
41   *  communication behavior or which only add to extensible field values.
42   *  The &lt;minor&gt; number is incremented when the changes made to the
43   *  protocol add features which do not change the general message parsing
44   *  algorithm, but which may add to the message semantics and imply
45   *  additional capabilities of the sender. The &lt;major&gt; number is
46   *  incremented when the format of a message within the protocol is
47   *  changed. See RFC 2145 [36] for a fuller explanation.
48   *  </p>
49   *  <p>
50   *  The version of an HTTP message is indicated by an HTTP-Version field
51   *  in the first line of the message.
52   *  </p>
53   *  <pre>
54   *     HTTP-Version   = "HTTP" "/" 1*DIGIT "." 1*DIGIT
55   *  </pre>
56   *  <p>
57   *   Note that the major and minor numbers MUST be treated as separate
58   *   integers and that each MAY be incremented higher than a single digit.
59   *   Thus, HTTP/2.4 is a lower version than HTTP/2.13, which in turn is
60   *   lower than HTTP/12.3. Leading zeros MUST be ignored by recipients and
61   *   MUST NOT be sent.
62   *  </p>
63   * 
64   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
65   * 
66   * @version $Revision: 155418 $ $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
67   *
68   * @since 3.0
69   */
70  public class HttpVersion implements Comparable {
71  
72      /*** Major version number of the HTTP protocol */
73      private int major = 0;
74  
75      /*** Minor version number of the HTTP protocol */
76      private int minor = 0;
77  
78      /*** HTTP protocol version 0.9 */
79      public static final HttpVersion HTTP_0_9 = new HttpVersion(0, 9);  
80  
81      /*** HTTP protocol version 1.0 */
82      public static final HttpVersion HTTP_1_0 = new HttpVersion(1, 0);  
83  
84      /*** HTTP protocol version 1.1 */
85      public static final HttpVersion HTTP_1_1 = new HttpVersion(1, 1);  
86      
87      /***
88       * Create an HTTP protocol version designator.
89       *
90       * @param major   the major version number of the HTTP protocol
91       * @param minor   the minor version number of the HTTP protocol
92       * 
93       * @throws IllegalArgumentException if either major or minor version number is negative
94       */
95      public HttpVersion(int major, int minor) {
96          if (major < 0) {
97              throw new IllegalArgumentException("HTTP major version number may not be negative");
98          }
99          this.major = major;
100         if (minor < 0) {
101             throw new IllegalArgumentException("HTTP minor version number may not be negative");
102         }
103         this.minor = minor;
104     }
105     
106     /***
107      * Returns the major version number of the HTTP protocol.
108      * 
109      * @return the major version number.
110      */
111     public int getMajor() {
112         return major;
113     }
114 
115     /***
116      * Returns the minor version number of the HTTP protocol.
117      * 
118      * @return the minor version number.
119      */
120     public int getMinor() {
121         return minor;
122     }
123 
124     /***
125      * @see java.lang.Object#hashCode()
126      */
127     public int hashCode() {
128         return this.major * 100000 + this.minor;
129     }
130         
131     /***
132      * @see java.lang.Object#equals(java.lang.Object)
133      */
134     public boolean equals(Object obj) {
135         if (this == obj) {
136             return true;
137         }
138         if (!(obj instanceof HttpVersion)) {
139             return false;
140         }
141         return equals((HttpVersion)obj);  
142     }
143 
144     /***
145      * Compares this HTTP protocol version with another one.
146      * 
147      * @param anotherVer the version to be compared with.
148      *  
149      * @return a negative integer, zero, or a positive integer as this version is less than, 
150      *    equal to, or greater than the specified version.     
151      */
152     public int compareTo(HttpVersion anotherVer) {
153         if (anotherVer == null) {
154             throw new IllegalArgumentException("Version parameter may not be null"); 
155         }
156         int delta = getMajor() - anotherVer.getMajor();
157         if (delta == 0) {
158             delta = getMinor() - anotherVer.getMinor();
159         }
160         return delta;
161     }
162 
163     /***
164      * @see java.lang.Comparable#compareTo(java.lang.Object)
165      */
166     public int compareTo(Object o) {
167         return compareTo((HttpVersion)o);
168     }
169 
170     /***
171      * Test if the HTTP protocol version is equal to the given number.
172      * 
173      * @return <tt>true</tt> if HTTP protocol version is given to the given number, 
174      *         <tt>false</tt> otherwise.
175      */
176     public boolean equals(HttpVersion version) {
177         return compareTo(version) == 0;  
178     }
179 
180     /***
181      * Test if the HTTP protocol version is greater or equal to the given number.
182      * 
183      * @return <tt>true</tt> if HTTP protocol version is greater or equal given to the 
184      *         given number, <tt>false</tt> otherwise.
185      */
186     public boolean greaterEquals(HttpVersion version) {
187         return compareTo(version) >= 0;
188     }
189 
190     /***
191      * Test if the HTTP protocol version is less or equal to the given number.
192      * 
193      * @return <tt>true</tt> if HTTP protocol version is less or equal to given to the 
194      *         given number, <tt>false</tt> otherwise.
195      */
196     public boolean lessEquals(HttpVersion version) {
197         return compareTo(version) <= 0;
198     }
199 
200     /***
201      * @see java.lang.Object#toString()
202      */
203     public String toString() {
204         StringBuffer buffer = new StringBuffer();
205         buffer.append("HTTP/"); 
206         buffer.append(this.major); 
207         buffer.append('.'); 
208         buffer.append(this.minor); 
209         return buffer.toString();
210     }
211 
212     /***
213      * Parses the textual representation of the given HTTP protocol version.
214      * 
215      * @return HTTP protocol version.
216      * 
217      * @throws ProtocolException if the string is not a valid HTTP protocol version. 
218      */
219     public static HttpVersion parse(final String s) throws ProtocolException {
220         if (s == null) {
221             throw new IllegalArgumentException("String may not be null");
222         }
223         if (!s.startsWith("HTTP/")) {
224             throw new ProtocolException("Invalid HTTP version string: " + s);
225         }
226         int major, minor;
227         
228         int i1 = "HTTP/".length();
229         int i2 = s.indexOf(".", i1);
230         if (i2 == -1) {
231             throw new ProtocolException("Invalid HTTP version number: " + s);
232         }
233         try {
234             major = Integer.parseInt(s.substring(i1, i2)); 
235         } catch (NumberFormatException e) {
236             throw new ProtocolException("Invalid HTTP major version number: " + s);
237         }
238         i1 = i2 + 1;
239         i2 = s.length();
240         try {
241             minor = Integer.parseInt(s.substring(i1, i2)); 
242         } catch (NumberFormatException e) {
243             throw new ProtocolException("Invalid HTTP minor version number: " + s);
244         }
245         return new HttpVersion(major, minor);
246     }
247 
248 }