From 5f904cc8c30184eaf3932bf93279f1b5655d3eb3 Mon Sep 17 00:00:00 2001 From: Andy Seaborne Date: Wed, 8 Jan 2025 17:02:57 +0000 Subject: [PATCH] Update HttpSC --- .../main/java/org/apache/jena/web/HttpSC.java | 884 +++++++++--------- 1 file changed, 418 insertions(+), 466 deletions(-) diff --git a/jena-arq/src/main/java/org/apache/jena/web/HttpSC.java b/jena-arq/src/main/java/org/apache/jena/web/HttpSC.java index 6414ea91ed1..955f29dd001 100644 --- a/jena-arq/src/main/java/org/apache/jena/web/HttpSC.java +++ b/jena-arq/src/main/java/org/apache/jena/web/HttpSC.java @@ -16,472 +16,424 @@ * limitations under the License. */ -// ======================================================================== -// Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== - -//package org.eclipse.jetty.http; -/* This code is a copy of the Jetty 7 HttpStatus class. - * I found the naming which includes the number, more useful than the standard constants. - */ - package org.apache.jena.web; -/** - *

- * HttpStatusCode enum class, for status codes based on various HTTP RFCs. (see - * table below) - *

- */ -public class HttpSC -{ - public final static int CONTINUE_100 = 100; - public final static int SWITCHING_PROTOCOLS_101 = 101; - public final static int PROCESSING_102 = 102; - - public final static int OK_200 = 200; - public final static int CREATED_201 = 201; - public final static int ACCEPTED_202 = 202; - public final static int NON_AUTHORITATIVE_INFORMATION_203 = 203; - public final static int NO_CONTENT_204 = 204; - public final static int RESET_CONTENT_205 = 205; - public final static int PARTIAL_CONTENT_206 = 206; - public final static int MULTI_STATUS_207 = 207; - - public final static int MULTIPLE_CHOICES_300 = 300; - public final static int MOVED_PERMANENTLY_301 = 301; - public final static int MOVED_TEMPORARILY_302 = 302; - public final static int FOUND_302 = 302; - public final static int SEE_OTHER_303 = 303; - public final static int NOT_MODIFIED_304 = 304; - public final static int USE_PROXY_305 = 305; - public final static int TEMPORARY_REDIRECT_307 = 307; - - public final static int BAD_REQUEST_400 = 400; - public final static int UNAUTHORIZED_401 = 401; - public final static int PAYMENT_REQUIRED_402 = 402; - public final static int FORBIDDEN_403 = 403; - public final static int NOT_FOUND_404 = 404; - public final static int METHOD_NOT_ALLOWED_405 = 405; - public final static int NOT_ACCEPTABLE_406 = 406; - public final static int PROXY_AUTHENTICATION_REQUIRED_407 = 407; - public final static int REQUEST_TIMEOUT_408 = 408; - public final static int CONFLICT_409 = 409; - public final static int GONE_410 = 410; - public final static int LENGTH_REQUIRED_411 = 411; - public final static int PRECONDITION_FAILED_412 = 412; - public final static int REQUEST_ENTITY_TOO_LARGE_413 = 413; - public final static int REQUEST_URI_TOO_LONG_414 = 414; - public final static int UNSUPPORTED_MEDIA_TYPE_415 = 415; - public final static int REQUESTED_RANGE_NOT_SATISFIABLE_416 = 416; - public final static int EXPECTATION_FAILED_417 = 417; - public final static int I_AM_A_TEAPOT_418 = 418; // RFC 2324 - public final static int UNPROCESSABLE_ENTITY_422 = 422; - public final static int LOCKED_423 = 423; - public final static int FAILED_DEPENDENCY_424 = 424; - - public final static int INTERNAL_SERVER_ERROR_500 = 500; - public final static int NOT_IMPLEMENTED_501 = 501; - public final static int BAD_GATEWAY_502 = 502; - public final static int SERVICE_UNAVAILABLE_503 = 503; - public final static int GATEWAY_TIMEOUT_504 = 504; - public final static int HTTP_VERSION_NOT_SUPPORTED_505 = 505; - public final static int INSUFFICIENT_STORAGE_507 = 507; - - public static final int MAX_CODE = 507; - - - private static final Code[] codeMap = new Code[MAX_CODE+1]; - - static - { - for (Code code : Code.values()) - { - codeMap[code._code] = code; - } - } - - public enum Code - { - /* - * -------------------------------------------------------------------- - * Informational messages in 1xx series. As defined by ... - * RFC 1945 - HTTP/1.0 - * RFC 2616 - HTTP/1.1 - * RFC 2518 - WebDAV - * and RFC2324 - */ - - /** 100 Continue */ - CONTINUE(CONTINUE_100, "Continue"), - /** 101 Switching Protocols */ - SWITCHING_PROTOCOLS(SWITCHING_PROTOCOLS_101, "Switching Protocols"), - /** 102 Processing */ - PROCESSING(PROCESSING_102, "Processing"), - - /* - * -------------------------------------------------------------------- - * Success messages in 2xx series. As defined by ... RFC 1945 - HTTP/1.0 - * RFC 2616 - HTTP/1.1 RFC 2518 - WebDAV - */ - - /** 200 OK */ - OK(OK_200, "OK"), - /** 201 Created */ - CREATED(CREATED_201, "Created"), - /** 202 Accepted */ - ACCEPTED(ACCEPTED_202, "Accepted"), - /** 203 Non Authoritative Information */ - NON_AUTHORITATIVE_INFORMATION(NON_AUTHORITATIVE_INFORMATION_203, "Non Authoritative Information"), - /** 204 No Content */ - NO_CONTENT(NO_CONTENT_204, "No Content"), - /** 205 Reset Content */ - RESET_CONTENT(RESET_CONTENT_205, "Reset Content"), - /** 206 Partial Content */ - PARTIAL_CONTENT(PARTIAL_CONTENT_206, "Partial Content"), - /** 207 Multi-Status */ - MULTI_STATUS(MULTI_STATUS_207, "Multi-Status"), - - /* - * -------------------------------------------------------------------- - * Redirection messages in 3xx series. As defined by ... RFC 1945 - - * HTTP/1.0 RFC 2616 - HTTP/1.1 - */ - - /** 300 Mutliple Choices */ - MULTIPLE_CHOICES(MULTIPLE_CHOICES_300, "Multiple Choices"), - /** 301 Moved Permanently */ - MOVED_PERMANENTLY(MOVED_PERMANENTLY_301, "Moved Permanently"), - /** 302 Moved Temporarily */ - MOVED_TEMPORARILY(MOVED_TEMPORARILY_302, "Moved Temporarily"), - /** 302 Found */ - FOUND(FOUND_302, "Found"), - /** 303 See Other */ - SEE_OTHER(SEE_OTHER_303, "See Other"), - /** 304 Not Modified */ - NOT_MODIFIED(NOT_MODIFIED_304, "Not Modified"), - /** 305 Use Proxy */ - USE_PROXY(USE_PROXY_305, "Use Proxy"), - /** 307 Temporary Redirect */ - TEMPORARY_REDIRECT(TEMPORARY_REDIRECT_307, "Temporary Redirect"), - - /* - * -------------------------------------------------------------------- - * Client Error messages in 4xx series. As defined by ... RFC 1945 - - * HTTP/1.0 RFC 2616 - HTTP/1.1 RFC 2518 - WebDAV - */ - - /** 400 Bad Request */ - BAD_REQUEST(BAD_REQUEST_400, "Bad Request"), - /** 401 Unauthorized */ - UNAUTHORIZED(UNAUTHORIZED_401, "Unauthorized"), - /** 402 Payment Required */ - PAYMENT_REQUIRED(PAYMENT_REQUIRED_402, "Payment Required"), - /** 403 Forbidden */ - FORBIDDEN(FORBIDDEN_403, "Forbidden"), - /** 404 Not Found */ - NOT_FOUND(NOT_FOUND_404, "Not Found"), - /** 405 Method Not Allowed */ - METHOD_NOT_ALLOWED(METHOD_NOT_ALLOWED_405, "Method Not Allowed"), - /** 406 Not Acceptable */ - NOT_ACCEPTABLE(NOT_ACCEPTABLE_406, "Not Acceptable"), - /** 407 Proxy Authentication Required */ - PROXY_AUTHENTICATION_REQUIRED(PROXY_AUTHENTICATION_REQUIRED_407, "Proxy Authentication Required"), - /** 408 Request Timeout */ - REQUEST_TIMEOUT(REQUEST_TIMEOUT_408, "Request Timeout"), - /** 409 Conflict */ - CONFLICT(CONFLICT_409, "Conflict"), - /** 410 Gone */ - GONE(GONE_410, "Gone"), - /** 411 Length Required */ - LENGTH_REQUIRED(LENGTH_REQUIRED_411, "Length Required"), - /** 412 Precondition Failed */ - PRECONDITION_FAILED(PRECONDITION_FAILED_412, "Precondition Failed"), - /** 413 Request Entity Too Large */ - REQUEST_ENTITY_TOO_LARGE(REQUEST_ENTITY_TOO_LARGE_413, "Request Entity Too Large"), - /** 414 Request-URI Too Long */ - REQUEST_URI_TOO_LONG(REQUEST_URI_TOO_LONG_414, "Request-URI Too Long"), - /** 415 Unsupported Media Type */ - UNSUPPORTED_MEDIA_TYPE(UNSUPPORTED_MEDIA_TYPE_415, "Unsupported Media Type"), - /** 416 Requested Range Not Satisfiable */ - REQUESTED_RANGE_NOT_SATISFIABLE(REQUESTED_RANGE_NOT_SATISFIABLE_416, "Requested Range Not Satisfiable"), - /** 417 Expectation Failed */ - EXPECTATION_FAILED(EXPECTATION_FAILED_417, "Expectation Failed"), - /** 417 I'm a teapot - * The response MAY be short and stout. - */ - I_AM_A_TEAPOT(I_AM_A_TEAPOT_418, "I'm a teapot"), - /** 422 Unprocessable Entity */ - UNPROCESSABLE_ENTITY(UNPROCESSABLE_ENTITY_422, "Unprocessable Entity"), - /** 423 Locked */ - LOCKED(LOCKED_423, "Locked"), - /** 424 Failed Dependency */ - FAILED_DEPENDENCY(FAILED_DEPENDENCY_424, "Failed Dependency"), - - /* - * -------------------------------------------------------------------- - * Server Error messages in 5xx series. As defined by ... RFC 1945 - - * HTTP/1.0 RFC 2616 - HTTP/1.1 RFC 2518 - WebDAV - */ - - /** 500 Server Error */ - INTERNAL_SERVER_ERROR(INTERNAL_SERVER_ERROR_500, "Server Error"), - /** 501 Not Implemented */ - NOT_IMPLEMENTED(NOT_IMPLEMENTED_501, "Not Implemented"), - /** 502 Bad Gateway */ - BAD_GATEWAY(BAD_GATEWAY_502, "Bad Gateway"), - /** 503 Service Unavailable */ - SERVICE_UNAVAILABLE(SERVICE_UNAVAILABLE_503, "Service Unavailable"), - /** 504 Gateway Timeout */ - GATEWAY_TIMEOUT(GATEWAY_TIMEOUT_504, "Gateway Timeout"), - /** 505 HTTP Version Not Supported */ - HTTP_VERSION_NOT_SUPPORTED(HTTP_VERSION_NOT_SUPPORTED_505, "HTTP Version Not Supported"), - /** 507 Insufficient Storage */ - INSUFFICIENT_STORAGE(INSUFFICIENT_STORAGE_507, "Insufficient Storage"); - - private final int _code; - private final String _message; - - private Code(int code, String message) - { - this._code = code; - _message=message; - } - - public int getCode() - { - return _code; - } - - public String getMessage() - { - return _message; - } - - - public boolean equals(int code) - { - return (this._code == code); - } - - @Override - public String toString() - { - return String.format("[%03d %s]",this._code,this.getMessage()); - } - - /** - * Simple test against an code to determine if it falls into the - * Informational message category as defined in the RFC 1945 - HTTP/1.0, - * and RFC 2616 - - * HTTP/1.1. - * - * @return true if within range of codes that belongs to - * Informational messages. - */ - public boolean isInformational() - { - return HttpSC.isInformational(this._code); - } - - /** - * Simple test against an code to determine if it falls into the - * Success message category as defined in the RFC 1945 - HTTP/1.0, - * and RFC 2616 - - * HTTP/1.1. - * - * @return true if within range of codes that belongs to - * Success messages. - */ - public boolean isSuccess() - { - return HttpSC.isSuccess(this._code); - } - - /** - * Simple test against an code to determine if it falls into the - * Redirection message category as defined in the RFC 1945 - HTTP/1.0, - * and RFC 2616 - - * HTTP/1.1. - * - * @return true if within range of codes that belongs to - * Redirection messages. - */ - public boolean isRedirection() - { - return HttpSC.isRedirection(this._code); - } - - /** - * Simple test against an code to determine if it falls into the - * Client Error message category as defined in the RFC 1945 - HTTP/1.0, - * and RFC 2616 - - * HTTP/1.1. - * - * @return true if within range of codes that belongs to - * Client Error messages. - */ - public boolean isClientError() - { - return HttpSC.isClientError(this._code); - } - - /** - * Simple test against an code to determine if it falls into the - * Server Error message category as defined in the RFC 1945 - HTTP/1.0, - * and RFC 2616 - - * HTTP/1.1. - * - * @return true if within range of codes that belongs to - * Server Error messages. - */ - public boolean isServerError() - { - return HttpSC.isServerError(this._code); - } - } - - - /** - * Get the HttpStatusCode for a specific code - * - * @param code - * the code to lookup. - * @return the {@link HttpSC} if found, or null if not found. - */ - public static Code getCode(int code) - { - if ( code < 0 ) - return null ; - - if (code <= MAX_CODE) - { - return codeMap[code]; - } - return null; - } - - /** - * Get the status message for a specific code. - * - * @param code - * the code to look up - * @return the specific message, or the code number itself if code - * does not match known list. - */ - public static String getMessage(int code) - { - Code codeEnum = getCode(code); - if (codeEnum != null) - { - return codeEnum.getMessage(); - } - else - { - return Integer.toString(code); - } - } - - /** - * Simple test against an code to determine if it falls into the - * Informational message category as defined in the RFC 1945 - HTTP/1.0, and RFC 2616 - HTTP/1.1. - * - * @param code - * the code to test. - * @return true if within range of codes that belongs to - * Informational messages. - */ - public static boolean isInformational(int code) - { - return ((100 <= code) && (code <= 199)); - } - - /** - * Simple test against an code to determine if it falls into the - * Success message category as defined in the RFC 1945 - HTTP/1.0, and RFC 2616 - HTTP/1.1. - * - * @param code - * the code to test. - * @return true if within range of codes that belongs to - * Success messages. - */ - public static boolean isSuccess(int code) - { - return ((200 <= code) && (code <= 299)); - } - - /** - * Simple test against an code to determine if it falls into the - * Redirection message category as defined in the RFC 1945 - HTTP/1.0, and RFC 2616 - HTTP/1.1. - * - * @param code - * the code to test. - * @return true if within range of codes that belongs to - * Redirection messages. - */ - public static boolean isRedirection(int code) - { - return ((300 <= code) && (code <= 399)); - } - - public static boolean isError(int code) { - return isClientError(code) || isServerError(code) ; - } - - - /** - * Simple test against an code to determine if it falls into the - * Client Error message category as defined in the RFC 1945 - HTTP/1.0, and RFC 2616 - HTTP/1.1. - * - * @param code - * the code to test. - * @return true if within range of codes that belongs to - * Client Error messages. - */ - public static boolean isClientError(int code) - { - return ((400 <= code) && (code <= 499)); - } - - /** - * Simple test against an code to determine if it falls into the - * Server Error message category as defined in the RFC 1945 - HTTP/1.0, and RFC 2616 - HTTP/1.1. - * - * @param code - * the code to test. - * @return true if within range of codes that belongs to - * Server Error messages. - */ - public static boolean isServerError(int code) - { - return ((500 <= code) && (code <= 599)); - } +public class HttpSC { + // Original From Eclipse Jetty 12.0.16, license Apache-2.0 + // org.eclipse.jetty.http.HttpStatus + // Header license applies to any local modifications. + // + // ======================================================================== + // Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. + // + // This program and the accompanying materials are made available under the + // terms of the Eclipse Public License v. 2.0 which is available at + // https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 + // which is available at https://www.apache.org/licenses/LICENSE-2.0. + // + // SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 + // ======================================================================== + // + + +// /** +// *

+// * Http Status Codes +// *

+// * +// * @see IANA HTTP Status Code Registry +// */ +// public class HttpStatus +// { + public static final int CONTINUE_100 = 100; + public static final int SWITCHING_PROTOCOLS_101 = 101; + public static final int PROCESSING_102 = 102; + public static final int EARLY_HINTS_103 = 103; + + public static final int OK_200 = 200; + public static final int CREATED_201 = 201; + public static final int ACCEPTED_202 = 202; + public static final int NON_AUTHORITATIVE_INFORMATION_203 = 203; + public static final int NO_CONTENT_204 = 204; + public static final int RESET_CONTENT_205 = 205; + public static final int PARTIAL_CONTENT_206 = 206; + public static final int MULTI_STATUS_207 = 207; + + public static final int MULTIPLE_CHOICES_300 = 300; + public static final int MOVED_PERMANENTLY_301 = 301; + public static final int MOVED_TEMPORARILY_302 = 302; + public static final int FOUND_302 = 302; + public static final int SEE_OTHER_303 = 303; + public static final int NOT_MODIFIED_304 = 304; + public static final int USE_PROXY_305 = 305; + public static final int TEMPORARY_REDIRECT_307 = 307; + public static final int PERMANENT_REDIRECT_308 = 308; + + public static final int BAD_REQUEST_400 = 400; + public static final int UNAUTHORIZED_401 = 401; + public static final int PAYMENT_REQUIRED_402 = 402; + public static final int FORBIDDEN_403 = 403; + public static final int NOT_FOUND_404 = 404; + public static final int METHOD_NOT_ALLOWED_405 = 405; + public static final int NOT_ACCEPTABLE_406 = 406; + public static final int PROXY_AUTHENTICATION_REQUIRED_407 = 407; + public static final int REQUEST_TIMEOUT_408 = 408; + public static final int CONFLICT_409 = 409; + public static final int GONE_410 = 410; + public static final int LENGTH_REQUIRED_411 = 411; + public static final int PRECONDITION_FAILED_412 = 412; + public static final int PAYLOAD_TOO_LARGE_413 = 413; + public static final int URI_TOO_LONG_414 = 414; + public static final int UNSUPPORTED_MEDIA_TYPE_415 = 415; + public static final int RANGE_NOT_SATISFIABLE_416 = 416; + public static final int EXPECTATION_FAILED_417 = 417; + +// @Deprecated +// public static final int I_AM_A_TEAPOT_418 = 418; + + public static final int IM_A_TEAPOT_418 = 418; + public static final int ENHANCE_YOUR_CALM_420 = 420; + public static final int MISDIRECTED_REQUEST_421 = 421; + public static final int UNPROCESSABLE_ENTITY_422 = 422; + public static final int LOCKED_423 = 423; + public static final int FAILED_DEPENDENCY_424 = 424; + public static final int UPGRADE_REQUIRED_426 = 426; + public static final int PRECONDITION_REQUIRED_428 = 428; + public static final int TOO_MANY_REQUESTS_429 = 429; + public static final int REQUEST_HEADER_FIELDS_TOO_LARGE_431 = 431; + public static final int UNAVAILABLE_FOR_LEGAL_REASONS_451 = 451; + + public static final int INTERNAL_SERVER_ERROR_500 = 500; + public static final int NOT_IMPLEMENTED_501 = 501; + public static final int BAD_GATEWAY_502 = 502; + public static final int SERVICE_UNAVAILABLE_503 = 503; + public static final int GATEWAY_TIMEOUT_504 = 504; + public static final int HTTP_VERSION_NOT_SUPPORTED_505 = 505; + public static final int INSUFFICIENT_STORAGE_507 = 507; + public static final int LOOP_DETECTED_508 = 508; + public static final int NOT_EXTENDED_510 = 510; + public static final int NETWORK_AUTHENTICATION_REQUIRED_511 = 511; + + public static final int MAX_CODE = 511; + + private static final Code[] codeMap = new Code[MAX_CODE + 1]; + + static + { + for (Code code : Code.values()) + { + codeMap[code._code] = code; + } + } + + public enum Code + { + CONTINUE(CONTINUE_100, "Continue"), + SWITCHING_PROTOCOLS(SWITCHING_PROTOCOLS_101, "Switching Protocols"), + PROCESSING(PROCESSING_102, "Processing"), + EARLY_HINTS(EARLY_HINTS_103, "Early Hints"), + + OK(OK_200, "OK"), + CREATED(CREATED_201, "Created"), + ACCEPTED(ACCEPTED_202, "Accepted"), + NON_AUTHORITATIVE_INFORMATION(NON_AUTHORITATIVE_INFORMATION_203, "Non Authoritative Information"), + NO_CONTENT(NO_CONTENT_204, "No Content"), + RESET_CONTENT(RESET_CONTENT_205, "Reset Content"), + PARTIAL_CONTENT(PARTIAL_CONTENT_206, "Partial Content"), + MULTI_STATUS(MULTI_STATUS_207, "Multi-Status"), + + MULTIPLE_CHOICES(MULTIPLE_CHOICES_300, "Multiple Choices"), + MOVED_PERMANENTLY(MOVED_PERMANENTLY_301, "Moved Permanently"), + MOVED_TEMPORARILY(MOVED_TEMPORARILY_302, "Moved Temporarily"), + FOUND(FOUND_302, "Found"), + SEE_OTHER(SEE_OTHER_303, "See Other"), + NOT_MODIFIED(NOT_MODIFIED_304, "Not Modified"), + USE_PROXY(USE_PROXY_305, "Use Proxy"), + TEMPORARY_REDIRECT(TEMPORARY_REDIRECT_307, "Temporary Redirect"), + PERMANENT_REDIRECT(PERMANENT_REDIRECT_308, "Permanent Redirect"), + + BAD_REQUEST(BAD_REQUEST_400, "Bad Request"), + UNAUTHORIZED(UNAUTHORIZED_401, "Unauthorized"), + PAYMENT_REQUIRED(PAYMENT_REQUIRED_402, "Payment Required"), + FORBIDDEN(FORBIDDEN_403, "Forbidden"), + NOT_FOUND(NOT_FOUND_404, "Not Found"), + METHOD_NOT_ALLOWED(METHOD_NOT_ALLOWED_405, "Method Not Allowed"), + NOT_ACCEPTABLE(NOT_ACCEPTABLE_406, "Not Acceptable"), + PROXY_AUTHENTICATION_REQUIRED(PROXY_AUTHENTICATION_REQUIRED_407, "Proxy Authentication Required"), + REQUEST_TIMEOUT(REQUEST_TIMEOUT_408, "Request Timeout"), + CONFLICT(CONFLICT_409, "Conflict"), + GONE(GONE_410, "Gone"), + LENGTH_REQUIRED(LENGTH_REQUIRED_411, "Length Required"), + PRECONDITION_FAILED(PRECONDITION_FAILED_412, "Precondition Failed"), + PAYLOAD_TOO_LARGE(PAYLOAD_TOO_LARGE_413, "Payload Too Large"), + URI_TOO_LONG(URI_TOO_LONG_414, "URI Too Long"), + UNSUPPORTED_MEDIA_TYPE(UNSUPPORTED_MEDIA_TYPE_415, "Unsupported Media Type"), + RANGE_NOT_SATISFIABLE(RANGE_NOT_SATISFIABLE_416, "Range Not Satisfiable"), + EXPECTATION_FAILED(EXPECTATION_FAILED_417, "Expectation Failed"), + IM_A_TEAPOT(IM_A_TEAPOT_418, "I'm a Teapot"), + ENHANCE_YOUR_CALM(ENHANCE_YOUR_CALM_420, "Enhance your Calm"), + MISDIRECTED_REQUEST(MISDIRECTED_REQUEST_421, "Misdirected Request"), + UNPROCESSABLE_ENTITY(UNPROCESSABLE_ENTITY_422, "Unprocessable Entity"), + LOCKED(LOCKED_423, "Locked"), + FAILED_DEPENDENCY(FAILED_DEPENDENCY_424, "Failed Dependency"), + UPGRADE_REQUIRED(UPGRADE_REQUIRED_426, "Upgrade Required"), + PRECONDITION_REQUIRED(PRECONDITION_REQUIRED_428, "Precondition Required"), + TOO_MANY_REQUESTS(TOO_MANY_REQUESTS_429, "Too Many Requests"), + REQUEST_HEADER_FIELDS_TOO_LARGE(REQUEST_HEADER_FIELDS_TOO_LARGE_431, "Request Header Fields Too Large"), + UNAVAILABLE_FOR_LEGAL_REASONS(UNAVAILABLE_FOR_LEGAL_REASONS_451, "Unavailable for Legal Reason"), + + INTERNAL_SERVER_ERROR(INTERNAL_SERVER_ERROR_500, "Server Error"), + NOT_IMPLEMENTED(NOT_IMPLEMENTED_501, "Not Implemented"), + BAD_GATEWAY(BAD_GATEWAY_502, "Bad Gateway"), + SERVICE_UNAVAILABLE(SERVICE_UNAVAILABLE_503, "Service Unavailable"), + GATEWAY_TIMEOUT(GATEWAY_TIMEOUT_504, "Gateway Timeout"), + HTTP_VERSION_NOT_SUPPORTED(HTTP_VERSION_NOT_SUPPORTED_505, "HTTP Version Not Supported"), + INSUFFICIENT_STORAGE(INSUFFICIENT_STORAGE_507, "Insufficient Storage"), + LOOP_DETECTED(LOOP_DETECTED_508, "Loop Detected"), + NOT_EXTENDED(NOT_EXTENDED_510, "Not Extended"), + NETWORK_AUTHENTICATION_REQUIRED(NETWORK_AUTHENTICATION_REQUIRED_511, "Network Authentication Required"), + + ; + + private final int _code; + private final String _message; + + private Code(int code, String message) + { + this._code = code; + _message = message; + } + + public int getCode() + { + return _code; + } + + public String getMessage() + { + return _message; + } + + public boolean equals(int code) + { + return (this._code == code); + } + + @Override + public String toString() + { + return String.format("[%03d %s]", this._code, this.getMessage()); + } + + /** + * Simple test against an code to determine if it falls into the + * Informational message category as defined in the RFC 1945 - HTTP/1.0, + * and RFC 7231 - + * HTTP/1.1. + * + * @return true if within range of codes that belongs to + * Informational messages. + */ + public boolean isInformational() + { + return HttpSC.isInformational(this._code); + } + + /** + * Simple test against an code to determine if it falls into the + * Success message category as defined in the RFC 1945 - HTTP/1.0, + * and RFC 7231 - + * HTTP/1.1. + * + * @return true if within range of codes that belongs to + * Success messages. + */ + public boolean isSuccess() + { + return HttpSC.isSuccess(this._code); + } + + /** + * Simple test against an code to determine if it falls into the + * Redirection message category as defined in the RFC 1945 - HTTP/1.0, + * and RFC 7231 - + * HTTP/1.1. + * + * @return true if within range of codes that belongs to + * Redirection messages. + */ + public boolean isRedirection() + { + return HttpSC.isRedirection(this._code); + } + + /** + * Simple test against an code to determine if it falls into the + * Client Error message category as defined in the RFC 1945 - HTTP/1.0, + * and RFC 7231 - + * HTTP/1.1. + * + * @return true if within range of codes that belongs to + * Client Error messages. + */ + public boolean isClientError() + { + return HttpSC.isClientError(this._code); + } + + /** + * Simple test against an code to determine if it falls into the + * Server Error message category as defined in the RFC 1945 - HTTP/1.0, + * and RFC 7231 - + * HTTP/1.1. + * + * @return true if within range of codes that belongs to + * Server Error messages. + */ + public boolean isServerError() + { + return HttpSC.isServerError(this._code); + } + } + + /** + * Get the HttpStatusCode for a specific code + * + * @param code the code to lookup. + * @return the {@link HttpSC} if found, or null if not found. + */ + public static Code getCode(int code) + { + if (code <= MAX_CODE) + { + return codeMap[code]; + } + return null; + } + + /** + * Get the status message for a specific code. + * + * @param code the code to look up + * @return the specific message, or the code number itself if code + * does not match known list. + */ + public static String getMessage(int code) + { + Code codeEnum = getCode(code); + if (codeEnum != null) + { + return codeEnum.getMessage(); + } + else + { + return Integer.toString(code); + } + } + + public static boolean hasNoBody(int status) + { + switch (status) + { + case NO_CONTENT_204: + case RESET_CONTENT_205: + case PARTIAL_CONTENT_206: + case NOT_MODIFIED_304: + return true; + + default: + return status < OK_200; + } + } + + /** + * Simple test against an code to determine if it falls into the + * Informational message category as defined in the RFC 1945 - HTTP/1.0, and RFC 7231 - HTTP/1.1. + * + * @param code the code to test. + * @return true if within range of codes that belongs to + * Informational messages. + */ + public static boolean isInformational(int code) + { + return ((100 <= code) && (code <= 199)); + } + + /** + * Tests whether the status code is informational but not {@code 101 Switching Protocols}. + * + * @param code the code to test + * @return whether the status code is informational but not {@code 101 Switching Protocols} + */ + public static boolean isInterim(int code) + { + return isInformational(code) && code != HttpSC.SWITCHING_PROTOCOLS_101; + } + + /** + * Simple test against an code to determine if it falls into the + * Success message category as defined in the RFC 1945 - HTTP/1.0, and RFC 7231 - HTTP/1.1. + * + * @param code the code to test. + * @return true if within range of codes that belongs to + * Success messages. + */ + public static boolean isSuccess(int code) + { + return ((200 <= code) && (code <= 299)); + } + + /** + * Simple test against an code to determine if it falls into the + * Redirection message category as defined in the RFC 1945 - HTTP/1.0, and RFC 7231 - HTTP/1.1. + * + * @param code the code to test. + * @return true if within range of codes that belongs to + * Redirection messages. + */ + public static boolean isRedirection(int code) + { + return ((300 <= code) && (code <= 399)); + } + + /** + * Simple test against an code to determine if it falls into the + * Client Error message category as defined in the RFC 1945 - HTTP/1.0, and RFC 7231 - HTTP/1.1. + * + * @param code the code to test. + * @return true if within range of codes that belongs to + * Client Error messages. + */ + public static boolean isClientError(int code) + { + return ((400 <= code) && (code <= 499)); + } + + /** + * Simple test against an code to determine if it falls into the + * Server Error message category as defined in the RFC 1945 - HTTP/1.0, and RFC 7231 - HTTP/1.1. + * + * @param code the code to test. + * @return true if within range of codes that belongs to + * Server Error messages. + */ + public static boolean isServerError(int code) + { + return ((500 <= code) && (code <= 599)); + } }