Home | History | Annotate | Download | only in impl
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/EnglishReasonPhraseCatalog.java $
      3  * $Revision: 505744 $
      4  * $Date: 2007-02-10 10:58:45 -0800 (Sat, 10 Feb 2007) $
      5  *
      6  * ====================================================================
      7  * Licensed to the Apache Software Foundation (ASF) under one
      8  * or more contributor license agreements.  See the NOTICE file
      9  * distributed with this work for additional information
     10  * regarding copyright ownership.  The ASF licenses this file
     11  * to you under the Apache License, Version 2.0 (the
     12  * "License"); you may not use this file except in compliance
     13  * with the License.  You may obtain a copy of the License at
     14  *
     15  *   http://www.apache.org/licenses/LICENSE-2.0
     16  *
     17  * Unless required by applicable law or agreed to in writing,
     18  * software distributed under the License is distributed on an
     19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     20  * KIND, either express or implied.  See the License for the
     21  * specific language governing permissions and limitations
     22  * under the License.
     23  * ====================================================================
     24  *
     25  * This software consists of voluntary contributions made by many
     26  * individuals on behalf of the Apache Software Foundation.  For more
     27  * information on the Apache Software Foundation, please see
     28  * <http://www.apache.org/>.
     29  *
     30  */
     31 
     32 package org.apache.http.impl;
     33 
     34 import java.util.Locale;
     35 
     36 import org.apache.http.HttpStatus;
     37 import org.apache.http.ReasonPhraseCatalog;
     38 
     39 
     40 /**
     41  * English reason phrases for HTTP status codes.
     42  * All status codes defined in RFC1945 (HTTP/1.0), RFC2616 (HTTP/1.1), and
     43  * RFC2518 (WebDAV) are supported.
     44  *
     45  * @author Unascribed
     46  * @author <a href="mailto:mbowler (at) GargoyleSoftware.com">Mike Bowler</a>
     47  * @author <a href="mailto:jsdever (at) apache.org">Jeff Dever</a>
     48  *
     49  * @version $Revision: 505744 $
     50  */
     51 public class EnglishReasonPhraseCatalog
     52     implements ReasonPhraseCatalog {
     53 
     54     // static array with english reason phrases defined below
     55 
     56     /**
     57      * The default instance of this catalog.
     58      * This catalog is thread safe, so there typically
     59      * is no need to create other instances.
     60      */
     61     public final static EnglishReasonPhraseCatalog INSTANCE =
     62         new EnglishReasonPhraseCatalog();
     63 
     64 
     65     /**
     66      * Restricted default constructor, for derived classes.
     67      * If you need an instance of this class, use {@link #INSTANCE INSTANCE}.
     68      */
     69     protected EnglishReasonPhraseCatalog() {
     70         // no body
     71     }
     72 
     73 
     74     /**
     75      * Obtains the reason phrase for a status code.
     76      *
     77      * @param status    the status code, in the range 100-599
     78      * @param loc       ignored
     79      *
     80      * @return  the reason phrase, or <code>null</code>
     81      */
     82     public String getReason(int status, Locale loc) {
     83         if ((status < 100) || (status >= 600)) {
     84             throw new IllegalArgumentException
     85                 ("Unknown category for status code " + status + ".");
     86         }
     87 
     88         final int category = status / 100;
     89         final int subcode  = status - 100*category;
     90 
     91         String reason = null;
     92         if (REASON_PHRASES[category].length > subcode)
     93             reason = REASON_PHRASES[category][subcode];
     94 
     95         return reason;
     96     }
     97 
     98 
     99     /** Reason phrases lookup table. */
    100     private static final String[][] REASON_PHRASES = new String[][]{
    101         null,
    102         new String[3],  // 1xx
    103         new String[8],  // 2xx
    104         new String[8],  // 3xx
    105         new String[25], // 4xx
    106         new String[8]   // 5xx
    107     };
    108 
    109 
    110 
    111     /**
    112      * Stores the given reason phrase, by status code.
    113      * Helper method to initialize the static lookup table.
    114      *
    115      * @param status    the status code for which to define the phrase
    116      * @param reason    the reason phrase for this status code
    117      */
    118     private static void setReason(int status, String reason) {
    119         final int category = status / 100;
    120         final int subcode  = status - 100*category;
    121         REASON_PHRASES[category][subcode] = reason;
    122     }
    123 
    124 
    125     // ----------------------------------------------------- Static Initializer
    126 
    127     /** Set up status code to "reason phrase" map. */
    128     static {
    129         // HTTP 1.0 Server status codes -- see RFC 1945
    130         setReason(HttpStatus.SC_OK,
    131                   "OK");
    132         setReason(HttpStatus.SC_CREATED,
    133                   "Created");
    134         setReason(HttpStatus.SC_ACCEPTED,
    135                   "Accepted");
    136         setReason(HttpStatus.SC_NO_CONTENT,
    137                   "No Content");
    138         setReason(HttpStatus.SC_MOVED_PERMANENTLY,
    139                   "Moved Permanently");
    140         setReason(HttpStatus.SC_MOVED_TEMPORARILY,
    141                   "Moved Temporarily");
    142         setReason(HttpStatus.SC_NOT_MODIFIED,
    143                   "Not Modified");
    144         setReason(HttpStatus.SC_BAD_REQUEST,
    145                   "Bad Request");
    146         setReason(HttpStatus.SC_UNAUTHORIZED,
    147                   "Unauthorized");
    148         setReason(HttpStatus.SC_FORBIDDEN,
    149                   "Forbidden");
    150         setReason(HttpStatus.SC_NOT_FOUND,
    151                   "Not Found");
    152         setReason(HttpStatus.SC_INTERNAL_SERVER_ERROR,
    153                   "Internal Server Error");
    154         setReason(HttpStatus.SC_NOT_IMPLEMENTED,
    155                   "Not Implemented");
    156         setReason(HttpStatus.SC_BAD_GATEWAY,
    157                   "Bad Gateway");
    158         setReason(HttpStatus.SC_SERVICE_UNAVAILABLE,
    159                   "Service Unavailable");
    160 
    161         // HTTP 1.1 Server status codes -- see RFC 2048
    162         setReason(HttpStatus.SC_CONTINUE,
    163                   "Continue");
    164         setReason(HttpStatus.SC_TEMPORARY_REDIRECT,
    165                   "Temporary Redirect");
    166         setReason(HttpStatus.SC_METHOD_NOT_ALLOWED,
    167                   "Method Not Allowed");
    168         setReason(HttpStatus.SC_CONFLICT,
    169                   "Conflict");
    170         setReason(HttpStatus.SC_PRECONDITION_FAILED,
    171                   "Precondition Failed");
    172         setReason(HttpStatus.SC_REQUEST_TOO_LONG,
    173                   "Request Too Long");
    174         setReason(HttpStatus.SC_REQUEST_URI_TOO_LONG,
    175                   "Request-URI Too Long");
    176         setReason(HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE,
    177                   "Unsupported Media Type");
    178         setReason(HttpStatus.SC_MULTIPLE_CHOICES,
    179                   "Multiple Choices");
    180         setReason(HttpStatus.SC_SEE_OTHER,
    181                   "See Other");
    182         setReason(HttpStatus.SC_USE_PROXY,
    183                   "Use Proxy");
    184         setReason(HttpStatus.SC_PAYMENT_REQUIRED,
    185                   "Payment Required");
    186         setReason(HttpStatus.SC_NOT_ACCEPTABLE,
    187                   "Not Acceptable");
    188         setReason(HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED,
    189                   "Proxy Authentication Required");
    190         setReason(HttpStatus.SC_REQUEST_TIMEOUT,
    191                   "Request Timeout");
    192 
    193         setReason(HttpStatus.SC_SWITCHING_PROTOCOLS,
    194                   "Switching Protocols");
    195         setReason(HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION,
    196                   "Non Authoritative Information");
    197         setReason(HttpStatus.SC_RESET_CONTENT,
    198                   "Reset Content");
    199         setReason(HttpStatus.SC_PARTIAL_CONTENT,
    200                   "Partial Content");
    201         setReason(HttpStatus.SC_GATEWAY_TIMEOUT,
    202                   "Gateway Timeout");
    203         setReason(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED,
    204                   "Http Version Not Supported");
    205         setReason(HttpStatus.SC_GONE,
    206                   "Gone");
    207         setReason(HttpStatus.SC_LENGTH_REQUIRED,
    208                   "Length Required");
    209         setReason(HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE,
    210                   "Requested Range Not Satisfiable");
    211         setReason(HttpStatus.SC_EXPECTATION_FAILED,
    212                   "Expectation Failed");
    213 
    214         // WebDAV Server-specific status codes
    215         setReason(HttpStatus.SC_PROCESSING,
    216                   "Processing");
    217         setReason(HttpStatus.SC_MULTI_STATUS,
    218                   "Multi-Status");
    219         setReason(HttpStatus.SC_UNPROCESSABLE_ENTITY,
    220                   "Unprocessable Entity");
    221         setReason(HttpStatus.SC_INSUFFICIENT_SPACE_ON_RESOURCE,
    222                   "Insufficient Space On Resource");
    223         setReason(HttpStatus.SC_METHOD_FAILURE,
    224                   "Method Failure");
    225         setReason(HttpStatus.SC_LOCKED,
    226                   "Locked");
    227         setReason(HttpStatus.SC_INSUFFICIENT_STORAGE,
    228                   "Insufficient Storage");
    229         setReason(HttpStatus.SC_FAILED_DEPENDENCY,
    230                   "Failed Dependency");
    231     }
    232 
    233 
    234 }
    235