Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Mar 7, 2023
1 parent 15e0042 commit a86a0a2
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,32 +1,21 @@
package com.redhat.openshift.knative.showcase.events;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
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;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.jboss.resteasy.reactive.RestStreamElementType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
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;
import java.util.List;
import java.util.UUID;
import java.util.random.RandomGenerator;

@Path("events")
@ApplicationScoped
Expand All @@ -35,64 +24,32 @@ class Endpoint {
private static final Logger LOGGER = LoggerFactory.getLogger(Endpoint.class);
private final List<CloudEvent> events = new ArrayList<>();

void init(@Observes StartupEvent ignored, ObjectMapper om)
throws JsonProcessingException {
var rg = RandomGenerator.getDefault();
for (int i = 0; i < 3; i++) {
var s = Score.random(rg);
events.add(CloudEventBuilder.v1()
.withId(String.valueOf(i))
.withSource(URI.create("//localhost/dev"))
.withType(Endpoint.class.getName())
.withData(MediaType.APPLICATION_JSON, om.writeValueAsBytes(s))
.build());
}
}

@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
@Consumes(MediaType.APPLICATION_JSON)
@Consumes({MediaType.APPLICATION_JSON, JsonFormat.CONTENT_TYPE})
@Operation(summary = "Receives a CloudEvent and stores it")
public void receive(CloudEvent event) {
events.add(event);
LOGGER.info("Received event: {}", event);
LOGGER.debug("Received event: {}", event);
}

private static final class Score {
@JsonProperty
Play play;
@JsonProperty
int score;

static Score random(RandomGenerator rg) {
var s = new Score();
s.score = rg.nextInt(1_000);
s.play = new Play();
s.play.id = new UUID(rg.nextLong(), rg.nextLong()).toString();
s.play.game = rg.nextInt(300);
return s;
}
/**
* 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 class Play {
@JsonProperty
String id;
@JsonProperty
Integer game;
}
}
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 a86a0a2

Please sign in to comment.