From a8e64eaff5d1c1d6f2c7b9dc5a498e58901e5b65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20Suszy=C5=84ski?= Date: Fri, 3 Mar 2023 16:29:50 +0100 Subject: [PATCH] Workaround for https://github.com/quarkusio/quarkus/issues/31587 and https://github.com/cloudevents/sdk-java/issues/533 --- .../knative/showcase/events/Endpoint.java | 19 ++----- .../knative/showcase/events/Event.java | 55 +++++++++++++++++++ 2 files changed, 60 insertions(+), 14 deletions(-) create mode 100644 quarkus/src/main/java/com/redhat/openshift/knative/showcase/events/Event.java diff --git a/quarkus/src/main/java/com/redhat/openshift/knative/showcase/events/Endpoint.java b/quarkus/src/main/java/com/redhat/openshift/knative/showcase/events/Endpoint.java index 777c6fc..2917a3b 100644 --- a/quarkus/src/main/java/com/redhat/openshift/knative/showcase/events/Endpoint.java +++ b/quarkus/src/main/java/com/redhat/openshift/knative/showcase/events/Endpoint.java @@ -5,7 +5,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; import io.cloudevents.CloudEvent; import io.cloudevents.core.builder.CloudEventBuilder; -import io.cloudevents.http.restful.ws.StructuredEncoding; import io.cloudevents.jackson.JsonFormat; import io.quarkus.runtime.StartupEvent; import io.smallrye.mutiny.Multi; @@ -20,7 +19,6 @@ import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; -import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import java.net.URI; import java.util.ArrayList; @@ -51,18 +49,11 @@ void init(@Observes StartupEvent ignored, ObjectMapper om) @GET @Operation(summary = "Retrieves all registered events as a JSON stream") - @RestStreamElementType(MediaType.APPLICATION_JSON) - @StructuredEncoding(JsonFormat.CONTENT_TYPE) - public Multi events() { - return Multi.createFrom().iterable(events); - } - - @GET - @Path("last") - @Produces(MediaType.APPLICATION_JSON) - @StructuredEncoding(JsonFormat.CONTENT_TYPE) - public CloudEvent last() { - return events.get(events.size() - 1); + @RestStreamElementType(JsonFormat.CONTENT_TYPE) + public Multi events() { + return Multi.createFrom() + .iterable(events) + .map(Event::from); } @POST diff --git a/quarkus/src/main/java/com/redhat/openshift/knative/showcase/events/Event.java b/quarkus/src/main/java/com/redhat/openshift/knative/showcase/events/Event.java new file mode 100644 index 0000000..c6b24f0 --- /dev/null +++ b/quarkus/src/main/java/com/redhat/openshift/knative/showcase/events/Event.java @@ -0,0 +1,55 @@ +package com.redhat.openshift.knative.showcase.events; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.cloudevents.CloudEvent; + +import javax.annotation.Nullable; +import javax.ws.rs.core.MediaType; +import java.io.IOException; +import java.util.Map; + +class Event { + @JsonProperty + String id; + @JsonProperty + String source; + @JsonProperty + String type; + @JsonProperty("specversion") + String specVersion; + @JsonProperty("datacontenttype") + String dataContentType; + @JsonProperty + Map data; + + static Event from(CloudEvent event) { + var e = new Event(); + e.id = event.getId(); + e.source = event.getSource().toString(); + e.type = event.getType(); + e.specVersion = event.getSpecVersion().toString(); + e.dataContentType = event.getDataContentType(); + e.data = dataToMap(event); + return e; + } + + @Nullable + private static Map dataToMap(CloudEvent event) { + var data = event.getData(); + if (data == null) { + return null; + } + var mt = MediaType.valueOf(event.getDataContentType()); + if (mt.isCompatible(MediaType.APPLICATION_JSON_TYPE)) { + ObjectMapper om = new ObjectMapper(); + try { + return om.readValue(data.toBytes(), Map.class); + } catch (IOException e) { + throw new IllegalArgumentException(e); + } + } + throw new IllegalArgumentException("Unsupported media type: " + mt); + } + +}