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