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
29
30 package org.apache.commons.httpclient;
31
32 /***
33 * Constants enumerating the HTTP status codes.
34 * All status codes defined in RFC1945 (HTTP/1.0, RFC2616 (HTTP/1.1), and
35 * RFC2518 (WebDAV) are supported.
36 *
37 * @see StatusLine
38 * @author Unascribed
39 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
40 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
41 *
42 * TODO: Internationalization of reason phrases
43 *
44 * @version $Id: HttpStatus.java 155418 2005-02-26 13:01:52Z dirkv $
45 */
46 public class HttpStatus {
47
48
49
50
51 /*** Reason phrases lookup table. */
52 private static final String[][] REASON_PHRASES = new String[][]{
53 new String[0],
54 new String[3],
55 new String[8],
56 new String[8],
57 new String[25],
58 new String[8]
59 };
60
61
62
63
64 /***
65 * Get the reason phrase for a particular status code.
66 *
67 * This method always returns the English text as specified in the
68 * relevent RFCs and is not internationalized.
69 *
70 * @param statusCode the numeric status code
71 * @return the reason phrase associated with the given status code
72 * or null if the status code is not recognized.
73 *
74 * TODO: getStatusText should be called getReasonPhrase to match RFC
75 */
76 public static String getStatusText(int statusCode) {
77
78 if (statusCode < 0) {
79 throw new IllegalArgumentException("status code may not be negative");
80 }
81 int classIndex = statusCode / 100;
82 int codeIndex = statusCode - classIndex * 100;
83 if (classIndex < 1 || classIndex > (REASON_PHRASES.length - 1)
84 || codeIndex < 0 || codeIndex > (REASON_PHRASES[classIndex].length - 1)) {
85 return null;
86 }
87 return REASON_PHRASES[classIndex][codeIndex];
88 }
89
90
91
92
93 /***
94 * Store the given reason phrase, by status code.
95 * @param statusCode The status code to lookup
96 * @param reasonPhrase The reason phrase for this status code
97 */
98 private static void addStatusCodeMap(int statusCode, String reasonPhrase) {
99 int classIndex = statusCode / 100;
100 REASON_PHRASES[classIndex][statusCode - classIndex * 100] = reasonPhrase;
101 }
102
103
104
105
106
107
108 /*** <tt>100 Continue</tt> (HTTP/1.1 - RFC 2616) */
109 public static final int SC_CONTINUE = 100;
110 /*** <tt>101 Switching Protocols</tt> (HTTP/1.1 - RFC 2616)*/
111 public static final int SC_SWITCHING_PROTOCOLS = 101;
112 /*** <tt>102 Processing</tt> (WebDAV - RFC 2518) */
113 public static final int SC_PROCESSING = 102;
114
115
116
117 /*** <tt>200 OK</tt> (HTTP/1.0 - RFC 1945) */
118 public static final int SC_OK = 200;
119 /*** <tt>201 Created</tt> (HTTP/1.0 - RFC 1945) */
120 public static final int SC_CREATED = 201;
121 /*** <tt>202 Accepted</tt> (HTTP/1.0 - RFC 1945) */
122 public static final int SC_ACCEPTED = 202;
123 /*** <tt>203 Non Authoritative Information</tt> (HTTP/1.1 - RFC 2616) */
124 public static final int SC_NON_AUTHORITATIVE_INFORMATION = 203;
125 /*** <tt>204 No Content</tt> (HTTP/1.0 - RFC 1945) */
126 public static final int SC_NO_CONTENT = 204;
127 /*** <tt>205 Reset Content</tt> (HTTP/1.1 - RFC 2616) */
128 public static final int SC_RESET_CONTENT = 205;
129 /*** <tt>206 Partial Content</tt> (HTTP/1.1 - RFC 2616) */
130 public static final int SC_PARTIAL_CONTENT = 206;
131 /***
132 * <tt>207 Multi-Status</tt> (WebDAV - RFC 2518) or <tt>207 Partial Update
133 * OK</tt> (HTTP/1.1 - draft-ietf-http-v11-spec-rev-01?)
134 */
135 public static final int SC_MULTI_STATUS = 207;
136
137
138
139 /*** <tt>300 Mutliple Choices</tt> (HTTP/1.1 - RFC 2616) */
140 public static final int SC_MULTIPLE_CHOICES = 300;
141 /*** <tt>301 Moved Permanently</tt> (HTTP/1.0 - RFC 1945) */
142 public static final int SC_MOVED_PERMANENTLY = 301;
143 /*** <tt>302 Moved Temporarily</tt> (Sometimes <tt>Found</tt>) (HTTP/1.0 - RFC 1945) */
144 public static final int SC_MOVED_TEMPORARILY = 302;
145 /*** <tt>303 See Other</tt> (HTTP/1.1 - RFC 2616) */
146 public static final int SC_SEE_OTHER = 303;
147 /*** <tt>304 Not Modified</tt> (HTTP/1.0 - RFC 1945) */
148 public static final int SC_NOT_MODIFIED = 304;
149 /*** <tt>305 Use Proxy</tt> (HTTP/1.1 - RFC 2616) */
150 public static final int SC_USE_PROXY = 305;
151 /*** <tt>307 Temporary Redirect</tt> (HTTP/1.1 - RFC 2616) */
152 public static final int SC_TEMPORARY_REDIRECT = 307;
153
154
155
156 /*** <tt>400 Bad Request</tt> (HTTP/1.1 - RFC 2616) */
157 public static final int SC_BAD_REQUEST = 400;
158 /*** <tt>401 Unauthorized</tt> (HTTP/1.0 - RFC 1945) */
159 public static final int SC_UNAUTHORIZED = 401;
160 /*** <tt>402 Payment Required</tt> (HTTP/1.1 - RFC 2616) */
161 public static final int SC_PAYMENT_REQUIRED = 402;
162 /*** <tt>403 Forbidden</tt> (HTTP/1.0 - RFC 1945) */
163 public static final int SC_FORBIDDEN = 403;
164 /*** <tt>404 Not Found</tt> (HTTP/1.0 - RFC 1945) */
165 public static final int SC_NOT_FOUND = 404;
166 /*** <tt>405 Method Not Allowed</tt> (HTTP/1.1 - RFC 2616) */
167 public static final int SC_METHOD_NOT_ALLOWED = 405;
168 /*** <tt>406 Not Acceptable</tt> (HTTP/1.1 - RFC 2616) */
169 public static final int SC_NOT_ACCEPTABLE = 406;
170 /*** <tt>407 Proxy Authentication Required</tt> (HTTP/1.1 - RFC 2616)*/
171 public static final int SC_PROXY_AUTHENTICATION_REQUIRED = 407;
172 /*** <tt>408 Request Timeout</tt> (HTTP/1.1 - RFC 2616) */
173 public static final int SC_REQUEST_TIMEOUT = 408;
174 /*** <tt>409 Conflict</tt> (HTTP/1.1 - RFC 2616) */
175 public static final int SC_CONFLICT = 409;
176 /*** <tt>410 Gone</tt> (HTTP/1.1 - RFC 2616) */
177 public static final int SC_GONE = 410;
178 /*** <tt>411 Length Required</tt> (HTTP/1.1 - RFC 2616) */
179 public static final int SC_LENGTH_REQUIRED = 411;
180 /*** <tt>412 Precondition Failed</tt> (HTTP/1.1 - RFC 2616) */
181 public static final int SC_PRECONDITION_FAILED = 412;
182 /*** <tt>413 Request Entity Too Large</tt> (HTTP/1.1 - RFC 2616) */
183 public static final int SC_REQUEST_TOO_LONG = 413;
184 /*** <tt>414 Request-URI Too Long</tt> (HTTP/1.1 - RFC 2616) */
185 public static final int SC_REQUEST_URI_TOO_LONG = 414;
186 /*** <tt>415 Unsupported Media Type</tt> (HTTP/1.1 - RFC 2616) */
187 public static final int SC_UNSUPPORTED_MEDIA_TYPE = 415;
188 /*** <tt>416 Requested Range Not Satisfiable</tt> (HTTP/1.1 - RFC 2616) */
189 public static final int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
190 /*** <tt>417 Expectation Failed</tt> (HTTP/1.1 - RFC 2616) */
191 public static final int SC_EXPECTATION_FAILED = 417;
192
193 /***
194 * Static constant for a 418 error.
195 * <tt>418 Unprocessable Entity</tt> (WebDAV drafts?)
196 * or <tt>418 Reauthentication Required</tt> (HTTP/1.1 drafts?)
197 */
198
199
200
201 /***
202 * Static constant for a 419 error.
203 * <tt>419 Insufficient Space on Resource</tt>
204 * (WebDAV - draft-ietf-webdav-protocol-05?)
205 * or <tt>419 Proxy Reauthentication Required</tt>
206 * (HTTP/1.1 drafts?)
207 */
208 public static final int SC_INSUFFICIENT_SPACE_ON_RESOURCE = 419;
209 /***
210 * Static constant for a 420 error.
211 * <tt>420 Method Failure</tt>
212 * (WebDAV - draft-ietf-webdav-protocol-05?)
213 */
214 public static final int SC_METHOD_FAILURE = 420;
215 /*** <tt>422 Unprocessable Entity</tt> (WebDAV - RFC 2518) */
216 public static final int SC_UNPROCESSABLE_ENTITY = 422;
217 /*** <tt>423 Locked</tt> (WebDAV - RFC 2518) */
218 public static final int SC_LOCKED = 423;
219 /*** <tt>424 Failed Dependency</tt> (WebDAV - RFC 2518) */
220 public static final int SC_FAILED_DEPENDENCY = 424;
221
222
223
224 /*** <tt>500 Server Error</tt> (HTTP/1.0 - RFC 1945) */
225 public static final int SC_INTERNAL_SERVER_ERROR = 500;
226 /*** <tt>501 Not Implemented</tt> (HTTP/1.0 - RFC 1945) */
227 public static final int SC_NOT_IMPLEMENTED = 501;
228 /*** <tt>502 Bad Gateway</tt> (HTTP/1.0 - RFC 1945) */
229 public static final int SC_BAD_GATEWAY = 502;
230 /*** <tt>503 Service Unavailable</tt> (HTTP/1.0 - RFC 1945) */
231 public static final int SC_SERVICE_UNAVAILABLE = 503;
232 /*** <tt>504 Gateway Timeout</tt> (HTTP/1.1 - RFC 2616) */
233 public static final int SC_GATEWAY_TIMEOUT = 504;
234 /*** <tt>505 HTTP Version Not Supported</tt> (HTTP/1.1 - RFC 2616) */
235 public static final int SC_HTTP_VERSION_NOT_SUPPORTED = 505;
236
237 /*** <tt>507 Insufficient Storage</tt> (WebDAV - RFC 2518) */
238 public static final int SC_INSUFFICIENT_STORAGE = 507;
239
240
241
242
243 /*** Set up status code to "reason phrase" map. */
244 static {
245
246 addStatusCodeMap(SC_OK, "OK");
247 addStatusCodeMap(SC_CREATED, "Created");
248 addStatusCodeMap(SC_ACCEPTED, "Accepted");
249 addStatusCodeMap(SC_NO_CONTENT, "No Content");
250 addStatusCodeMap(SC_MOVED_PERMANENTLY, "Moved Permanently");
251 addStatusCodeMap(SC_MOVED_TEMPORARILY, "Moved Temporarily");
252 addStatusCodeMap(SC_NOT_MODIFIED, "Not Modified");
253 addStatusCodeMap(SC_BAD_REQUEST, "Bad Request");
254 addStatusCodeMap(SC_UNAUTHORIZED, "Unauthorized");
255 addStatusCodeMap(SC_FORBIDDEN, "Forbidden");
256 addStatusCodeMap(SC_NOT_FOUND, "Not Found");
257 addStatusCodeMap(SC_INTERNAL_SERVER_ERROR, "Internal Server Error");
258 addStatusCodeMap(SC_NOT_IMPLEMENTED, "Not Implemented");
259 addStatusCodeMap(SC_BAD_GATEWAY, "Bad Gateway");
260 addStatusCodeMap(SC_SERVICE_UNAVAILABLE, "Service Unavailable");
261
262
263 addStatusCodeMap(SC_CONTINUE, "Continue");
264 addStatusCodeMap(SC_TEMPORARY_REDIRECT, "Temporary Redirect");
265 addStatusCodeMap(SC_METHOD_NOT_ALLOWED, "Method Not Allowed");
266 addStatusCodeMap(SC_CONFLICT, "Conflict");
267 addStatusCodeMap(SC_PRECONDITION_FAILED, "Precondition Failed");
268 addStatusCodeMap(SC_REQUEST_TOO_LONG, "Request Too Long");
269 addStatusCodeMap(SC_REQUEST_URI_TOO_LONG, "Request-URI Too Long");
270 addStatusCodeMap(SC_UNSUPPORTED_MEDIA_TYPE, "Unsupported Media Type");
271 addStatusCodeMap(SC_MULTIPLE_CHOICES, "Multiple Choices");
272 addStatusCodeMap(SC_SEE_OTHER, "See Other");
273 addStatusCodeMap(SC_USE_PROXY, "Use Proxy");
274 addStatusCodeMap(SC_PAYMENT_REQUIRED, "Payment Required");
275 addStatusCodeMap(SC_NOT_ACCEPTABLE, "Not Acceptable");
276 addStatusCodeMap(SC_PROXY_AUTHENTICATION_REQUIRED,
277 "Proxy Authentication Required");
278 addStatusCodeMap(SC_REQUEST_TIMEOUT,
279 "Request Timeout");
280
281 addStatusCodeMap(SC_SWITCHING_PROTOCOLS, "Switching Protocols");
282 addStatusCodeMap(SC_NON_AUTHORITATIVE_INFORMATION,
283 "Non Authoritative Information");
284 addStatusCodeMap(SC_RESET_CONTENT, "Reset Content");
285 addStatusCodeMap(SC_PARTIAL_CONTENT, "Partial Content");
286 addStatusCodeMap(SC_GATEWAY_TIMEOUT, "Gateway Timeout");
287 addStatusCodeMap(SC_HTTP_VERSION_NOT_SUPPORTED,
288 "Http Version Not Supported");
289 addStatusCodeMap(SC_GONE,
290 "Gone");
291 addStatusCodeMap(SC_LENGTH_REQUIRED,
292 "Length Required");
293 addStatusCodeMap(SC_REQUESTED_RANGE_NOT_SATISFIABLE,
294 "Requested Range Not Satisfiable");
295 addStatusCodeMap(SC_EXPECTATION_FAILED,
296 "Expectation Failed");
297
298
299 addStatusCodeMap(SC_PROCESSING, "Processing");
300 addStatusCodeMap(SC_MULTI_STATUS, "Multi-Status");
301 addStatusCodeMap(SC_UNPROCESSABLE_ENTITY, "Unprocessable Entity");
302 addStatusCodeMap(SC_INSUFFICIENT_SPACE_ON_RESOURCE,
303 "Insufficient Space On Resource");
304 addStatusCodeMap(SC_METHOD_FAILURE, "Method Failure");
305 addStatusCodeMap(SC_LOCKED, "Locked");
306 addStatusCodeMap(SC_INSUFFICIENT_STORAGE , "Insufficient Storage");
307 addStatusCodeMap(SC_FAILED_DEPENDENCY, "Failed Dependency");
308 }
309
310
311 }