1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.core5.http.message;
29
30 import java.io.Serializable;
31
32 import org.apache.hc.core5.annotation.Contract;
33 import org.apache.hc.core5.annotation.ThreadingBehavior;
34 import org.apache.hc.core5.http.HttpResponse;
35 import org.apache.hc.core5.http.HttpVersion;
36 import org.apache.hc.core5.http.ProtocolVersion;
37 import org.apache.hc.core5.util.Args;
38
39
40
41
42
43
44 @Contract(threading = ThreadingBehavior.IMMUTABLE)
45 public final class StatusLine implements Serializable {
46
47 private static final long serialVersionUID = -2443303766890459269L;
48
49
50
51
52 private final ProtocolVersion protoVersion;
53
54
55
56
57 private final int statusCode;
58
59
60
61
62 private final StatusClass statusClass;
63
64
65
66
67 private final String reasonPhrase;
68
69 public StatusLine(final HttpResponse response) {
70 super();
71 Args.notNull(response, "Response");
72 this.protoVersion = response.getVersion() != null ? response.getVersion() : HttpVersion.HTTP_1_1;
73 this.statusCode = response.getCode();
74 this.statusClass = StatusClass.from(this.statusCode);
75 this.reasonPhrase = response.getReasonPhrase();
76 }
77
78
79
80
81
82
83
84
85
86 public StatusLine(final ProtocolVersion version, final int statusCode, final String reasonPhrase) {
87 super();
88 this.statusCode = Args.notNegative(statusCode, "Status code");
89 this.statusClass = StatusClass.from(this.statusCode);
90 this.protoVersion = version != null ? version : HttpVersion.HTTP_1_1;
91 this.reasonPhrase = reasonPhrase;
92 }
93
94 public int getStatusCode() {
95 return this.statusCode;
96 }
97
98 public StatusClass getStatusClass() {
99 return this.statusClass;
100 }
101
102
103
104
105
106
107 public boolean isInformational() {
108 return getStatusClass() == StatusClass.INFORMATIONAL;
109 }
110
111
112
113
114
115
116 public boolean isSuccessful() {
117 return getStatusClass() == StatusClass.SUCCESSFUL;
118 }
119
120
121
122
123
124
125 public boolean isRedirection() {
126 return getStatusClass() == StatusClass.REDIRECTION;
127 }
128
129
130
131
132
133
134 public boolean isClientError() {
135 return getStatusClass() == StatusClass.CLIENT_ERROR;
136 }
137
138
139
140
141
142
143 public boolean isServerError() {
144 return getStatusClass() == StatusClass.SERVER_ERROR;
145 }
146
147
148
149
150
151
152
153 public boolean isError() {
154 return isClientError() || isServerError();
155 }
156
157 public ProtocolVersion getProtocolVersion() {
158 return this.protoVersion;
159 }
160
161 public String getReasonPhrase() {
162 return this.reasonPhrase;
163 }
164
165 @Override
166 public String toString() {
167 final StringBuilder buf = new StringBuilder();
168 buf.append(this.protoVersion).append(" ").append(this.statusCode).append(" ");
169 if (this.reasonPhrase != null) {
170 buf.append(this.reasonPhrase);
171 }
172 return buf.toString();
173 }
174
175
176
177
178 public enum StatusClass {
179
180
181
182
183 INFORMATIONAL,
184
185
186
187
188 SUCCESSFUL,
189
190
191
192
193 REDIRECTION,
194
195
196
197
198 CLIENT_ERROR,
199
200
201
202
203 SERVER_ERROR,
204
205
206
207
208 OTHER;
209
210
211
212
213
214
215
216 public static StatusClass from(final int statusCode) {
217 final StatusClass statusClass;
218
219 switch (statusCode / 100) {
220 case 1:
221 statusClass = INFORMATIONAL;
222 break;
223 case 2:
224 statusClass = SUCCESSFUL;
225 break;
226 case 3:
227 statusClass = REDIRECTION;
228 break;
229 case 4:
230 statusClass = CLIENT_ERROR;
231 break;
232 case 5:
233 statusClass = SERVER_ERROR;
234 break;
235 default:
236 statusClass = OTHER;
237 break;
238 }
239
240 return statusClass;
241 }
242 }
243 }