GH-422 Add initial set of Cloud Event samples
This commit adds initial set of samples and tests demonstrating various ways of how cloud event can be consumed (i.e., structured, bindary, pojo etc)
This commit is contained in:
@@ -17,7 +17,6 @@
|
||||
package org.springframework.cloud.function.context.config;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.cloud.function.json.JsonMapper;
|
||||
@@ -34,7 +33,7 @@ import org.springframework.util.MimeType;
|
||||
*
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
* @since 3.1.0
|
||||
* @since 3.1
|
||||
*/
|
||||
public class CloudEventJsonMessageConverter extends JsonMessageConverter {
|
||||
|
||||
@@ -47,12 +46,17 @@ public class CloudEventJsonMessageConverter extends JsonMessageConverter {
|
||||
|
||||
@Override
|
||||
protected Object convertFromInternal(Message<?> message, Class<?> targetClass, @Nullable Object conversionHint) {
|
||||
Type convertToType = conversionHint == null ? targetClass : (Type) conversionHint;
|
||||
String jsonString = (String) message.getPayload();
|
||||
Map<String, Object> mapEvent = this.mapper.fromJson(jsonString, Map.class);
|
||||
Object payload = this.mapper.fromJson(this.mapper.toJson(mapEvent.get("data")), convertToType);
|
||||
mapEvent.remove("data");
|
||||
return MessageBuilder.withPayload(payload).copyHeaders(mapEvent).build();
|
||||
if (this.isBinary(message)) {
|
||||
return super.convertFromInternal(message, targetClass, conversionHint);
|
||||
}
|
||||
else {
|
||||
Type convertToType = conversionHint == null ? targetClass : (Type) conversionHint;
|
||||
String jsonString = (String) message.getPayload();
|
||||
Map<String, Object> mapEvent = this.mapper.fromJson(jsonString, Map.class);
|
||||
Object payload = this.mapper.fromJson(this.mapper.toJson(mapEvent.get("data")), convertToType);
|
||||
mapEvent.remove("data");
|
||||
return MessageBuilder.withPayload(payload).copyHeaders(mapEvent).build();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -61,4 +65,8 @@ public class CloudEventJsonMessageConverter extends JsonMessageConverter {
|
||||
throw new UnsupportedOperationException("Temporarily not supported as this converter is work in progress");
|
||||
}
|
||||
|
||||
private boolean isBinary(Message<?> message) {
|
||||
Map<String, Object> headers = message.getHeaders();
|
||||
return headers.containsKey("source") && headers.containsKey("specversion") && headers.containsKey("type");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user