Search in sources :

Example 1 with EventFormat

use of io.cloudevents.core.format.EventFormat 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 EventFormat

use of io.cloudevents.core.format.EventFormat 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));
    }
}
Also used : EventFormat(io.cloudevents.core.format.EventFormat) ProtocolHandleException(org.apache.eventmesh.protocol.api.exception.ProtocolHandleException) CloudEventBuilder(io.cloudevents.core.builder.CloudEventBuilder) CloudEvent(io.cloudevents.CloudEvent)

Example 3 with EventFormat

use of io.cloudevents.core.format.EventFormat in project incubator-eventmesh by apache.

the class CloudEventsProtocolAdaptor 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() {
                byte[] eventByte = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE).serialize(cloudEvent);
                map.put("content", new String(eventByte, StandardCharsets.UTF_8));
                return map;
            }
        };
        body.toMap();
        httpCommand.setBody(body);
        return httpCommand;
    } else if (StringUtils.equals("tcp", protocolDesc)) {
        Package pkg = new Package();
        String dataContentType = cloudEvent.getDataContentType();
        Preconditions.checkNotNull(dataContentType, "DateContentType cannot be null");
        EventFormat eventFormat = EventFormatProvider.getInstance().resolveFormat(dataContentType);
        Preconditions.checkNotNull(eventFormat, String.format("DateContentType:%s is not supported", dataContentType));
        pkg.setBody(eventFormat.serialize(cloudEvent));
        return pkg;
    } else if (StringUtils.equals("grpc", protocolDesc)) {
        return GrpcMessageProtocolResolver.buildSimpleMessage(cloudEvent);
    } else {
        throw new ProtocolHandleException(String.format("Unsupported protocolDesc: %s", protocolDesc));
    }
}
Also used : EventFormat(io.cloudevents.core.format.EventFormat) ProtocolHandleException(org.apache.eventmesh.protocol.api.exception.ProtocolHandleException) ProtocolTransportObject(org.apache.eventmesh.common.protocol.ProtocolTransportObject) Package(org.apache.eventmesh.common.protocol.tcp.Package) Body(org.apache.eventmesh.common.protocol.http.body.Body) HashMap(java.util.HashMap) Map(java.util.Map) HttpCommand(org.apache.eventmesh.common.protocol.http.HttpCommand)

Example 4 with EventFormat

use of io.cloudevents.core.format.EventFormat in project incubator-eventmesh by apache.

the class GrpcMessageProtocolResolver method buildBatchEvents.

public static List<CloudEvent> buildBatchEvents(BatchMessage batchMessage) {
    List<CloudEvent> cloudEvents = new ArrayList<>();
    RequestHeader header = batchMessage.getHeader();
    for (BatchMessage.MessageItem item : batchMessage.getMessageItemList()) {
        String cloudEventJson = item.getContent();
        String contentType = item.getPropertiesOrDefault(ProtocolKey.CONTENT_TYPE, "application/cloudevents+json");
        EventFormat eventFormat = EventFormatProvider.getInstance().resolveFormat(contentType);
        CloudEvent event = eventFormat.deserialize(cloudEventJson.getBytes(StandardCharsets.UTF_8));
        String env = StringUtils.isEmpty(header.getEnv()) ? event.getExtension(ProtocolKey.ENV).toString() : header.getEnv();
        String idc = StringUtils.isEmpty(header.getIdc()) ? event.getExtension(ProtocolKey.IDC).toString() : header.getIdc();
        String ip = StringUtils.isEmpty(header.getIp()) ? event.getExtension(ProtocolKey.IP).toString() : header.getIp();
        String pid = StringUtils.isEmpty(header.getPid()) ? event.getExtension(ProtocolKey.PID).toString() : header.getPid();
        String sys = StringUtils.isEmpty(header.getSys()) ? event.getExtension(ProtocolKey.SYS).toString() : header.getSys();
        String language = StringUtils.isEmpty(header.getLanguage()) ? event.getExtension(ProtocolKey.LANGUAGE).toString() : header.getLanguage();
        String protocolType = StringUtils.isEmpty(header.getProtocolType()) ? event.getExtension(ProtocolKey.PROTOCOL_TYPE).toString() : header.getProtocolType();
        String protocolDesc = StringUtils.isEmpty(header.getProtocolDesc()) ? event.getExtension(ProtocolKey.PROTOCOL_DESC).toString() : header.getProtocolDesc();
        String protocolVersion = StringUtils.isEmpty(header.getProtocolVersion()) ? event.getExtension(ProtocolKey.PROTOCOL_VERSION).toString() : header.getProtocolVersion();
        String username = StringUtils.isEmpty(header.getUsername()) ? event.getExtension(ProtocolKey.USERNAME).toString() : header.getUsername();
        String passwd = StringUtils.isEmpty(header.getPassword()) ? event.getExtension(ProtocolKey.PASSWD).toString() : header.getPassword();
        String seqNum = StringUtils.isEmpty(item.getSeqNum()) ? event.getExtension(ProtocolKey.SEQ_NUM).toString() : item.getSeqNum();
        String uniqueId = StringUtils.isEmpty(item.getUniqueId()) ? event.getExtension(ProtocolKey.UNIQUE_ID).toString() : item.getUniqueId();
        String topic = StringUtils.isEmpty(batchMessage.getTopic()) ? event.getSubject() : batchMessage.getTopic();
        String producerGroup = StringUtils.isEmpty(batchMessage.getProducerGroup()) ? event.getExtension(ProtocolKey.PRODUCERGROUP).toString() : batchMessage.getProducerGroup();
        String ttl = StringUtils.isEmpty(item.getTtl()) ? event.getExtension(ProtocolKey.TTL).toString() : item.getTtl();
        CloudEventBuilder eventBuilder;
        if (StringUtils.equals(SpecVersion.V1.toString(), protocolVersion)) {
            eventBuilder = CloudEventBuilder.v1(event);
        } else {
            eventBuilder = CloudEventBuilder.v03(event);
        }
        eventBuilder.withSubject(topic).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, seqNum).withExtension(ProtocolKey.UNIQUE_ID, uniqueId).withExtension(ProtocolKey.PRODUCERGROUP, producerGroup).withExtension(ProtocolKey.TTL, ttl);
        item.getPropertiesMap().forEach((k, v) -> eventBuilder.withExtension(k, v));
        cloudEvents.add(eventBuilder.build());
    }
    return cloudEvents;
}
Also used : EventFormat(io.cloudevents.core.format.EventFormat) ArrayList(java.util.ArrayList) RequestHeader(org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader) CloudEventBuilder(io.cloudevents.core.builder.CloudEventBuilder) BatchMessage(org.apache.eventmesh.common.protocol.grpc.protos.BatchMessage) CloudEvent(io.cloudevents.CloudEvent)

Example 5 with EventFormat

use of io.cloudevents.core.format.EventFormat in project incubator-eventmesh by apache.

the class GrpcMessageProtocolResolver method buildEvent.

public static CloudEvent buildEvent(SimpleMessage message) {
    String cloudEventJson = message.getContent();
    String contentType = message.getPropertiesOrDefault(ProtocolKey.CONTENT_TYPE, "application/cloudevents+json");
    EventFormat eventFormat = EventFormatProvider.getInstance().resolveFormat(contentType);
    CloudEvent event = eventFormat.deserialize(cloudEventJson.getBytes(StandardCharsets.UTF_8));
    RequestHeader header = message.getHeader();
    String env = StringUtils.isEmpty(header.getEnv()) ? event.getExtension(ProtocolKey.ENV).toString() : header.getEnv();
    String idc = StringUtils.isEmpty(header.getIdc()) ? event.getExtension(ProtocolKey.IDC).toString() : header.getIdc();
    String ip = StringUtils.isEmpty(header.getIp()) ? event.getExtension(ProtocolKey.IP).toString() : header.getIp();
    String pid = StringUtils.isEmpty(header.getPid()) ? event.getExtension(ProtocolKey.PID).toString() : header.getPid();
    String sys = StringUtils.isEmpty(header.getSys()) ? event.getExtension(ProtocolKey.SYS).toString() : header.getSys();
    String language = StringUtils.isEmpty(header.getLanguage()) ? event.getExtension(ProtocolKey.LANGUAGE).toString() : header.getLanguage();
    String protocolType = StringUtils.isEmpty(header.getProtocolType()) ? event.getExtension(ProtocolKey.PROTOCOL_TYPE).toString() : header.getProtocolType();
    String protocolDesc = StringUtils.isEmpty(header.getProtocolDesc()) ? event.getExtension(ProtocolKey.PROTOCOL_DESC).toString() : header.getProtocolDesc();
    String protocolVersion = StringUtils.isEmpty(header.getProtocolVersion()) ? event.getExtension(ProtocolKey.PROTOCOL_VERSION).toString() : header.getProtocolVersion();
    String uniqueId = StringUtils.isEmpty(message.getUniqueId()) ? event.getExtension(ProtocolKey.UNIQUE_ID).toString() : message.getUniqueId();
    String seqNum = StringUtils.isEmpty(message.getSeqNum()) ? event.getExtension(ProtocolKey.SEQ_NUM).toString() : message.getSeqNum();
    String topic = StringUtils.isEmpty(message.getTopic()) ? event.getSubject() : message.getTopic();
    String username = StringUtils.isEmpty(header.getUsername()) ? event.getExtension(ProtocolKey.USERNAME).toString() : header.getUsername();
    String passwd = StringUtils.isEmpty(header.getPassword()) ? event.getExtension(ProtocolKey.PASSWD).toString() : header.getPassword();
    String ttl = StringUtils.isEmpty(message.getTtl()) ? event.getExtension(ProtocolKey.TTL).toString() : message.getTtl();
    String producerGroup = StringUtils.isEmpty(message.getProducerGroup()) ? event.getExtension(ProtocolKey.PRODUCERGROUP).toString() : message.getProducerGroup();
    CloudEventBuilder eventBuilder;
    if (StringUtils.equals(SpecVersion.V1.toString(), protocolVersion)) {
        eventBuilder = CloudEventBuilder.v1(event);
    } else {
        eventBuilder = CloudEventBuilder.v03(event);
    }
    eventBuilder.withSubject(topic).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, seqNum).withExtension(ProtocolKey.UNIQUE_ID, uniqueId).withExtension(ProtocolKey.PRODUCERGROUP, producerGroup).withExtension(ProtocolKey.TTL, ttl);
    message.getPropertiesMap().forEach((k, v) -> eventBuilder.withExtension(k, v));
    return eventBuilder.build();
}
Also used : EventFormat(io.cloudevents.core.format.EventFormat) RequestHeader(org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader) CloudEventBuilder(io.cloudevents.core.builder.CloudEventBuilder) CloudEvent(io.cloudevents.CloudEvent)

Aggregations

EventFormat (io.cloudevents.core.format.EventFormat)5 CloudEvent (io.cloudevents.CloudEvent)3 CloudEventBuilder (io.cloudevents.core.builder.CloudEventBuilder)3 RequestHeader (org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader)3 ProtocolHandleException (org.apache.eventmesh.protocol.api.exception.ProtocolHandleException)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ProtocolTransportObject (org.apache.eventmesh.common.protocol.ProtocolTransportObject)1 SimpleMessageWrapper (org.apache.eventmesh.common.protocol.grpc.common.SimpleMessageWrapper)1 BatchMessage (org.apache.eventmesh.common.protocol.grpc.protos.BatchMessage)1 SimpleMessage (org.apache.eventmesh.common.protocol.grpc.protos.SimpleMessage)1 HttpCommand (org.apache.eventmesh.common.protocol.http.HttpCommand)1 Body (org.apache.eventmesh.common.protocol.http.body.Body)1 Package (org.apache.eventmesh.common.protocol.tcp.Package)1