Skip to content

Commit

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

* [ISSUE apache#4269] Used switch to replace the if-else. [MeshMessageProtocolAdaptor]

* [ISSUE apache#4269] Used switch to replace the if-else. [MeshMessageProtocolAdaptor]

* [ISSUE apache#4269] Used switch to replace the if-else. [MeshMessageProtocolAdaptor]

* [ISSUE apache#4269] Used switch to replace the if-else. [MeshMessageProtocolAdaptor]
  • Loading branch information
devCod3r authored Jul 25, 2023
1 parent 2a3c680 commit 2cb5aa9
Showing 1 changed file with 37 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,10 @@
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 @@ -77,19 +75,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 {

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

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

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) {
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));
return map;
}
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));
};
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));
}
}

Expand Down

0 comments on commit 2cb5aa9

Please sign in to comment.