use of org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader 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.RequestHeader 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 org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader in project incubator-eventmesh by apache.
the class HeartbeatProcessor method doAclCheck.
private void doAclCheck(Heartbeat heartbeat) throws AclException {
RequestHeader header = heartbeat.getHeader();
if (eventMeshGrpcServer.getEventMeshGrpcConfiguration().eventMeshServerSecurityEnable) {
String remoteAdd = header.getIp();
String user = header.getUsername();
String pass = header.getPassword();
String sys = header.getSys();
int requestCode = Integer.valueOf(RequestCode.HEARTBEAT.getRequestCode());
for (Heartbeat.HeartbeatItem item : heartbeat.getHeartbeatItemsList()) {
Acl.doAclCheckInHttpHeartbeat(remoteAdd, user, pass, sys, item.getTopic(), requestCode);
}
}
}
use of org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader in project incubator-eventmesh by apache.
the class ReplyMessageProcessor method process.
public void process(SimpleMessage message, EventEmitter<SimpleMessage> emitter) throws Exception {
RequestHeader requestHeader = message.getHeader();
if (!ServiceUtils.validateHeader(requestHeader)) {
ServiceUtils.sendStreamRespAndDone(requestHeader, StatusCode.EVENTMESH_PROTOCOL_HEADER_ERR, emitter);
return;
}
if (!ServiceUtils.validateMessage(message)) {
ServiceUtils.sendStreamRespAndDone(requestHeader, StatusCode.EVENTMESH_PROTOCOL_BODY_ERR, emitter);
return;
}
try {
doAclCheck(message);
} catch (Exception e) {
aclLogger.warn("CLIENT HAS NO PERMISSION,RequestReplyMessageProcessor reply failed", e);
ServiceUtils.sendStreamRespAndDone(requestHeader, StatusCode.EVENTMESH_ACL_ERR, e.getMessage(), emitter);
return;
}
// control flow rate limit
if (!eventMeshGrpcServer.getMsgRateLimiter().tryAcquire(EventMeshConstants.DEFAULT_FASTFAIL_TIMEOUT_IN_MILLISECONDS, TimeUnit.MILLISECONDS)) {
logger.error("Send message speed over limit.");
ServiceUtils.sendStreamRespAndDone(requestHeader, StatusCode.EVENTMESH_SEND_MESSAGE_SPEED_OVER_LIMIT_ERR, emitter);
return;
}
String seqNum = message.getSeqNum();
String uniqueId = message.getUniqueId();
String producerGroup = message.getProducerGroup();
// set reply topic for ths message
String mqCluster = message.getPropertiesOrDefault(EventMeshConstants.PROPERTY_MESSAGE_CLUSTER, "defaultCluster");
String replyTopic = mqCluster + "-" + EventMeshConstants.RR_REPLY_TOPIC;
message = SimpleMessage.newBuilder(message).setTopic(replyTopic).build();
String protocolType = requestHeader.getProtocolType();
ProtocolAdaptor<ProtocolTransportObject> grpcCommandProtocolAdaptor = ProtocolPluginFactory.getProtocolAdaptor(protocolType);
CloudEvent cloudEvent = grpcCommandProtocolAdaptor.toCloudEvent(new SimpleMessageWrapper(message));
ProducerManager producerManager = eventMeshGrpcServer.getProducerManager();
EventMeshProducer eventMeshProducer = producerManager.getEventMeshProducer(producerGroup);
SendMessageContext sendMessageContext = new SendMessageContext(message.getSeqNum(), cloudEvent, eventMeshProducer, eventMeshGrpcServer);
long startTime = System.currentTimeMillis();
eventMeshProducer.reply(sendMessageContext, new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
long endTime = System.currentTimeMillis();
logger.info("message|mq2eventmesh|REPLY|ReplyToServer|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", endTime - startTime, replyTopic, seqNum, uniqueId);
}
@Override
public void onException(OnExceptionContext onExceptionContext) {
ServiceUtils.sendStreamRespAndDone(requestHeader, StatusCode.EVENTMESH_REPLY_MSG_ERR, EventMeshUtil.stackTrace(onExceptionContext.getException(), 2), emitter);
long endTime = System.currentTimeMillis();
logger.error("message|mq2eventmesh|REPLY|ReplyToServer|send2MQCost={}ms|topic={}|bizSeqNo={}|uniqueId={}", endTime - startTime, replyTopic, seqNum, uniqueId, onExceptionContext.getException());
}
});
}
use of org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader in project incubator-eventmesh by apache.
the class ReplyMessageProcessor method doAclCheck.
private void doAclCheck(SimpleMessage message) throws AclException {
RequestHeader requestHeader = message.getHeader();
if (eventMeshGrpcServer.getEventMeshGrpcConfiguration().eventMeshServerSecurityEnable) {
String remoteAdd = requestHeader.getIp();
String user = requestHeader.getUsername();
String pass = requestHeader.getPassword();
String subsystem = requestHeader.getSys();
String topic = message.getTopic();
Acl.doAclCheckInHttpSend(remoteAdd, user, pass, subsystem, topic, RequestCode.REPLY_MESSAGE.getRequestCode());
}
}
Aggregations