View Javadoc

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