Search in sources :

Example 1 with SimpleMessage

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);
}
Also used : EventFormat(io.cloudevents.core.format.EventFormat) SimpleMessage(org.apache.eventmesh.common.protocol.grpc.protos.SimpleMessage) SimpleMessageWrapper(org.apache.eventmesh.common.protocol.grpc.common.SimpleMessageWrapper) RequestHeader(org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader)

Example 2 with 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()));
    }
}
Also used : Header(org.apache.eventmesh.common.protocol.tcp.Header) SimpleMessageWrapper(org.apache.eventmesh.common.protocol.grpc.common.SimpleMessageWrapper) SimpleMessage(org.apache.eventmesh.common.protocol.grpc.protos.SimpleMessage) ProtocolHandleException(org.apache.eventmesh.protocol.api.exception.ProtocolHandleException) Package(org.apache.eventmesh.common.protocol.tcp.Package) Body(org.apache.eventmesh.common.protocol.http.body.Body) HttpCommand(org.apache.eventmesh.common.protocol.http.HttpCommand)

Example 3 with SimpleMessage

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()));
    }
}
Also used : Header(org.apache.eventmesh.common.protocol.tcp.Header) SimpleMessageWrapper(org.apache.eventmesh.common.protocol.grpc.common.SimpleMessageWrapper) SimpleMessage(org.apache.eventmesh.common.protocol.grpc.protos.SimpleMessage) ProtocolHandleException(org.apache.eventmesh.protocol.api.exception.ProtocolHandleException) Package(org.apache.eventmesh.common.protocol.tcp.Package) Body(org.apache.eventmesh.common.protocol.http.body.Body) HttpCommand(org.apache.eventmesh.common.protocol.http.HttpCommand)

Example 4 with SimpleMessage

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);
}
Also used : HashMap(java.util.HashMap) SimpleMessage(org.apache.eventmesh.common.protocol.grpc.protos.SimpleMessage)

Example 5 with 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();
}
Also used : HashMap(java.util.HashMap) SimpleMessage(org.apache.eventmesh.common.protocol.grpc.protos.SimpleMessage)

Aggregations

SimpleMessage (org.apache.eventmesh.common.protocol.grpc.protos.SimpleMessage)14 SimpleMessageWrapper (org.apache.eventmesh.common.protocol.grpc.common.SimpleMessageWrapper)4 CloudEvent (io.cloudevents.CloudEvent)3 HashMap (java.util.HashMap)2 EventMeshMessage (org.apache.eventmesh.common.EventMeshMessage)2 RequestHeader (org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader)2 Response (org.apache.eventmesh.common.protocol.grpc.protos.Response)2 Subscription (org.apache.eventmesh.common.protocol.grpc.protos.Subscription)2 HttpCommand (org.apache.eventmesh.common.protocol.http.HttpCommand)2 Body (org.apache.eventmesh.common.protocol.http.body.Body)2 Header (org.apache.eventmesh.common.protocol.tcp.Header)2 Package (org.apache.eventmesh.common.protocol.tcp.Package)2 ProtocolHandleException (org.apache.eventmesh.protocol.api.exception.ProtocolHandleException)2 EventFormat (io.cloudevents.core.format.EventFormat)1 StreamObserver (io.grpc.stub.StreamObserver)1 Map (java.util.Map)1 RequestMessageProcessor (org.apache.eventmesh.runtime.core.protocol.grpc.processor.RequestMessageProcessor)1