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