From 6dfbd1c014c0ae824152d32cf50184849cfe7166 Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Mon, 24 Feb 2025 10:11:53 +1300 Subject: [PATCH] Move SseHandler into sse package and use SseClient.handler() method --- avaje-jex/src/main/java/io/avaje/jex/Jex.java | 2 +- avaje-jex/src/main/java/io/avaje/jex/Routing.java | 4 ++-- .../main/java/io/avaje/jex/http/sse/SseClient.java | 12 +++++------- .../java/io/avaje/jex/{ => http/sse}/SseHandler.java | 7 +++---- 4 files changed, 11 insertions(+), 14 deletions(-) rename avaje-jex/src/main/java/io/avaje/jex/{ => http/sse}/SseHandler.java (89%) diff --git a/avaje-jex/src/main/java/io/avaje/jex/Jex.java b/avaje-jex/src/main/java/io/avaje/jex/Jex.java index f2c399e9..ef9e668e 100644 --- a/avaje-jex/src/main/java/io/avaje/jex/Jex.java +++ b/avaje-jex/src/main/java/io/avaje/jex/Jex.java @@ -153,7 +153,7 @@ default Jex options(String path, ExchangeHandler handler, Role... roles) { * @param roles An array of roles that are associated with this endpoint. */ default Jex sse(String path, Consumer handler, Role... roles) { - return get(path, new SseHandler(handler), roles); + return get(path, SseClient.handler(handler), roles); } /** Add a filter for all matched requests. */ diff --git a/avaje-jex/src/main/java/io/avaje/jex/Routing.java b/avaje-jex/src/main/java/io/avaje/jex/Routing.java index 76aa110a..dfa23a38 100644 --- a/avaje-jex/src/main/java/io/avaje/jex/Routing.java +++ b/avaje-jex/src/main/java/io/avaje/jex/Routing.java @@ -156,8 +156,8 @@ default Routing after(Consumer handler) { * @param handler The sse handler to invoke when a GET request matches the path. * @param roles An array of roles that are associated with this endpoint. */ - default Routing sse(String path, Consumer consumer, Role... roles) { - return get(path, new SseHandler(consumer), roles); + default Routing sse(String path, Consumer handler, Role... roles) { + return get(path, SseClient.handler(handler), roles); } /** Return all the registered handlers. */ diff --git a/avaje-jex/src/main/java/io/avaje/jex/http/sse/SseClient.java b/avaje-jex/src/main/java/io/avaje/jex/http/sse/SseClient.java index cfbf0dc2..6f7f111d 100644 --- a/avaje-jex/src/main/java/io/avaje/jex/http/sse/SseClient.java +++ b/avaje-jex/src/main/java/io/avaje/jex/http/sse/SseClient.java @@ -1,8 +1,10 @@ package io.avaje.jex.http.sse; import java.io.Closeable; +import java.util.function.Consumer; import io.avaje.jex.http.Context; +import io.avaje.jex.http.ExchangeHandler; import io.avaje.jex.spi.JsonService; /** @@ -16,13 +18,9 @@ */ public sealed interface SseClient extends Closeable permits SseClientImpl { - /** - * @param ctx - * @return the new SseClient instance - */ - static SseClient of(Context ctx) { - - return new SseClientImpl(ctx); + /** Return an SseClient handler. */ + static ExchangeHandler handler(Consumer consumer) { + return new SseHandler(consumer); } /** Close the SseClient and release keepAlive block if any */ diff --git a/avaje-jex/src/main/java/io/avaje/jex/SseHandler.java b/avaje-jex/src/main/java/io/avaje/jex/http/sse/SseHandler.java similarity index 89% rename from avaje-jex/src/main/java/io/avaje/jex/SseHandler.java rename to avaje-jex/src/main/java/io/avaje/jex/http/sse/SseHandler.java index 4eae1bbd..4ceea3b4 100644 --- a/avaje-jex/src/main/java/io/avaje/jex/SseHandler.java +++ b/avaje-jex/src/main/java/io/avaje/jex/http/sse/SseHandler.java @@ -1,4 +1,4 @@ -package io.avaje.jex; +package io.avaje.jex.http.sse; import java.io.IOException; import java.io.UncheckedIOException; @@ -8,10 +8,9 @@ import io.avaje.jex.http.BadRequestException; import io.avaje.jex.http.Context; import io.avaje.jex.http.ExchangeHandler; -import io.avaje.jex.http.sse.SseClient; /** Handler that configures a request for Server Sent Events */ -class SseHandler implements ExchangeHandler { +final class SseHandler implements ExchangeHandler { private static final String TEXT_EVENT_STREAM = "text/event-stream"; private final Consumer consumer; @@ -34,7 +33,7 @@ public void handle(Context ctx) throws Exception { headers.add("Cache-Control", "no-cache"); headers.add("X-Accel-Buffering", "no"); // See https://serverfault.com/a/801629 - try (var sse = SseClient.of(ctx)) { + try (var sse = new SseClientImpl(ctx)) { exchange.sendResponseHeaders(200, 0); consumer.accept(sse); } catch (final IOException e) {