Search in sources :

Example 1 with Body

use of org.apache.eventmesh.common.protocol.http.body.Body 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 2 with Body

use of org.apache.eventmesh.common.protocol.http.body.Body 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 3 with Body

use of org.apache.eventmesh.common.protocol.http.body.Body 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 Body

use of org.apache.eventmesh.common.protocol.http.body.Body 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)

Aggregations

HttpCommand (org.apache.eventmesh.common.protocol.http.HttpCommand)4 Body (org.apache.eventmesh.common.protocol.http.body.Body)4 ProtocolHandleException (org.apache.eventmesh.protocol.api.exception.ProtocolHandleException)4 Package (org.apache.eventmesh.common.protocol.tcp.Package)3 HashMap (java.util.HashMap)2 Map (java.util.Map)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 Header (org.apache.eventmesh.common.protocol.tcp.Header)2 EventFormat (io.cloudevents.core.format.EventFormat)1