/
ServerSentEvent.java
246 lines (199 loc) · 6.01 KB
/
ServerSentEvent.java
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
/*
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.http.codec;
import java.time.Duration;
import org.springframework.lang.Nullable;
/**
* Representation for a Server-Sent Event for use with Spring's reactive Web support.
* {@code Flux<ServerSentEvent>} or {@code Observable<ServerSentEvent>} is the
* reactive equivalent to Spring MVC's {@code SseEmitter}.
*
* @param <T> the type of data that this event contains
*
* @author Sebastien Deleuze
* @author Arjen Poutsma
* @since 5.0
* @see ServerSentEventHttpMessageWriter
* @see <a href="https://www.w3.org/TR/eventsource/">Server-Sent Events W3C recommendation</a>
*/
public class ServerSentEvent<T> {
@Nullable
private final String id;
@Nullable
private final String event;
@Nullable
private final Duration retry;
@Nullable
private final String comment;
@Nullable
private final T data;
private ServerSentEvent(@Nullable String id, @Nullable String event, @Nullable Duration retry,
@Nullable String comment, @Nullable T data) {
this.id = id;
this.event = event;
this.retry = retry;
this.comment = comment;
this.data = data;
}
/**
* Return the {@code id} field of this event, if available.
*/
@Nullable
public String id() {
return this.id;
}
/**
* Return the {@code event} field of this event, if available.
*/
@Nullable
public String event() {
return this.event;
}
/**
* Return the {@code retry} field of this event, if available.
*/
@Nullable
public Duration retry() {
return this.retry;
}
/**
* Return the comment of this event, if available.
*/
@Nullable
public String comment() {
return this.comment;
}
/**
* Return the {@code data} field of this event, if available.
*/
@Nullable
public T data() {
return this.data;
}
@Override
public String toString() {
return ("ServerSentEvent [id = '" + this.id + "\', event='" + this.event + "\', retry=" +
this.retry + ", comment='" + this.comment + "', data=" + this.data + ']');
}
/**
* Return a builder for a {@code SseEvent}.
* @param <T> the type of data that this event contains
* @return the builder
*/
public static <T> Builder<T> builder() {
return new BuilderImpl<>();
}
/**
* Return a builder for a {@code SseEvent}, populated with the give {@linkplain #data() data}.
* @param <T> the type of data that this event contains
* @return the builder
*/
public static <T> Builder<T> builder(T data) {
return new BuilderImpl<>(data);
}
/**
* A mutable builder for a {@code SseEvent}.
*
* @param <T> the type of data that this event contains
*/
public interface Builder<T> {
/**
* Set the value of the {@code id} field.
* @param id the value of the id field
* @return {@code this} builder
*/
Builder<T> id(String id);
/**
* Set the value of the {@code event} field.
* @param event the value of the event field
* @return {@code this} builder
*/
Builder<T> event(String event);
/**
* Set the value of the {@code retry} field.
* @param retry the value of the retry field
* @return {@code this} builder
*/
Builder<T> retry(Duration retry);
/**
* Set SSE comment. If a multi-line comment is provided, it will be turned into multiple
* SSE comment lines as defined in Server-Sent Events W3C recommendation.
* @param comment the comment to set
* @return {@code this} builder
*/
Builder<T> comment(String comment);
/**
* Set the value of the {@code data} field. If the {@code data} argument is a multi-line
* {@code String}, it will be turned into multiple {@code data} field lines as defined
* in the Server-Sent Events W3C recommendation. If {@code data} is not a String, it will
* be {@linkplain org.springframework.http.codec.json.Jackson2JsonEncoder encoded} into JSON.
* @param data the value of the data field
* @return {@code this} builder
*/
Builder<T> data(@Nullable T data);
/**
* Builds the event.
* @return the built event
*/
ServerSentEvent<T> build();
}
private static class BuilderImpl<T> implements Builder<T> {
@Nullable
private String id;
@Nullable
private String event;
@Nullable
private Duration retry;
@Nullable
private String comment;
@Nullable
private T data;
public BuilderImpl() {
}
public BuilderImpl(T data) {
this.data = data;
}
@Override
public Builder<T> id(String id) {
this.id = id;
return this;
}
@Override
public Builder<T> event(String event) {
this.event = event;
return this;
}
@Override
public Builder<T> retry(Duration retry) {
this.retry = retry;
return this;
}
@Override
public Builder<T> comment(String comment) {
this.comment = comment;
return this;
}
@Override
public Builder<T> data(@Nullable T data) {
this.data = data;
return this;
}
@Override
public ServerSentEvent<T> build() {
return new ServerSentEvent<>(this.id, this.event, this.retry, this.comment, this.data);
}
}
}