use of org.apache.eventmesh.common.protocol.grpc.protos.SimpleMessage in project incubator-eventmesh by apache.
the class GrpcMessageProtocolResolver method buildSimpleMessage.
public static SimpleMessageWrapper buildSimpleMessage(CloudEvent cloudEvent) {
String env = cloudEvent.getExtension(ProtocolKey.ENV) == null ? "env" : cloudEvent.getExtension(ProtocolKey.ENV).toString();
String idc = cloudEvent.getExtension(ProtocolKey.IDC) == null ? "idc" : cloudEvent.getExtension(ProtocolKey.IDC).toString();
String ip = cloudEvent.getExtension(ProtocolKey.IP) == null ? "127.0.0.1" : cloudEvent.getExtension(ProtocolKey.IP).toString();
String pid = cloudEvent.getExtension(ProtocolKey.PID) == null ? "123" : cloudEvent.getExtension(ProtocolKey.PID).toString();
String sys = cloudEvent.getExtension(ProtocolKey.SYS) == null ? "sys123" : cloudEvent.getExtension(ProtocolKey.SYS).toString();
String userName = cloudEvent.getExtension(ProtocolKey.USERNAME) == null ? "user" : cloudEvent.getExtension(ProtocolKey.USERNAME).toString();
String passwd = cloudEvent.getExtension(ProtocolKey.PASSWD) == null ? "pass" : cloudEvent.getExtension(ProtocolKey.PASSWD).toString();
String language = cloudEvent.getExtension(ProtocolKey.LANGUAGE) == null ? "JAVA" : cloudEvent.getExtension(ProtocolKey.LANGUAGE).toString();
String protocol = cloudEvent.getExtension(ProtocolKey.PROTOCOL_TYPE) == null ? "protocol" : cloudEvent.getExtension(ProtocolKey.PROTOCOL_TYPE).toString();
String protocolDesc = cloudEvent.getExtension(ProtocolKey.PROTOCOL_DESC) == null ? "protocolDesc" : cloudEvent.getExtension(ProtocolKey.PROTOCOL_DESC).toString();
String protocolVersion = cloudEvent.getExtension(ProtocolKey.PROTOCOL_VERSION) == null ? "1.0" : cloudEvent.getExtension(ProtocolKey.PROTOCOL_VERSION).toString();
String seqNum = cloudEvent.getExtension(ProtocolKey.SEQ_NUM) == null ? "" : cloudEvent.getExtension(ProtocolKey.SEQ_NUM).toString();
String uniqueId = cloudEvent.getExtension(ProtocolKey.UNIQUE_ID) == null ? "" : cloudEvent.getExtension(ProtocolKey.UNIQUE_ID).toString();
String producerGroup = cloudEvent.getExtension(ProtocolKey.PRODUCERGROUP) == null ? "producerGroup" : cloudEvent.getExtension(ProtocolKey.PRODUCERGROUP).toString();
String ttl = cloudEvent.getExtension(ProtocolKey.TTL) == null ? "3000" : cloudEvent.getExtension(ProtocolKey.TTL).toString();
RequestHeader header = RequestHeader.newBuilder().setEnv(env).setIdc(idc).setIp(ip).setPid(pid).setSys(sys).setUsername(userName).setPassword(passwd).setLanguage(language).setProtocolType(protocol).setProtocolDesc(protocolDesc).setProtocolVersion(protocolVersion).build();
String contentType = cloudEvent.getDataContentType();
EventFormat eventFormat = EventFormatProvider.getInstance().resolveFormat(contentType);
SimpleMessage.Builder messageBuilder = SimpleMessage.newBuilder().setHeader(header).setContent(new String(eventFormat.serialize(cloudEvent), StandardCharsets.UTF_8)).setProducerGroup(producerGroup).setSeqNum(seqNum).setUniqueId(uniqueId).setTopic(cloudEvent.getSubject()).setTtl(ttl).putProperties(ProtocolKey.CONTENT_TYPE, contentType);
for (String key : cloudEvent.getExtensionNames()) {
messageBuilder.putProperties(key, cloudEvent.getExtension(key).toString());
}
SimpleMessage simpleMessage = messageBuilder.build();
return new SimpleMessageWrapper(simpleMessage);
}
use of org.apache.eventmesh.common.protocol.grpc.protos.SimpleMessage 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.common.protocol.grpc.protos.SimpleMessage in project incubator-eventmesh by apache.
the class CloudEventsProtocolAdaptor method toCloudEvent.
@Override
public CloudEvent toCloudEvent(ProtocolTransportObject cloudEvent) throws ProtocolHandleException {
if (cloudEvent instanceof Package) {
Package tcpPackage = (Package) cloudEvent;
Header header = tcpPackage.getHeader();
String cloudEventJson = tcpPackage.getBody().toString();
return deserializeTcpProtocol(header, cloudEventJson);
} else if (cloudEvent instanceof HttpCommand) {
org.apache.eventmesh.common.protocol.http.header.Header header = ((HttpCommand) cloudEvent).getHeader();
Body body = ((HttpCommand) cloudEvent).getBody();
String requestCode = ((HttpCommand) cloudEvent).getRequestCode();
return deserializeHttpProtocol(requestCode, header, body);
} else if (cloudEvent instanceof SimpleMessageWrapper) {
SimpleMessage simpleMessage = ((SimpleMessageWrapper) cloudEvent).getMessage();
return GrpcMessageProtocolResolver.buildEvent(simpleMessage);
} else {
throw new ProtocolHandleException(String.format("protocol class: %s", cloudEvent.getClass()));
}
}
use of org.apache.eventmesh.common.protocol.grpc.protos.SimpleMessage in project incubator-eventmesh by apache.
the class ServiceUtils method sendStreamResp.
public static void sendStreamResp(RequestHeader header, StatusCode code, String message, EventEmitter<SimpleMessage> emitter) {
Map<String, String> resp = new HashMap<>();
resp.put("respCode", code.getRetCode());
resp.put("respMsg", code.getErrMsg() + " " + message);
SimpleMessage simpleMessage = SimpleMessage.newBuilder().setHeader(header).setContent(JsonUtils.serialize(resp)).build();
emitter.onNext(simpleMessage);
}
use of org.apache.eventmesh.common.protocol.grpc.protos.SimpleMessage in project incubator-eventmesh by apache.
the class ServiceUtils method sendStreamRespAndDone.
public static void sendStreamRespAndDone(RequestHeader header, StatusCode code, EventEmitter<SimpleMessage> emitter) {
Map<String, String> resp = new HashMap<>();
resp.put("respCode", code.getRetCode());
resp.put("respMsg", code.getErrMsg());
SimpleMessage simpleMessage = SimpleMessage.newBuilder().setHeader(header).setContent(JsonUtils.serialize(resp)).build();
emitter.onNext(simpleMessage);
emitter.onCompleted();
}
Aggregations