2 * Copyright (c) 2024 PANTHEON.tech, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.restconf.api;
10 import com.google.common.annotations.Beta;
11 import org.eclipse.jdt.annotation.NonNullByDefault;
12 import org.eclipse.jdt.annotation.Nullable;
15 * A simple DTO definitiong an <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15">HTTP Status Code</a>. Integer
16 * values used here are assigned through the
17 * <a href="https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml">IANA Status Code Registry</a>.
21 public final class HttpStatusCode {
23 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.3.1">200 OK</a>.
25 public static final HttpStatusCode OK = new HttpStatusCode(200, "OK");
27 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.3.2">201 Created</a>.
29 public static final HttpStatusCode CREATED = new HttpStatusCode(201, "Created");
31 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.3.3">202 Accepted</a>.
33 public static final HttpStatusCode ACCEPTED = new HttpStatusCode(202, "Accepted");
35 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.3.4">203 Non-Authoritative Information</a>.
37 public static final HttpStatusCode NON_AUTHORITATIVE_INFORMATION =
38 new HttpStatusCode(203, "Non-Authoritative Information");
40 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.3.5">204 No Content</a>.
42 public static final HttpStatusCode NO_CONTENT = new HttpStatusCode(204, "No Content");
44 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.3.6">205 Reset Content</a>.
46 public static final HttpStatusCode RESET_CONTENT = new HttpStatusCode(205, "Reset Content");
48 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.3.7">206 Partial Content</a>.
50 public static final HttpStatusCode PARTIAL_CONTENT = new HttpStatusCode(206, "Partial Content");
52 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.4.1">300 Multiple Choices</a>.
54 public static final HttpStatusCode MULTIPLE_CHOICES = new HttpStatusCode(300, "Multiple Choices");
56 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.4.2">301 Moved Permanently</a>.
58 public static final HttpStatusCode MOVED_PERMANENTLY = new HttpStatusCode(301, "Moved Permanently");
60 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.4.3">302 Found</a>.
62 public static final HttpStatusCode FOUND = new HttpStatusCode(302, "Found");
64 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.4.4">303 See Other</a>.
66 public static final HttpStatusCode SEE_OTHER = new HttpStatusCode(303, "See Other");
68 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.4.5">304 Not Modified</a>.
70 public static final HttpStatusCode NOT_MODIFIED = new HttpStatusCode(304, "Not Modified");
72 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.4.6">305 Use Proxy</a>.
74 public static final HttpStatusCode USE_PROXY = new HttpStatusCode(305, "Use Proxy");
76 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.4.8">307 Temporary Redirect</a>.
78 public static final HttpStatusCode TEMPORARY_REDIRECT = new HttpStatusCode(307, "Temporary Redirect");
80 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.4.9">308 Permanent Redirect</a>.
82 public static final HttpStatusCode PERMANENT_REDIRECT = new HttpStatusCode(308, "Permanent Redirect");
84 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.5.1">400 Bad Request</a>.
86 public static final HttpStatusCode BAD_REQUEST = new HttpStatusCode(400, "Bad Request");
88 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.5.2">401 Unauthorized</a>.
90 public static final HttpStatusCode UNAUTHORIZED = new HttpStatusCode(401, "Unauthorized");
92 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.5.3">402 Payment Required</a>.
94 public static final HttpStatusCode PAYMENT_REQUIRED = new HttpStatusCode(402, "Payment Required");
96 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.5.4">403 Forbidden</a>.
98 public static final HttpStatusCode FORBIDDEN = new HttpStatusCode(403, "Forbidden");
100 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.5.5">404 Not Found</a>.
102 public static final HttpStatusCode NOT_FOUND = new HttpStatusCode(404, "Not Found");
104 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.5.6">405 Method Not Allowed</a>.
106 public static final HttpStatusCode METHOD_NOT_ALLOWED = new HttpStatusCode(405, "Method Not Allowed");
108 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.5.7">406 Not Acceptable</a>.
110 public static final HttpStatusCode NOT_ACCEPTABLE = new HttpStatusCode(406, "Not Acceptable");
112 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.5.8">407 Proxy Authentication Required</a>.
114 public static final HttpStatusCode PROXY_AUTHENTICATION_REQUIRED =
115 new HttpStatusCode(407, "Proxy Authentication Required");
117 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.5.9">408 Request Timeout</a>.
119 public static final HttpStatusCode REQUEST_TIMEOUT = new HttpStatusCode(408, "Request Timeout");
121 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.5.10">409 Conflict</a>.
123 public static final HttpStatusCode CONFLICT = new HttpStatusCode(409, "Conflict");
125 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.5.11">410 Gone</a>.
127 public static final HttpStatusCode GONE = new HttpStatusCode(410, "Gone");
129 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.5.12">411 Length Required</a>.
131 public static final HttpStatusCode LENGTH_REQUIRED = new HttpStatusCode(411, "Length Required");
133 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.5.13">412 Precondition Failed</a>.
135 public static final HttpStatusCode PRECONDITION_FAILED = new HttpStatusCode(412, "Precondition Failed");
137 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.5.14">413 Content Too Large</a>.
139 public static final HttpStatusCode CONTENT_TOO_LARGE = new HttpStatusCode(413, "Content Too Large");
141 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.5.15">414 Content Too Long</a>.
143 public static final HttpStatusCode URI_TOO_LONG = new HttpStatusCode(414, "URI Too Long");
145 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.5.16">415 Unsupported Media Type</a>.
147 public static final HttpStatusCode UNSUPPORTED_MEDIA_TYPE = new HttpStatusCode(415, "Unsupported Media Type");
149 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.5.17">416 Requested Range Not Satisfiable</a>.
151 public static final HttpStatusCode REQUESTED_RANGE_NOT_SATISFIABLE =
152 new HttpStatusCode(416, "Requested Range Not Satisfiable");
154 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.5.18">417 Expectation Failed</a>.
156 public static final HttpStatusCode EXPECTATION_FAILED = new HttpStatusCode(417, "Expectation Failed");
158 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.5.19">418 (Unused)</a>.
160 @Deprecated(forRemoval = true)
161 public static final HttpStatusCode I_M_A_TEAPOT = new HttpStatusCode(418, "I'm a teapot");
163 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.5.20">421 Misdirected Request</a>.
165 public static final HttpStatusCode MISDIRECTED_REQUEST = new HttpStatusCode(421, "Misdirected Request");
167 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.5.21">422 Unprocessable Content</a>.
169 public static final HttpStatusCode UNPROCESSABLE_CONTENT = new HttpStatusCode(422, "Unprocessable Content");
171 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.5.22">426 Upgrade Required</a>.
173 public static final HttpStatusCode UPGRADE_REQUIRED = new HttpStatusCode(426, "Upgrade Required");
175 * <a href="https://www.rfc-editor.org/rfc/rfc6585#section-3">428 Precondition Required</a>.
177 public static final HttpStatusCode PRECONDITION_REQUIRED = new HttpStatusCode(428, "Precondition Required");
179 * <a href="https://www.rfc-editor.org/rfc/rfc6585#section-4">429 Too Many Requests</a>.
181 public static final HttpStatusCode TOO_MANY_REQUESTS = new HttpStatusCode(429, "Too Many Requests");
183 * <a href="https://www.rfc-editor.org/rfc/rfc6585#section-5">431 Request Header Fields Too Large</a>.
185 public static final HttpStatusCode REQUEST_HEADER_FIELDS_TOO_LARGE =
186 new HttpStatusCode(431, "Request Header Fields Too Large");
188 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.6.1">500 Internal Server Error</a>.
190 public static final HttpStatusCode INTERNAL_SERVER_ERROR = new HttpStatusCode(500, "Internal Server Error");
192 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.6.2">501 Not Implemented</a>.
194 public static final HttpStatusCode NOT_IMPLEMENTED = new HttpStatusCode(501, "Not Implemented");
196 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.6.3">502 Bad Gateway</a>.
198 public static final HttpStatusCode BAD_GATEWAY = new HttpStatusCode(502, "Bad Gateway");
200 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.6.4">503 Service Unavailable</a>.
202 public static final HttpStatusCode SERVICE_UNAVAILABLE = new HttpStatusCode(503, "Service Unavailable");
204 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.6.5">504 Gateway Timeout</a>.
206 public static final HttpStatusCode GATEWAY_TIMEOUT = new HttpStatusCode(504, "Gateway Timeout");
208 * <a href="https://www.rfc-editor.org/rfc/rfc9110#section-15.6.6">505 HTTP Version Not Supported</a>.
210 public static final HttpStatusCode HTTP_VERSION_NOT_SUPPORTED =
211 new HttpStatusCode(505, "HTTP Version Not Supported");
213 * <a href="https://www.rfc-editor.org/rfc/rfc6585#section-6">511 Network Authentication Required</a>.
215 public static final HttpStatusCode NETWORK_AUTHENTICATION_REQUIRED =
216 new HttpStatusCode(511, "Network Authentication Required");
218 private final int code;
219 private final String phrase;
221 public HttpStatusCode(final int code, final @Nullable String phrase) {
222 if (code < 100 || code > 599) {
223 throw new IllegalArgumentException("Invalid statusCode " + code);
226 this.phrase = phrase;
230 * Returns the HTTP status code, {@code 100-599}.
232 * @return the HTTP status code
239 * Returns the phrase or {@code null}.
241 * @return the phrase or {@code null}
243 public @Nullable String phrase() {
248 public int hashCode() {
253 public boolean equals(final @Nullable Object obj) {
254 return obj == this || obj instanceof HttpStatusCode other && code == other.code;
258 public String toString() {
259 final var sb = new StringBuilder(HttpStatusCode.class.getSimpleName()).append('(').append(code);
260 if (phrase != null) {
261 sb.append(' ').append(phrase);
263 return sb.append(')').toString();