Skip to content

Commit

Permalink
Allows an SSE event to be created using no data. Uses a static consta…
Browse files Browse the repository at this point in the history
…nt SseEvent.NO_DATA to avoid returning null in getters. Updates serialization code and tests.
  • Loading branch information
spericas committed Aug 27, 2024
1 parent 9aa5681 commit bfca29d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
14 changes: 8 additions & 6 deletions http/sse/src/main/java/io/helidon/http/sse/SseEvent.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
* Copyright (c) 2023, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -38,6 +38,11 @@
*/
public class SseEvent {

/**
* Value returned by {@link #data())} when no data has been set.
*/
public static final Object NO_DATA = new Object();

private static final WritableHeaders<?> EMPTY_HEADERS = WritableHeaders.create();

private final String id;
Expand Down Expand Up @@ -91,7 +96,7 @@ public static Builder builder() {
/**
* Get data for this event.
*
* @return the data
* @return the data or {@link #NO_DATA}
*/
public Object data() {
return data;
Expand Down Expand Up @@ -218,7 +223,7 @@ public static class Builder implements io.helidon.common.Builder<Builder, SseEve

private String id;
private String name;
private Object data;
private Object data = NO_DATA;
private String comment;
private MediaType mediaType;
private Duration reconnectMillis;
Expand All @@ -229,9 +234,6 @@ private Builder() {

@Override
public SseEvent build() {
if (data == null) {
throw new IllegalArgumentException("Event must include some data");
}
return new SseEvent(this);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
* Copyright (c) 2023, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -94,7 +94,7 @@ public SseSink emit(SseEvent sseEvent) {
outputStream.write(SSE_NL);
}
Object data = sseEvent.data();
if (data != null) {
if (data != SseEvent.NO_DATA) {
outputStream.write(SSE_DATA);
eventConsumer.accept(data, sseEvent.mediaType().orElse(MediaTypes.TEXT_PLAIN));
outputStream.write(SSE_NL);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
* Copyright (c) 2023, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -90,4 +90,20 @@ void testGoodJsonb() {
assertThat(json, is(notNullValue()));
assertThat(json.getHello(), is("world"));
}

@Test
void testNoData() {
SseEvent event = SseEvent.builder().build();
assertThat(event, is(notNullValue()));
assertThat(event.data(), is(SseEvent.NO_DATA));
}

@Test
void testConvertNoData() {
SseEvent event = SseEvent.builder()
.mediaContext(mediaContext)
.build();
assertThrows(IllegalStateException.class, () -> event.data(Object.class));
assertThrows(IllegalStateException.class, () -> event.data(Object.class, MediaTypes.TEXT_PLAIN));
}
}

0 comments on commit bfca29d

Please sign in to comment.