Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
HttpExchange.java
Go to the documentation of this file.
1package dev.rafex.ether.http.core;
2
3/*-
4 * #%L
5 * ether-http-core
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.List;
30import java.util.Map;
31import java.util.Set;
32
33public interface HttpExchange {
34
35 String method();
36
37 String path();
38
39 String pathParam(String name);
40
41 String queryFirst(String name);
42
43 List<String> queryAll(String name);
44
45 Map<String, String> pathParams();
46
47 Map<String, List<String>> queryParams();
48
49 Set<String> allowedMethods();
50
51 void json(int status, Object body);
52
53 void text(int status, String body);
54
55 void noContent(int status);
56
57 default void methodNotAllowed() {
58 json(405, Map.of("error", "method_not_allowed"));
59 }
60
61 default void options() {
62 noContent(204);
63 }
64
65 /**
66 * Starts a Server-Sent Events stream. Sets the transport's event-stream headers and
67 * returns an {@link EventStream} for subsequent writes; {@link #json}, {@link #text} and
68 * {@link #noContent} must not be called after this.
69 *
70 * <p>Default throws — only transports that implement long-lived/chunked writes (e.g. the
71 * Jetty 12 transport) support this.
72 */
74 throw new UnsupportedOperationException("SSE not supported by this transport");
75 }
76
77 /** A long-lived, transport-agnostic handle for pushing Server-Sent Events. */
78 interface EventStream {
79
80 /** Sends one SSE event. {@code event} may be null for an unnamed "message" event. */
81 void send(String event, String data);
82
83 /** Sends an SSE comment line (e.g. for heartbeats); ignored by EventSource clients. */
84 void comment(String text);
85
86 /** Registers a callback invoked once when the stream ends (client disconnect or {@link #close()}). */
87 void onClose(Runnable callback);
88
89 /** Ends the stream. Safe to call more than once. */
90 void close();
91 }
92}
A long-lived, transport-agnostic handle for pushing Server-Sent Events.
void onClose(Runnable callback)
Registers a callback invoked once when the stream ends (client disconnect or close()).
void send(String event, String data)
Sends one SSE event.
void comment(String text)
Sends an SSE comment line (e.g.
List< String > queryAll(String name)
void text(int status, String body)
void json(int status, Object body)
Map< String, List< String > > queryParams()
Map< String, String > pathParams()
default EventStream startEventStream()
Starts a Server-Sent Events stream.