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.http;
29
30 import java.io.Serializable;
31
32 import org.apache.http.annotation.Immutable;
33
34 /**
35 * Represents an HTTP version. HTTP uses a "major.minor" numbering
36 * scheme to indicate versions of the protocol.
37 * <p>
38 * The version of an HTTP message is indicated by an HTTP-Version field
39 * in the first line of the message.
40 * </p>
41 * <pre>
42 * HTTP-Version = "HTTP" "/" 1*DIGIT "." 1*DIGIT
43 * </pre>
44 *
45 * @since 4.0
46 */
47 @Immutable
48 public final class HttpVersion extends ProtocolVersion
49 implements Serializable {
50
51 private static final long serialVersionUID = -5856653513894415344L;
52
53 /** The protocol name. */
54 public static final String HTTP = "HTTP";
55
56 /** HTTP protocol version 0.9 */
57 public static final HttpVersion HTTP_0_9 = new HttpVersion(0, 9);
58
59 /** HTTP protocol version 1.0 */
60 public static final HttpVersion HTTP_1_0 = new HttpVersion(1, 0);
61
62 /** HTTP protocol version 1.1 */
63 public static final HttpVersion HTTP_1_1 = new HttpVersion(1, 1);
64
65
66 /**
67 * Create an HTTP protocol version designator.
68 *
69 * @param major the major version number of the HTTP protocol
70 * @param minor the minor version number of the HTTP protocol
71 *
72 * @throws IllegalArgumentException if either major or minor version number is negative
73 */
74 public HttpVersion(int major, int minor) {
75 super(HTTP, major, minor);
76 }
77
78
79 /**
80 * Obtains a specific HTTP version.
81 *
82 * @param major the major version
83 * @param minor the minor version
84 *
85 * @return an instance of {@link HttpVersion} with the argument version
86 */
87 @Override
88 public ProtocolVersion forVersion(int major, int minor) {
89
90 if ((major == this.major) && (minor == this.minor)) {
91 return this;
92 }
93
94 if (major == 1) {
95 if (minor == 0) {
96 return HTTP_1_0;
97 }
98 if (minor == 1) {
99 return HTTP_1_1;
100 }
101 }
102 if ((major == 0) && (minor == 9)) {
103 return HTTP_0_9;
104 }
105
106 // argument checking is done in the constructor
107 return new HttpVersion(major, minor);
108 }
109
110 }