View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.http;
29  
30  import org.apache.hc.core5.annotation.Contract;
31  import org.apache.hc.core5.annotation.ThreadingBehavior;
32  
33  /**
34   * Represents an HTTP version. HTTP uses a "major.minor" numbering
35   * scheme to indicate versions of the protocol.
36   * <p>
37   * The version of an HTTP message is indicated by an HTTP-Version field
38   * in the first line of the message.
39   * </p>
40   *
41   * @since 4.0
42   */
43  @Contract(threading = ThreadingBehavior.IMMUTABLE)
44  public final class HttpVersion extends ProtocolVersion {
45  
46      private static final long serialVersionUID = -5856653513894415344L;
47  
48      /** The protocol name. */
49      public static final String HTTP = "HTTP";
50  
51      /** HTTP protocol version 0.9 */
52      public static final HttpVersion HTTP_0_9 = new HttpVersion(0, 9);
53  
54      /** HTTP protocol version 1.0 */
55      public static final HttpVersion HTTP_1_0 = new HttpVersion(1, 0);
56  
57      /** HTTP protocol version 1.1 */
58      public static final HttpVersion HTTP_1_1 = new HttpVersion(1, 1);
59  
60      /** HTTP protocol version 2.0 */
61      public static final HttpVersion HTTP_2_0 = new HttpVersion(2, 0);
62      public static final HttpVersion HTTP_2   = HTTP_2_0;
63  
64      /** HTTP/1.1 is default */
65      public static final HttpVersion DEFAULT  = HTTP_1_1;
66  
67      /**
68       * All HTTP versions known to HttpCore.
69       */
70      public static final HttpVersion[] ALL = {HTTP_0_9, HTTP_1_0, HTTP_1_1, HTTP_2_0};
71  
72      /**
73       * Create an HTTP protocol version designator.
74       *
75       * @param major   the major version number of the HTTP protocol
76       * @param minor   the minor version number of the HTTP protocol
77       *
78       * @throws IllegalArgumentException if either major or minor version number is negative
79       */
80      public HttpVersion(final int major, final int minor) {
81          super(HTTP, major, minor);
82      }
83  
84  
85      /**
86       * Obtains a specific HTTP version.
87       *
88       * @param major     the major version
89       * @param minor     the minor version
90       *
91       * @return  an instance of {@link HttpVersion} with the argument version
92       */
93      @Override
94      public ProtocolVersion forVersion(final int major, final int minor) {
95  
96          if ((major == getMajor()) && (minor == getMinor())) {
97              return this;
98          }
99  
100         if (major == 1) {
101             if (minor == 0) {
102                 return HTTP_1_0;
103             }
104             if (minor == 1) {
105                 return HTTP_1_1;
106             }
107         }
108         if ((major == 0) && (minor == 9)) {
109             return HTTP_0_9;
110         }
111 
112         // argument checking is done in the constructor
113         return new HttpVersion(major, minor);
114     }
115 
116 }