Core HTTP protocol support library for Eclipse Jetty providing header handling, parsing, generation, compliance validation, and content processing capabilities
—
Standard HTTP method and status code handling with safety and idempotency checks, efficient parsing, caching, and comprehensive status code coverage.
Complete enumeration of HTTP methods with efficiency optimizations and semantic checks.
/**
* Enumeration of HTTP methods with all IANA-registered methods
*/
enum HttpMethod {
// IANA registered HTTP methods (from HttpMethod.java source)
ACL, BASELINE_CONTROL, BIND, CHECKIN, CHECKOUT, CONNECT, COPY, DELETE,
GET, HEAD, LABEL, LINK, LOCK, MERGE, MKACTIVITY, MKCALENDAR, MKCOL,
MKREDIRECTREF, MKWORKSPACE, MOVE, OPTIONS, ORDERPATCH, PATCH, POST,
PRI, PROPFIND, PROPPATCH, PUT, REBIND, REPORT, SEARCH, TRACE,
UNBIND, UNCHECKOUT, UNLINK, UNLOCK, UPDATE, UPDATEREDIRECTREF,
VERSION_CONTROL,
// Other methods
PROXY;
/** Get method as byte array for efficient processing */
byte[] getBytes();
/** Check if method matches string (case sensitive) */
boolean is(String s);
/** Check if method is safe (read-only, no side effects) */
boolean isSafe();
/** Check if method is idempotent (can be repeated safely) */
boolean isIdempotent();
/** Get method as ByteBuffer for network operations */
ByteBuffer asBuffer();
/** Get method as string */
String asString();
/** Parse method from string, null if not found */
static HttpMethod fromString(String method);
/** Optimized parsing for GET method from buffer */
static HttpMethod lookAheadGet(ByteBuffer buffer);
/** Optimized parsing with pre-computed integer representation */
static HttpMethod lookAheadGet(ByteBuffer buffer, int lookAhead);
/** Case-sensitive method cache for fast lookup */
static final Index<HttpMethod> CACHE;
/** Case-insensitive method cache */
static final Index<HttpMethod> INSENSITIVE_CACHE;
/** Look-ahead cache for method parsing with trailing space */
static final Index<HttpMethod> LOOK_AHEAD;
/** Pre-computed integer representations for efficiency */
static final int GET_AS_INT;
static final int POST_AS_INT;
static final int PUT_AS_INT;
static final int HEAD_AS_INT;
static final int PRI_AS_INT;
static final int ACL_AS_INT;
}Comprehensive HTTP status code handling with utilities and nested Code enumeration.
/**
* HTTP status codes and utilities
*/
class HttpStatus {
// 1xx Informational responses
static final int CONTINUE_100 = 100;
static final int SWITCHING_PROTOCOLS_101 = 101;
static final int PROCESSING_102 = 102;
static final int EARLY_HINTS_103 = 103;
// 2xx Success responses
static final int OK_200 = 200;
static final int CREATED_201 = 201;
static final int ACCEPTED_202 = 202;
static final int NON_AUTHORITATIVE_INFORMATION_203 = 203;
static final int NO_CONTENT_204 = 204;
static final int RESET_CONTENT_205 = 205;
static final int PARTIAL_CONTENT_206 = 206;
static final int MULTI_STATUS_207 = 207;
// 3xx Redirection responses
static final int MULTIPLE_CHOICES_300 = 300;
static final int MOVED_PERMANENTLY_301 = 301;
static final int MOVED_TEMPORARILY_302 = 302;
static final int FOUND_302 = 302;
static final int SEE_OTHER_303 = 303;
static final int NOT_MODIFIED_304 = 304;
static final int USE_PROXY_305 = 305;
static final int TEMPORARY_REDIRECT_307 = 307;
static final int PERMANENT_REDIRECT_308 = 308;
// 4xx Client error responses
static final int BAD_REQUEST_400 = 400;
static final int UNAUTHORIZED_401 = 401;
static final int PAYMENT_REQUIRED_402 = 402;
static final int FORBIDDEN_403 = 403;
static final int NOT_FOUND_404 = 404;
static final int METHOD_NOT_ALLOWED_405 = 405;
static final int NOT_ACCEPTABLE_406 = 406;
static final int PROXY_AUTHENTICATION_REQUIRED_407 = 407;
static final int REQUEST_TIMEOUT_408 = 408;
static final int CONFLICT_409 = 409;
static final int GONE_410 = 410;
static final int LENGTH_REQUIRED_411 = 411;
static final int PRECONDITION_FAILED_412 = 412;
static final int PAYLOAD_TOO_LARGE_413 = 413;
static final int URI_TOO_LONG_414 = 414;
static final int UNSUPPORTED_MEDIA_TYPE_415 = 415;
static final int RANGE_NOT_SATISFIABLE_416 = 416;
static final int EXPECTATION_FAILED_417 = 417;
static final int IM_A_TEAPOT_418 = 418;
static final int ENHANCE_YOUR_CALM_420 = 420;
static final int MISDIRECTED_REQUEST_421 = 421;
static final int UNPROCESSABLE_ENTITY_422 = 422;
static final int LOCKED_423 = 423;
static final int FAILED_DEPENDENCY_424 = 424;
static final int UPGRADE_REQUIRED_426 = 426;
static final int PRECONDITION_REQUIRED_428 = 428;
static final int TOO_MANY_REQUESTS_429 = 429;
static final int REQUEST_HEADER_FIELDS_TOO_LARGE_431 = 431;
static final int UNAVAILABLE_FOR_LEGAL_REASONS_451 = 451;
// 5xx Server error responses
static final int INTERNAL_SERVER_ERROR_500 = 500;
static final int NOT_IMPLEMENTED_501 = 501;
static final int BAD_GATEWAY_502 = 502;
static final int SERVICE_UNAVAILABLE_503 = 503;
static final int GATEWAY_TIMEOUT_504 = 504;
static final int HTTP_VERSION_NOT_SUPPORTED_505 = 505;
static final int INSUFFICIENT_STORAGE_507 = 507;
static final int LOOP_DETECTED_508 = 508;
static final int NOT_EXTENDED_510 = 510;
static final int NETWORK_AUTHENTICATION_REQUIRED_511 = 511;
/** Maximum status code value */
static final int MAX_CODE = 511;
/** Get Code enum for status code */
static Code getCode(int code);
/** Get standard message for status code */
static String getMessage(int code);
/** Check if status should have no response body */
static boolean hasNoBody(int status);
/** Check if status is informational (1xx) */
static boolean isInformational(int code);
/** Check if status is success (2xx) */
static boolean isSuccess(int code);
/** Check if status is redirection (3xx) */
static boolean isRedirection(int code);
/** Check if status is client error (4xx) */
static boolean isClientError(int code);
/** Check if status is server error (5xx) */
static boolean isServerError(int code);
/** Check if status is interim response (1xx but not 101) */
static boolean isInterim(int code);
/**
* HTTP status code enumeration with semantic methods
*/
enum Code {
CONTINUE(100, "Continue"),
SWITCHING_PROTOCOLS(101, "Switching Protocols"),
PROCESSING(102, "Processing"),
EARLY_HINT(103, "Early Hint"), // @Deprecated(forRemoval = true)
EARLY_HINTS(103, "Early Hints"),
OK(200, "OK"),
CREATED(201, "Created"),
ACCEPTED(202, "Accepted"),
NON_AUTHORITATIVE_INFORMATION(203, "Non-Authoritative Information"),
NO_CONTENT(204, "No Content"),
RESET_CONTENT(205, "Reset Content"),
PARTIAL_CONTENT(206, "Partial Content"),
MULTI_STATUS(207, "Multi-Status"),
MULTIPLE_CHOICES(300, "Multiple Choices"),
MOVED_PERMANENTLY(301, "Moved Permanently"),
MOVED_TEMPORARILY(302, "Moved Temporarily"),
FOUND(302, "Found"),
SEE_OTHER(303, "See Other"),
NOT_MODIFIED(304, "Not Modified"),
USE_PROXY(305, "Use Proxy"),
TEMPORARY_REDIRECT(307, "Temporary Redirect"),
PERMANENT_REDIRECT(308, "Permanent Redirect"),
BAD_REQUEST(400, "Bad Request"),
UNAUTHORIZED(401, "Unauthorized"),
PAYMENT_REQUIRED(402, "Payment Required"),
FORBIDDEN(403, "Forbidden"),
NOT_FOUND(404, "Not Found"),
METHOD_NOT_ALLOWED(405, "Method Not Allowed"),
NOT_ACCEPTABLE(406, "Not Acceptable"),
PROXY_AUTHENTICATION_REQUIRED(407, "Proxy Authentication Required"),
REQUEST_TIMEOUT(408, "Request Timeout"),
CONFLICT(409, "Conflict"),
GONE(410, "Gone"),
LENGTH_REQUIRED(411, "Length Required"),
PRECONDITION_FAILED(412, "Precondition Failed"),
PAYLOAD_TOO_LARGE(413, "Payload Too Large"),
URI_TOO_LONG(414, "URI Too Long"),
UNSUPPORTED_MEDIA_TYPE(415, "Unsupported Media Type"),
RANGE_NOT_SATISFIABLE(416, "Range Not Satisfiable"),
EXPECTATION_FAILED(417, "Expectation Failed"),
IM_A_TEAPOT(418, "I'm a Teapot"),
ENHANCE_YOUR_CALM(420, "Enhance your Calm"),
MISDIRECTED_REQUEST(421, "Misdirected Request"),
UNPROCESSABLE_ENTITY(422, "Unprocessable Entity"),
LOCKED(423, "Locked"),
FAILED_DEPENDENCY(424, "Failed Dependency"),
UPGRADE_REQUIRED(426, "Upgrade Required"),
PRECONDITION_REQUIRED(428, "Precondition Required"),
TOO_MANY_REQUESTS(429, "Too Many Requests"),
REQUEST_HEADER_FIELDS_TOO_LARGE(431, "Request Header Fields Too Large"),
UNAVAILABLE_FOR_LEGAL_REASONS(451, "Unavailable for Legal Reason"),
INTERNAL_SERVER_ERROR(500, "Server Error"),
NOT_IMPLEMENTED(501, "Not Implemented"),
BAD_GATEWAY(502, "Bad Gateway"),
SERVICE_UNAVAILABLE(503, "Service Unavailable"),
GATEWAY_TIMEOUT(504, "Gateway Timeout"),
HTTP_VERSION_NOT_SUPPORTED(505, "HTTP Version Not Supported"),
INSUFFICIENT_STORAGE(507, "Insufficient Storage"),
LOOP_DETECTED(508, "Loop Detected"),
NOT_EXTENDED(510, "Not Extended"),
NETWORK_AUTHENTICATION_REQUIRED(511, "Network Authentication Required");
/** Get numeric status code */
int getCode();
/** Get standard status message */
String getMessage();
/** Check if status is informational (1xx) */
boolean isInformational();
/** Check if status is success (2xx) */
boolean isSuccess();
/** Check if status is redirection (3xx) */
boolean isRedirection();
/** Check if status is client error (4xx) */
boolean isClientError();
/** Check if status is server error (5xx) */
boolean isServerError();
/** Check if enum matches given status code */
boolean equals(int code);
}
}Usage Examples:
import org.eclipse.jetty.http.*;
// Working with HTTP methods
HttpMethod method = HttpMethod.fromString("POST");
if (method != null) {
boolean safe = method.isSafe(); // false for POST
boolean idempotent = method.isIdempotent(); // false for POST
ByteBuffer buffer = method.asBuffer(); // Efficient network serialization
}
// Method safety checks
if (HttpMethod.GET.isSafe()) {
// Safe to cache, no side effects
}
if (HttpMethod.PUT.isIdempotent()) {
// Safe to retry
}
// Efficient method comparison
if (method.is("GET")) {
// Handle GET request
}
// Working with status codes
int status = HttpStatus.OK_200;
String message = HttpStatus.getMessage(status); // "OK"
// Status category checks
boolean isError = HttpStatus.isClientError(404); // true
boolean isSuccess = HttpStatus.isSuccess(201); // true
boolean hasBody = !HttpStatus.hasNoBody(204); // false (204 No Content)
// Using status code enum
HttpStatus.Code code = HttpStatus.getCode(404);
if (code == HttpStatus.Code.NOT_FOUND) {
// Handle not found
}
// Status range checks
if (HttpStatus.isInformational(103)) { // true
// Handle interim response
}
if (HttpStatus.isServerError(503)) { // true
// Handle server error - maybe retry
}
// Method caching for performance
HttpMethod cached = HttpMethod.CACHE.get("GET"); // Case-sensitive
HttpMethod insensitive = HttpMethod.INSENSITIVE_CACHE.get("get"); // Case-insensitive
// Integer representations for switch statements
switch (method.ordinal()) {
case HttpMethod.GET_AS_INT:
// Handle GET
break;
case HttpMethod.POST_AS_INT:
// Handle POST
break;
case HttpMethod.PUT_AS_INT:
// Handle PUT
break;
case HttpMethod.HEAD_AS_INT:
// Handle HEAD
break;
}Install with Tessl CLI
npx tessl i tessl/maven-org-eclipse-jetty--jetty-http