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/10/22 12:18:58 UTC

[incubator-eventmesh] branch rabbitmq-connector updated: add rabbitmq cloudevent and cloudevent write

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

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


The following commit(s) were added to refs/heads/rabbitmq-connector by this push:
     new f7873139 add rabbitmq cloudevent and cloudevent write
     new 2fc6148e Merge pull request #1731 from mroccyen/rabbitmq-cloudevent-and-writer
f7873139 is described below

commit f7873139e394c4791c76f78fdb18de7a6271c13a
Author: mroccyen <qi...@126.com>
AuthorDate: Fri Oct 21 16:18:41 2022 +0800

    add rabbitmq cloudevent and cloudevent write
---
 .../rabbitmq/cloudevent/RabbitmqCloudEvent.java    | 76 ++++++++++++++++++++++
 .../cloudevent/RabbitmqCloudEventWriter.java       | 68 +++++++++++++++++++
 .../exception/RabbitmqaConnectorException.java     | 31 +++++++++
 .../connector/rabbitmq/utils/ByteArrayUtils.java   | 49 ++++++++++++++
 4 files changed, 224 insertions(+)

diff --git a/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/main/java/org/apache/eventmesh/connector/rabbitmq/cloudevent/RabbitmqCloudEvent.java b/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/main/java/org/apache/eventmesh/connector/rabbitmq/cloudevent/RabbitmqCloudEvent.java
new file mode 100644
index 00000000..430c29cf
--- /dev/null
+++ b/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/main/java/org/apache/eventmesh/connector/rabbitmq/cloudevent/RabbitmqCloudEvent.java
@@ -0,0 +1,76 @@
+/*
+ * 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.connector.rabbitmq.cloudevent;
+
+import org.apache.eventmesh.connector.rabbitmq.exception.RabbitmqaConnectorException;
+import org.apache.eventmesh.connector.rabbitmq.utils.ByteArrayUtils;
+
+import java.io.Serializable;
+import java.net.URI;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+
+import io.cloudevents.CloudEvent;
+import io.cloudevents.SpecVersion;
+import io.cloudevents.core.builder.CloudEventBuilder;
+
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@Data
+@NoArgsConstructor
+public class RabbitmqCloudEvent implements Serializable {
+
+    private SpecVersion version;
+    private String data;
+    private Map<String, String> extensions = new HashMap<>();
+
+    public CloudEvent convertToCloudEvent() {
+        CloudEventBuilder builder;
+        switch (version) {
+            case V03:
+                builder = CloudEventBuilder.v03();
+                break;
+            case V1:
+                builder = CloudEventBuilder.v1();
+                break;
+            default:
+                throw new RabbitmqaConnectorException(String.format("CloudEvent version %s does not support.", version));
+        }
+        builder.withData(data.getBytes())
+                .withId(extensions.remove("id"))
+                .withSource(URI.create(extensions.remove("source")))
+                .withType(extensions.remove("type"))
+                .withDataContentType(extensions.remove("datacontenttype"))
+                .withSubject(extensions.remove("subject"));
+        extensions.forEach(builder::withExtension);
+
+        return builder.build();
+    }
+
+    public static byte[] toByteArray(RabbitmqCloudEvent rabbitmqCloudEvent) throws Exception {
+        Optional<byte[]> optionalBytes = ByteArrayUtils.objectToBytes(rabbitmqCloudEvent);
+        return optionalBytes.orElseGet(() -> new byte[]{});
+    }
+
+    public static RabbitmqCloudEvent getFromByteArray(byte[] body) throws Exception {
+        Optional<RabbitmqCloudEvent> optionalCloudEvent = ByteArrayUtils.bytesToObject(body);
+        return optionalCloudEvent.orElse(null);
+    }
+}
diff --git a/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/main/java/org/apache/eventmesh/connector/rabbitmq/cloudevent/RabbitmqCloudEventWriter.java b/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/main/java/org/apache/eventmesh/connector/rabbitmq/cloudevent/RabbitmqCloudEventWriter.java
new file mode 100644
index 00000000..b7e2e093
--- /dev/null
+++ b/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/main/java/org/apache/eventmesh/connector/rabbitmq/cloudevent/RabbitmqCloudEventWriter.java
@@ -0,0 +1,68 @@
+/*
+ * 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.connector.rabbitmq.cloudevent;
+
+import java.nio.charset.StandardCharsets;
+
+import io.cloudevents.CloudEventData;
+import io.cloudevents.SpecVersion;
+import io.cloudevents.core.format.EventFormat;
+import io.cloudevents.core.message.MessageWriter;
+import io.cloudevents.rw.CloudEventContextWriter;
+import io.cloudevents.rw.CloudEventRWException;
+import io.cloudevents.rw.CloudEventWriter;
+
+public class RabbitmqCloudEventWriter
+        implements MessageWriter<CloudEventWriter<RabbitmqCloudEvent>, RabbitmqCloudEvent>, CloudEventWriter<RabbitmqCloudEvent> {
+
+    private final RabbitmqCloudEvent rabbitmqCloudEvent;
+
+    public RabbitmqCloudEventWriter() {
+        rabbitmqCloudEvent = new RabbitmqCloudEvent();
+    }
+
+    @Override
+    public RabbitmqCloudEvent setEvent(EventFormat format, byte[] value) throws CloudEventRWException {
+        rabbitmqCloudEvent.setData(new String(value, StandardCharsets.UTF_8));
+        return rabbitmqCloudEvent;
+    }
+
+    @Override
+    public RabbitmqCloudEvent end(CloudEventData data) throws CloudEventRWException {
+        rabbitmqCloudEvent.setData(new String(data.toBytes(), StandardCharsets.UTF_8));
+        return rabbitmqCloudEvent;
+    }
+
+    @Override
+    public RabbitmqCloudEvent end() throws CloudEventRWException {
+        rabbitmqCloudEvent.setData("");
+        return rabbitmqCloudEvent;
+    }
+
+    @Override
+    public CloudEventContextWriter withContextAttribute(String name, String value) throws CloudEventRWException {
+        rabbitmqCloudEvent.getExtensions().put(name, value);
+        return this;
+    }
+
+    @Override
+    public CloudEventWriter<RabbitmqCloudEvent> create(SpecVersion version) throws CloudEventRWException {
+        rabbitmqCloudEvent.setVersion(version);
+        return this;
+    }
+}
diff --git a/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/main/java/org/apache/eventmesh/connector/rabbitmq/exception/RabbitmqaConnectorException.java b/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/main/java/org/apache/eventmesh/connector/rabbitmq/exception/RabbitmqaConnectorException.java
new file mode 100644
index 00000000..ba069998
--- /dev/null
+++ b/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/main/java/org/apache/eventmesh/connector/rabbitmq/exception/RabbitmqaConnectorException.java
@@ -0,0 +1,31 @@
+/*
+ * 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.connector.rabbitmq.exception;
+
+import org.apache.eventmesh.api.exception.ConnectorRuntimeException;
+
+public class RabbitmqaConnectorException extends ConnectorRuntimeException {
+
+    public RabbitmqaConnectorException(String message) {
+        super(message);
+    }
+
+    public RabbitmqaConnectorException(Throwable throwable) {
+        super(throwable);
+    }
+}
diff --git a/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/main/java/org/apache/eventmesh/connector/rabbitmq/utils/ByteArrayUtils.java b/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/main/java/org/apache/eventmesh/connector/rabbitmq/utils/ByteArrayUtils.java
new file mode 100644
index 00000000..5554d459
--- /dev/null
+++ b/eventmesh-connector-plugin/eventmesh-connector-rabbitmq/src/main/java/org/apache/eventmesh/connector/rabbitmq/utils/ByteArrayUtils.java
@@ -0,0 +1,49 @@
+/*
+ * 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.connector.rabbitmq.utils;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.Optional;
+
+@SuppressWarnings("all")
+public class ByteArrayUtils {
+
+    public static <T> Optional<byte[]> objectToBytes(T obj) throws IOException {
+        byte[] bytes = null;
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        ObjectOutputStream sOut = new ObjectOutputStream(out);
+        sOut.writeObject(obj);
+        sOut.flush();
+        bytes = out.toByteArray();
+
+        return Optional.ofNullable(bytes);
+    }
+
+    public static <T> Optional<T> bytesToObject(byte[] bytes) throws IOException, ClassNotFoundException {
+        T t = null;
+        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
+        ObjectInputStream sIn = new ObjectInputStream(in);
+        t = (T) sIn.readObject();
+        return Optional.ofNullable(t);
+
+    }
+}


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