Search in sources :

Example 1 with ProtocolHandleException

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());
    }
}
Also used : SendMessageBatchV2RequestBody(org.apache.eventmesh.common.protocol.http.body.message.SendMessageBatchV2RequestBody) SendMessageBatchV2RequestHeader(org.apache.eventmesh.common.protocol.http.header.message.SendMessageBatchV2RequestHeader) ProtocolHandleException(org.apache.eventmesh.protocol.api.exception.ProtocolHandleException) ProtocolVersion(org.apache.eventmesh.common.protocol.http.common.ProtocolVersion) CloudEvent(io.cloudevents.CloudEvent) ProtocolHandleException(org.apache.eventmesh.protocol.api.exception.ProtocolHandleException)

Example 2 with ProtocolHandleException

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));
    }
}
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 ProtocolHandleException

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()));
    }
}
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 ProtocolHandleException

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));
    }
}
Also used : ProtocolHandleException(org.apache.eventmesh.protocol.api.exception.ProtocolHandleException) ProtocolTransportObject(org.apache.eventmesh.common.protocol.ProtocolTransportObject) 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 5 with ProtocolHandleException

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());
    }
}
Also used : ProtocolHandleException(org.apache.eventmesh.protocol.api.exception.ProtocolHandleException) RequestHeader(org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader) CloudEventBuilder(io.cloudevents.core.builder.CloudEventBuilder) Map(java.util.Map) CloudEvent(io.cloudevents.CloudEvent) ProtocolHandleException(org.apache.eventmesh.protocol.api.exception.ProtocolHandleException)

Aggregations

ProtocolHandleException (org.apache.eventmesh.protocol.api.exception.ProtocolHandleException)11 CloudEvent (io.cloudevents.CloudEvent)6 CloudEventBuilder (io.cloudevents.core.builder.CloudEventBuilder)5 HttpCommand (org.apache.eventmesh.common.protocol.http.HttpCommand)4 Body (org.apache.eventmesh.common.protocol.http.body.Body)4 ProtocolVersion (org.apache.eventmesh.common.protocol.http.common.ProtocolVersion)4 Map (java.util.Map)3 Package (org.apache.eventmesh.common.protocol.tcp.Package)3 EventFormat (io.cloudevents.core.format.EventFormat)2 HashMap (java.util.HashMap)2 ProtocolTransportObject (org.apache.eventmesh.common.protocol.ProtocolTransportObject)2 SimpleMessageWrapper (org.apache.eventmesh.common.protocol.grpc.common.SimpleMessageWrapper)2 SimpleMessage (org.apache.eventmesh.common.protocol.grpc.protos.SimpleMessage)2 SendMessageBatchV2RequestBody (org.apache.eventmesh.common.protocol.http.body.message.SendMessageBatchV2RequestBody)2 SendMessageRequestBody (org.apache.eventmesh.common.protocol.http.body.message.SendMessageRequestBody)2 SendMessageBatchV2RequestHeader (org.apache.eventmesh.common.protocol.http.header.message.SendMessageBatchV2RequestHeader)2 SendMessageRequestHeader (org.apache.eventmesh.common.protocol.http.header.message.SendMessageRequestHeader)2 Header (org.apache.eventmesh.common.protocol.tcp.Header)2 RequestHeader (org.apache.eventmesh.common.protocol.grpc.protos.RequestHeader)1