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 a8e64ea
Show file tree
Hide file tree
Showing 2 changed files with 60 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 @@ -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;
Expand Down Expand Up @@ -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<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(Event::from);
}

@POST
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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<String, Object> 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);
}

}

0 comments on commit a8e64ea

Please sign in to comment.