1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.codec.http;
17
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20
21
22
23
24
25
26
27 public class HttpVersion implements Comparable<HttpVersion> {
28
29 private static final Pattern VERSION_PATTERN =
30 Pattern.compile("(\\S+)/(\\d+)\\.(\\d+)");
31
32
33
34
35 public static final HttpVersion HTTP_1_0 = new HttpVersion("HTTP", 1, 0, false);
36
37
38
39
40 public static final HttpVersion HTTP_1_1 = new HttpVersion("HTTP", 1, 1, true);
41
42
43
44
45
46
47
48
49
50 public static HttpVersion valueOf(String text) {
51 if (text == null) {
52 throw new NullPointerException("text");
53 }
54
55 text = text.trim().toUpperCase();
56 if (text.equals("HTTP/1.1")) {
57 return HTTP_1_1;
58 }
59 if (text.equals("HTTP/1.0")) {
60 return HTTP_1_0;
61 }
62 return new HttpVersion(text, true);
63 }
64
65 private final String protocolName;
66 private final int majorVersion;
67 private final int minorVersion;
68 private final String text;
69 private final boolean keepAliveDefault;
70
71
72
73
74 @Deprecated
75 public HttpVersion(String text) {
76 this(text, true);
77 }
78
79
80
81
82
83
84
85
86
87
88
89
90 public HttpVersion(String text, boolean keepAliveDefault) {
91 if (text == null) {
92 throw new NullPointerException("text");
93 }
94
95 text = text.trim().toUpperCase();
96 if (text.length() == 0) {
97 throw new IllegalArgumentException("empty text");
98 }
99
100 Matcher m = VERSION_PATTERN.matcher(text);
101 if (!m.matches()) {
102 throw new IllegalArgumentException("invalid version format: " + text);
103 }
104
105 protocolName = m.group(1);
106 majorVersion = Integer.parseInt(m.group(2));
107 minorVersion = Integer.parseInt(m.group(3));
108 this.text = protocolName + '/' + majorVersion + '.' + minorVersion;
109 this.keepAliveDefault = keepAliveDefault;
110 }
111
112
113
114
115 @Deprecated
116 public HttpVersion(
117 String protocolName, int majorVersion, int minorVersion) {
118 this(protocolName, majorVersion, minorVersion, true);
119 }
120
121
122
123
124
125
126
127
128
129
130
131
132 public HttpVersion(
133 String protocolName, int majorVersion, int minorVersion,
134 boolean keepAliveDefault) {
135 if (protocolName == null) {
136 throw new NullPointerException("protocolName");
137 }
138
139 protocolName = protocolName.trim().toUpperCase();
140 if (protocolName.length() == 0) {
141 throw new IllegalArgumentException("empty protocolName");
142 }
143
144 for (int i = 0; i < protocolName.length(); i ++) {
145 if (Character.isISOControl(protocolName.charAt(i)) ||
146 Character.isWhitespace(protocolName.charAt(i))) {
147 throw new IllegalArgumentException("invalid character in protocolName");
148 }
149 }
150
151 if (majorVersion < 0) {
152 throw new IllegalArgumentException("negative majorVersion");
153 }
154 if (minorVersion < 0) {
155 throw new IllegalArgumentException("negative minorVersion");
156 }
157
158 this.protocolName = protocolName;
159 this.majorVersion = majorVersion;
160 this.minorVersion = minorVersion;
161 text = protocolName + '/' + majorVersion + '.' + minorVersion;
162 this.keepAliveDefault = keepAliveDefault;
163 }
164
165
166
167
168 public String getProtocolName() {
169 return protocolName;
170 }
171
172
173
174
175 public int getMajorVersion() {
176 return majorVersion;
177 }
178
179
180
181
182 public int getMinorVersion() {
183 return minorVersion;
184 }
185
186
187
188
189 public String getText() {
190 return text;
191 }
192
193
194
195
196
197 public boolean isKeepAliveDefault() {
198 return keepAliveDefault;
199 }
200
201
202
203
204 @Override
205 public String toString() {
206 return getText();
207 }
208
209 @Override
210 public int hashCode() {
211 return (getProtocolName().hashCode() * 31 + getMajorVersion()) * 31 +
212 getMinorVersion();
213 }
214
215 @Override
216 public boolean equals(Object o) {
217 if (!(o instanceof HttpVersion)) {
218 return false;
219 }
220
221 HttpVersion that = (HttpVersion) o;
222 return getMinorVersion() == that.getMinorVersion() &&
223 getMajorVersion() == that.getMajorVersion() &&
224 getProtocolName().equals(that.getProtocolName());
225 }
226
227 public int compareTo(HttpVersion o) {
228 int v = getProtocolName().compareTo(o.getProtocolName());
229 if (v != 0) {
230 return v;
231 }
232
233 v = getMajorVersion() - o.getMajorVersion();
234 if (v != 0) {
235 return v;
236 }
237
238 return getMinorVersion() - o.getMinorVersion();
239 }
240 }