001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/HttpStatus.java,v 1.18 2004/05/02 11:21:13 olegk Exp $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient;
032:
033: /**
034: * Constants enumerating the HTTP status codes.
035: * All status codes defined in RFC1945 (HTTP/1.0, RFC2616 (HTTP/1.1), and
036: * RFC2518 (WebDAV) are supported.
037: *
038: * @see StatusLine
039: * @author Unascribed
040: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
041: * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
042: *
043: * TODO: Internationalization of reason phrases
044: *
045: * @version $Id: HttpStatus.java 480424 2006-11-29 05:56:49Z bayard $
046: */
047: public class HttpStatus {
048:
049: // -------------------------------------------------------- Class Variables
050:
051: /** Reason phrases lookup table. */
052: private static final String[][] REASON_PHRASES = new String[][] {
053: new String[0], new String[3], new String[8], new String[8],
054: new String[25], new String[8] };
055:
056: // --------------------------------------------------------- Public Methods
057:
058: /**
059: * Get the reason phrase for a particular status code.
060: *
061: * This method always returns the English text as specified in the
062: * relevent RFCs and is not internationalized.
063: *
064: * @param statusCode the numeric status code
065: * @return the reason phrase associated with the given status code
066: * or null if the status code is not recognized.
067: *
068: * TODO: getStatusText should be called getReasonPhrase to match RFC
069: */
070: public static String getStatusText(int statusCode) {
071:
072: if (statusCode < 0) {
073: throw new IllegalArgumentException(
074: "status code may not be negative");
075: }
076: int classIndex = statusCode / 100;
077: int codeIndex = statusCode - classIndex * 100;
078: if (classIndex < 1 || classIndex > (REASON_PHRASES.length - 1)
079: || codeIndex < 0
080: || codeIndex > (REASON_PHRASES[classIndex].length - 1)) {
081: return null;
082: }
083: return REASON_PHRASES[classIndex][codeIndex];
084: }
085:
086: // -------------------------------------------------------- Private Methods
087:
088: /**
089: * Store the given reason phrase, by status code.
090: * @param statusCode The status code to lookup
091: * @param reasonPhrase The reason phrase for this status code
092: */
093: private static void addStatusCodeMap(int statusCode,
094: String reasonPhrase) {
095: int classIndex = statusCode / 100;
096: REASON_PHRASES[classIndex][statusCode - classIndex * 100] = reasonPhrase;
097: }
098:
099: // -------------------------------------------------------------- Constants
100:
101: // --- 1xx Informational ---
102:
103: /** <tt>100 Continue</tt> (HTTP/1.1 - RFC 2616) */
104: public static final int SC_CONTINUE = 100;
105: /** <tt>101 Switching Protocols</tt> (HTTP/1.1 - RFC 2616)*/
106: public static final int SC_SWITCHING_PROTOCOLS = 101;
107: /** <tt>102 Processing</tt> (WebDAV - RFC 2518) */
108: public static final int SC_PROCESSING = 102;
109:
110: // --- 2xx Success ---
111:
112: /** <tt>200 OK</tt> (HTTP/1.0 - RFC 1945) */
113: public static final int SC_OK = 200;
114: /** <tt>201 Created</tt> (HTTP/1.0 - RFC 1945) */
115: public static final int SC_CREATED = 201;
116: /** <tt>202 Accepted</tt> (HTTP/1.0 - RFC 1945) */
117: public static final int SC_ACCEPTED = 202;
118: /** <tt>203 Non Authoritative Information</tt> (HTTP/1.1 - RFC 2616) */
119: public static final int SC_NON_AUTHORITATIVE_INFORMATION = 203;
120: /** <tt>204 No Content</tt> (HTTP/1.0 - RFC 1945) */
121: public static final int SC_NO_CONTENT = 204;
122: /** <tt>205 Reset Content</tt> (HTTP/1.1 - RFC 2616) */
123: public static final int SC_RESET_CONTENT = 205;
124: /** <tt>206 Partial Content</tt> (HTTP/1.1 - RFC 2616) */
125: public static final int SC_PARTIAL_CONTENT = 206;
126: /**
127: * <tt>207 Multi-Status</tt> (WebDAV - RFC 2518) or <tt>207 Partial Update
128: * OK</tt> (HTTP/1.1 - draft-ietf-http-v11-spec-rev-01?)
129: */
130: public static final int SC_MULTI_STATUS = 207;
131:
132: // --- 3xx Redirection ---
133:
134: /** <tt>300 Mutliple Choices</tt> (HTTP/1.1 - RFC 2616) */
135: public static final int SC_MULTIPLE_CHOICES = 300;
136: /** <tt>301 Moved Permanently</tt> (HTTP/1.0 - RFC 1945) */
137: public static final int SC_MOVED_PERMANENTLY = 301;
138: /** <tt>302 Moved Temporarily</tt> (Sometimes <tt>Found</tt>) (HTTP/1.0 - RFC 1945) */
139: public static final int SC_MOVED_TEMPORARILY = 302;
140: /** <tt>303 See Other</tt> (HTTP/1.1 - RFC 2616) */
141: public static final int SC_SEE_OTHER = 303;
142: /** <tt>304 Not Modified</tt> (HTTP/1.0 - RFC 1945) */
143: public static final int SC_NOT_MODIFIED = 304;
144: /** <tt>305 Use Proxy</tt> (HTTP/1.1 - RFC 2616) */
145: public static final int SC_USE_PROXY = 305;
146: /** <tt>307 Temporary Redirect</tt> (HTTP/1.1 - RFC 2616) */
147: public static final int SC_TEMPORARY_REDIRECT = 307;
148:
149: // --- 4xx Client Error ---
150:
151: /** <tt>400 Bad Request</tt> (HTTP/1.1 - RFC 2616) */
152: public static final int SC_BAD_REQUEST = 400;
153: /** <tt>401 Unauthorized</tt> (HTTP/1.0 - RFC 1945) */
154: public static final int SC_UNAUTHORIZED = 401;
155: /** <tt>402 Payment Required</tt> (HTTP/1.1 - RFC 2616) */
156: public static final int SC_PAYMENT_REQUIRED = 402;
157: /** <tt>403 Forbidden</tt> (HTTP/1.0 - RFC 1945) */
158: public static final int SC_FORBIDDEN = 403;
159: /** <tt>404 Not Found</tt> (HTTP/1.0 - RFC 1945) */
160: public static final int SC_NOT_FOUND = 404;
161: /** <tt>405 Method Not Allowed</tt> (HTTP/1.1 - RFC 2616) */
162: public static final int SC_METHOD_NOT_ALLOWED = 405;
163: /** <tt>406 Not Acceptable</tt> (HTTP/1.1 - RFC 2616) */
164: public static final int SC_NOT_ACCEPTABLE = 406;
165: /** <tt>407 Proxy Authentication Required</tt> (HTTP/1.1 - RFC 2616)*/
166: public static final int SC_PROXY_AUTHENTICATION_REQUIRED = 407;
167: /** <tt>408 Request Timeout</tt> (HTTP/1.1 - RFC 2616) */
168: public static final int SC_REQUEST_TIMEOUT = 408;
169: /** <tt>409 Conflict</tt> (HTTP/1.1 - RFC 2616) */
170: public static final int SC_CONFLICT = 409;
171: /** <tt>410 Gone</tt> (HTTP/1.1 - RFC 2616) */
172: public static final int SC_GONE = 410;
173: /** <tt>411 Length Required</tt> (HTTP/1.1 - RFC 2616) */
174: public static final int SC_LENGTH_REQUIRED = 411;
175: /** <tt>412 Precondition Failed</tt> (HTTP/1.1 - RFC 2616) */
176: public static final int SC_PRECONDITION_FAILED = 412;
177: /** <tt>413 Request Entity Too Large</tt> (HTTP/1.1 - RFC 2616) */
178: public static final int SC_REQUEST_TOO_LONG = 413;
179: /** <tt>414 Request-URI Too Long</tt> (HTTP/1.1 - RFC 2616) */
180: public static final int SC_REQUEST_URI_TOO_LONG = 414;
181: /** <tt>415 Unsupported Media Type</tt> (HTTP/1.1 - RFC 2616) */
182: public static final int SC_UNSUPPORTED_MEDIA_TYPE = 415;
183: /** <tt>416 Requested Range Not Satisfiable</tt> (HTTP/1.1 - RFC 2616) */
184: public static final int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
185: /** <tt>417 Expectation Failed</tt> (HTTP/1.1 - RFC 2616) */
186: public static final int SC_EXPECTATION_FAILED = 417;
187:
188: /**
189: * Static constant for a 418 error.
190: * <tt>418 Unprocessable Entity</tt> (WebDAV drafts?)
191: * or <tt>418 Reauthentication Required</tt> (HTTP/1.1 drafts?)
192: */
193: // not used
194: // public static final int SC_UNPROCESSABLE_ENTITY = 418;
195: /**
196: * Static constant for a 419 error.
197: * <tt>419 Insufficient Space on Resource</tt>
198: * (WebDAV - draft-ietf-webdav-protocol-05?)
199: * or <tt>419 Proxy Reauthentication Required</tt>
200: * (HTTP/1.1 drafts?)
201: */
202: public static final int SC_INSUFFICIENT_SPACE_ON_RESOURCE = 419;
203: /**
204: * Static constant for a 420 error.
205: * <tt>420 Method Failure</tt>
206: * (WebDAV - draft-ietf-webdav-protocol-05?)
207: */
208: public static final int SC_METHOD_FAILURE = 420;
209: /** <tt>422 Unprocessable Entity</tt> (WebDAV - RFC 2518) */
210: public static final int SC_UNPROCESSABLE_ENTITY = 422;
211: /** <tt>423 Locked</tt> (WebDAV - RFC 2518) */
212: public static final int SC_LOCKED = 423;
213: /** <tt>424 Failed Dependency</tt> (WebDAV - RFC 2518) */
214: public static final int SC_FAILED_DEPENDENCY = 424;
215:
216: // --- 5xx Server Error ---
217:
218: /** <tt>500 Server Error</tt> (HTTP/1.0 - RFC 1945) */
219: public static final int SC_INTERNAL_SERVER_ERROR = 500;
220: /** <tt>501 Not Implemented</tt> (HTTP/1.0 - RFC 1945) */
221: public static final int SC_NOT_IMPLEMENTED = 501;
222: /** <tt>502 Bad Gateway</tt> (HTTP/1.0 - RFC 1945) */
223: public static final int SC_BAD_GATEWAY = 502;
224: /** <tt>503 Service Unavailable</tt> (HTTP/1.0 - RFC 1945) */
225: public static final int SC_SERVICE_UNAVAILABLE = 503;
226: /** <tt>504 Gateway Timeout</tt> (HTTP/1.1 - RFC 2616) */
227: public static final int SC_GATEWAY_TIMEOUT = 504;
228: /** <tt>505 HTTP Version Not Supported</tt> (HTTP/1.1 - RFC 2616) */
229: public static final int SC_HTTP_VERSION_NOT_SUPPORTED = 505;
230:
231: /** <tt>507 Insufficient Storage</tt> (WebDAV - RFC 2518) */
232: public static final int SC_INSUFFICIENT_STORAGE = 507;
233:
234: // ----------------------------------------------------- Static Initializer
235:
236: /** Set up status code to "reason phrase" map. */
237: static {
238: // HTTP 1.0 Server status codes -- see RFC 1945
239: addStatusCodeMap(SC_OK, "OK");
240: addStatusCodeMap(SC_CREATED, "Created");
241: addStatusCodeMap(SC_ACCEPTED, "Accepted");
242: addStatusCodeMap(SC_NO_CONTENT, "No Content");
243: addStatusCodeMap(SC_MOVED_PERMANENTLY, "Moved Permanently");
244: addStatusCodeMap(SC_MOVED_TEMPORARILY, "Moved Temporarily");
245: addStatusCodeMap(SC_NOT_MODIFIED, "Not Modified");
246: addStatusCodeMap(SC_BAD_REQUEST, "Bad Request");
247: addStatusCodeMap(SC_UNAUTHORIZED, "Unauthorized");
248: addStatusCodeMap(SC_FORBIDDEN, "Forbidden");
249: addStatusCodeMap(SC_NOT_FOUND, "Not Found");
250: addStatusCodeMap(SC_INTERNAL_SERVER_ERROR,
251: "Internal Server Error");
252: addStatusCodeMap(SC_NOT_IMPLEMENTED, "Not Implemented");
253: addStatusCodeMap(SC_BAD_GATEWAY, "Bad Gateway");
254: addStatusCodeMap(SC_SERVICE_UNAVAILABLE, "Service Unavailable");
255:
256: // HTTP 1.1 Server status codes -- see RFC 2048
257: addStatusCodeMap(SC_CONTINUE, "Continue");
258: addStatusCodeMap(SC_TEMPORARY_REDIRECT, "Temporary Redirect");
259: addStatusCodeMap(SC_METHOD_NOT_ALLOWED, "Method Not Allowed");
260: addStatusCodeMap(SC_CONFLICT, "Conflict");
261: addStatusCodeMap(SC_PRECONDITION_FAILED, "Precondition Failed");
262: addStatusCodeMap(SC_REQUEST_TOO_LONG, "Request Too Long");
263: addStatusCodeMap(SC_REQUEST_URI_TOO_LONG,
264: "Request-URI Too Long");
265: addStatusCodeMap(SC_UNSUPPORTED_MEDIA_TYPE,
266: "Unsupported Media Type");
267: addStatusCodeMap(SC_MULTIPLE_CHOICES, "Multiple Choices");
268: addStatusCodeMap(SC_SEE_OTHER, "See Other");
269: addStatusCodeMap(SC_USE_PROXY, "Use Proxy");
270: addStatusCodeMap(SC_PAYMENT_REQUIRED, "Payment Required");
271: addStatusCodeMap(SC_NOT_ACCEPTABLE, "Not Acceptable");
272: addStatusCodeMap(SC_PROXY_AUTHENTICATION_REQUIRED,
273: "Proxy Authentication Required");
274: addStatusCodeMap(SC_REQUEST_TIMEOUT, "Request Timeout");
275:
276: addStatusCodeMap(SC_SWITCHING_PROTOCOLS, "Switching Protocols");
277: addStatusCodeMap(SC_NON_AUTHORITATIVE_INFORMATION,
278: "Non Authoritative Information");
279: addStatusCodeMap(SC_RESET_CONTENT, "Reset Content");
280: addStatusCodeMap(SC_PARTIAL_CONTENT, "Partial Content");
281: addStatusCodeMap(SC_GATEWAY_TIMEOUT, "Gateway Timeout");
282: addStatusCodeMap(SC_HTTP_VERSION_NOT_SUPPORTED,
283: "Http Version Not Supported");
284: addStatusCodeMap(SC_GONE, "Gone");
285: addStatusCodeMap(SC_LENGTH_REQUIRED, "Length Required");
286: addStatusCodeMap(SC_REQUESTED_RANGE_NOT_SATISFIABLE,
287: "Requested Range Not Satisfiable");
288: addStatusCodeMap(SC_EXPECTATION_FAILED, "Expectation Failed");
289:
290: // WebDAV Server-specific status codes
291: addStatusCodeMap(SC_PROCESSING, "Processing");
292: addStatusCodeMap(SC_MULTI_STATUS, "Multi-Status");
293: addStatusCodeMap(SC_UNPROCESSABLE_ENTITY,
294: "Unprocessable Entity");
295: addStatusCodeMap(SC_INSUFFICIENT_SPACE_ON_RESOURCE,
296: "Insufficient Space On Resource");
297: addStatusCodeMap(SC_METHOD_FAILURE, "Method Failure");
298: addStatusCodeMap(SC_LOCKED, "Locked");
299: addStatusCodeMap(SC_INSUFFICIENT_STORAGE,
300: "Insufficient Storage");
301: addStatusCodeMap(SC_FAILED_DEPENDENCY, "Failed Dependency");
302: }
303:
304: }
|