~ubuntu-branches/ubuntu/trusty/httpcomponents-core/trusty

« back to all changes in this revision

Viewing changes to httpcore/src/main/java/org/apache/http/impl/EnglishReasonPhraseCatalog.java

  • Committer: Bazaar Package Importer
  • Author(s): David Paleino
  • Date: 2010-06-12 08:37:34 UTC
  • Revision ID: james.westby@ubuntu.com-20100612083734-1y8kp6qm4sjk60az
Tags: upstream-4.0.1
ImportĀ upstreamĀ versionĀ 4.0.1

Show diffs side-by-side

added added

removed removed

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