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      * 431 Request Header Fields Too Large (RFC6585)
238      */
239     public static final HttpResponseStatus REQUEST_HEADER_FIELDS_TOO_LARGE =
240         new HttpResponseStatus(431, "Request Header Fields Too Large");
241 
242     /**
243      * 500 Internal Server Error
244      */
245     public static final HttpResponseStatus INTERNAL_SERVER_ERROR =
246             new HttpResponseStatus(500, "Internal Server Error");
247 
248     /**
249      * 501 Not Implemented
250      */
251     public static final HttpResponseStatus NOT_IMPLEMENTED = new HttpResponseStatus(501, "Not Implemented");
252 
253     /**
254      * 502 Bad Gateway
255      */
256     public static final HttpResponseStatus BAD_GATEWAY = new HttpResponseStatus(502, "Bad Gateway");
257 
258     /**
259      * 503 Service Unavailable
260      */
261     public static final HttpResponseStatus SERVICE_UNAVAILABLE = new HttpResponseStatus(503, "Service Unavailable");
262 
263     /**
264      * 504 Gateway Timeout
265      */
266     public static final HttpResponseStatus GATEWAY_TIMEOUT = new HttpResponseStatus(504, "Gateway Timeout");
267 
268     /**
269      * 505 HTTP Version Not Supported
270      */
271     public static final HttpResponseStatus HTTP_VERSION_NOT_SUPPORTED =
272             new HttpResponseStatus(505, "HTTP Version Not Supported");
273 
274     /**
275      * 506 Variant Also Negotiates (RFC2295)
276      */
277     public static final HttpResponseStatus VARIANT_ALSO_NEGOTIATES =
278             new HttpResponseStatus(506, "Variant Also Negotiates");
279 
280     /**
281      * 507 Insufficient Storage (WebDAV, RFC4918)
282      */
283     public static final HttpResponseStatus INSUFFICIENT_STORAGE = new HttpResponseStatus(507, "Insufficient Storage");
284 
285     /**
286      * 510 Not Extended (RFC2774)
287      */
288     public static final HttpResponseStatus NOT_EXTENDED = new HttpResponseStatus(510, "Not Extended");
289 
290     /**
291      * Returns the {@link HttpResponseStatus} represented by the specified code.
292      * If the specified code is a standard HTTP status code, a cached instance
293      * will be returned.  Otherwise, a new instance will be returned.
294      */
295     public static HttpResponseStatus valueOf(int code) {
296         switch (code) {
297         case 100:
298             return CONTINUE;
299         case 101:
300             return SWITCHING_PROTOCOLS;
301         case 102:
302             return PROCESSING;
303         case 200:
304             return OK;
305         case 201:
306             return CREATED;
307         case 202:
308             return ACCEPTED;
309         case 203:
310             return NON_AUTHORITATIVE_INFORMATION;
311         case 204:
312             return NO_CONTENT;
313         case 205:
314             return RESET_CONTENT;
315         case 206:
316             return PARTIAL_CONTENT;
317         case 207:
318             return MULTI_STATUS;
319         case 300:
320             return MULTIPLE_CHOICES;
321         case 301:
322             return MOVED_PERMANENTLY;
323         case 302:
324             return FOUND;
325         case 303:
326             return SEE_OTHER;
327         case 304:
328             return NOT_MODIFIED;
329         case 305:
330             return USE_PROXY;
331         case 307:
332             return TEMPORARY_REDIRECT;
333         case 400:
334             return BAD_REQUEST;
335         case 401:
336             return UNAUTHORIZED;
337         case 402:
338             return PAYMENT_REQUIRED;
339         case 403:
340             return FORBIDDEN;
341         case 404:
342             return NOT_FOUND;
343         case 405:
344             return METHOD_NOT_ALLOWED;
345         case 406:
346             return NOT_ACCEPTABLE;
347         case 407:
348             return PROXY_AUTHENTICATION_REQUIRED;
349         case 408:
350             return REQUEST_TIMEOUT;
351         case 409:
352             return CONFLICT;
353         case 410:
354             return GONE;
355         case 411:
356             return LENGTH_REQUIRED;
357         case 412:
358             return PRECONDITION_FAILED;
359         case 413:
360             return REQUEST_ENTITY_TOO_LARGE;
361         case 414:
362             return REQUEST_URI_TOO_LONG;
363         case 415:
364             return UNSUPPORTED_MEDIA_TYPE;
365         case 416:
366             return REQUESTED_RANGE_NOT_SATISFIABLE;
367         case 417:
368             return EXPECTATION_FAILED;
369         case 422:
370             return UNPROCESSABLE_ENTITY;
371         case 423:
372             return LOCKED;
373         case 424:
374             return FAILED_DEPENDENCY;
375         case 425:
376             return UNORDERED_COLLECTION;
377         case 426:
378             return UPGRADE_REQUIRED;
379         case 500:
380             return INTERNAL_SERVER_ERROR;
381         case 501:
382             return NOT_IMPLEMENTED;
383         case 502:
384             return BAD_GATEWAY;
385         case 503:
386             return SERVICE_UNAVAILABLE;
387         case 504:
388             return GATEWAY_TIMEOUT;
389         case 505:
390             return HTTP_VERSION_NOT_SUPPORTED;
391         case 506:
392             return VARIANT_ALSO_NEGOTIATES;
393         case 507:
394             return INSUFFICIENT_STORAGE;
395         case 510:
396             return NOT_EXTENDED;
397         }
398 
399         final String reasonPhrase;
400 
401         if (code < 100) {
402             reasonPhrase = "Unknown Status";
403         } else if (code < 200) {
404             reasonPhrase = "Informational";
405         } else if (code < 300) {
406             reasonPhrase = "Successful";
407         } else if (code < 400) {
408             reasonPhrase = "Redirection";
409         } else if (code < 500) {
410             reasonPhrase = "Client Error";
411         } else if (code < 600) {
412             reasonPhrase = "Server Error";
413         } else {
414             reasonPhrase = "Unknown Status";
415         }
416 
417         return new HttpResponseStatus(code, reasonPhrase + " (" + code + ')');
418     }
419 
420     private final int code;
421 
422     private final String reasonPhrase;
423 
424     /**
425      * Creates a new instance with the specified {@code code} and its
426      * {@code reasonPhrase}.
427      */
428     public HttpResponseStatus(int code, String reasonPhrase) {
429         if (code < 0) {
430             throw new IllegalArgumentException(
431                     "code: " + code + " (expected: 0+)");
432         }
433 
434         if (reasonPhrase == null) {
435             throw new NullPointerException("reasonPhrase");
436         }
437 
438         for (int i = 0; i < reasonPhrase.length(); i ++) {
439             char c = reasonPhrase.charAt(i);
440             // Check prohibited characters.
441             switch (c) {
442             case '\n': case '\r':
443                 throw new IllegalArgumentException(
444                         "reasonPhrase contains one of the following prohibited characters: " +
445                         "\\r\\n: " + reasonPhrase);
446             }
447         }
448 
449         this.code = code;
450         this.reasonPhrase = reasonPhrase;
451     }
452 
453     /**
454      * Returns the code of this status.
455      */
456     public int getCode() {
457         return code;
458     }
459 
460     /**
461      * Returns the reason phrase of this status.
462      */
463     public String getReasonPhrase() {
464         return reasonPhrase;
465     }
466 
467     @Override
468     public int hashCode() {
469         return getCode();
470     }
471 
472     @Override
473     public boolean equals(Object o) {
474         if (!(o instanceof HttpResponseStatus)) {
475             return false;
476         }
477 
478         return getCode() == ((HttpResponseStatus) o).getCode();
479     }
480 
481     public int compareTo(HttpResponseStatus o) {
482         return getCode() - o.getCode();
483     }
484 
485     @Override
486     public String toString() {
487         StringBuilder buf = new StringBuilder(reasonPhrase.length() + 5);
488         buf.append(code);
489         buf.append(' ');
490         buf.append(reasonPhrase);
491         return buf.toString();
492     }
493 }