michael@0: /*
michael@0: * ====================================================================
michael@0: * Licensed to the Apache Software Foundation (ASF) under one
michael@0: * or more contributor license agreements. See the NOTICE file
michael@0: * distributed with this work for additional information
michael@0: * regarding copyright ownership. The ASF licenses this file
michael@0: * to you under the Apache License, Version 2.0 (the
michael@0: * "License"); you may not use this file except in compliance
michael@0: * with the License. You may obtain a copy of the License at
michael@0: *
michael@0: * http://www.apache.org/licenses/LICENSE-2.0
michael@0: *
michael@0: * Unless required by applicable law or agreed to in writing,
michael@0: * software distributed under the License is distributed on an
michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0: * KIND, either express or implied. See the License for the
michael@0: * specific language governing permissions and limitations
michael@0: * under the License.
michael@0: * ====================================================================
michael@0: *
michael@0: * This software consists of voluntary contributions made by many
michael@0: * individuals on behalf of the Apache Software Foundation. For more
michael@0: * information on the Apache Software Foundation, please see
michael@0: * .
michael@0: *
michael@0: */
michael@0:
michael@0: package ch.boye.httpclientandroidlib.impl;
michael@0:
michael@0: import java.util.Locale;
michael@0:
michael@0: import ch.boye.httpclientandroidlib.HttpStatus;
michael@0: import ch.boye.httpclientandroidlib.ReasonPhraseCatalog;
michael@0:
michael@0: /**
michael@0: * English reason phrases for HTTP status codes.
michael@0: * All status codes defined in RFC1945 (HTTP/1.0), RFC2616 (HTTP/1.1), and
michael@0: * RFC2518 (WebDAV) are supported.
michael@0: *
michael@0: * @since 4.0
michael@0: */
michael@0: public class EnglishReasonPhraseCatalog implements ReasonPhraseCatalog {
michael@0:
michael@0: // static array with english reason phrases defined below
michael@0:
michael@0: /**
michael@0: * The default instance of this catalog.
michael@0: * This catalog is thread safe, so there typically
michael@0: * is no need to create other instances.
michael@0: */
michael@0: public final static EnglishReasonPhraseCatalog INSTANCE =
michael@0: new EnglishReasonPhraseCatalog();
michael@0:
michael@0:
michael@0: /**
michael@0: * Restricted default constructor, for derived classes.
michael@0: * If you need an instance of this class, use {@link #INSTANCE INSTANCE}.
michael@0: */
michael@0: protected EnglishReasonPhraseCatalog() {
michael@0: // no body
michael@0: }
michael@0:
michael@0:
michael@0: /**
michael@0: * Obtains the reason phrase for a status code.
michael@0: *
michael@0: * @param status the status code, in the range 100-599
michael@0: * @param loc ignored
michael@0: *
michael@0: * @return the reason phrase, or null
michael@0: */
michael@0: public String getReason(int status, Locale loc) {
michael@0: if ((status < 100) || (status >= 600)) {
michael@0: throw new IllegalArgumentException
michael@0: ("Unknown category for status code " + status + ".");
michael@0: }
michael@0:
michael@0: final int category = status / 100;
michael@0: final int subcode = status - 100*category;
michael@0:
michael@0: String reason = null;
michael@0: if (REASON_PHRASES[category].length > subcode)
michael@0: reason = REASON_PHRASES[category][subcode];
michael@0:
michael@0: return reason;
michael@0: }
michael@0:
michael@0:
michael@0: /** Reason phrases lookup table. */
michael@0: private static final String[][] REASON_PHRASES = new String[][]{
michael@0: null,
michael@0: new String[3], // 1xx
michael@0: new String[8], // 2xx
michael@0: new String[8], // 3xx
michael@0: new String[25], // 4xx
michael@0: new String[8] // 5xx
michael@0: };
michael@0:
michael@0:
michael@0:
michael@0: /**
michael@0: * Stores the given reason phrase, by status code.
michael@0: * Helper method to initialize the static lookup table.
michael@0: *
michael@0: * @param status the status code for which to define the phrase
michael@0: * @param reason the reason phrase for this status code
michael@0: */
michael@0: private static void setReason(int status, String reason) {
michael@0: final int category = status / 100;
michael@0: final int subcode = status - 100*category;
michael@0: REASON_PHRASES[category][subcode] = reason;
michael@0: }
michael@0:
michael@0:
michael@0: // ----------------------------------------------------- Static Initializer
michael@0:
michael@0: /** Set up status code to "reason phrase" map. */
michael@0: static {
michael@0: // HTTP 1.0 Server status codes -- see RFC 1945
michael@0: setReason(HttpStatus.SC_OK,
michael@0: "OK");
michael@0: setReason(HttpStatus.SC_CREATED,
michael@0: "Created");
michael@0: setReason(HttpStatus.SC_ACCEPTED,
michael@0: "Accepted");
michael@0: setReason(HttpStatus.SC_NO_CONTENT,
michael@0: "No Content");
michael@0: setReason(HttpStatus.SC_MOVED_PERMANENTLY,
michael@0: "Moved Permanently");
michael@0: setReason(HttpStatus.SC_MOVED_TEMPORARILY,
michael@0: "Moved Temporarily");
michael@0: setReason(HttpStatus.SC_NOT_MODIFIED,
michael@0: "Not Modified");
michael@0: setReason(HttpStatus.SC_BAD_REQUEST,
michael@0: "Bad Request");
michael@0: setReason(HttpStatus.SC_UNAUTHORIZED,
michael@0: "Unauthorized");
michael@0: setReason(HttpStatus.SC_FORBIDDEN,
michael@0: "Forbidden");
michael@0: setReason(HttpStatus.SC_NOT_FOUND,
michael@0: "Not Found");
michael@0: setReason(HttpStatus.SC_INTERNAL_SERVER_ERROR,
michael@0: "Internal Server Error");
michael@0: setReason(HttpStatus.SC_NOT_IMPLEMENTED,
michael@0: "Not Implemented");
michael@0: setReason(HttpStatus.SC_BAD_GATEWAY,
michael@0: "Bad Gateway");
michael@0: setReason(HttpStatus.SC_SERVICE_UNAVAILABLE,
michael@0: "Service Unavailable");
michael@0:
michael@0: // HTTP 1.1 Server status codes -- see RFC 2048
michael@0: setReason(HttpStatus.SC_CONTINUE,
michael@0: "Continue");
michael@0: setReason(HttpStatus.SC_TEMPORARY_REDIRECT,
michael@0: "Temporary Redirect");
michael@0: setReason(HttpStatus.SC_METHOD_NOT_ALLOWED,
michael@0: "Method Not Allowed");
michael@0: setReason(HttpStatus.SC_CONFLICT,
michael@0: "Conflict");
michael@0: setReason(HttpStatus.SC_PRECONDITION_FAILED,
michael@0: "Precondition Failed");
michael@0: setReason(HttpStatus.SC_REQUEST_TOO_LONG,
michael@0: "Request Too Long");
michael@0: setReason(HttpStatus.SC_REQUEST_URI_TOO_LONG,
michael@0: "Request-URI Too Long");
michael@0: setReason(HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE,
michael@0: "Unsupported Media Type");
michael@0: setReason(HttpStatus.SC_MULTIPLE_CHOICES,
michael@0: "Multiple Choices");
michael@0: setReason(HttpStatus.SC_SEE_OTHER,
michael@0: "See Other");
michael@0: setReason(HttpStatus.SC_USE_PROXY,
michael@0: "Use Proxy");
michael@0: setReason(HttpStatus.SC_PAYMENT_REQUIRED,
michael@0: "Payment Required");
michael@0: setReason(HttpStatus.SC_NOT_ACCEPTABLE,
michael@0: "Not Acceptable");
michael@0: setReason(HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED,
michael@0: "Proxy Authentication Required");
michael@0: setReason(HttpStatus.SC_REQUEST_TIMEOUT,
michael@0: "Request Timeout");
michael@0:
michael@0: setReason(HttpStatus.SC_SWITCHING_PROTOCOLS,
michael@0: "Switching Protocols");
michael@0: setReason(HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION,
michael@0: "Non Authoritative Information");
michael@0: setReason(HttpStatus.SC_RESET_CONTENT,
michael@0: "Reset Content");
michael@0: setReason(HttpStatus.SC_PARTIAL_CONTENT,
michael@0: "Partial Content");
michael@0: setReason(HttpStatus.SC_GATEWAY_TIMEOUT,
michael@0: "Gateway Timeout");
michael@0: setReason(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED,
michael@0: "Http Version Not Supported");
michael@0: setReason(HttpStatus.SC_GONE,
michael@0: "Gone");
michael@0: setReason(HttpStatus.SC_LENGTH_REQUIRED,
michael@0: "Length Required");
michael@0: setReason(HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE,
michael@0: "Requested Range Not Satisfiable");
michael@0: setReason(HttpStatus.SC_EXPECTATION_FAILED,
michael@0: "Expectation Failed");
michael@0:
michael@0: // WebDAV Server-specific status codes
michael@0: setReason(HttpStatus.SC_PROCESSING,
michael@0: "Processing");
michael@0: setReason(HttpStatus.SC_MULTI_STATUS,
michael@0: "Multi-Status");
michael@0: setReason(HttpStatus.SC_UNPROCESSABLE_ENTITY,
michael@0: "Unprocessable Entity");
michael@0: setReason(HttpStatus.SC_INSUFFICIENT_SPACE_ON_RESOURCE,
michael@0: "Insufficient Space On Resource");
michael@0: setReason(HttpStatus.SC_METHOD_FAILURE,
michael@0: "Method Failure");
michael@0: setReason(HttpStatus.SC_LOCKED,
michael@0: "Locked");
michael@0: setReason(HttpStatus.SC_INSUFFICIENT_STORAGE,
michael@0: "Insufficient Storage");
michael@0: setReason(HttpStatus.SC_FAILED_DEPENDENCY,
michael@0: "Failed Dependency");
michael@0: }
michael@0:
michael@0:
michael@0: }