You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2023/06/18 11:10:31 UTC

[shardingsphere] branch master updated: Refactor EncryptRule (#26414)

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

panjuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 4d8e7141d8b Refactor EncryptRule (#26414)
4d8e7141d8b is described below

commit 4d8e7141d8b2a78528b64743cbdcc4afcf6c7132
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sun Jun 18 19:10:25 2023 +0800

    Refactor EncryptRule (#26414)
---
 .../shardingsphere/encrypt/rule/EncryptRule.java   | 50 +++++++---------------
 1 file changed, 16 insertions(+), 34 deletions(-)

diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java
index 1e670ce6174..508332366e1 100644
--- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java
+++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java
@@ -150,11 +150,8 @@ public final class EncryptRule implements DatabaseRule, TableContainedRule {
         if (null == originalValue) {
             return null;
         }
-        @SuppressWarnings("rawtypes")
-        Optional<StandardEncryptAlgorithm> encryptor = findStandardEncryptor(tableName, logicColumnName);
-        ShardingSpherePreconditions.checkState(encryptor.isPresent(), () -> new MissingEncryptorException(tableName, logicColumnName, "STANDARD"));
         EncryptContext context = EncryptContextBuilder.build(databaseName, schemaName, tableName, logicColumnName);
-        return encryptor.get().encrypt(originalValue, context);
+        return getStandardEncryptor(tableName, logicColumnName).encrypt(originalValue, context);
     }
     
     /**
@@ -168,11 +165,8 @@ public final class EncryptRule implements DatabaseRule, TableContainedRule {
      * @return encrypted values
      */
     public List<Object> encrypt(final String databaseName, final String schemaName, final String tableName, final String logicColumnName, final List<Object> originalValues) {
-        @SuppressWarnings("rawtypes")
-        Optional<StandardEncryptAlgorithm> encryptor = findStandardEncryptor(tableName, logicColumnName);
-        ShardingSpherePreconditions.checkState(encryptor.isPresent(), () -> new MissingEncryptorException(tableName, logicColumnName, "STANDARD"));
         EncryptContext context = EncryptContextBuilder.build(databaseName, schemaName, tableName, logicColumnName);
-        return encrypt(encryptor.get(), originalValues, context);
+        return encrypt(getStandardEncryptor(tableName, logicColumnName), originalValues, context);
     }
     
     @SuppressWarnings("unchecked")
@@ -199,16 +193,14 @@ public final class EncryptRule implements DatabaseRule, TableContainedRule {
         if (null == cipherValue) {
             return null;
         }
-        @SuppressWarnings("rawtypes")
-        Optional<StandardEncryptAlgorithm> encryptor = findStandardEncryptor(tableName, logicColumnName);
-        ShardingSpherePreconditions.checkState(encryptor.isPresent(), () -> new MissingEncryptorException(tableName, logicColumnName, "STANDARD"));
         EncryptContext context = EncryptContextBuilder.build(databaseName, schemaName, tableName, logicColumnName);
-        return encryptor.get().decrypt(cipherValue, context);
+        return getStandardEncryptor(tableName, logicColumnName).decrypt(cipherValue, context);
     }
     
     @SuppressWarnings("rawtypes")
-    private Optional<StandardEncryptAlgorithm> findStandardEncryptor(final String tableName, final String logicColumnName) {
-        return findEncryptTable(tableName).flatMap(optional -> optional.findEncryptorName(logicColumnName).map(standardEncryptors::get));
+    private StandardEncryptAlgorithm getStandardEncryptor(final String tableName, final String logicColumnName) {
+        return findEncryptTable(tableName).flatMap(optional -> optional.findEncryptorName(logicColumnName).map(standardEncryptors::get))
+                .orElseThrow(() -> new MissingEncryptorException(tableName, logicColumnName, "STANDARD"));
     }
     
     /**
@@ -226,11 +218,8 @@ public final class EncryptRule implements DatabaseRule, TableContainedRule {
         if (null == originalValue) {
             return null;
         }
-        @SuppressWarnings("rawtypes")
-        Optional<AssistedEncryptAlgorithm> assistedQueryEncryptor = findAssistedQueryEncryptor(tableName, logicColumnName);
-        ShardingSpherePreconditions.checkState(assistedQueryEncryptor.isPresent(), () -> new MissingEncryptorException(tableName, logicColumnName, "ASSIST_QUERY"));
         EncryptContext context = EncryptContextBuilder.build(databaseName, schemaName, tableName, logicColumnName);
-        return assistedQueryEncryptor.get().encrypt(originalValue, context);
+        return getAssistedQueryEncryptor(tableName, logicColumnName).encrypt(originalValue, context);
     }
     
     /**
@@ -244,11 +233,8 @@ public final class EncryptRule implements DatabaseRule, TableContainedRule {
      * @return assisted query values
      */
     public List<Object> getEncryptAssistedQueryValues(final String databaseName, final String schemaName, final String tableName, final String logicColumnName, final List<Object> originalValues) {
-        @SuppressWarnings("rawtypes")
-        Optional<AssistedEncryptAlgorithm> assistedQueryEncryptor = findAssistedQueryEncryptor(tableName, logicColumnName);
-        ShardingSpherePreconditions.checkState(assistedQueryEncryptor.isPresent(), () -> new MissingEncryptorException(tableName, logicColumnName, "ASSIST_QUERY"));
         EncryptContext context = EncryptContextBuilder.build(databaseName, schemaName, tableName, logicColumnName);
-        return getEncryptAssistedQueryValues(assistedQueryEncryptor.get(), originalValues, context);
+        return getEncryptAssistedQueryValues(getAssistedQueryEncryptor(tableName, logicColumnName), originalValues, context);
     }
     
     @SuppressWarnings("unchecked")
@@ -262,8 +248,9 @@ public final class EncryptRule implements DatabaseRule, TableContainedRule {
     }
     
     @SuppressWarnings("rawtypes")
-    private Optional<AssistedEncryptAlgorithm> findAssistedQueryEncryptor(final String tableName, final String logicColumnName) {
-        return findEncryptTable(tableName).flatMap(optional -> optional.findAssistedQueryEncryptorName(logicColumnName).map(assistedEncryptors::get));
+    private AssistedEncryptAlgorithm getAssistedQueryEncryptor(final String tableName, final String logicColumnName) {
+        return findEncryptTable(tableName).flatMap(optional -> optional.findAssistedQueryEncryptorName(logicColumnName).map(assistedEncryptors::get))
+                .orElseThrow(() -> new MissingEncryptorException(tableName, logicColumnName, "ASSIST_QUERY"));
     }
     
     /**
@@ -281,11 +268,8 @@ public final class EncryptRule implements DatabaseRule, TableContainedRule {
         if (null == originalValue) {
             return null;
         }
-        @SuppressWarnings("rawtypes")
-        Optional<LikeEncryptAlgorithm> likeQueryEncryptor = findLikeQueryEncryptor(tableName, logicColumnName);
-        ShardingSpherePreconditions.checkState(likeQueryEncryptor.isPresent(), () -> new MissingEncryptorException(tableName, logicColumnName, "LIKE_QUERY"));
         EncryptContext context = EncryptContextBuilder.build(databaseName, schemaName, tableName, logicColumnName);
-        return likeQueryEncryptor.get().encrypt(originalValue, context);
+        return getLikeQueryEncryptor(tableName, logicColumnName).encrypt(originalValue, context);
     }
     
     /**
@@ -299,11 +283,8 @@ public final class EncryptRule implements DatabaseRule, TableContainedRule {
      * @return like query values
      */
     public List<Object> getEncryptLikeQueryValues(final String databaseName, final String schemaName, final String tableName, final String logicColumnName, final List<Object> originalValues) {
-        @SuppressWarnings("rawtypes")
-        Optional<LikeEncryptAlgorithm> likeQueryEncryptor = findLikeQueryEncryptor(tableName, logicColumnName);
-        ShardingSpherePreconditions.checkState(likeQueryEncryptor.isPresent(), () -> new MissingEncryptorException(tableName, logicColumnName, "LIKE_QUERY"));
         EncryptContext context = EncryptContextBuilder.build(databaseName, schemaName, tableName, logicColumnName);
-        return getEncryptLikeQueryValues(likeQueryEncryptor.get(), originalValues, context);
+        return getEncryptLikeQueryValues(getLikeQueryEncryptor(tableName, logicColumnName), originalValues, context);
     }
     
     @SuppressWarnings("unchecked")
@@ -316,8 +297,9 @@ public final class EncryptRule implements DatabaseRule, TableContainedRule {
     }
     
     @SuppressWarnings("rawtypes")
-    private Optional<LikeEncryptAlgorithm> findLikeQueryEncryptor(final String tableName, final String logicColumnName) {
-        return findEncryptTable(tableName).flatMap(optional -> optional.findLikeQueryEncryptorName(logicColumnName).map(likeEncryptors::get));
+    private LikeEncryptAlgorithm getLikeQueryEncryptor(final String tableName, final String logicColumnName) {
+        return findEncryptTable(tableName).flatMap(optional -> optional.findLikeQueryEncryptorName(logicColumnName).map(likeEncryptors::get))
+                .orElseThrow(() -> new MissingEncryptorException(tableName, logicColumnName, "LIKE_QUERY"));
     }
     
     @Override