Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
JettyHttpExchange.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.util.Collections;
30import java.util.List;
31import java.util.Map;
32import java.util.Set;
33import java.util.TreeSet;
34
35import org.eclipse.jetty.server.Request;
36import org.eclipse.jetty.server.Response;
37import org.eclipse.jetty.util.Callback;
38
39import dev.rafex.ether.json.JsonCodec;
40import dev.rafex.ether.http.core.HttpExchange;
41
42public final class JettyHttpExchange implements HttpExchange {
43
44 private final Request request;
45 private final Response response;
46 private final Callback callback;
47 private final Map<String, String> pathParams;
48 private final Map<String, List<String>> queryParams;
49 private final Set<String> allowedMethods;
50 private final JettyApiResponses apiResponses;
51
52 public JettyHttpExchange(final Request request, final Response response, final Callback callback,
53 final Map<String, String> pathParams, final Map<String, List<String>> queryParams,
54 final Set<String> allowedMethods, final JsonCodec jsonCodec) {
55 this.request = request;
56 this.response = response;
57 this.callback = callback;
58 this.pathParams = pathParams;
59 this.queryParams = queryParams;
60 this.allowedMethods = normalizeMethods(allowedMethods);
61 this.apiResponses = new JettyApiResponses(jsonCodec);
62 }
63
64 public Request request() {
65 return request;
66 }
67
68 public Response response() {
69 return response;
70 }
71
72 public Callback callback() {
73 return callback;
74 }
75
76 @Override
77 public String method() {
78 return request.getMethod();
79 }
80
81 @Override
82 public String path() {
83 return request.getHttpURI().getPath();
84 }
85
86 @Override
87 public String pathParam(final String name) {
88 return pathParams.get(name);
89 }
90
91 @Override
92 public String queryFirst(final String name) {
93 final var values = queryParams.get(name);
94 if (values == null || values.isEmpty()) {
95 return null;
96 }
97 return values.get(0);
98 }
99
100 @Override
101 public List<String> queryAll(final String name) {
102 return queryParams.getOrDefault(name, List.of());
103 }
104
105 @Override
106 public Map<String, String> pathParams() {
107 return Collections.unmodifiableMap(pathParams);
108 }
109
110 @Override
111 public Map<String, List<String>> queryParams() {
112 return Collections.unmodifiableMap(queryParams);
113 }
114
115 @Override
116 public Set<String> allowedMethods() {
117 return Collections.unmodifiableSet(allowedMethods);
118 }
119
120 @Override
121 public void json(final int status, final Object body) {
122 apiResponses.json(response, callback, status, body);
123 }
124
125 @Override
126 public void text(final int status, final String body) {
127 apiResponses.text(response, callback, status, body);
128 }
129
130 @Override
131 public void noContent(final int status) {
132 apiResponses.noContent(response, callback, status);
133 }
134
135 @Override
136 public void methodNotAllowed() {
137 response.getHeaders().put("Allow", String.join(", ", allowedMethods));
139 }
140
141 @Override
142 public void options() {
143 response.getHeaders().put("Allow", String.join(", ", allowedMethods));
144 HttpExchange.super.options();
145 }
146
147 private static Set<String> normalizeMethods(final Set<String> methods) {
148 final var out = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
149 if (methods != null) {
150 for (final var method : methods) {
151 if (method != null && !method.isBlank()) {
152 out.add(method.trim().toUpperCase());
153 }
154 }
155 }
156 out.add("OPTIONS");
157 return out;
158 }
159}
void text(final int status, final String body)
void json(final int status, final Object body)
JettyHttpExchange(final Request request, final Response response, final Callback callback, final Map< String, String > pathParams, final Map< String, List< String > > queryParams, final Set< String > allowedMethods, final JsonCodec jsonCodec)