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
77 additions
and
59 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
61 changes: 61 additions & 0 deletions
61
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,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); | ||
} | ||
|
||
} |