Skip to content

Commit

Permalink
[ISSUE #4099]Refactor re-used constant's usage from dedicated file (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
Ruhshan authored Jul 25, 2023
1 parent 2cb5aa9 commit 984eacc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";



}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -45,27 +47,27 @@ 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();
}

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;
Expand Down

0 comments on commit 984eacc

Please sign in to comment.