1 /*
2  * JBoss, Home of Professional Open Source.
3  * Copyright 2014 Red Hat, Inc., and individual contributors
4  * as indicated by the @author tags.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */

18
19 package io.undertow.util;
20
21 /**
22  * @author Stuart Douglas
23  */

24 public class StatusCodes {
25
26     //chosen simply because it gives no collisions
27     //if more codes are added this will need to be re-evaluated
28     private static final int SIZE = 0x2df;
29     private static final Entry[] TABLE = new Entry[SIZE];
30
31     public static final int CONTINUE = 100;
32     public static final int SWITCHING_PROTOCOLS = 101;
33     public static final int PROCESSING = 102;
34     public static final int OK = 200;
35     public static final int CREATED = 201;
36     public static final int ACCEPTED = 202;
37     public static final int NON_AUTHORITATIVE_INFORMATION = 203;
38     public static final int NO_CONTENT = 204;
39     public static final int RESET_CONTENT = 205;
40     public static final int PARTIAL_CONTENT = 206;
41     public static final int MULTI_STATUS = 207;
42     public static final int ALREADY_REPORTED = 208;
43     public static final int IM_USED = 226;
44     public static final int MULTIPLE_CHOICES = 300;
45     public static final int MOVED_PERMANENTLY = 301;
46     @Deprecated //typo, but left in for now due to backwards compat
47     public static final int MOVED_PERMENANTLY = MOVED_PERMANENTLY;
48     public static final int FOUND = 302;
49     public static final int SEE_OTHER = 303;
50     public static final int NOT_MODIFIED = 304;
51     public static final int USE_PROXY = 305;
52     public static final int TEMPORARY_REDIRECT = 307;
53     public static final int PERMANENT_REDIRECT = 308;
54     public static final int BAD_REQUEST = 400;
55     public static final int UNAUTHORIZED = 401;
56     public static final int PAYMENT_REQUIRED = 402;
57     public static final int FORBIDDEN = 403;
58     public static final int NOT_FOUND = 404;
59     public static final int METHOD_NOT_ALLOWED = 405;
60     public static final int NOT_ACCEPTABLE = 406;
61     public static final int PROXY_AUTHENTICATION_REQUIRED = 407;
62     public static final int REQUEST_TIME_OUT = 408;
63     public static final int CONFLICT = 409;
64     public static final int GONE = 410;
65     public static final int LENGTH_REQUIRED = 411;
66     public static final int PRECONDITION_FAILED = 412;
67     public static final int REQUEST_ENTITY_TOO_LARGE = 413;
68     public static final int REQUEST_URI_TOO_LARGE = 414;
69     public static final int UNSUPPORTED_MEDIA_TYPE = 415;
70     public static final int REQUEST_RANGE_NOT_SATISFIABLE = 416;
71     public static final int EXPECTATION_FAILED = 417;
72     public static final int UNPROCESSABLE_ENTITY = 422;
73     public static final int LOCKED = 423;
74     public static final int FAILED_DEPENDENCY = 424;
75     public static final int UPGRADE_REQUIRED = 426;
76     public static final int PRECONDITION_REQUIRED = 428;
77     public static final int TOO_MANY_REQUESTS = 429;
78     public static final int REQUEST_HEADER_FIELDS_TOO_LARGE = 431;
79     public static final int INTERNAL_SERVER_ERROR = 500;
80     public static final int NOT_IMPLEMENTED = 501;
81     public static final int BAD_GATEWAY = 502;
82     public static final int SERVICE_UNAVAILABLE = 503;
83     public static final int GATEWAY_TIME_OUT = 504;
84     public static final int HTTP_VERSION_NOT_SUPPORTED = 505;
85     public static final int INSUFFICIENT_STORAGE = 507;
86     public static final int LOOP_DETECTED = 508;
87     public static final int NOT_EXTENDED = 510;
88     public static final int NETWORK_AUTHENTICATION_REQUIRED = 511;
89
90     public static final String CONTINUE_STRING = "Continue";
91     public static final String SWITCHING_PROTOCOLS_STRING = "Switching Protocols";
92     public static final String PROCESSING_STRING = "Processing";
93     public static final String OK_STRING = "OK";
94     public static final String CREATED_STRING = "Created";
95     public static final String ACCEPTED_STRING = "Accepted";
96     public static final String NON_AUTHORITATIVE_INFORMATION_STRING = "Non-Authoritative Information";
97     public static final String NO_CONTENT_STRING = "No Content";
98     public static final String RESET_CONTENT_STRING = "Reset Content";
99     public static final String PARTIAL_CONTENT_STRING = "Partial Content";
100     public static final String MULTI_STATUS_STRING = "Multi-Status";
101     public static final String ALREADY_REPORTED_STRING = "Already Reported";
102     public static final String IM_USED_STRING = "IM Used";
103     public static final String MULTIPLE_CHOICES_STRING = "Multiple Choices";
104     public static final String MOVED_PERMANENTLY_STRING = "Moved Permanently";
105     public static final String FOUND_STRING = "Found";
106     public static final String SEE_OTHER_STRING = "See Other";
107     public static final String NOT_MODIFIED_STRING = "Not Modified";
108     public static final String USE_PROXY_STRING = "Use Proxy";
109     public static final String TEMPORARY_REDIRECT_STRING = "Temporary Redirect";
110     public static final String PERMANENT_REDIRECT_STRING = "Permanent Redirect";
111     public static final String BAD_REQUEST_STRING = "Bad Request";
112     public static final String UNAUTHORIZED_STRING = "Unauthorized";
113     public static final String PAYMENT_REQUIRED_STRING = "Payment Required";
114     public static final String FORBIDDEN_STRING = "Forbidden";
115     public static final String NOT_FOUND_STRING = "Not Found";
116     public static final String METHOD_NOT_ALLOWED_STRING = "Method Not Allowed";
117     public static final String NOT_ACCEPTABLE_STRING = "Not Acceptable";
118     public static final String PROXY_AUTHENTICATION_REQUIRED_STRING = "Proxy Authentication Required";
119     public static final String REQUEST_TIME_OUT_STRING = "Request Time-out";
120     public static final String CONFLICT_STRING = "Conflict";
121     public static final String GONE_STRING = "Gone";
122     public static final String LENGTH_REQUIRED_STRING = "Length Required";
123     public static final String PRECONDITION_FAILED_STRING = "Precondition Failed";
124     public static final String REQUEST_ENTITY_TOO_LARGE_STRING = "Request Entity Too Large";
125     public static final String REQUEST_URI_TOO_LARGE_STRING = "Request-URI Too Large";
126     public static final String UNSUPPORTED_MEDIA_TYPE_STRING = "Unsupported Media Type";
127     public static final String REQUEST_RANGE_NOT_SATISFIABLE_STRING = "Requested range not satisfiable";
128     public static final String EXPECTATION_FAILED_STRING = "Expectation Failed";
129     public static final String UNPROCESSABLE_ENTITY_STRING = "Unprocessable Entity";
130     public static final String LOCKED_STRING = "Locked";
131     public static final String FAILED_DEPENDENCY_STRING = "Failed Dependency";
132     public static final String UPGRADE_REQUIRED_STRING = "Upgrade Required";
133     public static final String PRECONDITION_REQUIRED_STRING = "Precondition Required";
134     public static final String TOO_MANY_REQUESTS_STRING = "Too Many Requests";
135     public static final String REQUEST_HEADER_FIELDS_TOO_LARGE_STRING = "Request Header Fields Too Large";
136     public static final String INTERNAL_SERVER_ERROR_STRING = "Internal Server Error";
137     public static final String NOT_IMPLEMENTED_STRING = "Not Implemented";
138     public static final String BAD_GATEWAY_STRING = "Bad Gateway";
139     public static final String SERVICE_UNAVAILABLE_STRING = "Service Unavailable";
140     public static final String GATEWAY_TIME_OUT_STRING = "Gateway Time-out";
141     public static final String HTTP_VERSION_NOT_SUPPORTED_STRING = "HTTP Version not supported";
142     public static final String INSUFFICIENT_STORAGE_STRING = "Insufficient Storage";
143     public static final String LOOP_DETECTED_STRING = "Loop Detected";
144     public static final String NOT_EXTENDED_STRING = "Not Extended";
145     public static final String NETWORK_AUTHENTICATION_REQUIRED_STRING = "Network Authentication Required";
146
147     static {
148         putCode(CONTINUE, CONTINUE_STRING);
149         putCode(SWITCHING_PROTOCOLS, SWITCHING_PROTOCOLS_STRING);
150         putCode(PROCESSING, PROCESSING_STRING);
151         putCode(OK, OK_STRING);
152         putCode(CREATED, CREATED_STRING);
153         putCode(ACCEPTED, ACCEPTED_STRING);
154         putCode(NON_AUTHORITATIVE_INFORMATION, NON_AUTHORITATIVE_INFORMATION_STRING);
155         putCode(NO_CONTENT, NO_CONTENT_STRING);
156         putCode(RESET_CONTENT, RESET_CONTENT_STRING);
157         putCode(PARTIAL_CONTENT, PARTIAL_CONTENT_STRING);
158         putCode(MULTI_STATUS, MULTI_STATUS_STRING);
159         putCode(ALREADY_REPORTED, ALREADY_REPORTED_STRING);
160         putCode(IM_USED, IM_USED_STRING);
161         putCode(MULTIPLE_CHOICES, MULTIPLE_CHOICES_STRING);
162         putCode(MOVED_PERMANENTLY, MOVED_PERMANENTLY_STRING);
163         putCode(FOUND, FOUND_STRING);
164         putCode(SEE_OTHER, SEE_OTHER_STRING);
165         putCode(NOT_MODIFIED, NOT_MODIFIED_STRING);
166         putCode(USE_PROXY, USE_PROXY_STRING);
167         putCode(TEMPORARY_REDIRECT, TEMPORARY_REDIRECT_STRING);
168         putCode(PERMANENT_REDIRECT, PERMANENT_REDIRECT_STRING);
169         putCode(BAD_REQUEST, BAD_REQUEST_STRING);
170         putCode(UNAUTHORIZED, UNAUTHORIZED_STRING);
171         putCode(PAYMENT_REQUIRED, PAYMENT_REQUIRED_STRING);
172         putCode(FORBIDDEN, FORBIDDEN_STRING);
173         putCode(NOT_FOUND, NOT_FOUND_STRING);
174         putCode(METHOD_NOT_ALLOWED, METHOD_NOT_ALLOWED_STRING);
175         putCode(NOT_ACCEPTABLE, NOT_ACCEPTABLE_STRING);
176         putCode(PROXY_AUTHENTICATION_REQUIRED, PROXY_AUTHENTICATION_REQUIRED_STRING);
177         putCode(REQUEST_TIME_OUT, REQUEST_TIME_OUT_STRING);
178         putCode(CONFLICT, CONFLICT_STRING);
179         putCode(GONE, GONE_STRING);
180         putCode(LENGTH_REQUIRED, LENGTH_REQUIRED_STRING);
181         putCode(PRECONDITION_FAILED, PRECONDITION_FAILED_STRING);
182         putCode(REQUEST_ENTITY_TOO_LARGE, REQUEST_ENTITY_TOO_LARGE_STRING);
183         putCode(REQUEST_URI_TOO_LARGE, REQUEST_URI_TOO_LARGE_STRING);
184         putCode(UNSUPPORTED_MEDIA_TYPE, UNSUPPORTED_MEDIA_TYPE_STRING);
185         putCode(REQUEST_RANGE_NOT_SATISFIABLE, REQUEST_RANGE_NOT_SATISFIABLE_STRING);
186         putCode(EXPECTATION_FAILED, EXPECTATION_FAILED_STRING);
187         putCode(UNPROCESSABLE_ENTITY, UNPROCESSABLE_ENTITY_STRING);
188         putCode(LOCKED, LOCKED_STRING);
189         putCode(FAILED_DEPENDENCY, FAILED_DEPENDENCY_STRING);
190         putCode(UPGRADE_REQUIRED, UPGRADE_REQUIRED_STRING);
191         putCode(PRECONDITION_REQUIRED, PRECONDITION_REQUIRED_STRING);
192         putCode(TOO_MANY_REQUESTS, TOO_MANY_REQUESTS_STRING);
193         putCode(REQUEST_HEADER_FIELDS_TOO_LARGE, REQUEST_HEADER_FIELDS_TOO_LARGE_STRING);
194         putCode(INTERNAL_SERVER_ERROR, INTERNAL_SERVER_ERROR_STRING);
195         putCode(NOT_IMPLEMENTED, NOT_IMPLEMENTED_STRING);
196         putCode(BAD_GATEWAY, BAD_GATEWAY_STRING);
197         putCode(SERVICE_UNAVAILABLE, SERVICE_UNAVAILABLE_STRING);
198         putCode(GATEWAY_TIME_OUT, GATEWAY_TIME_OUT_STRING);
199         putCode(HTTP_VERSION_NOT_SUPPORTED, HTTP_VERSION_NOT_SUPPORTED_STRING);
200         putCode(INSUFFICIENT_STORAGE, INSUFFICIENT_STORAGE_STRING);
201         putCode(LOOP_DETECTED, LOOP_DETECTED_STRING);
202         putCode(NOT_EXTENDED, NOT_EXTENDED_STRING);
203         putCode(NETWORK_AUTHENTICATION_REQUIRED, NETWORK_AUTHENTICATION_REQUIRED_STRING);
204
205     }
206
207     private static void putCode(int code, String reason) {
208         Entry e = new Entry(reason, code);
209         int h = code & SIZE;
210         if(TABLE[h] != null) {
211             throw new IllegalArgumentException("hash collision");
212         }
213         TABLE[h] = e;
214     }
215
216     private StatusCodes() {
217     }
218
219     public static final String getReason(final int code) {
220         final int hash = code & SIZE;
221         if (hash == SIZE) {
222             return "Unknown";
223         }
224         final Entry result = TABLE[hash];
225         if (result == null || result.code != code) {
226             return "Unknown";
227         } else {
228             return result.reason;
229         }
230     }
231
232     private static final class Entry {
233         final String reason;
234         final int code;
235
236         private Entry(final String reason, final int code) {
237             this.reason = reason;
238             this.code = code;
239         }
240     }
241 }
242