Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Mar 3, 2023
1 parent 15e0042 commit 1fa8765
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,11 +15,11 @@

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
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;
Expand All @@ -34,6 +33,12 @@ class Endpoint {

private static final Logger LOGGER = LoggerFactory.getLogger(Endpoint.class);
private final List<CloudEvent> events = new ArrayList<>();
private final ObjectMapper om;

@Inject
Endpoint(ObjectMapper om) {
this.om = om;
}

void init(@Observes StartupEvent ignored, ObjectMapper om)
throws JsonProcessingException {
Expand All @@ -51,18 +56,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<CloudEvent> 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<Event> events() {
return Multi.createFrom()
.iterable(events)
.map(this::workaroundQuarkus31587);
}

@POST
Expand All @@ -73,6 +71,17 @@ public void receive(CloudEvent event) {
LOGGER.info("Received event: {}", event);
}

/**
* A workaround for
* <a href="https://github.com/quarkusio/quarkus/issues/31587">quarkusio/quarkus#31587</a>
* and <a href="https://github.com/cloudevents/sdk-java/issues/533">cloudevents/sdk-java#533</a>.
*
* TODO: Remove this method once the above issues is fixed.
*/
private Event workaroundQuarkus31587(CloudEvent event) {
return Event.from(event, om);
}

private static final class Score {
@JsonProperty
Play play;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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;

/**
* A workaround for
* <a href="https://github.com/quarkusio/quarkus/issues/31587">quarkusio/quarkus#31587</a>
* and <a href="https://github.com/cloudevents/sdk-java/issues/533">cloudevents/sdk-java#533</a>.
*
* TODO: Remove this class once the above issues is fixed.
*/
class Event {
@JsonProperty
String id;
@JsonProperty
String source;
@JsonProperty
String type;
@JsonProperty("specversion")
String specVersion;
@JsonProperty("datacontenttype")
String dataContentType;
@JsonProperty
Map<String, Object> data;

static Event from(CloudEvent event, ObjectMapper om) {
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, om);
return e;
}

@Nullable
private static Map<String, Object> dataToMap(CloudEvent event, ObjectMapper om) {
var data = event.getData();
if (data == null) {
return null;
}
var mt = MediaType.valueOf(event.getDataContentType());
if (mt.isCompatible(MediaType.APPLICATION_JSON_TYPE)) {
try {
return om.readValue(data.toBytes(), Map.class);
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}
throw new IllegalArgumentException("Unsupported media type: " + mt);
}

}

0 comments on commit 1fa8765

Please sign in to comment.