001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/impl/EnglishReasonPhraseCatalog.java $
003: * $Revision: 505744 $
004: * $Date: 2007-02-10 19:58:45 +0100 (Sat, 10 Feb 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with 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,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.impl;
033:
034: import java.util.Locale;
035:
036: import org.apache.http.HttpStatus;
037: import org.apache.http.ReasonPhraseCatalog;
038:
039: /**
040: * English reason phrases for HTTP status codes.
041: * All status codes defined in RFC1945 (HTTP/1.0), RFC2616 (HTTP/1.1), and
042: * RFC2518 (WebDAV) are supported.
043: *
044: * @author Unascribed
045: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
046: * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
047: *
048: * @version $Revision: 505744 $
049: */
050: public class EnglishReasonPhraseCatalog implements ReasonPhraseCatalog {
051:
052: // static array with english reason phrases defined below
053:
054: /**
055: * The default instance of this catalog.
056: * This catalog is thread safe, so there typically
057: * is no need to create other instances.
058: */
059: public final static EnglishReasonPhraseCatalog INSTANCE = new EnglishReasonPhraseCatalog();
060:
061: /**
062: * Restricted default constructor, for derived classes.
063: * If you need an instance of this class, use {@link #INSTANCE INSTANCE}.
064: */
065: protected EnglishReasonPhraseCatalog() {
066: // no body
067: }
068:
069: /**
070: * Obtains the reason phrase for a status code.
071: *
072: * @param status the status code, in the range 100-599
073: * @param loc ignored
074: *
075: * @return the reason phrase, or <code>null</code>
076: */
077: public String getReason(int status, Locale loc) {
078: if ((status < 100) || (status >= 600)) {
079: throw new IllegalArgumentException(
080: "Unknown category for status code " + status + ".");
081: }
082:
083: final int category = status / 100;
084: final int subcode = status - 100 * category;
085:
086: String reason = null;
087: if (REASON_PHRASES[category].length > subcode)
088: reason = REASON_PHRASES[category][subcode];
089:
090: return reason;
091: }
092:
093: /** Reason phrases lookup table. */
094: private static final String[][] REASON_PHRASES = new String[][] {
095: null, new String[3], // 1xx
096: new String[8], // 2xx
097: new String[8], // 3xx
098: new String[25], // 4xx
099: new String[8] // 5xx
100: };
101:
102: /**
103: * Stores the given reason phrase, by status code.
104: * Helper method to initialize the static lookup table.
105: *
106: * @param status the status code for which to define the phrase
107: * @param reason the reason phrase for this status code
108: */
109: private static void setReason(int status, String reason) {
110: final int category = status / 100;
111: final int subcode = status - 100 * category;
112: REASON_PHRASES[category][subcode] = reason;
113: }
114:
115: // ----------------------------------------------------- Static Initializer
116:
117: /** Set up status code to "reason phrase" map. */
118: static {
119: // HTTP 1.0 Server status codes -- see RFC 1945
120: setReason(HttpStatus.SC_OK, "OK");
121: setReason(HttpStatus.SC_CREATED, "Created");
122: setReason(HttpStatus.SC_ACCEPTED, "Accepted");
123: setReason(HttpStatus.SC_NO_CONTENT, "No Content");
124: setReason(HttpStatus.SC_MOVED_PERMANENTLY, "Moved Permanently");
125: setReason(HttpStatus.SC_MOVED_TEMPORARILY, "Moved Temporarily");
126: setReason(HttpStatus.SC_NOT_MODIFIED, "Not Modified");
127: setReason(HttpStatus.SC_BAD_REQUEST, "Bad Request");
128: setReason(HttpStatus.SC_UNAUTHORIZED, "Unauthorized");
129: setReason(HttpStatus.SC_FORBIDDEN, "Forbidden");
130: setReason(HttpStatus.SC_NOT_FOUND, "Not Found");
131: setReason(HttpStatus.SC_INTERNAL_SERVER_ERROR,
132: "Internal Server Error");
133: setReason(HttpStatus.SC_NOT_IMPLEMENTED, "Not Implemented");
134: setReason(HttpStatus.SC_BAD_GATEWAY, "Bad Gateway");
135: setReason(HttpStatus.SC_SERVICE_UNAVAILABLE,
136: "Service Unavailable");
137:
138: // HTTP 1.1 Server status codes -- see RFC 2048
139: setReason(HttpStatus.SC_CONTINUE, "Continue");
140: setReason(HttpStatus.SC_TEMPORARY_REDIRECT,
141: "Temporary Redirect");
142: setReason(HttpStatus.SC_METHOD_NOT_ALLOWED,
143: "Method Not Allowed");
144: setReason(HttpStatus.SC_CONFLICT, "Conflict");
145: setReason(HttpStatus.SC_PRECONDITION_FAILED,
146: "Precondition Failed");
147: setReason(HttpStatus.SC_REQUEST_TOO_LONG, "Request Too Long");
148: setReason(HttpStatus.SC_REQUEST_URI_TOO_LONG,
149: "Request-URI Too Long");
150: setReason(HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE,
151: "Unsupported Media Type");
152: setReason(HttpStatus.SC_MULTIPLE_CHOICES, "Multiple Choices");
153: setReason(HttpStatus.SC_SEE_OTHER, "See Other");
154: setReason(HttpStatus.SC_USE_PROXY, "Use Proxy");
155: setReason(HttpStatus.SC_PAYMENT_REQUIRED, "Payment Required");
156: setReason(HttpStatus.SC_NOT_ACCEPTABLE, "Not Acceptable");
157: setReason(HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED,
158: "Proxy Authentication Required");
159: setReason(HttpStatus.SC_REQUEST_TIMEOUT, "Request Timeout");
160:
161: setReason(HttpStatus.SC_SWITCHING_PROTOCOLS,
162: "Switching Protocols");
163: setReason(HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION,
164: "Non Authoritative Information");
165: setReason(HttpStatus.SC_RESET_CONTENT, "Reset Content");
166: setReason(HttpStatus.SC_PARTIAL_CONTENT, "Partial Content");
167: setReason(HttpStatus.SC_GATEWAY_TIMEOUT, "Gateway Timeout");
168: setReason(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED,
169: "Http Version Not Supported");
170: setReason(HttpStatus.SC_GONE, "Gone");
171: setReason(HttpStatus.SC_LENGTH_REQUIRED, "Length Required");
172: setReason(HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE,
173: "Requested Range Not Satisfiable");
174: setReason(HttpStatus.SC_EXPECTATION_FAILED,
175: "Expectation Failed");
176:
177: // WebDAV Server-specific status codes
178: setReason(HttpStatus.SC_PROCESSING, "Processing");
179: setReason(HttpStatus.SC_MULTI_STATUS, "Multi-Status");
180: setReason(HttpStatus.SC_UNPROCESSABLE_ENTITY,
181: "Unprocessable Entity");
182: setReason(HttpStatus.SC_INSUFFICIENT_SPACE_ON_RESOURCE,
183: "Insufficient Space On Resource");
184: setReason(HttpStatus.SC_METHOD_FAILURE, "Method Failure");
185: setReason(HttpStatus.SC_LOCKED, "Locked");
186: setReason(HttpStatus.SC_INSUFFICIENT_STORAGE,
187: "Insufficient Storage");
188: setReason(HttpStatus.SC_FAILED_DEPENDENCY, "Failed Dependency");
189: }
190:
191: }
|