Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
JettyResponseUtil.java
Go to the documentation of this file.
1package dev.rafex.ether.http.jetty12;
2
3/*-
4 * #%L
5 * ether-http-jetty12
6 * %%
7 * Copyright (C) 2025 - 2026 Raúl Eduardo González Argote
8 * %%
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 * #L%
27 */
28
29import java.nio.ByteBuffer;
30import java.nio.charset.StandardCharsets;
31import java.util.Objects;
32
33import org.eclipse.jetty.server.Response;
34import org.eclipse.jetty.util.Callback;
35
36import dev.rafex.ether.json.JsonCodec;
37
38final class JettyResponseUtil {
39
40 private JettyResponseUtil() {
41 }
42
43 static void json(final Response response, final Callback callback, final JsonCodec codec, final int status,
44 final Object body) {
45 try {
46 jsonOrThrow(response, callback, codec, status, body);
47 } catch (final JettyTransportException e) {
48 throw new JettyTransportRuntimeException("Error writing JSON response", e);
49 }
50 }
51
52 static void jsonOrThrow(final Response response, final Callback callback, final JsonCodec codec, final int status,
53 final Object body) throws JettyTransportException {
54 response.setStatus(status);
55 response.getHeaders().put("content-type", "application/json; charset=utf-8");
56 final var jsonBody = toJsonBody(codec, body);
57 writeUtf8(response, callback, jsonBody);
58 }
59
60 static void text(final Response response, final Callback callback, final int status, final String body) {
61 response.setStatus(status);
62 response.getHeaders().put("content-type", "text/plain; charset=utf-8");
63 writeUtf8(response, callback, body == null ? "" : body);
64 }
65
66 static void noContent(final Response response, final Callback callback, final int status) {
67 response.setStatus(status);
68 callback.succeeded();
69 }
70
71 private static void writeUtf8(final Response response, final Callback callback, final String body) {
72 final var bytes = body.getBytes(StandardCharsets.UTF_8);
73 response.write(true, ByteBuffer.wrap(bytes), callback);
74 }
75
76 private static String toJsonBody(final JsonCodec codec, final Object body) throws JettyTransportException {
77 if (body instanceof final String s) {
78 return s;
79 }
80 try {
81 return Objects.requireNonNull(codec).toJson(body);
82 } catch (final RuntimeException e) {
83 throw new JettyTransportException("Error serializing JSON payload", e);
84 }
85 }
86}