View Javadoc

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