Skip to content

Commit

Permalink
Enum Response ContentStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
SentryMan committed Feb 9, 2025
1 parent 0c67b39 commit a579a23
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 53 deletions.
13 changes: 12 additions & 1 deletion avaje-jex/src/main/java/io/avaje/jex/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
import com.sun.net.httpserver.HttpExchange;

import io.avaje.jex.core.Constants;
import io.avaje.jex.http.ContentType;
import io.avaje.jex.http.HttpStatus;
import io.avaje.jex.security.BasicAuthCredentials;
import io.avaje.jex.security.Role;

/** Provides access to functions for handling the request and response. */
public interface Context {


/**
* Gets the attribute with the specified key from the request.
*
Expand Down Expand Up @@ -94,6 +95,11 @@ public interface Context {
/** Set the response content type. */
Context contentType(String contentType);

/** Set the response content type. */
default Context contentType(ContentType contentType) {
return contentType(contentType.contentType());
}

/** Return the request context path. */
String contextPath();

Expand Down Expand Up @@ -423,6 +429,11 @@ default Context render(String name) {
/** Set the status code on the response. */
Context status(int statusCode);

/** Set the status code on the response. */
default Context status(HttpStatus statusCode) {
return status(statusCode.status());
}

/** Write plain text content to the response. */
void text(String content);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import io.avaje.jex.Context;
import io.avaje.jex.ExceptionHandler;
import io.avaje.jex.http.ErrorCode;
import io.avaje.jex.http.HttpStatus;
import io.avaje.jex.http.HttpResponseException;
import io.avaje.jex.http.InternalServerErrorException;

Expand Down Expand Up @@ -52,13 +52,13 @@ void handle(JdkContext ctx, Exception e) {

private void unhandledException(JdkContext ctx, Exception e) {
log.log(ERROR, "Uncaught exception", e);
defaultHandling(ctx, new InternalServerErrorException(ErrorCode.INTERNAL_SERVER_ERROR.message()));
defaultHandling(ctx, new InternalServerErrorException("Internal Server Error"));
}

private void defaultHandling(JdkContext ctx, HttpResponseException exception) {
ctx.status(exception.status());
var jsonResponse = exception.jsonResponse();
if (exception.status() == ErrorCode.REDIRECT.status()) {
if (exception.status() == HttpStatus.REDIRECT_302.status()) {
ctx.performRedirect();
} else if (jsonResponse != null) {
ctx.json(jsonResponse);
Expand Down
4 changes: 2 additions & 2 deletions avaje-jex/src/main/java/io/avaje/jex/core/JdkContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import com.sun.net.httpserver.HttpsExchange;

import io.avaje.jex.Context;
import io.avaje.jex.http.ErrorCode;
import io.avaje.jex.http.HttpStatus;
import io.avaje.jex.http.RedirectException;
import io.avaje.jex.security.BasicAuthCredentials;
import io.avaje.jex.security.Role;
Expand Down Expand Up @@ -415,7 +415,7 @@ public void redirect(String location, int statusCode) {
header(Constants.LOCATION, location);
status(statusCode);
if (mode != Mode.EXCHANGE) {
throw new RedirectException(ErrorCode.REDIRECT.message());
throw new RedirectException("Redirect");
} else {
performRedirect();
}
Expand Down
76 changes: 76 additions & 0 deletions avaje-jex/src/main/java/io/avaje/jex/http/ContentType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package io.avaje.jex.http;

public enum ContentType {

TEXT_PLAIN("text/plain"),
TEXT_CSS("text/css"),
TEXT_CSV("text/csv"),
TEXT_HTML("text/html"),
TEXT_JS("text/javascript"),
TEXT_MARKDOWN("text/markdown"),
TEXT_PROPERTIES("text/x-java-properties"),
TEXT_XML("text/xml"),

IMAGE_AVIF("image/avif"),
IMAGE_BMP("image/bmp"),
IMAGE_GIF("image/gif"),
IMAGE_ICO("image/vnd.microsoft.icon"),
IMAGE_JPEG("image/jpeg"),
IMAGE_PNG("image/png"),
IMAGE_SVG("image/svg+xml"),
IMAGE_TIFF("image/tiff"),
IMAGE_WEBP("image/webp"),

AUDIO_AAC("audio/aac"),
AUDIO_MIDI("audio/midi"),
AUDIO_MPEG("audio/mpeg"),
AUDIO_OGA("audio/ogg"),
AUDIO_OPUS("audio/opus"),
AUDIO_WAV("audio/wav"),
AUDIO_WEBA("audio/weba"),

VIDEO_AVI("video/x-msvideo"),
VIDEO_MP4("video/mp4"),
VIDEO_MPEG("video/mpeg"),
VIDEO_OGG("video/ogg"),
VIDEO_WEBM("video/webm"),

FONT_OTF("font/otf"),
FONT_TTF("font/ttf"),
FONT_WOFF("font/woff"),
FONT_WOFF2("font/woff2"),

APPLICATION_OCTET_STREAM("application/octet-stream"),
APPLICATION_BZ("application/x-bzip"),
APPLICATION_BZ2("application/x-bzip2"),
APPLICATION_CDN("application/cdn"),
APPLICATION_DOC("application/msword"),
APPLICATION_DOCX("application/vnd.openxmlformats-officedocument.wordprocessingml.document"),
APPLICATION_EPUB("application/epub+zip"),
APPLICATION_GZ("application/gzip"),
APPLICATION_JSON("application/json"),
APPLICATION_MPKG("application/vnd.apple.installer+xml"),
APPLICATION_JAR("application/java-archive"),
APPLICATION_PDF("application/pdf"),
APPLICATION_POM("application/xml"),
APPLICATION_RAR("application/vnd.rar"),
APPLICATION_SH("application/x-sh"),
APPLICATION_SWF("application/x-shockwave-flash"),
APPLICATION_TAR("application/x-tar"),
APPLICATION_XHTML("application/xhtml+xml"),
APPLICATION_YAML("application/yaml"),
APPLICATION_ZIP("application/zip"),
APPLICATION_7Z("application/x-7z-compressed"),

MULTIPART_FORM_DATA("multipart/form-data");

private final String content;

ContentType(String contentType) {
this.content = contentType;
}

public String contentType() {
return content;
}
}
45 changes: 0 additions & 45 deletions avaje-jex/src/main/java/io/avaje/jex/http/ErrorCode.java

This file was deleted.

78 changes: 78 additions & 0 deletions avaje-jex/src/main/java/io/avaje/jex/http/HttpStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package io.avaje.jex.http;

/** Http Error Status codes */
public enum HttpStatus {

// 1xx Informational
CONTINUE_100(100),
SWITCHING_PROTOCOLS_101(101),

// 2xx Success
OK_200(200),
CREATED_201(201),
ACCEPTED_202(202),
NON_AUTHORITATIVE_INFORMATION_203(203),
NO_CONTENT_204(204),
RESET_CONTENT_205(205),
PARTIAL_CONTENT_206(206),
MULTI_STATUS_207(207),

// 3xx Redirection
MOVED_PERMANENTLY_301(301),
FOUND_302(302),
SEE_OTHER_303(303),
NOT_MODIFIED_304(304),
USE_PROXY_305(305),
TEMPORARY_REDIRECT_307(307),
PERMANENT_REDIRECT_308(308),

// 4xx Client Error
BAD_REQUEST_400(400),
UNAUTHORIZED_401(401),
PAYMENT_REQUIRED_402(402),
FORBIDDEN_403(403),
NOT_FOUND_404(404),
METHOD_NOT_ALLOWED_405(405),
NOT_ACCEPTABLE_406(406),
PROXY_AUTHENTICATION_REQUIRED_407(407),
REQUEST_TIMEOUT_408(408),
CONFLICT_409(409),
GONE_410(410),
LENGTH_REQUIRED_411(411),
PRECONDITION_FAILED_412(412),
REQUEST_ENTITY_TOO_LARGE_413(413),
REQUEST_URI_TOO_LONG_414(414),
UNSUPPORTED_MEDIA_TYPE_415(415),
REQUESTED_RANGE_NOT_SATISFIABLE_416(416),
EXPECTATION_FAILED_417(417),
I_AM_A_TEAPOT_418(418),
MISDIRECTED_REQUEST_421(421),
UNPROCESSABLE_CONTENT_422(422),
LOCKED_423(423),
FAILED_DEPENDENCY_424(424),
UPGRADE_REQUIRED_426(426),
PRECONDITION_REQUIRED_428(428),
TOO_MANY_REQUESTS_429(429),

// 5xx Server Error
INTERNAL_SERVER_ERROR_500(500),
NOT_IMPLEMENTED_501(501),
BAD_GATEWAY_502(502),
SERVICE_UNAVAILABLE_503(503),
GATEWAY_TIMEOUT_504(504),
HTTP_VERSION_NOT_SUPPORTED_505(505),
INSUFFICIENT_STORAGE_507(507),
LOOP_DETECTED_508(508),
NOT_EXTENDED_510(510),
NETWORK_AUTHENTICATION_REQUIRED_511(511);

private final int status;

HttpStatus(int status) {
this.status = status;
}

public int status() {
return status;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import io.avaje.jex.Jex;
import io.avaje.jex.http.BadRequestException;
import io.avaje.jex.http.ErrorCode;
import io.avaje.jex.http.HttpStatus;
import io.avaje.jex.http.HttpResponseException;
import io.avaje.json.JsonException;

Expand All @@ -22,7 +22,7 @@ static TestPair init() {
final Jex app = Jex.create()
.routing(routing -> routing
.get("/", ctx -> {
throw new HttpResponseException(ErrorCode.FORBIDDEN.status(), ErrorCode.FORBIDDEN.message());
throw new HttpResponseException(HttpStatus.FORBIDDEN.status(), "Forbidden");
})
.post("/", ctx -> {
throw new IllegalStateException("foo");
Expand Down

0 comments on commit a579a23

Please sign in to comment.