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