From 6b786be77d11629ce6a66ec914e242dcbe46c9ad Mon Sep 17 00:00:00 2001 From: Wojtach Date: Fri, 27 Jun 2025 18:02:24 +0200 Subject: [PATCH] added way to build without ee features --- CouchbaseReflectionHelper.kt | 53 ++++++++++++++++++++++++++++++++++++ DatabaseManager.kt | 8 +++--- ReplicatorHelper.kt | 3 +- 3 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 CouchbaseReflectionHelper.kt diff --git a/CouchbaseReflectionHelper.kt b/CouchbaseReflectionHelper.kt new file mode 100644 index 0000000..f4dbc89 --- /dev/null +++ b/CouchbaseReflectionHelper.kt @@ -0,0 +1,53 @@ +package com.cblreactnative + +import android.util.Log +import com.couchbase.lite.Database +import com.couchbase.lite.DatabaseConfiguration +import com.couchbase.lite.ReplicatorConfiguration + +object CouchbaseReflectionHelper { + + fun setEncryptionKey(config: DatabaseConfiguration, encryptionKey: String) { + try { + val encryptionKeyClass = Class.forName("com.couchbase.lite.EncryptionKey") + val encryptionKeyInstance = encryptionKeyClass.getConstructor(String::class.java).newInstance(encryptionKey) + val setMethod = config.javaClass.getMethod("setEncryptionKey", encryptionKeyClass) + setMethod.invoke(config, encryptionKeyInstance) + } catch (e: Exception) { + Log.w("CouchbaseLite", "Failed to set encryption key: ${e.message}") + } + } + + fun changeEncryptionKey(database: Database, encryptionKey: String?) { + try { + val encryptionKeyClass = Class.forName("com.couchbase.lite.EncryptionKey") + val encryptionKeyInstance = if (encryptionKey != null) { + encryptionKeyClass.getConstructor(String::class.java).newInstance(encryptionKey) + } else null + + val changeMethod = database.javaClass.getMethod("changeEncryptionKey", encryptionKeyClass) + changeMethod.invoke(database, encryptionKeyInstance) + } catch (e: Exception) { + Log.w("CouchbaseLite", "Failed to change encryption key: ${e.message}") + } + } + + fun setAcceptOnlySelfSignedServerCertificate(config: ReplicatorConfiguration, value: Boolean) { + try { + val method = config.javaClass.getMethod("setAcceptOnlySelfSignedServerCertificate", Boolean::class.javaPrimitiveType) + method.invoke(config, value) + } catch (e: Exception) { + Log.w("CouchbaseLite", "Failed to set certificate option: ${e.message}") + } + } + + fun isAcceptOnlySelfSignedServerCertificate(config: ReplicatorConfiguration): Boolean { + return try { + val method = config.javaClass.getMethod("isAcceptOnlySelfSignedServerCertificate") + method.invoke(config) as Boolean + } catch (e: Exception) { + Log.w("CouchbaseLite", "Failed to get certificate option: ${e.message}") + false + } + } +} \ No newline at end of file diff --git a/DatabaseManager.kt b/DatabaseManager.kt index 8db6bc7..2fbcf90 100644 --- a/DatabaseManager.kt +++ b/DatabaseManager.kt @@ -6,6 +6,7 @@ import kotlin.random.Random import org.json.JSONObject import java.io.File +import com.cblreactnative.CouchbaseReflectionHelper typealias CBLCollection = com.couchbase.lite.Collection @@ -30,7 +31,7 @@ object DatabaseManager { if (jsonConfig.has("encryptionKey")) { val encryptionKey = jsonConfig.getString("encryptionKey") if (encryptionKey.isNotEmpty()) { - databaseConfig.setEncryptionKey(EncryptionKey(encryptionKey)) + CouchbaseReflectionHelper.setEncryptionKey(databaseConfig, encryptionKey) } } } @@ -40,11 +41,10 @@ object DatabaseManager { fun changeEncryptionKey(databaseName: String, encryptionKey: String?) { val db = getDatabase(databaseName) ?: throw Exception("Error: Database not found.") if (encryptionKey == null) { - db.changeEncryptionKey(null) + CouchbaseReflectionHelper.changeEncryptionKey(db, null) return } - val encryptionKeyValue = EncryptionKey(encryptionKey) - db.changeEncryptionKey(encryptionKeyValue) + CouchbaseReflectionHelper.changeEncryptionKey(db, encryptionKey) } fun closeDatabase(databaseName: String) { diff --git a/ReplicatorHelper.kt b/ReplicatorHelper.kt index a85930b..3a47285 100644 --- a/ReplicatorHelper.kt +++ b/ReplicatorHelper.kt @@ -12,6 +12,7 @@ import org.json.JSONException import org.json.JSONObject import java.net.URI import java.net.URISyntaxException +import com.cblreactnative.CouchbaseReflectionHelper import com.couchbase.lite.Collection as CBLCollection object ReplicatorHelper { @@ -42,7 +43,7 @@ object ReplicatorHelper { // Set other properties try { - replicatorConfig.isAcceptOnlySelfSignedServerCertificate = config.getBoolean("acceptSelfSignedCerts") + CouchbaseReflectionHelper.setAcceptOnlySelfSignedServerCertificate(replicatorConfig, config.getBoolean("acceptSelfSignedCerts")) } catch (e: Exception) { Log.d(TAG, "Could not set acceptSelfSignedCerts: ${e.message}") }