use of io.cloudevents.core.builder.CloudEventBuilder in project incubator-eventmesh by apache.
the class TcpMessageProtocolResolver method buildEvent.
public static CloudEvent buildEvent(Header header, String cloudEventJson) throws ProtocolHandleException {
CloudEventBuilder cloudEventBuilder;
String protocolType = header.getProperty(Constants.PROTOCOL_TYPE).toString();
String protocolVersion = header.getProperty(Constants.PROTOCOL_VERSION).toString();
String protocolDesc = header.getProperty(Constants.PROTOCOL_DESC).toString();
if (StringUtils.isBlank(protocolType) || StringUtils.isBlank(protocolVersion) || StringUtils.isBlank(protocolDesc)) {
throw new ProtocolHandleException(String.format("invalid protocol params protocolType %s|protocolVersion %s|protocolDesc %s", protocolType, protocolVersion, protocolDesc));
}
if (!StringUtils.equals(CloudEventsProtocolConstant.PROTOCOL_NAME, protocolType)) {
throw new ProtocolHandleException(String.format("Unsupported protocolType: %s", protocolType));
}
if (StringUtils.equals(SpecVersion.V1.toString(), protocolVersion)) {
// todo:resolve different format
EventFormat eventFormat = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE);
Preconditions.checkNotNull(eventFormat, String.format("EventFormat: %s is not supported", JsonFormat.CONTENT_TYPE));
CloudEvent event = eventFormat.deserialize(cloudEventJson.getBytes(StandardCharsets.UTF_8));
cloudEventBuilder = CloudEventBuilder.v1(event);
for (String propKey : header.getProperties().keySet()) {
cloudEventBuilder.withExtension(propKey, header.getProperty(propKey).toString());
}
return cloudEventBuilder.build();
} else if (StringUtils.equals(SpecVersion.V03.toString(), protocolVersion)) {
// todo:resolve different format
CloudEvent event = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE).deserialize(cloudEventJson.getBytes(StandardCharsets.UTF_8));
cloudEventBuilder = CloudEventBuilder.v03(event);
for (String propKey : header.getProperties().keySet()) {
cloudEventBuilder.withExtension(propKey, header.getProperty(propKey).toString());
}
return cloudEventBuilder.build();
} else {
throw new ProtocolHandleException(String.format("Unsupported protocolVersion: %s", protocolVersion));
}
}
use of io.cloudevents.core.builder.CloudEventBuilder in project incubator-eventmesh by apache.
the class GrpcMessageProtocolResolver method buildEvent.
public static CloudEvent buildEvent(SimpleMessage message) throws ProtocolHandleException {
try {
RequestHeader requestHeader = message.getHeader();
String protocolType = requestHeader.getProtocolType();
String protocolDesc = requestHeader.getProtocolDesc();
String protocolVersion = requestHeader.getProtocolVersion();
String env = requestHeader.getEnv();
String idc = requestHeader.getIdc();
String ip = requestHeader.getIp();
String pid = requestHeader.getPid();
String sys = requestHeader.getSys();
String username = requestHeader.getUsername();
String passwd = requestHeader.getPassword();
String language = requestHeader.getLanguage();
String content = message.getContent();
CloudEvent event = null;
CloudEventBuilder cloudEventBuilder;
if (StringUtils.equals(SpecVersion.V1.toString(), protocolVersion)) {
cloudEventBuilder = CloudEventBuilder.v1();
cloudEventBuilder = cloudEventBuilder.withId(message.getSeqNum()).withSubject(message.getTopic()).withType("eventmeshmessage").withSource(URI.create("/")).withData(content.getBytes(StandardCharsets.UTF_8)).withExtension(ProtocolKey.ENV, env).withExtension(ProtocolKey.IDC, idc).withExtension(ProtocolKey.IP, ip).withExtension(ProtocolKey.PID, pid).withExtension(ProtocolKey.SYS, sys).withExtension(ProtocolKey.USERNAME, username).withExtension(ProtocolKey.PASSWD, passwd).withExtension(ProtocolKey.LANGUAGE, language).withExtension(ProtocolKey.PROTOCOL_TYPE, protocolType).withExtension(ProtocolKey.PROTOCOL_DESC, protocolDesc).withExtension(ProtocolKey.PROTOCOL_VERSION, protocolVersion).withExtension(ProtocolKey.SEQ_NUM, message.getSeqNum()).withExtension(ProtocolKey.UNIQUE_ID, message.getUniqueId()).withExtension(ProtocolKey.PRODUCERGROUP, message.getProducerGroup()).withExtension(ProtocolKey.TTL, message.getTtl());
for (Map.Entry<String, String> entry : message.getPropertiesMap().entrySet()) {
cloudEventBuilder.withExtension(entry.getKey(), entry.getValue());
}
if (StringUtils.isNotEmpty(message.getTag())) {
cloudEventBuilder = cloudEventBuilder.withExtension(ProtocolKey.TAG, message.getTag());
}
event = cloudEventBuilder.build();
} else if (StringUtils.equals(SpecVersion.V03.toString(), protocolVersion)) {
cloudEventBuilder = CloudEventBuilder.v03();
cloudEventBuilder = cloudEventBuilder.withId(message.getSeqNum()).withSubject(message.getTopic()).withType("eventmeshmessage").withSource(URI.create("/")).withData(content.getBytes(StandardCharsets.UTF_8)).withExtension(ProtocolKey.ENV, env).withExtension(ProtocolKey.IDC, idc).withExtension(ProtocolKey.IP, ip).withExtension(ProtocolKey.PID, pid).withExtension(ProtocolKey.SYS, sys).withExtension(ProtocolKey.USERNAME, username).withExtension(ProtocolKey.PASSWD, passwd).withExtension(ProtocolKey.LANGUAGE, language).withExtension(ProtocolKey.PROTOCOL_TYPE, protocolType).withExtension(ProtocolKey.PROTOCOL_DESC, protocolDesc).withExtension(ProtocolKey.PROTOCOL_VERSION, protocolVersion).withExtension(ProtocolKey.SEQ_NUM, message.getSeqNum()).withExtension(ProtocolKey.UNIQUE_ID, message.getUniqueId()).withExtension(ProtocolKey.PRODUCERGROUP, message.getProducerGroup()).withExtension(ProtocolKey.TTL, message.getTtl());
for (Map.Entry<String, String> entry : message.getPropertiesMap().entrySet()) {
cloudEventBuilder.withExtension(entry.getKey(), entry.getValue());
}
if (StringUtils.isNotEmpty(message.getTag())) {
cloudEventBuilder = cloudEventBuilder.withExtension(ProtocolKey.TAG, message.getTag());
}
event = cloudEventBuilder.build();
}
return event;
} catch (Exception e) {
throw new ProtocolHandleException(e.getMessage(), e.getCause());
}
}
use of io.cloudevents.core.builder.CloudEventBuilder in project incubator-eventmesh by apache.
the class SendMessageBatchV2ProtocolResolver method buildEvent.
public static CloudEvent buildEvent(Header header, Body body) throws ProtocolHandleException {
try {
SendMessageBatchV2RequestHeader sendMessageBatchV2RequestHeader = (SendMessageBatchV2RequestHeader) header;
SendMessageBatchV2RequestBody sendMessageBatchV2RequestBody = (SendMessageBatchV2RequestBody) body;
String protocolType = sendMessageBatchV2RequestHeader.getProtocolType();
String protocolDesc = sendMessageBatchV2RequestHeader.getProtocolDesc();
String protocolVersion = sendMessageBatchV2RequestHeader.getProtocolVersion();
String code = sendMessageBatchV2RequestHeader.getCode();
String env = sendMessageBatchV2RequestHeader.getEnv();
String idc = sendMessageBatchV2RequestHeader.getIdc();
String ip = sendMessageBatchV2RequestHeader.getIp();
String pid = sendMessageBatchV2RequestHeader.getPid();
String sys = sendMessageBatchV2RequestHeader.getSys();
String username = sendMessageBatchV2RequestHeader.getUsername();
String passwd = sendMessageBatchV2RequestHeader.getPasswd();
ProtocolVersion version = sendMessageBatchV2RequestHeader.getVersion();
String language = sendMessageBatchV2RequestHeader.getLanguage();
String content = sendMessageBatchV2RequestBody.getMsg();
CloudEvent event = null;
CloudEventBuilder cloudEventBuilder;
if (StringUtils.equals(SpecVersion.V1.toString(), protocolVersion)) {
cloudEventBuilder = CloudEventBuilder.v1();
cloudEventBuilder = cloudEventBuilder.withId(sendMessageBatchV2RequestBody.getBizSeqNo()).withSubject(sendMessageBatchV2RequestBody.getTopic()).withType("eventmeshmessage").withSource(URI.create("/")).withData(content.getBytes(StandardCharsets.UTF_8)).withExtension(ProtocolKey.REQUEST_CODE, code).withExtension(ProtocolKey.ClientInstanceKey.ENV, env).withExtension(ProtocolKey.ClientInstanceKey.IDC, idc).withExtension(ProtocolKey.ClientInstanceKey.IP, ip).withExtension(ProtocolKey.ClientInstanceKey.PID, pid).withExtension(ProtocolKey.ClientInstanceKey.SYS, sys).withExtension(ProtocolKey.ClientInstanceKey.USERNAME, username).withExtension(ProtocolKey.ClientInstanceKey.PASSWD, passwd).withExtension(ProtocolKey.VERSION, version.getVersion()).withExtension(ProtocolKey.LANGUAGE, language).withExtension(ProtocolKey.PROTOCOL_TYPE, protocolType).withExtension(ProtocolKey.PROTOCOL_DESC, protocolDesc).withExtension(ProtocolKey.PROTOCOL_VERSION, protocolVersion).withExtension(SendMessageBatchV2RequestBody.BIZSEQNO, sendMessageBatchV2RequestBody.getBizSeqNo()).withExtension(SendMessageBatchV2RequestBody.PRODUCERGROUP, sendMessageBatchV2RequestBody.getProducerGroup()).withExtension(SendMessageBatchV2RequestBody.TTL, sendMessageBatchV2RequestBody.getTtl());
if (StringUtils.isNotEmpty(sendMessageBatchV2RequestBody.getTag())) {
cloudEventBuilder = cloudEventBuilder.withExtension(SendMessageRequestBody.TAG, sendMessageBatchV2RequestBody.getTag());
}
event = cloudEventBuilder.build();
} else if (StringUtils.equals(SpecVersion.V03.toString(), protocolVersion)) {
cloudEventBuilder = CloudEventBuilder.v03();
cloudEventBuilder = cloudEventBuilder.withId(sendMessageBatchV2RequestBody.getBizSeqNo()).withSubject(sendMessageBatchV2RequestBody.getTopic()).withType("eventmeshmessage").withSource(URI.create("/")).withData(content.getBytes(StandardCharsets.UTF_8)).withExtension(ProtocolKey.REQUEST_CODE, code).withExtension(ProtocolKey.ClientInstanceKey.ENV, env).withExtension(ProtocolKey.ClientInstanceKey.IDC, idc).withExtension(ProtocolKey.ClientInstanceKey.IP, ip).withExtension(ProtocolKey.ClientInstanceKey.PID, pid).withExtension(ProtocolKey.ClientInstanceKey.SYS, sys).withExtension(ProtocolKey.ClientInstanceKey.USERNAME, username).withExtension(ProtocolKey.ClientInstanceKey.PASSWD, passwd).withExtension(ProtocolKey.VERSION, version.getVersion()).withExtension(ProtocolKey.LANGUAGE, language).withExtension(ProtocolKey.PROTOCOL_TYPE, protocolType).withExtension(ProtocolKey.PROTOCOL_DESC, protocolDesc).withExtension(ProtocolKey.PROTOCOL_VERSION, protocolVersion).withExtension(SendMessageBatchV2RequestBody.BIZSEQNO, sendMessageBatchV2RequestBody.getBizSeqNo()).withExtension(SendMessageBatchV2RequestBody.PRODUCERGROUP, sendMessageBatchV2RequestBody.getProducerGroup()).withExtension(SendMessageBatchV2RequestBody.TTL, sendMessageBatchV2RequestBody.getTtl());
if (StringUtils.isNotEmpty(sendMessageBatchV2RequestBody.getTag())) {
cloudEventBuilder = cloudEventBuilder.withExtension(SendMessageRequestBody.TAG, sendMessageBatchV2RequestBody.getTag());
}
event = cloudEventBuilder.build();
}
return event;
} catch (Exception e) {
throw new ProtocolHandleException(e.getMessage(), e.getCause());
}
}
use of io.cloudevents.core.builder.CloudEventBuilder in project incubator-eventmesh by apache.
the class TcpMessageProtocolResolver method buildEvent.
public static CloudEvent buildEvent(Header header, EventMeshMessage message) throws ProtocolHandleException {
CloudEventBuilder cloudEventBuilder;
String protocolType = header.getProperty(Constants.PROTOCOL_TYPE).toString();
String protocolVersion = header.getProperty(Constants.PROTOCOL_VERSION).toString();
String protocolDesc = header.getProperty(Constants.PROTOCOL_DESC).toString();
if (StringUtils.isBlank(protocolType) || StringUtils.isBlank(protocolVersion) || StringUtils.isBlank(protocolDesc)) {
throw new ProtocolHandleException(String.format("invalid protocol params protocolType %s|protocolVersion %s|protocolDesc %s", protocolType, protocolVersion, protocolDesc));
}
if (!StringUtils.equals(MeshMessageProtocolConstant.PROTOCOL_NAME, protocolType)) {
throw new ProtocolHandleException(String.format("Unsupported protocolType: %s", protocolType));
}
String topic = message.getTopic();
String content = message.getBody();
if (StringUtils.equals(SpecVersion.V1.toString(), protocolVersion)) {
cloudEventBuilder = CloudEventBuilder.v1();
} else if (StringUtils.equals(SpecVersion.V03.toString(), protocolVersion)) {
cloudEventBuilder = CloudEventBuilder.v03();
} else {
throw new ProtocolHandleException(String.format("Unsupported protocolVersion: %s", protocolVersion));
}
cloudEventBuilder = cloudEventBuilder.withId(header.getSeq()).withSource(URI.create("/")).withType("eventmeshmessage").withSubject(topic).withData(content.getBytes(StandardCharsets.UTF_8));
for (String propKey : header.getProperties().keySet()) {
cloudEventBuilder.withExtension(propKey, header.getProperty(propKey).toString());
}
for (String propKey : message.getProperties().keySet()) {
cloudEventBuilder.withExtension(propKey, message.getProperties().get(propKey));
}
return cloudEventBuilder.build();
}
use of io.cloudevents.core.builder.CloudEventBuilder in project incubator-eventmesh by apache.
the class EventMeshClientUtil method buildMessage.
public static <T> T buildMessage(SimpleMessage message, String protocolType) {
String seq = message.getSeqNum();
String uniqueId = message.getUniqueId();
String content = message.getContent();
// This is GRPC response message
if (StringUtils.isEmpty(seq) && StringUtils.isEmpty(uniqueId)) {
HashMap<String, String> response = JsonUtils.deserialize(content, new TypeReference<HashMap<String, String>>() {
});
return (T) response;
}
if (EventMeshCommon.CLOUD_EVENTS_PROTOCOL_NAME.equals(protocolType)) {
String contentType = message.getPropertiesOrDefault(ProtocolKey.CONTENT_TYPE, JsonFormat.CONTENT_TYPE);
try {
CloudEvent cloudEvent = EventFormatProvider.getInstance().resolveFormat(contentType).deserialize(content.getBytes(StandardCharsets.UTF_8));
CloudEventBuilder cloudEventBuilder = CloudEventBuilder.from(cloudEvent).withSubject(message.getTopic()).withExtension(ProtocolKey.SEQ_NUM, message.getSeqNum()).withExtension(ProtocolKey.UNIQUE_ID, message.getUniqueId());
message.getPropertiesMap().forEach((k, v) -> cloudEventBuilder.withExtension(k, v));
return (T) cloudEventBuilder.build();
} catch (Throwable t) {
logger.warn("Error in building message. {}", t.getMessage());
return null;
}
} else {
EventMeshMessage eventMeshMessage = EventMeshMessage.builder().content(content).topic(message.getTopic()).bizSeqNo(seq).uniqueId(uniqueId).prop(message.getPropertiesMap()).build();
return (T) eventMeshMessage;
}
}
Aggregations