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  import org.apache.http.util.Args;
36  
37  /**
38   * English reason phrases for HTTP status codes.
39   * All status codes defined in RFC1945 (HTTP/1.0), RFC2616 (HTTP/1.1), and
40   * RFC2518 (WebDAV) are supported.
41   *
42   * @since 4.0
43   */
44  @Immutable
45  public class EnglishReasonPhraseCatalog implements ReasonPhraseCatalog {
46  
47      // static array with english reason phrases defined below
48  
49      /**
50       * The default instance of this catalog.
51       * This catalog is thread safe, so there typically
52       * is no need to create other instances.
53       */
54      public final static EnglishReasonPhraseCatalog INSTANCE =
55          new EnglishReasonPhraseCatalog();
56  
57  
58      /**
59       * Restricted default constructor, for derived classes.
60       * If you need an instance of this class, use {@link #INSTANCE INSTANCE}.
61       */
62      protected EnglishReasonPhraseCatalog() {
63          // no body
64      }
65  
66  
67      /**
68       * Obtains the reason phrase for a status code.
69       *
70       * @param status    the status code, in the range 100-599
71       * @param loc       ignored
72       *
73       * @return  the reason phrase, or <code>null</code>
74       */
75      public String getReason(final int status, final Locale loc) {
76          Args.check(status >= 100 && status < 600, "Unknown category for status code " + status);
77          final int category = status / 100;
78          final int subcode  = status - 100*category;
79  
80          String reason = null;
81          if (REASON_PHRASES[category].length > subcode) {
82              reason = REASON_PHRASES[category][subcode];
83          }
84  
85          return reason;
86      }
87  
88  
89      /** Reason phrases lookup table. */
90      private static final String[][] REASON_PHRASES = new String[][]{
91          null,
92          new String[3],  // 1xx
93          new String[8],  // 2xx
94          new String[8],  // 3xx
95          new String[25], // 4xx
96          new String[8]   // 5xx
97      };
98  
99  
100 
101     /**
102      * Stores the given reason phrase, by status code.
103      * Helper method to initialize the static lookup table.
104      *
105      * @param status    the status code for which to define the phrase
106      * @param reason    the reason phrase for this status code
107      */
108     private static void setReason(final int status, final String reason) {
109         final int category = status / 100;
110         final int subcode  = status - 100*category;
111         REASON_PHRASES[category][subcode] = reason;
112     }
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,
121                   "OK");
122         setReason(HttpStatus.SC_CREATED,
123                   "Created");
124         setReason(HttpStatus.SC_ACCEPTED,
125                   "Accepted");
126         setReason(HttpStatus.SC_NO_CONTENT,
127                   "No Content");
128         setReason(HttpStatus.SC_MOVED_PERMANENTLY,
129                   "Moved Permanently");
130         setReason(HttpStatus.SC_MOVED_TEMPORARILY,
131                   "Moved Temporarily");
132         setReason(HttpStatus.SC_NOT_MODIFIED,
133                   "Not Modified");
134         setReason(HttpStatus.SC_BAD_REQUEST,
135                   "Bad Request");
136         setReason(HttpStatus.SC_UNAUTHORIZED,
137                   "Unauthorized");
138         setReason(HttpStatus.SC_FORBIDDEN,
139                   "Forbidden");
140         setReason(HttpStatus.SC_NOT_FOUND,
141                   "Not Found");
142         setReason(HttpStatus.SC_INTERNAL_SERVER_ERROR,
143                   "Internal Server Error");
144         setReason(HttpStatus.SC_NOT_IMPLEMENTED,
145                   "Not Implemented");
146         setReason(HttpStatus.SC_BAD_GATEWAY,
147                   "Bad Gateway");
148         setReason(HttpStatus.SC_SERVICE_UNAVAILABLE,
149                   "Service Unavailable");
150 
151         // HTTP 1.1 Server status codes -- see RFC 2048
152         setReason(HttpStatus.SC_CONTINUE,
153                   "Continue");
154         setReason(HttpStatus.SC_TEMPORARY_REDIRECT,
155                   "Temporary Redirect");
156         setReason(HttpStatus.SC_METHOD_NOT_ALLOWED,
157                   "Method Not Allowed");
158         setReason(HttpStatus.SC_CONFLICT,
159                   "Conflict");
160         setReason(HttpStatus.SC_PRECONDITION_FAILED,
161                   "Precondition Failed");
162         setReason(HttpStatus.SC_REQUEST_TOO_LONG,
163                   "Request Too Long");
164         setReason(HttpStatus.SC_REQUEST_URI_TOO_LONG,
165                   "Request-URI Too Long");
166         setReason(HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE,
167                   "Unsupported Media Type");
168         setReason(HttpStatus.SC_MULTIPLE_CHOICES,
169                   "Multiple Choices");
170         setReason(HttpStatus.SC_SEE_OTHER,
171                   "See Other");
172         setReason(HttpStatus.SC_USE_PROXY,
173                   "Use Proxy");
174         setReason(HttpStatus.SC_PAYMENT_REQUIRED,
175                   "Payment Required");
176         setReason(HttpStatus.SC_NOT_ACCEPTABLE,
177                   "Not Acceptable");
178         setReason(HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED,
179                   "Proxy Authentication Required");
180         setReason(HttpStatus.SC_REQUEST_TIMEOUT,
181                   "Request Timeout");
182 
183         setReason(HttpStatus.SC_SWITCHING_PROTOCOLS,
184                   "Switching Protocols");
185         setReason(HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION,
186                   "Non Authoritative Information");
187         setReason(HttpStatus.SC_RESET_CONTENT,
188                   "Reset Content");
189         setReason(HttpStatus.SC_PARTIAL_CONTENT,
190                   "Partial Content");
191         setReason(HttpStatus.SC_GATEWAY_TIMEOUT,
192                   "Gateway Timeout");
193         setReason(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED,
194                   "Http Version Not Supported");
195         setReason(HttpStatus.SC_GONE,
196                   "Gone");
197         setReason(HttpStatus.SC_LENGTH_REQUIRED,
198                   "Length Required");
199         setReason(HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE,
200                   "Requested Range Not Satisfiable");
201         setReason(HttpStatus.SC_EXPECTATION_FAILED,
202                   "Expectation Failed");
203 
204         // WebDAV Server-specific status codes
205         setReason(HttpStatus.SC_PROCESSING,
206                   "Processing");
207         setReason(HttpStatus.SC_MULTI_STATUS,
208                   "Multi-Status");
209         setReason(HttpStatus.SC_UNPROCESSABLE_ENTITY,
210                   "Unprocessable Entity");
211         setReason(HttpStatus.SC_INSUFFICIENT_SPACE_ON_RESOURCE,
212                   "Insufficient Space On Resource");
213         setReason(HttpStatus.SC_METHOD_FAILURE,
214                   "Method Failure");
215         setReason(HttpStatus.SC_LOCKED,
216                   "Locked");
217         setReason(HttpStatus.SC_INSUFFICIENT_STORAGE,
218                   "Insufficient Storage");
219         setReason(HttpStatus.SC_FAILED_DEPENDENCY,
220                   "Failed Dependency");
221     }
222 
223 
224 }