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  * @deprecated Please use {@link java.net.URL#openConnection} instead.
     52  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
     53  *     for further details.
     54  */
     55 @Deprecated
     56 public class EnglishReasonPhraseCatalog
     57     implements ReasonPhraseCatalog {
     58 
     59     // static array with english reason phrases defined below
     60 
     61     /**
     62      * The default instance of this catalog.
     63      * This catalog is thread safe, so there typically
     64      * is no need to create other instances.
     65      */
     66     public final static EnglishReasonPhraseCatalog INSTANCE =
     67         new EnglishReasonPhraseCatalog();
     68 
     69 
     70     /**
     71      * Restricted default constructor, for derived classes.
     72      * If you need an instance of this class, use {@link #INSTANCE INSTANCE}.
     73      */
     74     protected EnglishReasonPhraseCatalog() {
     75         // no body
     76     }
     77 
     78 
     79     /**
     80      * Obtains the reason phrase for a status code.
     81      *
     82      * @param status    the status code, in the range 100-599
     83      * @param loc       ignored
     84      *
     85      * @return  the reason phrase, or <code>null</code>
     86      */
     87     public String getReason(int status, Locale loc) {
     88         if ((status < 100) || (status >= 600)) {
     89             throw new IllegalArgumentException
     90                 ("Unknown category for status code " + status + ".");
     91         }
     92 
     93         final int category = status / 100;
     94         final int subcode  = status - 100*category;
     95 
     96         String reason = null;
     97         if (REASON_PHRASES[category].length > subcode)
     98             reason = REASON_PHRASES[category][subcode];
     99 
    100         return reason;
    101     }
    102 
    103 
    104     /** Reason phrases lookup table. */
    105     private static final String[][] REASON_PHRASES = new String[][]{
    106         null,
    107         new String[3],  // 1xx
    108         new String[8],  // 2xx
    109         new String[8],  // 3xx
    110         new String[25], // 4xx
    111         new String[8]   // 5xx
    112     };
    113 
    114 
    115 
    116     /**
    117      * Stores the given reason phrase, by status code.
    118      * Helper method to initialize the static lookup table.
    119      *
    120      * @param status    the status code for which to define the phrase
    121      * @param reason    the reason phrase for this status code
    122      */
    123     private static void setReason(int status, String reason) {
    124         final int category = status / 100;
    125         final int subcode  = status - 100*category;
    126         REASON_PHRASES[category][subcode] = reason;
    127     }
    128 
    129 
    130     // ----------------------------------------------------- Static Initializer
    131 
    132     /** Set up status code to "reason phrase" map. */
    133     static {
    134         // HTTP 1.0 Server status codes -- see RFC 1945
    135         setReason(HttpStatus.SC_OK,
    136                   "OK");
    137         setReason(HttpStatus.SC_CREATED,
    138                   "Created");
    139         setReason(HttpStatus.SC_ACCEPTED,
    140                   "Accepted");
    141         setReason(HttpStatus.SC_NO_CONTENT,
    142                   "No Content");
    143         setReason(HttpStatus.SC_MOVED_PERMANENTLY,
    144                   "Moved Permanently");
    145         setReason(HttpStatus.SC_MOVED_TEMPORARILY,
    146                   "Moved Temporarily");
    147         setReason(HttpStatus.SC_NOT_MODIFIED,
    148                   "Not Modified");
    149         setReason(HttpStatus.SC_BAD_REQUEST,
    150                   "Bad Request");
    151         setReason(HttpStatus.SC_UNAUTHORIZED,
    152                   "Unauthorized");
    153         setReason(HttpStatus.SC_FORBIDDEN,
    154                   "Forbidden");
    155         setReason(HttpStatus.SC_NOT_FOUND,
    156                   "Not Found");
    157         setReason(HttpStatus.SC_INTERNAL_SERVER_ERROR,
    158                   "Internal Server Error");
    159         setReason(HttpStatus.SC_NOT_IMPLEMENTED,
    160                   "Not Implemented");
    161         setReason(HttpStatus.SC_BAD_GATEWAY,
    162                   "Bad Gateway");
    163         setReason(HttpStatus.SC_SERVICE_UNAVAILABLE,
    164                   "Service Unavailable");
    165 
    166         // HTTP 1.1 Server status codes -- see RFC 2048
    167         setReason(HttpStatus.SC_CONTINUE,
    168                   "Continue");
    169         setReason(HttpStatus.SC_TEMPORARY_REDIRECT,
    170                   "Temporary Redirect");
    171         setReason(HttpStatus.SC_METHOD_NOT_ALLOWED,
    172                   "Method Not Allowed");
    173         setReason(HttpStatus.SC_CONFLICT,
    174                   "Conflict");
    175         setReason(HttpStatus.SC_PRECONDITION_FAILED,
    176                   "Precondition Failed");
    177         setReason(HttpStatus.SC_REQUEST_TOO_LONG,
    178                   "Request Too Long");
    179         setReason(HttpStatus.SC_REQUEST_URI_TOO_LONG,
    180                   "Request-URI Too Long");
    181         setReason(HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE,
    182                   "Unsupported Media Type");
    183         setReason(HttpStatus.SC_MULTIPLE_CHOICES,
    184                   "Multiple Choices");
    185         setReason(HttpStatus.SC_SEE_OTHER,
    186                   "See Other");
    187         setReason(HttpStatus.SC_USE_PROXY,
    188                   "Use Proxy");
    189         setReason(HttpStatus.SC_PAYMENT_REQUIRED,
    190                   "Payment Required");
    191         setReason(HttpStatus.SC_NOT_ACCEPTABLE,
    192                   "Not Acceptable");
    193         setReason(HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED,
    194                   "Proxy Authentication Required");
    195         setReason(HttpStatus.SC_REQUEST_TIMEOUT,
    196                   "Request Timeout");
    197 
    198         setReason(HttpStatus.SC_SWITCHING_PROTOCOLS,
    199                   "Switching Protocols");
    200         setReason(HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION,
    201                   "Non Authoritative Information");
    202         setReason(HttpStatus.SC_RESET_CONTENT,
    203                   "Reset Content");
    204         setReason(HttpStatus.SC_PARTIAL_CONTENT,
    205                   "Partial Content");
    206         setReason(HttpStatus.SC_GATEWAY_TIMEOUT,
    207                   "Gateway Timeout");
    208         setReason(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED,
    209                   "Http Version Not Supported");
    210         setReason(HttpStatus.SC_GONE,
    211                   "Gone");
    212         setReason(HttpStatus.SC_LENGTH_REQUIRED,
    213                   "Length Required");
    214         setReason(HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE,
    215                   "Requested Range Not Satisfiable");
    216         setReason(HttpStatus.SC_EXPECTATION_FAILED,
    217                   "Expectation Failed");
    218 
    219         // WebDAV Server-specific status codes
    220         setReason(HttpStatus.SC_PROCESSING,
    221                   "Processing");
    222         setReason(HttpStatus.SC_MULTI_STATUS,
    223                   "Multi-Status");
    224         setReason(HttpStatus.SC_UNPROCESSABLE_ENTITY,
    225                   "Unprocessable Entity");
    226         setReason(HttpStatus.SC_INSUFFICIENT_SPACE_ON_RESOURCE,
    227                   "Insufficient Space On Resource");
    228         setReason(HttpStatus.SC_METHOD_FAILURE,
    229                   "Method Failure");
    230         setReason(HttpStatus.SC_LOCKED,
    231                   "Locked");
    232         setReason(HttpStatus.SC_INSUFFICIENT_STORAGE,
    233                   "Insufficient Storage");
    234         setReason(HttpStatus.SC_FAILED_DEPENDENCY,
    235                   "Failed Dependency");
    236     }
    237 
    238 
    239 }
    240