You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@eventmesh.apache.org by ch...@apache.org on 2022/09/06 02:20:07 UTC

[incubator-eventmesh] branch master updated: Extraction of constants grpc、JAVA and application/cloudevents+json.

This is an automated email from the ASF dual-hosted git repository.

chenguangsheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-eventmesh.git


The following commit(s) were added to refs/heads/master by this push:
     new ba38bba1 Extraction of constants grpc、JAVA and application/cloudevents+json.
     new 7ce2c459 Merge pull request #1226 from RiemannChow/master
ba38bba1 is described below

commit ba38bba19161e3231371137ebfe8e9cf91208f4b
Author: “RiemannChow” <“riemannchowcn@gmail.com”>
AuthorDate: Tue Sep 6 00:02:16 2022 +0800

    Extraction of constants grpc、JAVA and application/cloudevents+json.
---
 .../src/main/java/org/apache/eventmesh/common/Constants.java | 12 +++++++++++-
 .../org/apache/eventmesh/tcp/common/EventMeshTestUtils.java  |  2 +-
 .../protocol/cloudevents/CloudEventsProtocolAdaptor.java     |  2 +-
 .../resolver/grpc/GrpcMessageProtocolResolver.java           |  7 ++++---
 .../protocol/meshmessage/MeshMessageProtocolAdaptor.java     |  2 +-
 .../resolver/grpc/GrpcMessageProtocolResolver.java           |  3 ++-
 .../runtime/core/protocol/grpc/push/WebhookPushRequest.java  |  2 +-
 .../eventmesh/client/grpc/producer/CloudEventProducer.java   |  4 ++--
 .../eventmesh/client/grpc/util/EventMeshClientUtil.java      |  6 +++---
 .../eventmesh/client/grpc/util/EventMeshClientUtilTest.java  |  2 +-
 10 files changed, 27 insertions(+), 15 deletions(-)

diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/Constants.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/Constants.java
index faf82275..ba594d61 100644
--- a/eventmesh-common/src/main/java/org/apache/eventmesh/common/Constants.java
+++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/Constants.java
@@ -154,4 +154,14 @@ public class Constants {
     public static final String AND = "&";
 
     public static final String EMPTY = "";
-}
+
+    /**
+     * GRPC PROTOCOL
+     */
+    public static final String PROTOCOL_GRPC = "grpc";
+
+    /**
+     * application/cloudevents+json Content-type
+     */
+    public static final String CONTENT_TYPE_CLOUDEVENTS_JSON = "application/cloudevents+json";
+}
\ No newline at end of file
diff --git a/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java b/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java
index db3182a4..b0631e92 100644
--- a/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java
+++ b/eventmesh-examples/src/main/java/org/apache/eventmesh/tcp/common/EventMeshTestUtils.java
@@ -171,7 +171,7 @@ public class EventMeshTestUtils {
                 .withId(UUID.randomUUID().toString())
                 .withSubject(ExampleConstants.EVENTMESH_TCP_ASYNC_TEST_TOPIC)
                 .withSource(URI.create("/"))
-                .withDataContentType("application/cloudevents+json")
+                .withDataContentType(ExampleConstants.CLOUDEVENT_CONTENT_TYPE)
                 .withType(EventMeshCommon.CLOUD_EVENTS_PROTOCOL_NAME)
                 .withData(JsonUtils.serialize(content).getBytes(StandardCharsets.UTF_8))
                 .withExtension("ttl", "30000")
diff --git a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/CloudEventsProtocolAdaptor.java b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/CloudEventsProtocolAdaptor.java
index 5763e60e..c75cbaa0 100644
--- a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/CloudEventsProtocolAdaptor.java
+++ b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/CloudEventsProtocolAdaptor.java
@@ -143,7 +143,7 @@ public class CloudEventsProtocolAdaptor<T extends ProtocolTransportObject>
                 String.format("DateContentType:%s is not supported", dataContentType));
             pkg.setBody(eventFormat.serialize(cloudEvent));
             return pkg;
-        } else if (StringUtils.equals("grpc", protocolDesc)) {
+        } else if (StringUtils.equals(Constants.PROTOCOL_GRPC, protocolDesc)) {
             return GrpcMessageProtocolResolver.buildSimpleMessage(cloudEvent);
         } else {
             throw new ProtocolHandleException(String.format("Unsupported protocolDesc: %s", protocolDesc));
diff --git a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/grpc/GrpcMessageProtocolResolver.java b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/grpc/GrpcMessageProtocolResolver.java
index 6424c724..e75b57e1 100644
--- a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/grpc/GrpcMessageProtocolResolver.java
+++ b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/grpc/GrpcMessageProtocolResolver.java
@@ -17,6 +17,7 @@
 
 package org.apache.eventmesh.protocol.cloudevents.resolver.grpc;
 
+import org.apache.eventmesh.common.Constants;
 import org.apache.eventmesh.common.protocol.grpc.common.ProtocolKey;
 import org.apache.eventmesh.common.protocol.grpc.common.SimpleMessageWrapper;
 import org.apache.eventmesh.common.protocol.grpc.protos.BatchMessage;
@@ -40,7 +41,7 @@ public class GrpcMessageProtocolResolver {
     public static CloudEvent buildEvent(SimpleMessage message) {
         String cloudEventJson = message.getContent();
 
-        String contentType = message.getPropertiesOrDefault(ProtocolKey.CONTENT_TYPE, "application/cloudevents+json");
+        String contentType = message.getPropertiesOrDefault(ProtocolKey.CONTENT_TYPE, Constants.CONTENT_TYPE_CLOUDEVENTS_JSON);
         EventFormat eventFormat = EventFormatProvider.getInstance().resolveFormat(contentType);
         CloudEvent event = eventFormat.deserialize(cloudEventJson.getBytes(StandardCharsets.UTF_8));
 
@@ -115,7 +116,7 @@ public class GrpcMessageProtocolResolver {
         String sys = cloudEvent.getExtension(ProtocolKey.SYS) == null ? "sys123" : cloudEvent.getExtension(ProtocolKey.SYS).toString();
         String userName = cloudEvent.getExtension(ProtocolKey.USERNAME) == null ? "user" : cloudEvent.getExtension(ProtocolKey.USERNAME).toString();
         String passwd = cloudEvent.getExtension(ProtocolKey.PASSWD) == null ? "pass" : cloudEvent.getExtension(ProtocolKey.PASSWD).toString();
-        String language = cloudEvent.getExtension(ProtocolKey.LANGUAGE) == null ? "JAVA" : cloudEvent.getExtension(ProtocolKey.LANGUAGE).toString();
+        String language = cloudEvent.getExtension(ProtocolKey.LANGUAGE) == null ? Constants.LANGUAGE_JAVA : cloudEvent.getExtension(ProtocolKey.LANGUAGE).toString();
         String protocol = cloudEvent.getExtension(ProtocolKey.PROTOCOL_TYPE) == null ? "protocol" :
             cloudEvent.getExtension(ProtocolKey.PROTOCOL_TYPE).toString();
         String protocolDesc = cloudEvent.getExtension(ProtocolKey.PROTOCOL_DESC) == null ? "protocolDesc" :
@@ -166,7 +167,7 @@ public class GrpcMessageProtocolResolver {
         for (BatchMessage.MessageItem item : batchMessage.getMessageItemList()) {
             String cloudEventJson = item.getContent();
 
-            String contentType = item.getPropertiesOrDefault(ProtocolKey.CONTENT_TYPE, "application/cloudevents+json");
+            String contentType = item.getPropertiesOrDefault(ProtocolKey.CONTENT_TYPE, Constants.CONTENT_TYPE_CLOUDEVENTS_JSON);
             EventFormat eventFormat = EventFormatProvider.getInstance().resolveFormat(contentType);
             CloudEvent event = eventFormat.deserialize(cloudEventJson.getBytes(StandardCharsets.UTF_8));
 
diff --git a/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/MeshMessageProtocolAdaptor.java b/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/MeshMessageProtocolAdaptor.java
index df60c396..26a390d2 100644
--- a/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/MeshMessageProtocolAdaptor.java
+++ b/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/MeshMessageProtocolAdaptor.java
@@ -127,7 +127,7 @@ public class MeshMessageProtocolAdaptor implements ProtocolAdaptor<ProtocolTrans
             body.toMap();
             httpCommand.setBody(body);
             return httpCommand;
-        } else if (StringUtils.equals("grpc", protocolDesc)) {
+        } else if (StringUtils.equals(Constants.PROTOCOL_GRPC, protocolDesc)) {
             return GrpcMessageProtocolResolver.buildSimpleMessage(cloudEvent);
         } else if (StringUtils.equals("tcp", protocolDesc)) {
             return TcpMessageProtocolResolver.buildEventMeshMessage(cloudEvent);
diff --git a/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/resolver/grpc/GrpcMessageProtocolResolver.java b/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/resolver/grpc/GrpcMessageProtocolResolver.java
index 47b4278c..bc74785c 100644
--- a/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/resolver/grpc/GrpcMessageProtocolResolver.java
+++ b/eventmesh-protocol-plugin/eventmesh-protocol-meshmessage/src/main/java/org/apache/eventmesh/protocol/meshmessage/resolver/grpc/GrpcMessageProtocolResolver.java
@@ -17,6 +17,7 @@
 
 package org.apache.eventmesh.protocol.meshmessage.resolver.grpc;
 
+import org.apache.eventmesh.common.Constants;
 import org.apache.eventmesh.common.protocol.grpc.common.ProtocolKey;
 import org.apache.eventmesh.common.protocol.grpc.common.SimpleMessageWrapper;
 import org.apache.eventmesh.common.protocol.grpc.protos.BatchMessage;
@@ -227,7 +228,7 @@ public class GrpcMessageProtocolResolver {
         String sys = cloudEvent.getExtension(ProtocolKey.SYS) == null ? "sys" : cloudEvent.getExtension(ProtocolKey.SYS).toString();
         String userName = cloudEvent.getExtension(ProtocolKey.USERNAME) == null ? "user" : cloudEvent.getExtension(ProtocolKey.USERNAME).toString();
         String passwd = cloudEvent.getExtension(ProtocolKey.PASSWD) == null ? "pass" : cloudEvent.getExtension(ProtocolKey.PASSWD).toString();
-        String language = cloudEvent.getExtension(ProtocolKey.LANGUAGE) == null ? "JAVA" : cloudEvent.getExtension(ProtocolKey.LANGUAGE).toString();
+        String language = cloudEvent.getExtension(ProtocolKey.LANGUAGE) == null ? Constants.LANGUAGE_JAVA : cloudEvent.getExtension(ProtocolKey.LANGUAGE).toString();
         String protocol = cloudEvent.getExtension(ProtocolKey.PROTOCOL_TYPE) == null ? "" :
             cloudEvent.getExtension(ProtocolKey.PROTOCOL_TYPE).toString();
         String protocolDesc = cloudEvent.getExtension(ProtocolKey.PROTOCOL_DESC) == null ? "" :
diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/grpc/push/WebhookPushRequest.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/grpc/push/WebhookPushRequest.java
index f5d6bcf0..a27864bd 100644
--- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/grpc/push/WebhookPushRequest.java
+++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/core/protocol/grpc/push/WebhookPushRequest.java
@@ -116,7 +116,7 @@ public class WebhookPushRequest extends AbstractPushRequest {
             builder.addHeader(ProtocolKey.PROTOCOL_DESC, requestHeader.getProtocolDesc());
             builder.addHeader(ProtocolKey.PROTOCOL_VERSION, requestHeader.getProtocolVersion());
             builder.addHeader(ProtocolKey.CONTENT_TYPE, simpleMessage.getPropertiesOrDefault(ProtocolKey.CONTENT_TYPE,
-                    "application/cloudevents+json"));
+                Constants.CONTENT_TYPE_CLOUDEVENTS_JSON));
 
             List<NameValuePair> body = new ArrayList<>();
             body.add(new BasicNameValuePair(PushMessageRequestBody.CONTENT, simpleMessage.getContent()));
diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/grpc/producer/CloudEventProducer.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/grpc/producer/CloudEventProducer.java
index 99118f1d..fc6297e0 100644
--- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/grpc/producer/CloudEventProducer.java
+++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/grpc/producer/CloudEventProducer.java
@@ -118,9 +118,9 @@ public class CloudEventProducer {
             .withExtension(ProtocolKey.IP, IPUtils.getLocalAddress())
             .withExtension(ProtocolKey.PID, Long.toString(ThreadUtils.getPID()))
             .withExtension(ProtocolKey.SYS, clientConfig.getSys())
-            .withExtension(ProtocolKey.LANGUAGE, "JAVA")
+            .withExtension(ProtocolKey.LANGUAGE, Constants.LANGUAGE_JAVA)
             .withExtension(ProtocolKey.PROTOCOL_TYPE, PROTOCOL_TYPE)
-            .withExtension(ProtocolKey.PROTOCOL_DESC, "grpc")
+            .withExtension(ProtocolKey.PROTOCOL_DESC, Constants.PROTOCOL_GRPC)
             .withExtension(ProtocolKey.PROTOCOL_VERSION, cloudEvent.getSpecVersion().toString())
             .withExtension(ProtocolKey.UNIQUE_ID, RandomStringUtils.generateNum(30))
             .withExtension(ProtocolKey.SEQ_NUM, RandomStringUtils.generateNum(30))
diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/grpc/util/EventMeshClientUtil.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/grpc/util/EventMeshClientUtil.java
index 71b0e2aa..2bce7570 100644
--- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/grpc/util/EventMeshClientUtil.java
+++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/grpc/util/EventMeshClientUtil.java
@@ -64,7 +64,7 @@ public class EventMeshClientUtil {
             .setUsername(clientConfig.getUserName())
             .setPassword(clientConfig.getPassword())
             .setProtocolType(protocolType)
-            .setProtocolDesc("grpc")
+            .setProtocolDesc(Constants.PROTOCOL_GRPC)
             // default CloudEvents version is V1
             .setProtocolVersion(SpecVersion.V1.toString())
             .build();
@@ -117,7 +117,7 @@ public class EventMeshClientUtil {
                                                        String protocolType) {
         if (EventMeshCommon.CLOUD_EVENTS_PROTOCOL_NAME.equals(protocolType)) {
             CloudEvent cloudEvent = (CloudEvent) message;
-            String contentType = StringUtils.isEmpty(cloudEvent.getDataContentType()) ? "application/cloudevents+json"
+            String contentType = StringUtils.isEmpty(cloudEvent.getDataContentType()) ? Constants.CONTENT_TYPE_CLOUDEVENTS_JSON
                 : cloudEvent.getDataContentType();
             byte[] bodyByte = EventFormatProvider.getInstance().resolveFormat(contentType)
                 .serialize(cloudEvent);
@@ -183,7 +183,7 @@ public class EventMeshClientUtil {
                 .setTopic(events.get(0).getSubject());
 
             for (CloudEvent event : events) {
-                String contentType = StringUtils.isEmpty(event.getDataContentType()) ? "application/cloudevents+json"
+                String contentType = StringUtils.isEmpty(event.getDataContentType()) ? Constants.CONTENT_TYPE_CLOUDEVENTS_JSON
                     : event.getDataContentType();
                 byte[] bodyByte = EventFormatProvider.getInstance().resolveFormat(contentType)
                     .serialize(event);
diff --git a/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/grpc/util/EventMeshClientUtilTest.java b/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/grpc/util/EventMeshClientUtilTest.java
index 4b54314e..7c15cf3a 100644
--- a/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/grpc/util/EventMeshClientUtilTest.java
+++ b/eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/grpc/util/EventMeshClientUtilTest.java
@@ -58,7 +58,7 @@ public class EventMeshClientUtilTest {
             .hasFieldOrPropertyWithValue("username", clientConfig.getUserName())
             .hasFieldOrPropertyWithValue("password", clientConfig.getPassword())
             .hasFieldOrPropertyWithValue("protocolType", "protocolType")
-            .hasFieldOrPropertyWithValue("protocolDesc", "grpc")
+            .hasFieldOrPropertyWithValue("protocolDesc", Constants.PROTOCOL_GRPC)
             .hasFieldOrPropertyWithValue("protocolVersion", SpecVersion.V1.toString());
     }
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: commits-help@eventmesh.apache.org