~ubuntu-branches/ubuntu/lucid/commons-httpclient/lucid

« back to all changes in this revision

Viewing changes to src/java/org/apache/commons/httpclient/HttpStatus.java

  • Committer: Bazaar Package Importer
  • Author(s): Barry Hawkins
  • Date: 2005-11-25 13:12:23 UTC
  • Revision ID: james.westby@ubuntu.com-20051125131223-2g7eyo21pqgrohpo
Tags: upstream-2.0.2
ImportĀ upstreamĀ versionĀ 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpStatus.java,v 1.15.2.2 2004/05/02 11:19:58 olegk Exp $
 
3
 * $Revision: 1.15.2.2 $
 
4
 * $Date: 2004/05/02 11:19:58 $
 
5
 *
 
6
 * ====================================================================
 
7
 *
 
8
 *  Copyright 1999-2004 The Apache Software Foundation
 
9
 *
 
10
 *  Licensed under the Apache License, Version 2.0 (the "License");
 
11
 *  you may not use this file except in compliance with the License.
 
12
 *  You may obtain a copy of the License at
 
13
 *
 
14
 *      http://www.apache.org/licenses/LICENSE-2.0
 
15
 *
 
16
 *  Unless required by applicable law or agreed to in writing, software
 
17
 *  distributed under the License is distributed on an "AS IS" BASIS,
 
18
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
19
 *  See the License for the specific language governing permissions and
 
20
 *  limitations under the License.
 
21
 * ====================================================================
 
22
 *
 
23
 * This software consists of voluntary contributions made by many
 
24
 * individuals on behalf of the Apache Software Foundation.  For more
 
25
 * information on the Apache Software Foundation, please see
 
26
 * <http://www.apache.org/>.
 
27
 *
 
28
 * [Additional notices, if required by prior licensing conditions]
 
29
 *
 
30
 */
 
31
 
 
32
package org.apache.commons.httpclient;
 
33
 
 
34
/**
 
35
 * Constants enumerating the HTTP status codes.
 
36
 * All status codes defined in RFC1945 (HTTP/1.0, RFC2616 (HTTP/1.1), and
 
37
 * RFC2518 (WebDAV) are supported.
 
38
 * 
 
39
 * @see StatusLine
 
40
 * @author Unascribed
 
41
 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
 
42
 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
 
43
 * 
 
44
 * TODO: Internationalization of reason phrases 
 
45
 * 
 
46
 * @version $Id: HttpStatus.java,v 1.15.2.2 2004/05/02 11:19:58 olegk Exp $
 
47
 */
 
48
public class HttpStatus {
 
49
 
 
50
 
 
51
    // -------------------------------------------------------- Class Variables
 
52
 
 
53
    /** Reason phrases lookup table. */
 
54
    private static final String[][] REASON_PHRASES = new String[][]{
 
55
        new String[0],
 
56
        new String[3],
 
57
        new String[8],
 
58
        new String[8],
 
59
        new String[25],
 
60
        new String[8]
 
61
    };
 
62
 
 
63
 
 
64
    // --------------------------------------------------------- Public Methods
 
65
 
 
66
    /**
 
67
     * Get the reason phrase for a particular status code.
 
68
     * 
 
69
     * This method always returns the English text as specified in the
 
70
     * relevent RFCs and is not internationalized.
 
71
     * 
 
72
     * @param statusCode the numeric status code
 
73
     * @return the reason phrase associated with the given status code
 
74
     * or null if the status code is not recognized.
 
75
     * 
 
76
     * TODO: getStatusText should be called getReasonPhrase to match RFC
 
77
     */
 
78
    public static String getStatusText(int statusCode) {
 
79
 
 
80
        if (statusCode < 0) {
 
81
            throw new IllegalArgumentException("status code may not be negative");
 
82
        }
 
83
        int classIndex = statusCode / 100;
 
84
        int codeIndex = statusCode - classIndex * 100;
 
85
        if (classIndex < 1 || classIndex > (REASON_PHRASES.length - 1) 
 
86
            || codeIndex < 0 || codeIndex > (REASON_PHRASES[classIndex].length - 1)) {
 
87
            return null;
 
88
        }
 
89
        return REASON_PHRASES[classIndex][codeIndex];
 
90
    }
 
91
 
 
92
 
 
93
    // -------------------------------------------------------- Private Methods
 
94
 
 
95
    /**
 
96
     * Store the given reason phrase, by status code.
 
97
     * @param statusCode The status code to lookup
 
98
     * @param reasonPhrase The reason phrase for this status code
 
99
     */
 
100
    private static void addStatusCodeMap(int statusCode, String reasonPhrase) {
 
101
        int classIndex = statusCode / 100;
 
102
        REASON_PHRASES[classIndex][statusCode - classIndex * 100] = reasonPhrase;
 
103
    }
 
104
 
 
105
 
 
106
    // -------------------------------------------------------------- Constants
 
107
 
 
108
    // --- 1xx Informational ---
 
109
 
 
110
    /** <tt>100 Continue</tt> (HTTP/1.1 - RFC 2616) */
 
111
    public static final int SC_CONTINUE = 100;
 
112
    /** <tt>101 Switching Protocols</tt> (HTTP/1.1 - RFC 2616)*/
 
113
    public static final int SC_SWITCHING_PROTOCOLS = 101;
 
114
    /** <tt>102 Processing</tt> (WebDAV - RFC 2518) */
 
115
    public static final int SC_PROCESSING = 102;
 
116
 
 
117
    // --- 2xx Success ---
 
118
 
 
119
    /** <tt>200 OK</tt> (HTTP/1.0 - RFC 1945) */
 
120
    public static final int SC_OK = 200;
 
121
    /** <tt>201 Created</tt> (HTTP/1.0 - RFC 1945) */
 
122
    public static final int SC_CREATED = 201;
 
123
    /** <tt>202 Accepted</tt> (HTTP/1.0 - RFC 1945) */
 
124
    public static final int SC_ACCEPTED = 202;
 
125
    /** <tt>203 Non Authoritative Information</tt> (HTTP/1.1 - RFC 2616) */
 
126
    public static final int SC_NON_AUTHORITATIVE_INFORMATION = 203;
 
127
    /** <tt>204 No Content</tt> (HTTP/1.0 - RFC 1945) */
 
128
    public static final int SC_NO_CONTENT = 204;
 
129
    /** <tt>205 Reset Content</tt> (HTTP/1.1 - RFC 2616) */
 
130
    public static final int SC_RESET_CONTENT = 205;
 
131
    /** <tt>206 Partial Content</tt> (HTTP/1.1 - RFC 2616) */
 
132
    public static final int SC_PARTIAL_CONTENT = 206;
 
133
    /** 
 
134
     * <tt>207 Multi-Status</tt> (WebDAV - RFC 2518) or <tt>207 Partial Update
 
135
     * OK</tt> (HTTP/1.1 - draft-ietf-http-v11-spec-rev-01?)
 
136
     */
 
137
    public static final int SC_MULTI_STATUS = 207;
 
138
 
 
139
    // --- 3xx Redirection ---
 
140
 
 
141
    /** <tt>300 Mutliple Choices</tt> (HTTP/1.1 - RFC 2616) */
 
142
    public static final int SC_MULTIPLE_CHOICES = 300;
 
143
    /** <tt>301 Moved Permanently</tt> (HTTP/1.0 - RFC 1945) */
 
144
    public static final int SC_MOVED_PERMANENTLY = 301;
 
145
    /** <tt>302 Moved Temporarily</tt> (Sometimes <tt>Found</tt>) (HTTP/1.0 - RFC 1945) */
 
146
    public static final int SC_MOVED_TEMPORARILY = 302;
 
147
    /** <tt>303 See Other</tt> (HTTP/1.1 - RFC 2616) */
 
148
    public static final int SC_SEE_OTHER = 303;
 
149
    /** <tt>304 Not Modified</tt> (HTTP/1.0 - RFC 1945) */
 
150
    public static final int SC_NOT_MODIFIED = 304;
 
151
    /** <tt>305 Use Proxy</tt> (HTTP/1.1 - RFC 2616) */
 
152
    public static final int SC_USE_PROXY = 305;
 
153
    /** <tt>307 Temporary Redirect</tt> (HTTP/1.1 - RFC 2616) */
 
154
    public static final int SC_TEMPORARY_REDIRECT = 307;
 
155
 
 
156
    // --- 4xx Client Error ---
 
157
 
 
158
    /** <tt>400 Bad Request</tt> (HTTP/1.1 - RFC 2616) */
 
159
    public static final int SC_BAD_REQUEST = 400;
 
160
    /** <tt>401 Unauthorized</tt> (HTTP/1.0 - RFC 1945) */
 
161
    public static final int SC_UNAUTHORIZED = 401;
 
162
    /** <tt>402 Payment Required</tt> (HTTP/1.1 - RFC 2616) */
 
163
    public static final int SC_PAYMENT_REQUIRED = 402;
 
164
    /** <tt>403 Forbidden</tt> (HTTP/1.0 - RFC 1945) */
 
165
    public static final int SC_FORBIDDEN = 403;
 
166
    /** <tt>404 Not Found</tt> (HTTP/1.0 - RFC 1945) */
 
167
    public static final int SC_NOT_FOUND = 404;
 
168
    /** <tt>405 Method Not Allowed</tt> (HTTP/1.1 - RFC 2616) */
 
169
    public static final int SC_METHOD_NOT_ALLOWED = 405;
 
170
    /** <tt>406 Not Acceptable</tt> (HTTP/1.1 - RFC 2616) */
 
171
    public static final int SC_NOT_ACCEPTABLE = 406;
 
172
    /** <tt>407 Proxy Authentication Required</tt> (HTTP/1.1 - RFC 2616)*/
 
173
    public static final int SC_PROXY_AUTHENTICATION_REQUIRED = 407;
 
174
    /** <tt>408 Request Timeout</tt> (HTTP/1.1 - RFC 2616) */
 
175
    public static final int SC_REQUEST_TIMEOUT = 408;
 
176
    /** <tt>409 Conflict</tt> (HTTP/1.1 - RFC 2616) */
 
177
    public static final int SC_CONFLICT = 409;
 
178
    /** <tt>410 Gone</tt> (HTTP/1.1 - RFC 2616) */
 
179
    public static final int SC_GONE = 410;
 
180
    /** <tt>411 Length Required</tt> (HTTP/1.1 - RFC 2616) */
 
181
    public static final int SC_LENGTH_REQUIRED = 411;
 
182
    /** <tt>412 Precondition Failed</tt> (HTTP/1.1 - RFC 2616) */
 
183
    public static final int SC_PRECONDITION_FAILED = 412;
 
184
    /** <tt>413 Request Entity Too Large</tt> (HTTP/1.1 - RFC 2616) */
 
185
    public static final int SC_REQUEST_TOO_LONG = 413;
 
186
    /** <tt>414 Request-URI Too Long</tt> (HTTP/1.1 - RFC 2616) */
 
187
    public static final int SC_REQUEST_URI_TOO_LONG = 414;
 
188
    /** <tt>415 Unsupported Media Type</tt> (HTTP/1.1 - RFC 2616) */
 
189
    public static final int SC_UNSUPPORTED_MEDIA_TYPE = 415;
 
190
    /** <tt>416 Requested Range Not Satisfiable</tt> (HTTP/1.1 - RFC 2616) */
 
191
    public static final int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
 
192
    /** <tt>417 Expectation Failed</tt> (HTTP/1.1 - RFC 2616) */
 
193
    public static final int SC_EXPECTATION_FAILED = 417;
 
194
 
 
195
    /**
 
196
     * Static constant for a 418 error.
 
197
     * <tt>418 Unprocessable Entity</tt> (WebDAV drafts?)
 
198
     * or <tt>418 Reauthentication Required</tt> (HTTP/1.1 drafts?)
 
199
     */
 
200
    // not used
 
201
    // public static final int SC_UNPROCESSABLE_ENTITY = 418;
 
202
 
 
203
    /**
 
204
     * Static constant for a 419 error.
 
205
     * <tt>419 Insufficient Space on Resource</tt>
 
206
     * (WebDAV - draft-ietf-webdav-protocol-05?)
 
207
     * or <tt>419 Proxy Reauthentication Required</tt>
 
208
     * (HTTP/1.1 drafts?)
 
209
     */
 
210
    public static final int SC_INSUFFICIENT_SPACE_ON_RESOURCE = 419;
 
211
    /**
 
212
     * Static constant for a 420 error.
 
213
     * <tt>420 Method Failure</tt>
 
214
     * (WebDAV - draft-ietf-webdav-protocol-05?)
 
215
     */
 
216
    public static final int SC_METHOD_FAILURE = 420;
 
217
    /** <tt>422 Unprocessable Entity</tt> (WebDAV - RFC 2518) */
 
218
    public static final int SC_UNPROCESSABLE_ENTITY = 422;
 
219
    /** <tt>423 Locked</tt> (WebDAV - RFC 2518) */
 
220
    public static final int SC_LOCKED = 423;
 
221
    /** <tt>424 Failed Dependency</tt> (WebDAV - RFC 2518) */
 
222
    public static final int SC_FAILED_DEPENDENCY = 424;
 
223
 
 
224
    // --- 5xx Server Error ---
 
225
 
 
226
    /** <tt>500 Server Error</tt> (HTTP/1.0 - RFC 1945) */
 
227
    public static final int SC_INTERNAL_SERVER_ERROR = 500;
 
228
    /** <tt>501 Not Implemented</tt> (HTTP/1.0 - RFC 1945) */
 
229
    public static final int SC_NOT_IMPLEMENTED = 501;
 
230
    /** <tt>502 Bad Gateway</tt> (HTTP/1.0 - RFC 1945) */
 
231
    public static final int SC_BAD_GATEWAY = 502;
 
232
    /** <tt>503 Service Unavailable</tt> (HTTP/1.0 - RFC 1945) */
 
233
    public static final int SC_SERVICE_UNAVAILABLE = 503;
 
234
    /** <tt>504 Gateway Timeout</tt> (HTTP/1.1 - RFC 2616) */
 
235
    public static final int SC_GATEWAY_TIMEOUT = 504;
 
236
    /** <tt>505 HTTP Version Not Supported</tt> (HTTP/1.1 - RFC 2616) */
 
237
    public static final int SC_HTTP_VERSION_NOT_SUPPORTED = 505;
 
238
 
 
239
    /** <tt>507 Insufficient Storage</tt> (WebDAV - RFC 2518) */
 
240
    public static final int SC_INSUFFICIENT_STORAGE = 507;
 
241
 
 
242
 
 
243
    // ----------------------------------------------------- Static Initializer
 
244
 
 
245
    /** Set up status code to "reason phrase" map. */
 
246
    static {
 
247
        // HTTP 1.0 Server status codes -- see RFC 1945
 
248
        addStatusCodeMap(SC_OK, "OK");
 
249
        addStatusCodeMap(SC_CREATED, "Created");
 
250
        addStatusCodeMap(SC_ACCEPTED, "Accepted");
 
251
        addStatusCodeMap(SC_NO_CONTENT, "No Content");
 
252
        addStatusCodeMap(SC_MOVED_PERMANENTLY, "Moved Permanently");
 
253
        addStatusCodeMap(SC_MOVED_TEMPORARILY, "Moved Temporarily");
 
254
        addStatusCodeMap(SC_NOT_MODIFIED, "Not Modified");
 
255
        addStatusCodeMap(SC_BAD_REQUEST, "Bad Request");
 
256
        addStatusCodeMap(SC_UNAUTHORIZED, "Unauthorized");
 
257
        addStatusCodeMap(SC_FORBIDDEN, "Forbidden");
 
258
        addStatusCodeMap(SC_NOT_FOUND, "Not Found");
 
259
        addStatusCodeMap(SC_INTERNAL_SERVER_ERROR, "Internal Server Error");
 
260
        addStatusCodeMap(SC_NOT_IMPLEMENTED, "Not Implemented");
 
261
        addStatusCodeMap(SC_BAD_GATEWAY, "Bad Gateway");
 
262
        addStatusCodeMap(SC_SERVICE_UNAVAILABLE, "Service Unavailable");
 
263
 
 
264
        // HTTP 1.1 Server status codes -- see RFC 2048
 
265
        addStatusCodeMap(SC_CONTINUE, "Continue");
 
266
        addStatusCodeMap(SC_TEMPORARY_REDIRECT, "Temporary Redirect");
 
267
        addStatusCodeMap(SC_METHOD_NOT_ALLOWED, "Method Not Allowed");
 
268
        addStatusCodeMap(SC_CONFLICT, "Conflict");
 
269
        addStatusCodeMap(SC_PRECONDITION_FAILED, "Precondition Failed");
 
270
        addStatusCodeMap(SC_REQUEST_TOO_LONG, "Request Too Long");
 
271
        addStatusCodeMap(SC_REQUEST_URI_TOO_LONG, "Request-URI Too Long");
 
272
        addStatusCodeMap(SC_UNSUPPORTED_MEDIA_TYPE, "Unsupported Media Type");
 
273
        addStatusCodeMap(SC_MULTIPLE_CHOICES, "Multiple Choices");
 
274
        addStatusCodeMap(SC_SEE_OTHER, "See Other");
 
275
        addStatusCodeMap(SC_USE_PROXY, "Use Proxy");
 
276
        addStatusCodeMap(SC_PAYMENT_REQUIRED, "Payment Required");
 
277
        addStatusCodeMap(SC_NOT_ACCEPTABLE, "Not Acceptable");
 
278
        addStatusCodeMap(SC_PROXY_AUTHENTICATION_REQUIRED, 
 
279
            "Proxy Authentication Required");
 
280
        addStatusCodeMap(SC_REQUEST_TIMEOUT, 
 
281
            "Request Timeout");
 
282
 
 
283
        addStatusCodeMap(SC_SWITCHING_PROTOCOLS, "Switching Protocols");
 
284
        addStatusCodeMap(SC_NON_AUTHORITATIVE_INFORMATION,
 
285
                         "Non Authoritative Information");
 
286
        addStatusCodeMap(SC_RESET_CONTENT, "Reset Content");
 
287
        addStatusCodeMap(SC_PARTIAL_CONTENT, "Partial Content");
 
288
        addStatusCodeMap(SC_GATEWAY_TIMEOUT, "Gateway Timeout");
 
289
        addStatusCodeMap(SC_HTTP_VERSION_NOT_SUPPORTED,
 
290
                         "Http Version Not Supported");
 
291
        addStatusCodeMap(SC_GONE,
 
292
                         "Gone");
 
293
        addStatusCodeMap(SC_LENGTH_REQUIRED,
 
294
                         "Length Required");
 
295
        addStatusCodeMap(SC_REQUESTED_RANGE_NOT_SATISFIABLE,
 
296
                         "Requested Range Not Satisfiable");
 
297
        addStatusCodeMap(SC_EXPECTATION_FAILED,
 
298
                         "Expectation Failed");
 
299
 
 
300
        // WebDAV Server-specific status codes
 
301
        addStatusCodeMap(SC_PROCESSING, "Processing");
 
302
        addStatusCodeMap(SC_MULTI_STATUS, "Multi-Status");
 
303
        addStatusCodeMap(SC_UNPROCESSABLE_ENTITY, "Unprocessable Entity");
 
304
        addStatusCodeMap(SC_INSUFFICIENT_SPACE_ON_RESOURCE,
 
305
                         "Insufficient Space On Resource");
 
306
        addStatusCodeMap(SC_METHOD_FAILURE, "Method Failure");
 
307
        addStatusCodeMap(SC_LOCKED, "Locked");
 
308
        addStatusCodeMap(SC_INSUFFICIENT_STORAGE , "Insufficient Storage");
 
309
        addStatusCodeMap(SC_FAILED_DEPENDENCY, "Failed Dependency");
 
310
    }
 
311
 
 
312
 
 
313
}