forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerSentEmitter.java
More file actions
248 lines (220 loc) · 6.07 KB
/
ServerSentEmitter.java
File metadata and controls
248 lines (220 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* Server-Sent message emitter.
*
* <pre>{@code
*
* sse("/pattern", sse -> {
* sse.send("Hello Server-Sent events");
* })
*
* }</pre>
*
* @author edgar
* @since 2.5.0
*/
public interface ServerSentEmitter {
/**
* Keep-alive task.
*/
class KeepAlive implements Runnable {
private final Logger log = LoggerFactory.getLogger(ServerSentEmitter.class);
private ServerSentEmitter emitter;
private long retry;
public KeepAlive(final ServerSentEmitter emitter, final long retry) {
this.emitter = emitter;
this.retry = retry;
}
@Override
public void run() {
if (emitter.isOpen()) {
String sseId = emitter.getId();
try {
log.debug("running heart beat for {}", sseId);
emitter.send(":" + sseId + "\n");
emitter.keepAlive(retry);
} catch (Exception x) {
log.debug("connection lost for {}", sseId, x);
emitter.close();
}
}
}
}
/**
* Server-Sent event handler.
*/
interface Handler {
/**
* Callback with a readonly context and sse configurer.
*
* @param sse Server sent event.
* @throws Exception If something goes wrong.
*/
void handle(@Nonnull ServerSentEmitter sse) throws Exception;
}
/**
* Generated when client close the connection or when explicit calls to
* {@link #close()}.
*
* @param task Cleanup task.
*/
void onClose(SneakyThrows.Runnable task);
/**
* Originating HTTP context. Please note this is a read-only context, so you are not allowed
* to modify or produces a response from it.
*
* The context let give you access to originating request (then one that was upgrade it).
*
* @return Read-only originating HTTP request.
*/
@Nonnull Context getContext();
/**
* Context attributes (a.k.a request attributes).
*
* @return Context attributes.
*/
default @Nonnull Map<String, Object> getAttributes() {
return getContext().getAttributes();
}
/**
* Get an attribute by his key. This is just an utility method around {@link #getAttributes()}.
* This method look first in current context and fallback to application attributes.
*
* @param key Attribute key.
* @param <T> Attribute type.
* @return Attribute value.
*/
default @Nonnull <T> T attribute(@Nonnull String key) {
return getContext().attribute(key);
}
/**
* Set an application attribute.
*
* @param key Attribute key.
* @param value Attribute value.
* @return This ServerSent.
*/
default @Nonnull ServerSentEmitter attribute(@Nonnull String key, Object value) {
getContext().attribute(key, value);
return this;
}
/**
* Send a text message to client.
*
* @param data Text Message.
* @return This ServerSent.
*/
default @Nonnull ServerSentEmitter send(@Nonnull String data) {
return send(new ServerSentMessage(data));
}
/**
* Send a text message to client.
*
* @param data Text Message.
* @return This ServerSent.
*/
default @Nonnull ServerSentEmitter send(@Nonnull byte[] data) {
return send(new ServerSentMessage(data));
}
/**
* Send a text message to client.
*
* @param data Text Message.
* @return This ServerSent.
*/
default @Nonnull ServerSentEmitter send(@Nonnull Object data) {
if (data instanceof ServerSentMessage) {
return send((ServerSentMessage) data);
} else {
return send(new ServerSentMessage(data));
}
}
/**
* Send a message to the client and set the event type attribute.
*
* @param event Event type.
* @param data Message.
* @return This emitter.
*/
default @Nonnull ServerSentEmitter send(@Nonnull String event, @Nonnull Object data) {
return send(new ServerSentMessage(data).setEvent(event));
}
/**
* Send a message to the client.
*
* @param data Message.
* @return This emitter.
*/
@Nonnull ServerSentEmitter send(@Nonnull ServerSentMessage data);
/**
* Send a comment message to the client. The comment line can be used to prevent connections
* from timing out; a server can send a comment periodically to keep the connection alive.
*
* @param time Period of time.
* @param unit Time unit.
* @return This emitter.
*/
default @Nonnull ServerSentEmitter keepAlive(final long time, final @Nonnull TimeUnit unit) {
return keepAlive(unit.toMillis(time));
}
/**
* Send a comment message to the client. The comment line can be used to prevent connections
* from timing out; a server can send a comment periodically to keep the connection alive.
*
* @param timeInMillis Period of time in millis.
* @return This emitter.
*/
@Nonnull ServerSentEmitter keepAlive(long timeInMillis);
/**
* Read the <code>Last-Event-ID</code> header and retrieve it. Might be null.
*
* @return Last event ID or <code>null</code>.
*/
default @Nullable String getLastEventId() {
return lastEventId(String.class);
}
/**
* Read the <code>Last-Event-ID</code> header and retrieve it. Might be null.
*
* @param type Type to convert.
* @param <T> Type to convert.
* @return Last event ID or <code>null</code>.
*/
default @Nullable <T> T lastEventId(Class<T> type) {
return getContext().header("Last-Event-ID").toOptional(type).orElse(null);
}
/**
* Server-Sent ID. Defaults to UUID.
*
* @return Server-Sent ID. Defaults to UUID.
*/
@Nonnull String getId();
/**
* Set Server-Sent ID.
*
* @param id Set Server-Sent ID.
* @return This emitter.
*/
@Nonnull ServerSentEmitter setId(@Nonnull String id);
/**
* True if connection is open.
*
* @return True if connection is open.
*/
boolean isOpen();
/**
* Close the ServerSent and fir the close event.
*/
void close();
}