From 984eacc82629aa78c41e295b65c0f97d4211700b Mon Sep 17 00:00:00 2001 From: ruhshan Date: Tue, 25 Jul 2023 08:29:53 +0600 Subject: [PATCH] [ISSUE #4099]Refactor re-used constant's usage from dedicated file (#4257) * [ISSUE #4099]: Refactor re-used constant's usage from dedicated file * [ISSUE #4099] Rename some constants for mongodb storage plugin * [ISSUE #4099] Refactor constant imports in mongodb cloud event util * [ISSUE #4099]: Replace contants imports to class import in mongo cloud event util --- .../mongodb/constant/MongodbConstants.java | 9 ++++++ .../mongodb/utils/MongodbCloudEventUtil.java | 30 ++++++++++--------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/eventmesh-storage-plugin/eventmesh-storage-mongodb/src/main/java/org/apache/eventmesh/storage/mongodb/constant/MongodbConstants.java b/eventmesh-storage-plugin/eventmesh-storage-mongodb/src/main/java/org/apache/eventmesh/storage/mongodb/constant/MongodbConstants.java index 6af239bea5..23a2570db8 100644 --- a/eventmesh-storage-plugin/eventmesh-storage-mongodb/src/main/java/org/apache/eventmesh/storage/mongodb/constant/MongodbConstants.java +++ b/eventmesh-storage-plugin/eventmesh-storage-mongodb/src/main/java/org/apache/eventmesh/storage/mongodb/constant/MongodbConstants.java @@ -24,5 +24,14 @@ public class MongodbConstants { public static final String SEQUENCE_COLLECTION_NAME = "pub_sub_seq"; public static final String SEQUENCE_KEY_FN = "topic"; public static final String SEQUENCE_VALUE_FN = "value"; + public static final String CLOUD_EVENT_DOC_VERSION = "version"; + public static final String CLOUD_EVENT_DOC_DATA = "data"; + public static final String CLOUD_EVENT_DOC_ID = "id"; + public static final String CLOUD_EVENT_DOC_SOURCE = "source"; + public static final String CLOUD_EVENT_DOC_TYPE = "type"; + public static final String CLOUD_EVENT_DOC_DATACONTENTTYPE = "datacontenttype"; + public static final String CLOUD_EVENT_DOC_SUBJECT = "subject"; + + } diff --git a/eventmesh-storage-plugin/eventmesh-storage-mongodb/src/main/java/org/apache/eventmesh/storage/mongodb/utils/MongodbCloudEventUtil.java b/eventmesh-storage-plugin/eventmesh-storage-mongodb/src/main/java/org/apache/eventmesh/storage/mongodb/utils/MongodbCloudEventUtil.java index 51bb19d061..c18c711ef1 100644 --- a/eventmesh-storage-plugin/eventmesh-storage-mongodb/src/main/java/org/apache/eventmesh/storage/mongodb/utils/MongodbCloudEventUtil.java +++ b/eventmesh-storage-plugin/eventmesh-storage-mongodb/src/main/java/org/apache/eventmesh/storage/mongodb/utils/MongodbCloudEventUtil.java @@ -18,6 +18,7 @@ package org.apache.eventmesh.storage.mongodb.utils; import org.apache.eventmesh.common.Constants; +import org.apache.eventmesh.storage.mongodb.constant.MongodbConstants; import org.apache.eventmesh.storage.mongodb.exception.MongodbStorageException; import java.net.URI; @@ -29,10 +30,11 @@ import io.cloudevents.SpecVersion; import io.cloudevents.core.builder.CloudEventBuilder; + public class MongodbCloudEventUtil { public static CloudEvent convertToCloudEvent(Document document) { document.remove("_id"); - String versionStr = document.getString("version"); + String versionStr = document.getString(MongodbConstants.CLOUD_EVENT_DOC_VERSION); SpecVersion version = SpecVersion.valueOf(versionStr); CloudEventBuilder builder; switch (version) { @@ -45,12 +47,12 @@ public static CloudEvent convertToCloudEvent(Document document) { default: throw new MongodbStorageException(String.format("CloudEvent version %s does not support.", version)); } - builder.withData(document.remove("data").toString().getBytes(Constants.DEFAULT_CHARSET)) - .withId(document.remove("id").toString()) - .withSource(URI.create(document.remove("source").toString())) - .withType(document.remove("type").toString()) - .withDataContentType(document.remove("datacontenttype").toString()) - .withSubject(document.remove("subject").toString()); + builder.withData(document.remove(MongodbConstants.CLOUD_EVENT_DOC_DATA).toString().getBytes(Constants.DEFAULT_CHARSET)) + .withId(document.remove(MongodbConstants.CLOUD_EVENT_DOC_ID).toString()) + .withSource(URI.create(document.remove(MongodbConstants.CLOUD_EVENT_DOC_SOURCE).toString())) + .withType(document.remove(MongodbConstants.CLOUD_EVENT_DOC_TYPE).toString()) + .withDataContentType(document.remove(MongodbConstants.CLOUD_EVENT_DOC_DATACONTENTTYPE).toString()) + .withSubject(document.remove(MongodbConstants.CLOUD_EVENT_DOC_SUBJECT).toString()); document.forEach((key, value) -> builder.withExtension(key, value.toString())); return builder.build(); @@ -58,14 +60,14 @@ public static CloudEvent convertToCloudEvent(Document document) { public static Document convertToDocument(CloudEvent cloudEvent) { Document document = new Document(); - document.put("version", cloudEvent.getSpecVersion().name()); - document.put("data", cloudEvent.getData() == null + document.put(MongodbConstants.CLOUD_EVENT_DOC_VERSION, cloudEvent.getSpecVersion().name()); + document.put(MongodbConstants.CLOUD_EVENT_DOC_DATA, cloudEvent.getData() == null ? null : new String(cloudEvent.getData().toBytes(), StandardCharsets.UTF_8)); - document.put("id", cloudEvent.getId()); - document.put("source", cloudEvent.getSource().toString()); - document.put("type", cloudEvent.getType()); - document.put("datacontenttype", cloudEvent.getDataContentType()); - document.put("subject", cloudEvent.getSubject()); + document.put(MongodbConstants.CLOUD_EVENT_DOC_ID, cloudEvent.getId()); + document.put(MongodbConstants.CLOUD_EVENT_DOC_SOURCE, cloudEvent.getSource().toString()); + document.put(MongodbConstants.CLOUD_EVENT_DOC_TYPE, cloudEvent.getType()); + document.put(MongodbConstants.CLOUD_EVENT_DOC_DATACONTENTTYPE, cloudEvent.getDataContentType()); + document.put(MongodbConstants.CLOUD_EVENT_DOC_SUBJECT, cloudEvent.getSubject()); cloudEvent.getExtensionNames().forEach(key -> document.put(key, cloudEvent.getExtension(key))); return document;