forked from openshift-knative/showcase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Workaround for quarkusio/quarkus#31587 and cloudevents/sdk-java#533
- Loading branch information
Showing
2 changed files
with
60 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
quarkus/src/main/java/com/redhat/openshift/knative/showcase/events/Event.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |