use of org.apache.eventmesh.protocol.api.exception.ProtocolHandleException 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 producerGroup = sendMessageBatchV2RequestBody.getProducerGroup();
String content = sendMessageBatchV2RequestBody.getMsg();
CloudEvent event = null;
if (StringUtils.equals(SpecVersion.V1.toString(), protocolVersion)) {
event = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE).deserialize(content.getBytes(StandardCharsets.UTF_8));
event = CloudEventBuilder.from(event).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.PRODUCERGROUP, producerGroup).build();
} else if (StringUtils.equals(SpecVersion.V03.toString(), protocolVersion)) {
event = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE).deserialize(content.getBytes(StandardCharsets.UTF_8));
event = CloudEventBuilder.from(event).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.PRODUCERGROUP, producerGroup).build();
}
return event;
} catch (Exception e) {
throw new ProtocolHandleException(e.getMessage(), e.getCause());
}
}
use of org.apache.eventmesh.protocol.api.exception.ProtocolHandleException 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 org.apache.eventmesh.protocol.api.exception.ProtocolHandleException in project incubator-eventmesh by apache.
the class MeshMessageProtocolAdaptor method toCloudEvent.
@Override
public CloudEvent toCloudEvent(ProtocolTransportObject protocol) throws ProtocolHandleException {
if (protocol instanceof Package) {
Package tcpPackage = (Package) protocol;
Header header = tcpPackage.getHeader();
String bodyJson = (String) tcpPackage.getBody();
return deserializeTcpProtocol(header, bodyJson);
} else if (protocol instanceof HttpCommand) {
org.apache.eventmesh.common.protocol.http.header.Header header = ((HttpCommand) protocol).getHeader();
Body body = ((HttpCommand) protocol).getBody();
String requestCode = ((HttpCommand) protocol).getRequestCode();
return deserializeHttpProtocol(requestCode, header, body);
} else if (protocol instanceof SimpleMessageWrapper) {
SimpleMessage message = ((SimpleMessageWrapper) protocol).getMessage();
return deserializeGrpcProtocol(message);
} else {
throw new ProtocolHandleException(String.format("protocol class: %s", protocol.getClass()));
}
}
use of org.apache.eventmesh.protocol.api.exception.ProtocolHandleException in project incubator-eventmesh by apache.
the class MeshMessageProtocolAdaptor method fromCloudEvent.
@Override
public ProtocolTransportObject fromCloudEvent(CloudEvent cloudEvent) throws ProtocolHandleException {
String protocolDesc = cloudEvent.getExtension(Constants.PROTOCOL_DESC).toString();
if (StringUtils.equals("http", protocolDesc)) {
HttpCommand httpCommand = new HttpCommand();
Body body = new Body() {
final Map<String, Object> map = new HashMap<>();
@Override
public Map<String, Object> toMap() {
map.put("content", new String(cloudEvent.getData().toBytes(), StandardCharsets.UTF_8));
return map;
}
};
body.toMap();
httpCommand.setBody(body);
return httpCommand;
} else if (StringUtils.equals("grpc", protocolDesc)) {
return GrpcMessageProtocolResolver.buildSimpleMessage(cloudEvent);
} else if (StringUtils.equals("tcp", protocolDesc)) {
return TcpMessageProtocolResolver.buildEventMeshMessage(cloudEvent);
} else {
throw new ProtocolHandleException(String.format("Unsupported protocolDesc: %s", protocolDesc));
}
}
use of org.apache.eventmesh.protocol.api.exception.ProtocolHandleException 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());
}
}
Aggregations