View Javadoc

1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/java/org/apache/commons/httpclient/HttpStatus.java $
3    * $Revision: 1425331 $
4    * $Date: 2012-12-22 19:29:41 +0100 (Sat, 22 Dec 2012) $
5    *
6    * ====================================================================
7    *
8    *  Licensed to the Apache Software Foundation (ASF) under one or more
9    *  contributor license agreements.  See the NOTICE file distributed with
10   *  this work for additional information regarding copyright ownership.
11   *  The ASF licenses this file to You under the Apache License, Version 2.0
12   *  (the "License"); you may not use this file except in compliance with
13   *  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, software
18   *  distributed under the License is distributed on an "AS IS" BASIS,
19   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   *  See the License for the specific language governing permissions and
21   *  limitations under the License.
22   * ====================================================================
23   *
24   * This software consists of voluntary contributions made by many
25   * individuals on behalf of the Apache Software Foundation.  For more
26   * information on the Apache Software Foundation, please see
27   * <http://www.apache.org/>.
28   *
29   */
30  
31  package org.apache.commons.httpclient;
32  
33  /***
34   * Constants enumerating the HTTP status codes.
35   * All status codes defined in RFC1945 (HTTP/1.0, RFC2616 (HTTP/1.1), and
36   * RFC2518 (WebDAV) are supported.
37   * 
38   * @see StatusLine
39   * @author Unascribed
40   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
41   * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
42   * 
43   * TODO: Internationalization of reason phrases 
44   * 
45   * @version $Id: HttpStatus.java 608014 2008-01-02 05:48:53Z rolandw $
46   */
47  public class HttpStatus {
48  
49  
50      // -------------------------------------------------------- Class Variables
51  
52      /*** Reason phrases lookup table. */
53      private static final String[][] REASON_PHRASES = new String[][]{
54          new String[0],
55          new String[3],
56          new String[8],
57          new String[8],
58          new String[25],
59          new String[8]
60      };
61  
62  
63      // --------------------------------------------------------- Public Methods
64  
65      /***
66       * Get the reason phrase for a particular status code.
67       * 
68       * This method always returns the English text as specified in the
69       * relevent RFCs and is not internationalized.
70       * 
71       * @param statusCode the numeric status code
72       * @return the reason phrase associated with the given status code
73       * or null if the status code is not recognized.
74       * 
75       * TODO: getStatusText should be called getReasonPhrase to match RFC
76       */
77      public static String getStatusText(int statusCode) {
78  
79          if (statusCode < 0) {
80              throw new IllegalArgumentException("status code may not be negative");
81          }
82          int classIndex = statusCode / 100;
83          int codeIndex = statusCode - classIndex * 100;
84          if (classIndex < 1 || classIndex > (REASON_PHRASES.length - 1) 
85              || codeIndex < 0 || codeIndex > (REASON_PHRASES[classIndex].length - 1)) {
86              return null;
87          }
88          return REASON_PHRASES[classIndex][codeIndex];
89      }
90  
91  
92      // -------------------------------------------------------- Private Methods
93  
94      /***
95       * Store the given reason phrase, by status code.
96       * @param statusCode The status code to lookup
97       * @param reasonPhrase The reason phrase for this status code
98       */
99      private static void addStatusCodeMap(int statusCode, String reasonPhrase) {
100         int classIndex = statusCode / 100;
101         REASON_PHRASES[classIndex][statusCode - classIndex * 100] = reasonPhrase;
102     }
103 
104 
105     // -------------------------------------------------------------- Constants
106 
107     // --- 1xx Informational ---
108 
109     /*** <tt>100 Continue</tt> (HTTP/1.1 - RFC 2616) */
110     public static final int SC_CONTINUE = 100;
111     /*** <tt>101 Switching Protocols</tt> (HTTP/1.1 - RFC 2616)*/
112     public static final int SC_SWITCHING_PROTOCOLS = 101;
113     /*** <tt>102 Processing</tt> (WebDAV - RFC 2518) */
114     public static final int SC_PROCESSING = 102;
115 
116     // --- 2xx Success ---
117 
118     /*** <tt>200 OK</tt> (HTTP/1.0 - RFC 1945) */
119     public static final int SC_OK = 200;
120     /*** <tt>201 Created</tt> (HTTP/1.0 - RFC 1945) */
121     public static final int SC_CREATED = 201;
122     /*** <tt>202 Accepted</tt> (HTTP/1.0 - RFC 1945) */
123     public static final int SC_ACCEPTED = 202;
124     /*** <tt>203 Non Authoritative Information</tt> (HTTP/1.1 - RFC 2616) */
125     public static final int SC_NON_AUTHORITATIVE_INFORMATION = 203;
126     /*** <tt>204 No Content</tt> (HTTP/1.0 - RFC 1945) */
127     public static final int SC_NO_CONTENT = 204;
128     /*** <tt>205 Reset Content</tt> (HTTP/1.1 - RFC 2616) */
129     public static final int SC_RESET_CONTENT = 205;
130     /*** <tt>206 Partial Content</tt> (HTTP/1.1 - RFC 2616) */
131     public static final int SC_PARTIAL_CONTENT = 206;
132     /*** 
133      * <tt>207 Multi-Status</tt> (WebDAV - RFC 2518) or <tt>207 Partial Update
134      * OK</tt> (HTTP/1.1 - draft-ietf-http-v11-spec-rev-01?)
135      */
136     public static final int SC_MULTI_STATUS = 207;
137 
138     // --- 3xx Redirection ---
139 
140     /*** <tt>300 Mutliple Choices</tt> (HTTP/1.1 - RFC 2616) */
141     public static final int SC_MULTIPLE_CHOICES = 300;
142     /*** <tt>301 Moved Permanently</tt> (HTTP/1.0 - RFC 1945) */
143     public static final int SC_MOVED_PERMANENTLY = 301;
144     /*** <tt>302 Moved Temporarily</tt> (Sometimes <tt>Found</tt>) (HTTP/1.0 - RFC 1945) */
145     public static final int SC_MOVED_TEMPORARILY = 302;
146     /*** <tt>303 See Other</tt> (HTTP/1.1 - RFC 2616) */
147     public static final int SC_SEE_OTHER = 303;
148     /*** <tt>304 Not Modified</tt> (HTTP/1.0 - RFC 1945) */
149     public static final int SC_NOT_MODIFIED = 304;
150     /*** <tt>305 Use Proxy</tt> (HTTP/1.1 - RFC 2616) */
151     public static final int SC_USE_PROXY = 305;
152     /*** <tt>307 Temporary Redirect</tt> (HTTP/1.1 - RFC 2616) */
153     public static final int SC_TEMPORARY_REDIRECT = 307;
154 
155     // --- 4xx Client Error ---
156 
157     /*** <tt>400 Bad Request</tt> (HTTP/1.1 - RFC 2616) */
158     public static final int SC_BAD_REQUEST = 400;
159     /*** <tt>401 Unauthorized</tt> (HTTP/1.0 - RFC 1945) */
160     public static final int SC_UNAUTHORIZED = 401;
161     /*** <tt>402 Payment Required</tt> (HTTP/1.1 - RFC 2616) */
162     public static final int SC_PAYMENT_REQUIRED = 402;
163     /*** <tt>403 Forbidden</tt> (HTTP/1.0 - RFC 1945) */
164     public static final int SC_FORBIDDEN = 403;
165     /*** <tt>404 Not Found</tt> (HTTP/1.0 - RFC 1945) */
166     public static final int SC_NOT_FOUND = 404;
167     /*** <tt>405 Method Not Allowed</tt> (HTTP/1.1 - RFC 2616) */
168     public static final int SC_METHOD_NOT_ALLOWED = 405;
169     /*** <tt>406 Not Acceptable</tt> (HTTP/1.1 - RFC 2616) */
170     public static final int SC_NOT_ACCEPTABLE = 406;
171     /*** <tt>407 Proxy Authentication Required</tt> (HTTP/1.1 - RFC 2616)*/
172     public static final int SC_PROXY_AUTHENTICATION_REQUIRED = 407;
173     /*** <tt>408 Request Timeout</tt> (HTTP/1.1 - RFC 2616) */
174     public static final int SC_REQUEST_TIMEOUT = 408;
175     /*** <tt>409 Conflict</tt> (HTTP/1.1 - RFC 2616) */
176     public static final int SC_CONFLICT = 409;
177     /*** <tt>410 Gone</tt> (HTTP/1.1 - RFC 2616) */
178     public static final int SC_GONE = 410;
179     /*** <tt>411 Length Required</tt> (HTTP/1.1 - RFC 2616) */
180     public static final int SC_LENGTH_REQUIRED = 411;
181     /*** <tt>412 Precondition Failed</tt> (HTTP/1.1 - RFC 2616) */
182     public static final int SC_PRECONDITION_FAILED = 412;
183     /*** <tt>413 Request Entity Too Large</tt> (HTTP/1.1 - RFC 2616) */
184     public static final int SC_REQUEST_TOO_LONG = 413;
185     /*** <tt>414 Request-URI Too Long</tt> (HTTP/1.1 - RFC 2616) */
186     public static final int SC_REQUEST_URI_TOO_LONG = 414;
187     /*** <tt>415 Unsupported Media Type</tt> (HTTP/1.1 - RFC 2616) */
188     public static final int SC_UNSUPPORTED_MEDIA_TYPE = 415;
189     /*** <tt>416 Requested Range Not Satisfiable</tt> (HTTP/1.1 - RFC 2616) */
190     public static final int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
191     /*** <tt>417 Expectation Failed</tt> (HTTP/1.1 - RFC 2616) */
192     public static final int SC_EXPECTATION_FAILED = 417;
193 
194     /***
195      * Static constant for a 418 error.
196      * <tt>418 Unprocessable Entity</tt> (WebDAV drafts?)
197      * or <tt>418 Reauthentication Required</tt> (HTTP/1.1 drafts?)
198      */
199     // not used
200     // public static final int SC_UNPROCESSABLE_ENTITY = 418;
201 
202     /***
203      * Static constant for a 419 error.
204      * <tt>419 Insufficient Space on Resource</tt>
205      * (WebDAV - draft-ietf-webdav-protocol-05?)
206      * or <tt>419 Proxy Reauthentication Required</tt>
207      * (HTTP/1.1 drafts?)
208      */
209     public static final int SC_INSUFFICIENT_SPACE_ON_RESOURCE = 419;
210     /***
211      * Static constant for a 420 error.
212      * <tt>420 Method Failure</tt>
213      * (WebDAV - draft-ietf-webdav-protocol-05?)
214      */
215     public static final int SC_METHOD_FAILURE = 420;
216     /*** <tt>422 Unprocessable Entity</tt> (WebDAV - RFC 2518) */
217     public static final int SC_UNPROCESSABLE_ENTITY = 422;
218     /*** <tt>423 Locked</tt> (WebDAV - RFC 2518) */
219     public static final int SC_LOCKED = 423;
220     /*** <tt>424 Failed Dependency</tt> (WebDAV - RFC 2518) */
221     public static final int SC_FAILED_DEPENDENCY = 424;
222 
223     // --- 5xx Server Error ---
224 
225     /*** <tt>500 Server Error</tt> (HTTP/1.0 - RFC 1945) */
226     public static final int SC_INTERNAL_SERVER_ERROR = 500;
227     /*** <tt>501 Not Implemented</tt> (HTTP/1.0 - RFC 1945) */
228     public static final int SC_NOT_IMPLEMENTED = 501;
229     /*** <tt>502 Bad Gateway</tt> (HTTP/1.0 - RFC 1945) */
230     public static final int SC_BAD_GATEWAY = 502;
231     /*** <tt>503 Service Unavailable</tt> (HTTP/1.0 - RFC 1945) */
232     public static final int SC_SERVICE_UNAVAILABLE = 503;
233     /*** <tt>504 Gateway Timeout</tt> (HTTP/1.1 - RFC 2616) */
234     public static final int SC_GATEWAY_TIMEOUT = 504;
235     /*** <tt>505 HTTP Version Not Supported</tt> (HTTP/1.1 - RFC 2616) */
236     public static final int SC_HTTP_VERSION_NOT_SUPPORTED = 505;
237 
238     /*** <tt>507 Insufficient Storage</tt> (WebDAV - RFC 2518) */
239     public static final int SC_INSUFFICIENT_STORAGE = 507;
240 
241 
242     // ----------------------------------------------------- Static Initializer
243 
244     /*** Set up status code to "reason phrase" map. */
245     static {
246         // HTTP 1.0 Server status codes -- see RFC 1945
247         addStatusCodeMap(SC_OK, "OK");
248         addStatusCodeMap(SC_CREATED, "Created");
249         addStatusCodeMap(SC_ACCEPTED, "Accepted");
250         addStatusCodeMap(SC_NO_CONTENT, "No Content");
251         addStatusCodeMap(SC_MOVED_PERMANENTLY, "Moved Permanently");
252         addStatusCodeMap(SC_MOVED_TEMPORARILY, "Moved Temporarily");
253         addStatusCodeMap(SC_NOT_MODIFIED, "Not Modified");
254         addStatusCodeMap(SC_BAD_REQUEST, "Bad Request");
255         addStatusCodeMap(SC_UNAUTHORIZED, "Unauthorized");
256         addStatusCodeMap(SC_FORBIDDEN, "Forbidden");
257         addStatusCodeMap(SC_NOT_FOUND, "Not Found");
258         addStatusCodeMap(SC_INTERNAL_SERVER_ERROR, "Internal Server Error");
259         addStatusCodeMap(SC_NOT_IMPLEMENTED, "Not Implemented");
260         addStatusCodeMap(SC_BAD_GATEWAY, "Bad Gateway");
261         addStatusCodeMap(SC_SERVICE_UNAVAILABLE, "Service Unavailable");
262 
263         // HTTP 1.1 Server status codes -- see RFC 2048
264         addStatusCodeMap(SC_CONTINUE, "Continue");
265         addStatusCodeMap(SC_TEMPORARY_REDIRECT, "Temporary Redirect");
266         addStatusCodeMap(SC_METHOD_NOT_ALLOWED, "Method Not Allowed");
267         addStatusCodeMap(SC_CONFLICT, "Conflict");
268         addStatusCodeMap(SC_PRECONDITION_FAILED, "Precondition Failed");
269         addStatusCodeMap(SC_REQUEST_TOO_LONG, "Request Too Long");
270         addStatusCodeMap(SC_REQUEST_URI_TOO_LONG, "Request-URI Too Long");
271         addStatusCodeMap(SC_UNSUPPORTED_MEDIA_TYPE, "Unsupported Media Type");
272         addStatusCodeMap(SC_MULTIPLE_CHOICES, "Multiple Choices");
273         addStatusCodeMap(SC_SEE_OTHER, "See Other");
274         addStatusCodeMap(SC_USE_PROXY, "Use Proxy");
275         addStatusCodeMap(SC_PAYMENT_REQUIRED, "Payment Required");
276         addStatusCodeMap(SC_NOT_ACCEPTABLE, "Not Acceptable");
277         addStatusCodeMap(SC_PROXY_AUTHENTICATION_REQUIRED, 
278             "Proxy Authentication Required");
279         addStatusCodeMap(SC_REQUEST_TIMEOUT, 
280             "Request Timeout");
281 
282         addStatusCodeMap(SC_SWITCHING_PROTOCOLS, "Switching Protocols");
283         addStatusCodeMap(SC_NON_AUTHORITATIVE_INFORMATION,
284                          "Non Authoritative Information");
285         addStatusCodeMap(SC_RESET_CONTENT, "Reset Content");
286         addStatusCodeMap(SC_PARTIAL_CONTENT, "Partial Content");
287         addStatusCodeMap(SC_GATEWAY_TIMEOUT, "Gateway Timeout");
288         addStatusCodeMap(SC_HTTP_VERSION_NOT_SUPPORTED,
289                          "Http Version Not Supported");
290         addStatusCodeMap(SC_GONE,
291                          "Gone");
292         addStatusCodeMap(SC_LENGTH_REQUIRED,
293                          "Length Required");
294         addStatusCodeMap(SC_REQUESTED_RANGE_NOT_SATISFIABLE,
295                          "Requested Range Not Satisfiable");
296         addStatusCodeMap(SC_EXPECTATION_FAILED,
297                          "Expectation Failed");
298 
299         // WebDAV Server-specific status codes
300         addStatusCodeMap(SC_PROCESSING, "Processing");
301         addStatusCodeMap(SC_MULTI_STATUS, "Multi-Status");
302         addStatusCodeMap(SC_UNPROCESSABLE_ENTITY, "Unprocessable Entity");
303         addStatusCodeMap(SC_INSUFFICIENT_SPACE_ON_RESOURCE,
304                          "Insufficient Space On Resource");
305         addStatusCodeMap(SC_METHOD_FAILURE, "Method Failure");
306         addStatusCodeMap(SC_LOCKED, "Locked");
307         addStatusCodeMap(SC_INSUFFICIENT_STORAGE , "Insufficient Storage");
308         addStatusCodeMap(SC_FAILED_DEPENDENCY, "Failed Dependency");
309     }
310 
311 
312 }