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