forked from apache/eventmesh
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature apache#562] Implement EventMeshMessage protocol adaptor
- Loading branch information
Showing
12 changed files
with
500 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
.../java/org/apache/eventmesh/protocol/eventmeshmessage/EventMeshMessageProtocolAdaptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.eventmesh.protocol.eventmeshmessage; | ||
|
||
import io.cloudevents.CloudEvent; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.eventmesh.common.Constants; | ||
import org.apache.eventmesh.common.command.HttpCommand; | ||
import org.apache.eventmesh.common.protocol.http.body.Body; | ||
import org.apache.eventmesh.common.protocol.http.common.RequestCode; | ||
import org.apache.eventmesh.common.protocol.tcp.EventMeshMessage; | ||
import org.apache.eventmesh.common.protocol.tcp.Header; | ||
import org.apache.eventmesh.common.protocol.tcp.Package; | ||
import org.apache.eventmesh.protocol.api.ProtocolAdaptor; | ||
import org.apache.eventmesh.protocol.api.exception.ProtocolHandleException; | ||
import org.apache.eventmesh.protocol.eventmeshmessage.resolver.http.SendMessageBatchProtocolResolver; | ||
import org.apache.eventmesh.protocol.eventmeshmessage.resolver.http.SendMessageBatchV2ProtocolResolver; | ||
import org.apache.eventmesh.protocol.eventmeshmessage.resolver.http.SendMessageRequestProtocolResolver; | ||
import org.apache.eventmesh.protocol.eventmeshmessage.resolver.tcp.TcpMessageProtocolResolver; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
|
||
public class EventMeshMessageProtocolAdaptor<T> implements ProtocolAdaptor<T> { | ||
|
||
@Override | ||
public CloudEvent toCloudEvent(T protocol) throws ProtocolHandleException { | ||
if (protocol instanceof Package) { | ||
Header header = ((Package) protocol).getHeader(); | ||
Object body = ((Package) protocol).getBody(); | ||
|
||
return deserializeTcpProtocol(header, body); | ||
|
||
} 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 { | ||
throw new ProtocolHandleException(String.format("protocol class: %s", protocol.getClass())); | ||
} | ||
} | ||
|
||
private CloudEvent deserializeTcpProtocol(Header header, Object body) throws ProtocolHandleException { | ||
return TcpMessageProtocolResolver.buildEvent(header, body); | ||
} | ||
|
||
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)); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public List<CloudEvent> toBatchCloudEvent(T protocol) throws ProtocolHandleException { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Object fromCloudEvent(CloudEvent cloudEvent) throws ProtocolHandleException { | ||
String protocolDesc = cloudEvent.getExtension(Constants.PROTOCOL_DESC).toString(); | ||
|
||
if (StringUtils.equals("http", protocolDesc)) { | ||
return new String(cloudEvent.getData().toBytes(), StandardCharsets.UTF_8); | ||
} else if (StringUtils.equals("tcp", protocolDesc)) { | ||
return TcpMessageProtocolResolver.buildEventMeshMessage(cloudEvent); | ||
} else { | ||
throw new ProtocolHandleException(String.format("Unsupported protocolDesc: %s", protocolDesc)); | ||
} | ||
} | ||
|
||
@Override | ||
public String getProtocolType() { | ||
return EventMeshMessageProtocolConstant.PROTOCOL_NAME; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...java/org/apache/eventmesh/protocol/eventmeshmessage/EventMeshMessageProtocolConstant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.eventmesh.protocol.eventmeshmessage; | ||
|
||
public enum EventMeshMessageProtocolConstant { | ||
; | ||
public static final String PROTOCOL_NAME = "eventmeshmessage"; | ||
} |
11 changes: 11 additions & 0 deletions
11
...e/eventmesh/protocol/eventmeshmessage/resolver/http/SendMessageBatchProtocolResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.apache.eventmesh.protocol.eventmeshmessage.resolver.http; | ||
|
||
import io.cloudevents.CloudEvent; | ||
import org.apache.eventmesh.common.protocol.http.body.Body; | ||
import org.apache.eventmesh.common.protocol.http.header.Header; | ||
|
||
public class SendMessageBatchProtocolResolver { | ||
public static CloudEvent buildEvent(Header header, Body body) { | ||
return null; | ||
} | ||
} |
Oops, something went wrong.