Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | * ==================================================================== |
michael@0 | 3 | * Licensed to the Apache Software Foundation (ASF) under one |
michael@0 | 4 | * or more contributor license agreements. See the NOTICE file |
michael@0 | 5 | * distributed with this work for additional information |
michael@0 | 6 | * regarding copyright ownership. The ASF licenses this file |
michael@0 | 7 | * to you under the Apache License, Version 2.0 (the |
michael@0 | 8 | * "License"); you may not use this file except in compliance |
michael@0 | 9 | * with the License. You may obtain a copy of the License at |
michael@0 | 10 | * |
michael@0 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 12 | * |
michael@0 | 13 | * Unless required by applicable law or agreed to in writing, |
michael@0 | 14 | * software distributed under the License is distributed on an |
michael@0 | 15 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
michael@0 | 16 | * KIND, either express or implied. See the License for the |
michael@0 | 17 | * specific language governing permissions and limitations |
michael@0 | 18 | * under the License. |
michael@0 | 19 | * ==================================================================== |
michael@0 | 20 | * |
michael@0 | 21 | * This software consists of voluntary contributions made by many |
michael@0 | 22 | * individuals on behalf of the Apache Software Foundation. For more |
michael@0 | 23 | * information on the Apache Software Foundation, please see |
michael@0 | 24 | * <http://www.apache.org/>. |
michael@0 | 25 | * |
michael@0 | 26 | */ |
michael@0 | 27 | |
michael@0 | 28 | package ch.boye.httpclientandroidlib.impl; |
michael@0 | 29 | |
michael@0 | 30 | import java.util.Locale; |
michael@0 | 31 | |
michael@0 | 32 | import ch.boye.httpclientandroidlib.HttpStatus; |
michael@0 | 33 | import ch.boye.httpclientandroidlib.ReasonPhraseCatalog; |
michael@0 | 34 | |
michael@0 | 35 | /** |
michael@0 | 36 | * English reason phrases for HTTP status codes. |
michael@0 | 37 | * All status codes defined in RFC1945 (HTTP/1.0), RFC2616 (HTTP/1.1), and |
michael@0 | 38 | * RFC2518 (WebDAV) are supported. |
michael@0 | 39 | * |
michael@0 | 40 | * @since 4.0 |
michael@0 | 41 | */ |
michael@0 | 42 | public class EnglishReasonPhraseCatalog implements ReasonPhraseCatalog { |
michael@0 | 43 | |
michael@0 | 44 | // static array with english reason phrases defined below |
michael@0 | 45 | |
michael@0 | 46 | /** |
michael@0 | 47 | * The default instance of this catalog. |
michael@0 | 48 | * This catalog is thread safe, so there typically |
michael@0 | 49 | * is no need to create other instances. |
michael@0 | 50 | */ |
michael@0 | 51 | public final static EnglishReasonPhraseCatalog INSTANCE = |
michael@0 | 52 | new EnglishReasonPhraseCatalog(); |
michael@0 | 53 | |
michael@0 | 54 | |
michael@0 | 55 | /** |
michael@0 | 56 | * Restricted default constructor, for derived classes. |
michael@0 | 57 | * If you need an instance of this class, use {@link #INSTANCE INSTANCE}. |
michael@0 | 58 | */ |
michael@0 | 59 | protected EnglishReasonPhraseCatalog() { |
michael@0 | 60 | // no body |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | |
michael@0 | 64 | /** |
michael@0 | 65 | * Obtains the reason phrase for a status code. |
michael@0 | 66 | * |
michael@0 | 67 | * @param status the status code, in the range 100-599 |
michael@0 | 68 | * @param loc ignored |
michael@0 | 69 | * |
michael@0 | 70 | * @return the reason phrase, or <code>null</code> |
michael@0 | 71 | */ |
michael@0 | 72 | public String getReason(int status, Locale loc) { |
michael@0 | 73 | if ((status < 100) || (status >= 600)) { |
michael@0 | 74 | throw new IllegalArgumentException |
michael@0 | 75 | ("Unknown category for status code " + status + "."); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | final int category = status / 100; |
michael@0 | 79 | final int subcode = status - 100*category; |
michael@0 | 80 | |
michael@0 | 81 | String reason = null; |
michael@0 | 82 | if (REASON_PHRASES[category].length > subcode) |
michael@0 | 83 | reason = REASON_PHRASES[category][subcode]; |
michael@0 | 84 | |
michael@0 | 85 | return reason; |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | |
michael@0 | 89 | /** Reason phrases lookup table. */ |
michael@0 | 90 | private static final String[][] REASON_PHRASES = new String[][]{ |
michael@0 | 91 | null, |
michael@0 | 92 | new String[3], // 1xx |
michael@0 | 93 | new String[8], // 2xx |
michael@0 | 94 | new String[8], // 3xx |
michael@0 | 95 | new String[25], // 4xx |
michael@0 | 96 | new String[8] // 5xx |
michael@0 | 97 | }; |
michael@0 | 98 | |
michael@0 | 99 | |
michael@0 | 100 | |
michael@0 | 101 | /** |
michael@0 | 102 | * Stores the given reason phrase, by status code. |
michael@0 | 103 | * Helper method to initialize the static lookup table. |
michael@0 | 104 | * |
michael@0 | 105 | * @param status the status code for which to define the phrase |
michael@0 | 106 | * @param reason the reason phrase for this status code |
michael@0 | 107 | */ |
michael@0 | 108 | private static void setReason(int status, String reason) { |
michael@0 | 109 | final int category = status / 100; |
michael@0 | 110 | final int subcode = status - 100*category; |
michael@0 | 111 | REASON_PHRASES[category][subcode] = reason; |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | |
michael@0 | 115 | // ----------------------------------------------------- Static Initializer |
michael@0 | 116 | |
michael@0 | 117 | /** Set up status code to "reason phrase" map. */ |
michael@0 | 118 | static { |
michael@0 | 119 | // HTTP 1.0 Server status codes -- see RFC 1945 |
michael@0 | 120 | setReason(HttpStatus.SC_OK, |
michael@0 | 121 | "OK"); |
michael@0 | 122 | setReason(HttpStatus.SC_CREATED, |
michael@0 | 123 | "Created"); |
michael@0 | 124 | setReason(HttpStatus.SC_ACCEPTED, |
michael@0 | 125 | "Accepted"); |
michael@0 | 126 | setReason(HttpStatus.SC_NO_CONTENT, |
michael@0 | 127 | "No Content"); |
michael@0 | 128 | setReason(HttpStatus.SC_MOVED_PERMANENTLY, |
michael@0 | 129 | "Moved Permanently"); |
michael@0 | 130 | setReason(HttpStatus.SC_MOVED_TEMPORARILY, |
michael@0 | 131 | "Moved Temporarily"); |
michael@0 | 132 | setReason(HttpStatus.SC_NOT_MODIFIED, |
michael@0 | 133 | "Not Modified"); |
michael@0 | 134 | setReason(HttpStatus.SC_BAD_REQUEST, |
michael@0 | 135 | "Bad Request"); |
michael@0 | 136 | setReason(HttpStatus.SC_UNAUTHORIZED, |
michael@0 | 137 | "Unauthorized"); |
michael@0 | 138 | setReason(HttpStatus.SC_FORBIDDEN, |
michael@0 | 139 | "Forbidden"); |
michael@0 | 140 | setReason(HttpStatus.SC_NOT_FOUND, |
michael@0 | 141 | "Not Found"); |
michael@0 | 142 | setReason(HttpStatus.SC_INTERNAL_SERVER_ERROR, |
michael@0 | 143 | "Internal Server Error"); |
michael@0 | 144 | setReason(HttpStatus.SC_NOT_IMPLEMENTED, |
michael@0 | 145 | "Not Implemented"); |
michael@0 | 146 | setReason(HttpStatus.SC_BAD_GATEWAY, |
michael@0 | 147 | "Bad Gateway"); |
michael@0 | 148 | setReason(HttpStatus.SC_SERVICE_UNAVAILABLE, |
michael@0 | 149 | "Service Unavailable"); |
michael@0 | 150 | |
michael@0 | 151 | // HTTP 1.1 Server status codes -- see RFC 2048 |
michael@0 | 152 | setReason(HttpStatus.SC_CONTINUE, |
michael@0 | 153 | "Continue"); |
michael@0 | 154 | setReason(HttpStatus.SC_TEMPORARY_REDIRECT, |
michael@0 | 155 | "Temporary Redirect"); |
michael@0 | 156 | setReason(HttpStatus.SC_METHOD_NOT_ALLOWED, |
michael@0 | 157 | "Method Not Allowed"); |
michael@0 | 158 | setReason(HttpStatus.SC_CONFLICT, |
michael@0 | 159 | "Conflict"); |
michael@0 | 160 | setReason(HttpStatus.SC_PRECONDITION_FAILED, |
michael@0 | 161 | "Precondition Failed"); |
michael@0 | 162 | setReason(HttpStatus.SC_REQUEST_TOO_LONG, |
michael@0 | 163 | "Request Too Long"); |
michael@0 | 164 | setReason(HttpStatus.SC_REQUEST_URI_TOO_LONG, |
michael@0 | 165 | "Request-URI Too Long"); |
michael@0 | 166 | setReason(HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE, |
michael@0 | 167 | "Unsupported Media Type"); |
michael@0 | 168 | setReason(HttpStatus.SC_MULTIPLE_CHOICES, |
michael@0 | 169 | "Multiple Choices"); |
michael@0 | 170 | setReason(HttpStatus.SC_SEE_OTHER, |
michael@0 | 171 | "See Other"); |
michael@0 | 172 | setReason(HttpStatus.SC_USE_PROXY, |
michael@0 | 173 | "Use Proxy"); |
michael@0 | 174 | setReason(HttpStatus.SC_PAYMENT_REQUIRED, |
michael@0 | 175 | "Payment Required"); |
michael@0 | 176 | setReason(HttpStatus.SC_NOT_ACCEPTABLE, |
michael@0 | 177 | "Not Acceptable"); |
michael@0 | 178 | setReason(HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED, |
michael@0 | 179 | "Proxy Authentication Required"); |
michael@0 | 180 | setReason(HttpStatus.SC_REQUEST_TIMEOUT, |
michael@0 | 181 | "Request Timeout"); |
michael@0 | 182 | |
michael@0 | 183 | setReason(HttpStatus.SC_SWITCHING_PROTOCOLS, |
michael@0 | 184 | "Switching Protocols"); |
michael@0 | 185 | setReason(HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION, |
michael@0 | 186 | "Non Authoritative Information"); |
michael@0 | 187 | setReason(HttpStatus.SC_RESET_CONTENT, |
michael@0 | 188 | "Reset Content"); |
michael@0 | 189 | setReason(HttpStatus.SC_PARTIAL_CONTENT, |
michael@0 | 190 | "Partial Content"); |
michael@0 | 191 | setReason(HttpStatus.SC_GATEWAY_TIMEOUT, |
michael@0 | 192 | "Gateway Timeout"); |
michael@0 | 193 | setReason(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED, |
michael@0 | 194 | "Http Version Not Supported"); |
michael@0 | 195 | setReason(HttpStatus.SC_GONE, |
michael@0 | 196 | "Gone"); |
michael@0 | 197 | setReason(HttpStatus.SC_LENGTH_REQUIRED, |
michael@0 | 198 | "Length Required"); |
michael@0 | 199 | setReason(HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE, |
michael@0 | 200 | "Requested Range Not Satisfiable"); |
michael@0 | 201 | setReason(HttpStatus.SC_EXPECTATION_FAILED, |
michael@0 | 202 | "Expectation Failed"); |
michael@0 | 203 | |
michael@0 | 204 | // WebDAV Server-specific status codes |
michael@0 | 205 | setReason(HttpStatus.SC_PROCESSING, |
michael@0 | 206 | "Processing"); |
michael@0 | 207 | setReason(HttpStatus.SC_MULTI_STATUS, |
michael@0 | 208 | "Multi-Status"); |
michael@0 | 209 | setReason(HttpStatus.SC_UNPROCESSABLE_ENTITY, |
michael@0 | 210 | "Unprocessable Entity"); |
michael@0 | 211 | setReason(HttpStatus.SC_INSUFFICIENT_SPACE_ON_RESOURCE, |
michael@0 | 212 | "Insufficient Space On Resource"); |
michael@0 | 213 | setReason(HttpStatus.SC_METHOD_FAILURE, |
michael@0 | 214 | "Method Failure"); |
michael@0 | 215 | setReason(HttpStatus.SC_LOCKED, |
michael@0 | 216 | "Locked"); |
michael@0 | 217 | setReason(HttpStatus.SC_INSUFFICIENT_STORAGE, |
michael@0 | 218 | "Insufficient Storage"); |
michael@0 | 219 | setReason(HttpStatus.SC_FAILED_DEPENDENCY, |
michael@0 | 220 | "Failed Dependency"); |
michael@0 | 221 | } |
michael@0 | 222 | |
michael@0 | 223 | |
michael@0 | 224 | } |