From bfca29d6ee29fdf41d94e61de5cad90609d47df1 Mon Sep 17 00:00:00 2001 From: Santiago Pericas-Geertsen Date: Tue, 27 Aug 2024 09:35:52 -0400 Subject: [PATCH] Allows an SSE event to be created using no data. Uses a static constant SseEvent.NO_DATA to avoid returning null in getters. Updates serialization code and tests. --- .../java/io/helidon/http/sse/SseEvent.java | 14 ++++++++------ .../java/io/helidon/webserver/sse/SseSink.java | 4 ++-- .../webserver/tests/sse/SseEventTest.java | 18 +++++++++++++++++- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/http/sse/src/main/java/io/helidon/http/sse/SseEvent.java b/http/sse/src/main/java/io/helidon/http/sse/SseEvent.java index add347f4662..24a94e35f49 100644 --- a/http/sse/src/main/java/io/helidon/http/sse/SseEvent.java +++ b/http/sse/src/main/java/io/helidon/http/sse/SseEvent.java @@ -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. @@ -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; @@ -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; @@ -218,7 +223,7 @@ public static class Builder implements io.helidon.common.Builder event.data(Object.class)); + assertThrows(IllegalStateException.class, () -> event.data(Object.class, MediaTypes.TEXT_PLAIN)); + } }