• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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@GargoyleSoftware.com">Mike Bowler</a>
47  * @author <a href="mailto:jsdever@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      */
EnglishReasonPhraseCatalog()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      */
getReason(int status, Locale loc)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      */
setReason(int status, String reason)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
setReason(HttpStatus.SC_OK, "OK")130         setReason(HttpStatus.SC_OK,
131                   "OK");
setReason(HttpStatus.SC_CREATED, "Created")132         setReason(HttpStatus.SC_CREATED,
133                   "Created");
setReason(HttpStatus.SC_ACCEPTED, "Accepted")134         setReason(HttpStatus.SC_ACCEPTED,
135                   "Accepted");
setReason(HttpStatus.SC_NO_CONTENT, "No Content")136         setReason(HttpStatus.SC_NO_CONTENT,
137                   "No Content");
setReason(HttpStatus.SC_MOVED_PERMANENTLY, "Moved Permanently")138         setReason(HttpStatus.SC_MOVED_PERMANENTLY,
139                   "Moved Permanently");
setReason(HttpStatus.SC_MOVED_TEMPORARILY, "Moved Temporarily")140         setReason(HttpStatus.SC_MOVED_TEMPORARILY,
141                   "Moved Temporarily");
setReason(HttpStatus.SC_NOT_MODIFIED, "Not Modified")142         setReason(HttpStatus.SC_NOT_MODIFIED,
143                   "Not Modified");
setReason(HttpStatus.SC_BAD_REQUEST, "Bad Request")144         setReason(HttpStatus.SC_BAD_REQUEST,
145                   "Bad Request");
setReason(HttpStatus.SC_UNAUTHORIZED, "Unauthorized")146         setReason(HttpStatus.SC_UNAUTHORIZED,
147                   "Unauthorized");
setReason(HttpStatus.SC_FORBIDDEN, "Forbidden")148         setReason(HttpStatus.SC_FORBIDDEN,
149                   "Forbidden");
setReason(HttpStatus.SC_NOT_FOUND, "Not Found")150         setReason(HttpStatus.SC_NOT_FOUND,
151                   "Not Found");
setReason(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Internal Server Error")152         setReason(HttpStatus.SC_INTERNAL_SERVER_ERROR,
153                   "Internal Server Error");
setReason(HttpStatus.SC_NOT_IMPLEMENTED, "Not Implemented")154         setReason(HttpStatus.SC_NOT_IMPLEMENTED,
155                   "Not Implemented");
setReason(HttpStatus.SC_BAD_GATEWAY, "Bad Gateway")156         setReason(HttpStatus.SC_BAD_GATEWAY,
157                   "Bad Gateway");
setReason(HttpStatus.SC_SERVICE_UNAVAILABLE, "Service Unavailable")158         setReason(HttpStatus.SC_SERVICE_UNAVAILABLE,
159                   "Service Unavailable");
160 
161         // HTTP 1.1 Server status codes -- see RFC 2048
setReason(HttpStatus.SC_CONTINUE, "Continue")162         setReason(HttpStatus.SC_CONTINUE,
163                   "Continue");
setReason(HttpStatus.SC_TEMPORARY_REDIRECT, "Temporary Redirect")164         setReason(HttpStatus.SC_TEMPORARY_REDIRECT,
165                   "Temporary Redirect");
setReason(HttpStatus.SC_METHOD_NOT_ALLOWED, "Method Not Allowed")166         setReason(HttpStatus.SC_METHOD_NOT_ALLOWED,
167                   "Method Not Allowed");
setReason(HttpStatus.SC_CONFLICT, "Conflict")168         setReason(HttpStatus.SC_CONFLICT,
169                   "Conflict");
setReason(HttpStatus.SC_PRECONDITION_FAILED, "Precondition Failed")170         setReason(HttpStatus.SC_PRECONDITION_FAILED,
171                   "Precondition Failed");
setReason(HttpStatus.SC_REQUEST_TOO_LONG, "Request Too Long")172         setReason(HttpStatus.SC_REQUEST_TOO_LONG,
173                   "Request Too Long");
setReason(HttpStatus.SC_REQUEST_URI_TOO_LONG, "Request-URI Too Long")174         setReason(HttpStatus.SC_REQUEST_URI_TOO_LONG,
175                   "Request-URI Too Long");
setReason(HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE, "Unsupported Media Type")176         setReason(HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE,
177                   "Unsupported Media Type");
setReason(HttpStatus.SC_MULTIPLE_CHOICES, "Multiple Choices")178         setReason(HttpStatus.SC_MULTIPLE_CHOICES,
179                   "Multiple Choices");
setReason(HttpStatus.SC_SEE_OTHER, "See Other")180         setReason(HttpStatus.SC_SEE_OTHER,
181                   "See Other");
setReason(HttpStatus.SC_USE_PROXY, "Use Proxy")182         setReason(HttpStatus.SC_USE_PROXY,
183                   "Use Proxy");
setReason(HttpStatus.SC_PAYMENT_REQUIRED, "Payment Required")184         setReason(HttpStatus.SC_PAYMENT_REQUIRED,
185                   "Payment Required");
setReason(HttpStatus.SC_NOT_ACCEPTABLE, "Not Acceptable")186         setReason(HttpStatus.SC_NOT_ACCEPTABLE,
187                   "Not Acceptable");
setReason(HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED, "Proxy Authentication Required")188         setReason(HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED,
189                   "Proxy Authentication Required");
setReason(HttpStatus.SC_REQUEST_TIMEOUT, "Request Timeout")190         setReason(HttpStatus.SC_REQUEST_TIMEOUT,
191                   "Request Timeout");
192 
setReason(HttpStatus.SC_SWITCHING_PROTOCOLS, "Switching Protocols")193         setReason(HttpStatus.SC_SWITCHING_PROTOCOLS,
194                   "Switching Protocols");
setReason(HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION, "Non Authoritative Information")195         setReason(HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION,
196                   "Non Authoritative Information");
setReason(HttpStatus.SC_RESET_CONTENT, "Reset Content")197         setReason(HttpStatus.SC_RESET_CONTENT,
198                   "Reset Content");
setReason(HttpStatus.SC_PARTIAL_CONTENT, "Partial Content")199         setReason(HttpStatus.SC_PARTIAL_CONTENT,
200                   "Partial Content");
setReason(HttpStatus.SC_GATEWAY_TIMEOUT, "Gateway Timeout")201         setReason(HttpStatus.SC_GATEWAY_TIMEOUT,
202                   "Gateway Timeout");
setReason(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED, "Http Version Not Supported")203         setReason(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED,
204                   "Http Version Not Supported");
setReason(HttpStatus.SC_GONE, "Gone")205         setReason(HttpStatus.SC_GONE,
206                   "Gone");
setReason(HttpStatus.SC_LENGTH_REQUIRED, "Length Required")207         setReason(HttpStatus.SC_LENGTH_REQUIRED,
208                   "Length Required");
setReason(HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE, "Requested Range Not Satisfiable")209         setReason(HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE,
210                   "Requested Range Not Satisfiable");
setReason(HttpStatus.SC_EXPECTATION_FAILED, "Expectation Failed")211         setReason(HttpStatus.SC_EXPECTATION_FAILED,
212                   "Expectation Failed");
213 
214         // WebDAV Server-specific status codes
setReason(HttpStatus.SC_PROCESSING, "Processing")215         setReason(HttpStatus.SC_PROCESSING,
216                   "Processing");
setReason(HttpStatus.SC_MULTI_STATUS, "Multi-Status")217         setReason(HttpStatus.SC_MULTI_STATUS,
218                   "Multi-Status");
setReason(HttpStatus.SC_UNPROCESSABLE_ENTITY, "Unprocessable Entity")219         setReason(HttpStatus.SC_UNPROCESSABLE_ENTITY,
220                   "Unprocessable Entity");
setReason(HttpStatus.SC_INSUFFICIENT_SPACE_ON_RESOURCE, "Insufficient Space On Resource")221         setReason(HttpStatus.SC_INSUFFICIENT_SPACE_ON_RESOURCE,
222                   "Insufficient Space On Resource");
setReason(HttpStatus.SC_METHOD_FAILURE, "Method Failure")223         setReason(HttpStatus.SC_METHOD_FAILURE,
224                   "Method Failure");
setReason(HttpStatus.SC_LOCKED, "Locked")225         setReason(HttpStatus.SC_LOCKED,
226                   "Locked");
setReason(HttpStatus.SC_INSUFFICIENT_STORAGE, "Insufficient Storage")227         setReason(HttpStatus.SC_INSUFFICIENT_STORAGE,
228                   "Insufficient Storage");
setReason(HttpStatus.SC_FAILED_DEPENDENCY, "Failed Dependency")229         setReason(HttpStatus.SC_FAILED_DEPENDENCY,
230                   "Failed Dependency");
231     }
232 
233 
234 }
235