Skip to content

added way to build without ee features #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions CouchbaseReflectionHelper.kt
Original file line number Diff line number Diff line change
@@ -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}")
}
Comment on lines +16 to +18

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

Catching a generic Exception is dangerous as it can swallow any exception, including RuntimeExceptions that indicate a bug in the code, not just the absence of EE features. This can hide bugs and lead to unpredictable application behavior.

You should only catch specific reflection-related exceptions. Any other exception should be allowed to propagate. This ensures you are only handling the expected 'feature not available' scenario.

        } catch (e: java.lang.reflect.ReflectiveOperationException) {
            Log.w("CouchbaseLite", "Failed to set encryption key: ${e.message}")
        } catch (e: ClassNotFoundException) {
            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
}
}
}
8 changes: 4 additions & 4 deletions DatabaseManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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)
}
}
}
Expand All @@ -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)
Comment on lines +44 to +47

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The if statement here is now redundant. CouchbaseReflectionHelper.changeEncryptionKey is called in both branches, just with different values for encryptionKey. You can simplify this by removing the conditional and calling the helper method once, since the helper already handles the null case.

Suggested change
CouchbaseReflectionHelper.changeEncryptionKey(db, null)
return
}
val encryptionKeyValue = EncryptionKey(encryptionKey)
db.changeEncryptionKey(encryptionKeyValue)
CouchbaseReflectionHelper.changeEncryptionKey(db, encryptionKey)
CouchbaseReflectionHelper.changeEncryptionKey(db, encryptionKey)

}

fun closeDatabase(databaseName: String) {
Expand Down
3 changes: 2 additions & 1 deletion ReplicatorHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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}")
}
Expand Down