Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
JettyApiErrorResponses.java
Go to the documentation of this file.
1package dev.rafex.ether.http.jetty12.response;
2
3import java.net.URI;
4
5/*-
6 * #%L
7 * ether-http-jetty12
8 * %%
9 * Copyright (C) 2025 - 2026 Raúl Eduardo González Argote
10 * %%
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 * #L%
29 */
30
31import java.time.Clock;
32import java.util.Objects;
33
34import org.eclipse.jetty.server.Response;
35import org.eclipse.jetty.util.Callback;
36
37import dev.rafex.ether.http.core.HttpError;
38import dev.rafex.ether.http.problem.model.ProblemDetails;
39import dev.rafex.ether.json.JsonCodec;
40
41public final class JettyApiErrorResponses {
42
43 private final JettyApiResponses responses;
44 private final Clock clock;
45
46 public JettyApiErrorResponses(final JsonCodec jsonCodec) {
47 this(jsonCodec, Clock.systemUTC());
48 }
49
50 public JettyApiErrorResponses(final JsonCodec jsonCodec, final Clock clock) {
51 this.responses = new JettyApiResponses(Objects.requireNonNull(jsonCodec));
52 this.clock = Objects.requireNonNull(clock);
53 }
54
55 public void notFound(final Response response, final Callback callback) {
56 error(response, callback, 404, "not_found", null, "resource not found", null);
57 }
58
59 public void notFound(final Response response, final Callback callback, final String path) {
60 error(response, callback, 404, "not_found", null, "resource not found", path);
61 }
62
63 public void badRequest(final Response response, final Callback callback, final String message) {
64 error(response, callback, 400, "bad_request", "bad_request", message, null);
65 }
66
67 public void unauthorized(final Response response, final Callback callback, final String code) {
68 error(response, callback, 401, "unauthorized", code, null, null);
69 }
70
71 public void forbidden(final Response response, final Callback callback, final String code) {
72 error(response, callback, 403, "forbidden", code, null, null);
73 }
74
75 public void internalServerError(final Response response, final Callback callback, final String message) {
76 error(response, callback, 500, "internal_server_error", "internal_error", message, null);
77 }
78
79 public void error(final Response response, final Callback callback, final HttpError mapped) {
80 error(response, callback, mapped, null);
81 }
82
83 public void error(final Response response, final Callback callback, final HttpError mapped, final String path) {
84 if (mapped == null) {
85 internalServerError(response, callback, "internal_error");
86 return;
87 }
88 error(response, callback, mapped.status(), mapped.code(), null, mapped.message(), path);
89 }
90
91 public void error(final Response response, final Callback callback, final int status, final String error,
92 final String code) {
93 error(response, callback, status, error, code, null, null);
94 }
95
96 public void error(final Response response, final Callback callback, final int status, final String error,
97 final String code, final String message) {
98 error(response, callback, status, error, code, message, null);
99 }
100
101 public void error(final Response response, final Callback callback, final int status, final String error,
102 final String code, final String message, final String path) {
103 problem(response, callback, toProblem(status, error, code, message, path));
104 }
105
106 public void problem(final Response response, final Callback callback, final ProblemDetails problem) {
107 if (problem == null) {
108 internalServerError(response, callback, "problem payload is required");
109 return;
110 }
111 responses.json(response, callback, problem.status(), problem);
112 }
113
114 private static String normalizeError(final String error) {
115 if (error == null || error.isBlank()) {
116 return "error";
117 }
118 return error;
119 }
120
121 private ProblemDetails toProblem(final int status, final String error, final String code, final String message,
122 final String path) {
123 final var normalizedError = normalizeError(error);
124 final var title = toTitle(normalizedError);
125 final var builder = ProblemDetails.builder().type(URI.create("https://rafex.dev/problems/" + normalizedError))
126 .title(title).status(status).detail(message == null || message.isBlank() ? title : message)
127 .property("error", normalizedError).property("timestamp", clock.instant().toString());
128 if (code != null && !code.isBlank()) {
129 builder.property("code", code);
130 }
131 if (path != null && !path.isBlank()) {
132 builder.instance(URI.create(path));
133 builder.property("path", path);
134 }
135 return builder.build();
136 }
137
138 private static String toTitle(final String error) {
139 final var normalized = normalizeError(error).replace('_', ' ').trim();
140 if (normalized.isEmpty()) {
141 return "Error";
142 }
143 final var parts = normalized.split("\\s+");
144 final var title = new StringBuilder();
145 for (final var part : parts) {
146 if (part.isEmpty()) {
147 continue;
148 }
149 if (!title.isEmpty()) {
150 title.append(' ');
151 }
152 title.append(Character.toUpperCase(part.charAt(0)));
153 if (part.length() > 1) {
154 title.append(part.substring(1));
155 }
156 }
157 return title.toString();
158 }
159}
void error(final Response response, final Callback callback, final int status, final String error, final String code)
void badRequest(final Response response, final Callback callback, final String message)
JettyApiErrorResponses(final JsonCodec jsonCodec, final Clock clock)
void unauthorized(final Response response, final Callback callback, final String code)
void notFound(final Response response, final Callback callback)
void error(final Response response, final Callback callback, final HttpError mapped)
void internalServerError(final Response response, final Callback callback, final String message)
void notFound(final Response response, final Callback callback, final String path)
void error(final Response response, final Callback callback, final int status, final String error, final String code, final String message, final String path)
void problem(final Response response, final Callback callback, final ProblemDetails problem)
void error(final Response response, final Callback callback, final int status, final String error, final String code, final String message)
void error(final Response response, final Callback callback, final HttpError mapped, final String path)
void forbidden(final Response response, final Callback callback, final String code)