View Javadoc

1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.handler.codec.http;
17  
18  /**
19   * The response code and its description of HTTP or its derived protocols, such as
20   * <a href="http://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol">RTSP</a> and
21   * <a href="http://en.wikipedia.org/wiki/Internet_Content_Adaptation_Protocol">ICAP</a>.
22   * @apiviz.exclude
23   */
24  public class HttpResponseStatus implements Comparable<HttpResponseStatus> {
25  
26      /**
27       * 100 Continue
28       */
29      public static final HttpResponseStatus CONTINUE = new HttpResponseStatus(100, "Continue");
30  
31      /**
32       * 101 Switching Protocols
33       */
34      public static final HttpResponseStatus SWITCHING_PROTOCOLS = new HttpResponseStatus(101, "Switching Protocols");
35  
36      /**
37       * 102 Processing (WebDAV, RFC2518)
38       */
39      public static final HttpResponseStatus PROCESSING = new HttpResponseStatus(102, "Processing");
40  
41      /**
42       * 200 OK
43       */
44      public static final HttpResponseStatus OK = new HttpResponseStatus(200, "OK");
45  
46      /**
47       * 201 Created
48       */
49      public static final HttpResponseStatus CREATED = new HttpResponseStatus(201, "Created");
50  
51      /**
52       * 202 Accepted
53       */
54      public static final HttpResponseStatus ACCEPTED = new HttpResponseStatus(202, "Accepted");
55  
56      /**
57       * 203 Non-Authoritative Information (since HTTP/1.1)
58       */
59      public static final HttpResponseStatus NON_AUTHORITATIVE_INFORMATION =
60              new HttpResponseStatus(203, "Non-Authoritative Information");
61  
62      /**
63       * 204 No Content
64       */
65      public static final HttpResponseStatus NO_CONTENT = new HttpResponseStatus(204, "No Content");
66  
67      /**
68       * 205 Reset Content
69       */
70      public static final HttpResponseStatus RESET_CONTENT = new HttpResponseStatus(205, "Reset Content");
71  
72      /**
73       * 206 Partial Content
74       */
75      public static final HttpResponseStatus PARTIAL_CONTENT = new HttpResponseStatus(206, "Partial Content");
76  
77      /**
78       * 207 Multi-Status (WebDAV, RFC2518)
79       */
80      public static final HttpResponseStatus MULTI_STATUS = new HttpResponseStatus(207, "Multi-Status");
81  
82      /**
83       * 300 Multiple Choices
84       */
85      public static final HttpResponseStatus MULTIPLE_CHOICES = new HttpResponseStatus(300, "Multiple Choices");
86  
87      /**
88       * 301 Moved Permanently
89       */
90      public static final HttpResponseStatus MOVED_PERMANENTLY = new HttpResponseStatus(301, "Moved Permanently");
91  
92      /**
93       * 302 Found
94       */
95      public static final HttpResponseStatus FOUND = new HttpResponseStatus(302, "Found");
96  
97      /**
98       * 303 See Other (since HTTP/1.1)
99       */
100     public static final HttpResponseStatus SEE_OTHER = new HttpResponseStatus(303, "See Other");
101 
102     /**
103      * 304 Not Modified
104      */
105     public static final HttpResponseStatus NOT_MODIFIED = new HttpResponseStatus(304, "Not Modified");
106 
107     /**
108      * 305 Use Proxy (since HTTP/1.1)
109      */
110     public static final HttpResponseStatus USE_PROXY = new HttpResponseStatus(305, "Use Proxy");
111 
112     /**
113      * 307 Temporary Redirect (since HTTP/1.1)
114      */
115     public static final HttpResponseStatus TEMPORARY_REDIRECT = new HttpResponseStatus(307, "Temporary Redirect");
116 
117     /**
118      * 400 Bad Request
119      */
120     public static final HttpResponseStatus BAD_REQUEST = new HttpResponseStatus(400, "Bad Request");
121 
122     /**
123      * 401 Unauthorized
124      */
125     public static final HttpResponseStatus UNAUTHORIZED = new HttpResponseStatus(401, "Unauthorized");
126 
127     /**
128      * 402 Payment Required
129      */
130     public static final HttpResponseStatus PAYMENT_REQUIRED = new HttpResponseStatus(402, "Payment Required");
131 
132     /**
133      * 403 Forbidden
134      */
135     public static final HttpResponseStatus FORBIDDEN = new HttpResponseStatus(403, "Forbidden");
136 
137     /**
138      * 404 Not Found
139      */
140     public static final HttpResponseStatus NOT_FOUND = new HttpResponseStatus(404, "Not Found");
141 
142     /**
143      * 405 Method Not Allowed
144      */
145     public static final HttpResponseStatus METHOD_NOT_ALLOWED = new HttpResponseStatus(405, "Method Not Allowed");
146 
147     /**
148      * 406 Not Acceptable
149      */
150     public static final HttpResponseStatus NOT_ACCEPTABLE = new HttpResponseStatus(406, "Not Acceptable");
151 
152     /**
153      * 407 Proxy Authentication Required
154      */
155     public static final HttpResponseStatus PROXY_AUTHENTICATION_REQUIRED =
156             new HttpResponseStatus(407, "Proxy Authentication Required");
157 
158     /**
159      * 408 Request Timeout
160      */
161     public static final HttpResponseStatus REQUEST_TIMEOUT = new HttpResponseStatus(408, "Request Timeout");
162 
163     /**
164      * 409 Conflict
165      */
166     public static final HttpResponseStatus CONFLICT = new HttpResponseStatus(409, "Conflict");
167 
168     /**
169      * 410 Gone
170      */
171     public static final HttpResponseStatus GONE = new HttpResponseStatus(410, "Gone");
172 
173     /**
174      * 411 Length Required
175      */
176     public static final HttpResponseStatus LENGTH_REQUIRED = new HttpResponseStatus(411, "Length Required");
177 
178     /**
179      * 412 Precondition Failed
180      */
181     public static final HttpResponseStatus PRECONDITION_FAILED = new HttpResponseStatus(412, "Precondition Failed");
182 
183     /**
184      * 413 Request Entity Too Large
185      */
186     public static final HttpResponseStatus REQUEST_ENTITY_TOO_LARGE =
187             new HttpResponseStatus(413, "Request Entity Too Large");
188 
189     /**
190      * 414 Request-URI Too Long
191      */
192     public static final HttpResponseStatus REQUEST_URI_TOO_LONG = new HttpResponseStatus(414, "Request-URI Too Long");
193 
194     /**
195      * 415 Unsupported Media Type
196      */
197     public static final HttpResponseStatus UNSUPPORTED_MEDIA_TYPE =
198             new HttpResponseStatus(415, "Unsupported Media Type");
199 
200     /**
201      * 416 Requested Range Not Satisfiable
202      */
203     public static final HttpResponseStatus REQUESTED_RANGE_NOT_SATISFIABLE =
204             new HttpResponseStatus(416, "Requested Range Not Satisfiable");
205 
206     /**
207      * 417 Expectation Failed
208      */
209     public static final HttpResponseStatus EXPECTATION_FAILED = new HttpResponseStatus(417, "Expectation Failed");
210 
211     /**
212      * 422 Unprocessable Entity (WebDAV, RFC4918)
213      */
214     public static final HttpResponseStatus UNPROCESSABLE_ENTITY = new HttpResponseStatus(422, "Unprocessable Entity");
215 
216     /**
217      * 423 Locked (WebDAV, RFC4918)
218      */
219     public static final HttpResponseStatus LOCKED = new HttpResponseStatus(423, "Locked");
220 
221     /**
222      * 424 Failed Dependency (WebDAV, RFC4918)
223      */
224     public static final HttpResponseStatus FAILED_DEPENDENCY = new HttpResponseStatus(424, "Failed Dependency");
225 
226     /**
227      * 425 Unordered Collection (WebDAV, RFC3648)
228      */
229     public static final HttpResponseStatus UNORDERED_COLLECTION = new HttpResponseStatus(425, "Unordered Collection");
230 
231     /**
232      * 426 Upgrade Required (RFC2817)
233      */
234     public static final HttpResponseStatus UPGRADE_REQUIRED = new HttpResponseStatus(426, "Upgrade Required");
235 
236     /**
237      * 428 Precondition Required (RFC6585)
238      */
239     public static final HttpResponseStatus PRECONDITION_REQUIRED = new HttpResponseStatus(428, "Precondition Required");
240 
241     /**
242      * 429 Too Many Requests (RFC6585)
243      */
244     public static final HttpResponseStatus TOO_MANY_REQUESTS = new HttpResponseStatus(429, "Too Many Requests");
245 
246     /**
247      * 431 Request Header Fields Too Large (RFC6585)
248      */
249     public static final HttpResponseStatus REQUEST_HEADER_FIELDS_TOO_LARGE =
250         new HttpResponseStatus(431, "Request Header Fields Too Large");
251 
252     /**
253      * 500 Internal Server Error
254      */
255     public static final HttpResponseStatus INTERNAL_SERVER_ERROR =
256             new HttpResponseStatus(500, "Internal Server Error");
257 
258     /**
259      * 501 Not Implemented
260      */
261     public static final HttpResponseStatus NOT_IMPLEMENTED = new HttpResponseStatus(501, "Not Implemented");
262 
263     /**
264      * 502 Bad Gateway
265      */
266     public static final HttpResponseStatus BAD_GATEWAY = new HttpResponseStatus(502, "Bad Gateway");
267 
268     /**
269      * 503 Service Unavailable
270      */
271     public static final HttpResponseStatus SERVICE_UNAVAILABLE = new HttpResponseStatus(503, "Service Unavailable");
272 
273     /**
274      * 504 Gateway Timeout
275      */
276     public static final HttpResponseStatus GATEWAY_TIMEOUT = new HttpResponseStatus(504, "Gateway Timeout");
277 
278     /**
279      * 505 HTTP Version Not Supported
280      */
281     public static final HttpResponseStatus HTTP_VERSION_NOT_SUPPORTED =
282             new HttpResponseStatus(505, "HTTP Version Not Supported");
283 
284     /**
285      * 506 Variant Also Negotiates (RFC2295)
286      */
287     public static final HttpResponseStatus VARIANT_ALSO_NEGOTIATES =
288             new HttpResponseStatus(506, "Variant Also Negotiates");
289 
290     /**
291      * 507 Insufficient Storage (WebDAV, RFC4918)
292      */
293     public static final HttpResponseStatus INSUFFICIENT_STORAGE = new HttpResponseStatus(507, "Insufficient Storage");
294 
295     /**
296      * 510 Not Extended (RFC2774)
297      */
298     public static final HttpResponseStatus NOT_EXTENDED = new HttpResponseStatus(510, "Not Extended");
299 
300     /**
301      * 511 Network Authentication Required (RFC6585)
302      */
303     public static final HttpResponseStatus NETWORK_AUTHENTICATION_REQUIRED =
304             new HttpResponseStatus(511, "Network Authentication Required");
305 
306     /**
307      * Returns the {@link HttpResponseStatus} represented by the specified code.
308      * If the specified code is a standard HTTP status code, a cached instance
309      * will be returned.  Otherwise, a new instance will be returned.
310      */
311     public static HttpResponseStatus valueOf(int code) {
312         switch (code) {
313         case 100:
314             return CONTINUE;
315         case 101:
316             return SWITCHING_PROTOCOLS;
317         case 102:
318             return PROCESSING;
319         case 200:
320             return OK;
321         case 201:
322             return CREATED;
323         case 202:
324             return ACCEPTED;
325         case 203:
326             return NON_AUTHORITATIVE_INFORMATION;
327         case 204:
328             return NO_CONTENT;
329         case 205:
330             return RESET_CONTENT;
331         case 206:
332             return PARTIAL_CONTENT;
333         case 207:
334             return MULTI_STATUS;
335         case 300:
336             return MULTIPLE_CHOICES;
337         case 301:
338             return MOVED_PERMANENTLY;
339         case 302:
340             return FOUND;
341         case 303:
342             return SEE_OTHER;
343         case 304:
344             return NOT_MODIFIED;
345         case 305:
346             return USE_PROXY;
347         case 307:
348             return TEMPORARY_REDIRECT;
349         case 400:
350             return BAD_REQUEST;
351         case 401:
352             return UNAUTHORIZED;
353         case 402:
354             return PAYMENT_REQUIRED;
355         case 403:
356             return FORBIDDEN;
357         case 404:
358             return NOT_FOUND;
359         case 405:
360             return METHOD_NOT_ALLOWED;
361         case 406:
362             return NOT_ACCEPTABLE;
363         case 407:
364             return PROXY_AUTHENTICATION_REQUIRED;
365         case 408:
366             return REQUEST_TIMEOUT;
367         case 409:
368             return CONFLICT;
369         case 410:
370             return GONE;
371         case 411:
372             return LENGTH_REQUIRED;
373         case 412:
374             return PRECONDITION_FAILED;
375         case 413:
376             return REQUEST_ENTITY_TOO_LARGE;
377         case 414:
378             return REQUEST_URI_TOO_LONG;
379         case 415:
380             return UNSUPPORTED_MEDIA_TYPE;
381         case 416:
382             return REQUESTED_RANGE_NOT_SATISFIABLE;
383         case 417:
384             return EXPECTATION_FAILED;
385         case 422:
386             return UNPROCESSABLE_ENTITY;
387         case 423:
388             return LOCKED;
389         case 424:
390             return FAILED_DEPENDENCY;
391         case 425:
392             return UNORDERED_COLLECTION;
393         case 426:
394             return UPGRADE_REQUIRED;
395         case 428:
396             return PRECONDITION_REQUIRED;
397         case 429:
398             return TOO_MANY_REQUESTS;
399         case 431:
400             return REQUEST_HEADER_FIELDS_TOO_LARGE;
401         case 500:
402             return INTERNAL_SERVER_ERROR;
403         case 501:
404             return NOT_IMPLEMENTED;
405         case 502:
406             return BAD_GATEWAY;
407         case 503:
408             return SERVICE_UNAVAILABLE;
409         case 504:
410             return GATEWAY_TIMEOUT;
411         case 505:
412             return HTTP_VERSION_NOT_SUPPORTED;
413         case 506:
414             return VARIANT_ALSO_NEGOTIATES;
415         case 507:
416             return INSUFFICIENT_STORAGE;
417         case 510:
418             return NOT_EXTENDED;
419         case 511:
420             return NETWORK_AUTHENTICATION_REQUIRED;
421         }
422 
423         final String reasonPhrase;
424 
425         if (code < 100) {
426             reasonPhrase = "Unknown Status";
427         } else if (code < 200) {
428             reasonPhrase = "Informational";
429         } else if (code < 300) {
430             reasonPhrase = "Successful";
431         } else if (code < 400) {
432             reasonPhrase = "Redirection";
433         } else if (code < 500) {
434             reasonPhrase = "Client Error";
435         } else if (code < 600) {
436             reasonPhrase = "Server Error";
437         } else {
438             reasonPhrase = "Unknown Status";
439         }
440 
441         return new HttpResponseStatus(code, reasonPhrase + " (" + code + ')');
442     }
443 
444     private final int code;
445 
446     private final String reasonPhrase;
447 
448     /**
449      * Creates a new instance with the specified {@code code} and its
450      * {@code reasonPhrase}.
451      */
452     public HttpResponseStatus(int code, String reasonPhrase) {
453         if (code < 0) {
454             throw new IllegalArgumentException(
455                     "code: " + code + " (expected: 0+)");
456         }
457 
458         if (reasonPhrase == null) {
459             throw new NullPointerException("reasonPhrase");
460         }
461 
462         for (int i = 0; i < reasonPhrase.length(); i ++) {
463             char c = reasonPhrase.charAt(i);
464             // Check prohibited characters.
465             switch (c) {
466             case '\n': case '\r':
467                 throw new IllegalArgumentException(
468                         "reasonPhrase contains one of the following prohibited characters: " +
469                         "\\r\\n: " + reasonPhrase);
470             }
471         }
472 
473         this.code = code;
474         this.reasonPhrase = reasonPhrase;
475     }
476 
477     /**
478      * Returns the code of this status.
479      */
480     public int getCode() {
481         return code;
482     }
483 
484     /**
485      * Returns the reason phrase of this status.
486      */
487     public String getReasonPhrase() {
488         return reasonPhrase;
489     }
490 
491     @Override
492     public int hashCode() {
493         return getCode();
494     }
495 
496     @Override
497     public boolean equals(Object o) {
498         if (!(o instanceof HttpResponseStatus)) {
499             return false;
500         }
501 
502         return getCode() == ((HttpResponseStatus) o).getCode();
503     }
504 
505     public int compareTo(HttpResponseStatus o) {
506         return getCode() - o.getCode();
507     }
508 
509     @Override
510     public String toString() {
511         StringBuilder buf = new StringBuilder(reasonPhrase.length() + 5);
512         buf.append(code);
513         buf.append(' ');
514         buf.append(reasonPhrase);
515         return buf.toString();
516     }
517 }