Skip to content

Commit

Permalink
Revert "[ISSUE apache#4269] Used switch to replace the if-else. [Mesh…
Browse files Browse the repository at this point in the history
…MessageProtocolAdaptor] (apache#4270)"

This reverts commit 2cb5aa9.
  • Loading branch information
hhuang1231 authored Jul 27, 2023
1 parent 3652000 commit 00e3172
Showing 1 changed file with 37 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@
import org.apache.eventmesh.protocol.meshmessage.resolver.http.SendMessageRequestProtocolResolver;
import org.apache.eventmesh.protocol.meshmessage.resolver.tcp.TcpMessageProtocolResolver;

import org.apache.commons.lang3.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import io.cloudevents.CloudEvent;

Expand Down Expand Up @@ -75,19 +77,19 @@ private CloudEvent deserializeTcpProtocol(Header header, String bodyJson) throws
}

private CloudEvent deserializeHttpProtocol(String requestCode,
org.apache.eventmesh.common.protocol.http.header.Header header,
Body body) throws ProtocolHandleException {

switch (RequestCode.valueOf(requestCode)) {
case MSG_BATCH_SEND:
return SendMessageBatchProtocolResolver.buildEvent(header, body);
case MSG_BATCH_SEND_V2:
return SendMessageBatchV2ProtocolResolver.buildEvent(header, body);
case MSG_SEND_SYNC:
case MSG_SEND_ASYNC:
return SendMessageRequestProtocolResolver.buildEvent(header, body);
default:
throw new ProtocolHandleException(String.format("unsupported requestCode: %s", requestCode));
org.apache.eventmesh.common.protocol.http.header.Header header,
Body body) throws ProtocolHandleException {

if (String.valueOf(RequestCode.MSG_BATCH_SEND.getRequestCode()).equals(requestCode)) {
return SendMessageBatchProtocolResolver.buildEvent(header, body);
} else if (String.valueOf(RequestCode.MSG_BATCH_SEND_V2.getRequestCode()).equals(requestCode)) {
return SendMessageBatchV2ProtocolResolver.buildEvent(header, body);
} else if (String.valueOf(RequestCode.MSG_SEND_SYNC.getRequestCode()).equals(requestCode)) {
return SendMessageRequestProtocolResolver.buildEvent(header, body);
} else if (String.valueOf(RequestCode.MSG_SEND_ASYNC.getRequestCode()).equals(requestCode)) {
return SendMessageRequestProtocolResolver.buildEvent(header, body);
} else {
throw new ProtocolHandleException(String.format("unsupported requestCode: %s", requestCode));
}

}
Expand All @@ -108,31 +110,29 @@ public ProtocolTransportObject fromCloudEvent(CloudEvent cloudEvent) throws Prot
String protocolDesc =
cloudEvent.getExtension(Constants.PROTOCOL_DESC) == null ? null : cloudEvent.getExtension(Constants.PROTOCOL_DESC).toString();

switch (Objects.requireNonNull(protocolDesc)) {
case MeshMessageProtocolConstant.PROTOCOL_DESC_HTTP:
HttpCommand httpCommand = new HttpCommand();
Body body = new Body() {
final Map<String, Object> map = new HashMap<>();

@Override
public Map<String, Object> toMap() {
if (cloudEvent.getData() == null) {
return map;
}
map.put(MeshMessageProtocolConstant.PROTOCOL_KEY_CONTENT,
new String(cloudEvent.getData().toBytes(), Constants.DEFAULT_CHARSET));
if (StringUtils.equals(MeshMessageProtocolConstant.PROTOCOL_DESC_HTTP, protocolDesc)) {
HttpCommand httpCommand = new HttpCommand();
Body body = new Body() {
final Map<String, Object> map = new HashMap<>();

@Override
public Map<String, Object> toMap() {
if (cloudEvent.getData() == null) {
return map;
}
};
body.toMap();
httpCommand.setBody(body);
return httpCommand;
case MeshMessageProtocolConstant.PROTOCOL_DESC_TCP:
return TcpMessageProtocolResolver.buildEventMeshMessage(cloudEvent);
case MeshMessageProtocolConstant.PROTOCOL_DESC_GRPC_CLOUD_EVENT:
return GrpcEventMeshCloudEventProtocolResolver.buildEventMeshCloudEvent(cloudEvent);
default:
throw new ProtocolHandleException(String.format("Unsupported protocolDesc: %s", protocolDesc));
map.put(MeshMessageProtocolConstant.PROTOCOL_KEY_CONTENT, new String(cloudEvent.getData().toBytes(), StandardCharsets.UTF_8));
return map;
}
};
body.toMap();
httpCommand.setBody(body);
return httpCommand;
} else if (StringUtils.equals(MeshMessageProtocolConstant.PROTOCOL_DESC_TCP, protocolDesc)) {
return TcpMessageProtocolResolver.buildEventMeshMessage(cloudEvent);
} else if (StringUtils.equals(MeshMessageProtocolConstant.PROTOCOL_DESC_GRPC_CLOUD_EVENT, protocolDesc)) {
return GrpcEventMeshCloudEventProtocolResolver.buildEventMeshCloudEvent(cloudEvent);
} else {
throw new ProtocolHandleException(String.format("Unsupported protocolDesc: %s", protocolDesc));
}
}

Expand Down

0 comments on commit 00e3172

Please sign in to comment.