-
Notifications
You must be signed in to change notification settings - Fork 641
[ISSUE #4268] Used switch to replace the if-else [CloudEventsProtocolAdaptor] #4298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,8 +35,6 @@ | |
| import org.apache.eventmesh.protocol.cloudevents.resolver.http.SendMessageRequestProtocolResolver; | ||
| import org.apache.eventmesh.protocol.cloudevents.resolver.tcp.TcpMessageProtocolResolver; | ||
|
|
||
| import org.apache.commons.lang3.StringUtils; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
@@ -89,16 +87,16 @@ 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)); | ||
| switch (RequestCode.valueOf(requestCode)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, my mistake here, the valueof here returns the enum constant for the specified string value, here it is judged by the |
||
| 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)); | ||
| } | ||
|
|
||
| } | ||
|
|
@@ -117,36 +115,37 @@ public List<CloudEvent> toBatchCloudEvent(ProtocolTransportObject protocol) thro | |
| public ProtocolTransportObject fromCloudEvent(CloudEvent cloudEvent) throws ProtocolHandleException { | ||
| Preconditions.checkNotNull(cloudEvent, "cloudEvent cannot be null"); | ||
| String protocolDesc = Objects.requireNonNull(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 = | ||
| Objects.requireNonNull(EventFormatProvider.getInstance() | ||
| .resolveFormat(JsonFormat.CONTENT_TYPE)).serialize(cloudEvent); | ||
| map.put("content", new String(eventByte, Constants.DEFAULT_CHARSET)); | ||
| 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(CloudEventsProtocolConstant.PROTOCOL_DESC_GRPC_CLOUD_EVENT, protocolDesc)) { | ||
| return GrpcEventMeshCloudEventProtocolResolver.buildEventMeshCloudEvent(cloudEvent); | ||
| } else { | ||
| throw new ProtocolHandleException(String.format("Unsupported protocolDesc: %s", protocolDesc)); | ||
| switch (protocolDesc) { | ||
| case "http": | ||
| HttpCommand httpCommand = new HttpCommand(); | ||
| Body body = new Body() { | ||
| final Map<String, Object> map = new HashMap<>(); | ||
|
|
||
| @Override | ||
| public Map<String, Object> toMap() { | ||
| byte[] eventByte = | ||
| Objects.requireNonNull(EventFormatProvider.getInstance() | ||
| .resolveFormat(JsonFormat.CONTENT_TYPE)).serialize(cloudEvent); | ||
| map.put("content", new String(eventByte, Constants.DEFAULT_CHARSET)); | ||
| return map; | ||
| } | ||
| }; | ||
| body.toMap(); | ||
| httpCommand.setBody(body); | ||
| return httpCommand; | ||
| case "tcp": | ||
| 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; | ||
| case CloudEventsProtocolConstant.PROTOCOL_DESC_GRPC_CLOUD_EVENT: | ||
| return GrpcEventMeshCloudEventProtocolResolver.buildEventMeshCloudEvent(cloudEvent); | ||
| default: | ||
| throw new ProtocolHandleException(String.format("Unsupported protocolDesc: %s", protocolDesc)); | ||
| } | ||
|
|
||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
valueOfmethod is to transfer an Enum constant specified name to return an Enum constant.So It can not transferrequestCodeas specified name to get any Enum constant.So as the same as #4270.It causes client to fail to connect to the runtime server.
