60
60
* @author Marek Potociar (marek.potociar at oracle.com)
61
61
*/
62
62
public class InboundEvent {
63
- private static final GenericType <String > STRING_AS_GENERIC_TYPE = new GenericType <String >(String .class );
63
+
64
+ private static final GenericType <String > STRING_AS_GENERIC_TYPE = new GenericType <>(String .class );
64
65
65
66
private final String name ;
66
67
private final String id ;
68
+ private final String comment ;
67
69
private final byte [] data ;
68
70
private final long reconnectDelay ;
69
71
@@ -76,6 +78,7 @@ public class InboundEvent {
76
78
* Inbound event builder. This implementation is not thread-safe.
77
79
*/
78
80
static class Builder {
81
+
79
82
private String name ;
80
83
private String id ;
81
84
private long reconnectDelay = SseFeature .RECONNECT_NOT_SET ;
@@ -85,6 +88,7 @@ static class Builder {
85
88
private final Annotation [] annotations ;
86
89
private final MediaType mediaType ;
87
90
private final MultivaluedMap <String , String > headers ;
91
+ private final StringBuilder commentBuilder ;
88
92
89
93
/**
90
94
* Create new inbound event builder.
@@ -106,12 +110,13 @@ public Builder(MessageBodyWorkers workers,
106
110
this .mediaType = mediaType ;
107
111
this .headers = headers ;
108
112
113
+ this .commentBuilder = new StringBuilder ();
109
114
this .dataStream = new ByteArrayOutputStream ();
110
115
}
111
116
112
117
/**
113
118
* Set inbound event name.
114
- *
119
+ * <p/>
115
120
* Value of the received SSE {@code "event"} field.
116
121
*
117
122
* @param name {@code "event"} field value.
@@ -124,7 +129,7 @@ public Builder name(String name) {
124
129
125
130
/**
126
131
* Set inbound event identifier.
127
- *
132
+ * <p/>
128
133
* Value of the received SSE {@code "id"} field.
129
134
*
130
135
* @param id {@code "id"} field value.
@@ -135,12 +140,32 @@ public Builder id(String id) {
135
140
return this ;
136
141
}
137
142
143
+ /**
144
+ * Add a comment line to the event.
145
+ * <p>
146
+ * The comment line will be added to the received SSE event comment as a new line in the comment field.
147
+ * If the comment line parameter is {@code null}, the call will be ignored.
148
+ * </p>
149
+ *
150
+ * @param commentLine comment line to be added to the event comment.
151
+ * @return updated builder instance.
152
+ * @since 2.21
153
+ */
154
+ public Builder commentLine (final CharSequence commentLine ) {
155
+ if (commentLine != null ) {
156
+ commentBuilder .append (commentLine ).append ('\n' );
157
+ }
158
+
159
+ return this ;
160
+ }
161
+
138
162
/**
139
163
* Set reconnection delay (in milliseconds) that indicates how long the event receiver should wait
140
164
* before attempting to reconnect in case a connection to SSE event source is lost.
141
165
* <p>
142
166
* Value of the received SSE {@code "retry"} field.
143
167
* </p>
168
+ *
144
169
* @param milliseconds reconnection delay in milliseconds. Negative values un-set the reconnection delay.
145
170
* @return updated builder instance.
146
171
* @since 2.3
@@ -181,6 +206,7 @@ public InboundEvent build() {
181
206
return new InboundEvent (
182
207
name ,
183
208
id ,
209
+ commentBuilder .length () > 0 ? commentBuilder .substring (0 , commentBuilder .length () - 1 ) : null ,
184
210
reconnectDelay ,
185
211
dataStream .toByteArray (),
186
212
workers ,
@@ -190,16 +216,18 @@ public InboundEvent build() {
190
216
}
191
217
}
192
218
193
- private InboundEvent (String name ,
194
- String id ,
195
- long reconnectDelay ,
196
- byte [] data ,
197
- MessageBodyWorkers messageBodyWorkers ,
198
- Annotation [] annotations ,
199
- MediaType mediaType ,
200
- MultivaluedMap <String , String > headers ) {
219
+ private InboundEvent (final String name ,
220
+ final String id ,
221
+ final String comment ,
222
+ final long reconnectDelay ,
223
+ final byte [] data ,
224
+ final MessageBodyWorkers messageBodyWorkers ,
225
+ final Annotation [] annotations ,
226
+ final MediaType mediaType ,
227
+ final MultivaluedMap <String , String > headers ) {
201
228
this .name = name ;
202
229
this .id = id ;
230
+ this .comment = comment ;
203
231
this .reconnectDelay = reconnectDelay ;
204
232
this .data = data ;
205
233
this .messageBodyWorkers = messageBodyWorkers ;
@@ -235,6 +263,20 @@ public String getId() {
235
263
return id ;
236
264
}
237
265
266
+ /**
267
+ * Get a comment string that accompanies the event.
268
+ * <p>
269
+ * Contains value of the comment associated with SSE event. This field is optional. Method may return {@code null},
270
+ * if the event comment is not specified.
271
+ * </p>
272
+ *
273
+ * @return comment associated with the event.
274
+ * @since 2.21
275
+ */
276
+ public String getComment () {
277
+ return comment ;
278
+ }
279
+
238
280
/**
239
281
* Get new connection retry time in milliseconds the event receiver should wait before attempting to
240
282
* reconnect after a connection to the SSE event source is lost.
@@ -273,8 +315,7 @@ public boolean isEmpty() {
273
315
* Get the original event data string {@link String}.
274
316
*
275
317
* @return event data de-serialized into a string.
276
- * @throws javax.ws.rs.ProcessingException
277
- * when provided type can't be read. The thrown exception wraps the original cause.
318
+ * @throws javax.ws.rs.ProcessingException when provided type can't be read. The thrown exception wraps the original cause.
278
319
* @since 2.3
279
320
*/
280
321
public String readData () {
@@ -286,8 +327,7 @@ public String readData() {
286
327
*
287
328
* @param type Java type to be used for event data de-serialization.
288
329
* @return event data de-serialized as an instance of a given type.
289
- * @throws javax.ws.rs.ProcessingException
290
- * when provided type can't be read. The thrown exception wraps the original cause.
330
+ * @throws javax.ws.rs.ProcessingException when provided type can't be read. The thrown exception wraps the original cause.
291
331
* @since 2.3
292
332
*/
293
333
public <T > T readData (Class <T > type ) {
@@ -299,10 +339,10 @@ public <T> T readData(Class<T> type) {
299
339
*
300
340
* @param type generic type to be used for event data de-serialization.
301
341
* @return event data de-serialized as an instance of a given type.
302
- * @throws javax.ws.rs.ProcessingException
303
- * when provided type can't be read. The thrown exception wraps the original cause.
342
+ * @throws javax.ws.rs.ProcessingException when provided type can't be read. The thrown exception wraps the original cause.
304
343
* @since 2.3
305
344
*/
345
+ @ SuppressWarnings ("unused" )
306
346
public <T > T readData (GenericType <T > type ) {
307
347
return readData (type , null );
308
348
}
@@ -313,22 +353,21 @@ public <T> T readData(GenericType<T> type) {
313
353
* @param messageType Java type to be used for event data de-serialization.
314
354
* @param mediaType {@link MediaType media type} to be used for event data de-serialization.
315
355
* @return event data de-serialized as an instance of a given type.
316
- * @throws javax.ws.rs.ProcessingException
317
- * when provided type can't be read. The thrown exception wraps the original cause.
356
+ * @throws javax.ws.rs.ProcessingException when provided type can't be read. The thrown exception wraps the original cause.
318
357
* @since 2.3
319
358
*/
359
+ @ SuppressWarnings ("unused" )
320
360
public <T > T readData (Class <T > messageType , MediaType mediaType ) {
321
361
return readData (new GenericType <T >(messageType ), mediaType );
322
362
}
323
363
324
364
/**
325
365
* Read event data as a given generic type.
326
366
*
327
- * @param type generic type to be used for event data de-serialization.
328
- * @param mediaType {@link MediaType media type} to be used for event data de-serialization.
367
+ * @param type generic type to be used for event data de-serialization.
368
+ * @param mediaType {@link MediaType media type} to be used for event data de-serialization.
329
369
* @return event data de-serialized as an instance of a given type.
330
- * @throws javax.ws.rs.ProcessingException
331
- * when provided type can't be read. The thrown exception wraps the original cause.
370
+ * @throws javax.ws.rs.ProcessingException when provided type can't be read. The thrown exception wraps the original cause.
332
371
* @since 2.3
333
372
*/
334
373
public <T > T readData (GenericType <T > type , MediaType mediaType ) {
@@ -360,8 +399,9 @@ private <T> T readAndCast(GenericType<T> type, MediaType effectiveMediaType, Mes
360
399
* Get the raw event data bytes.
361
400
*
362
401
* @return raw event data bytes. The returned byte array may be empty if the event does not
363
- * contain any data.
402
+ * contain any data.
364
403
*/
404
+ @ SuppressWarnings ("unused" )
365
405
public byte [] getRawData () {
366
406
if (isEmpty ()) {
367
407
return data ;
@@ -383,6 +423,7 @@ public String toString() {
383
423
return "InboundEvent{"
384
424
+ "name='" + name + '\''
385
425
+ ", id='" + id + '\''
426
+ + ", comment=" + (comment == null ? "[no comments]" : '\'' + comment + '\'' )
386
427
+ ", data=" + s
387
428
+ '}' ;
388
429
}
0 commit comments