@ -15,20 +15,20 @@
@@ -15,20 +15,20 @@
* /
package org.springframework.data.mongodb.core.encryption ;
import java.util.Map ;
import java.util.Objects ;
import java.util.Optional ;
import com.mongodb.client.model.vault.RangeOptions ;
import org.bson.Document ;
import org.springframework.data.mongodb.core.FindAndReplaceOptions ;
import org.springframework.data.mongodb.util.BsonUtils ;
import org.springframework.data.util.Optionals ;
import org.springframework.lang.Nullable ;
import org.springframework.util.Assert ;
import org.springframework.util.CollectionUtils ;
import org.springframework.util.ObjectUtils ;
import com.mongodb.client.model.vault.RangeOptions ;
/ * *
* Options , like the { @link # algorithm ( ) } , to apply when encrypting values .
* Options used to provide additional information when { @link Encryption encrypting } values . like the
* { @link # algorithm ( ) } to be used .
*
* @author Christoph Strobl
* @author Ross Lawley
@ -45,6 +45,7 @@ public class EncryptionOptions {
@@ -45,6 +45,7 @@ public class EncryptionOptions {
}
public EncryptionOptions ( String algorithm , EncryptionKey key , QueryableEncryptionOptions queryableEncryptionOptions ) {
Assert . hasText ( algorithm , "Algorithm must not be empty" ) ;
Assert . notNull ( key , "EncryptionKey must not be empty" ) ;
Assert . notNull ( key , "QueryableEncryptionOptions must not be empty" ) ;
@ -107,20 +108,23 @@ public class EncryptionOptions {
@@ -107,20 +108,23 @@ public class EncryptionOptions {
* Options , like the { @link # getQueryType ( ) } , to apply when encrypting queryable values .
*
* @author Ross Lawley
* @author Christoph Strobl
* @since 4 . 5
* /
public static class QueryableEncryptionOptions {
private static final QueryableEncryptionOptions NONE = new QueryableEncryptionOptions ( null , null , null ) ;
public static final QueryableEncryptionOptions NONE = new QueryableEncryptionOptions ( null , null , Map . of ( ) ) ;
private final @Nullable String queryType ;
private final @Nullable Long contentionFactor ;
private final @Nullable Document rangeOption s;
private final Map < String , Object > attribute s;
private QueryableEncryptionOptions ( @Nullable String queryType , @Nullable Long contentionFactor ,
@Nullable Document rangeOptions ) {
Map < String , Object > attributes ) {
this . queryType = queryType ;
this . contentionFactor = contentionFactor ;
this . rangeOptions = rangeOption s;
this . attributes = attribute s;
}
/ * *
@ -139,7 +143,7 @@ public class EncryptionOptions {
@@ -139,7 +143,7 @@ public class EncryptionOptions {
* @return new instance of { @link QueryableEncryptionOptions } .
* /
public QueryableEncryptionOptions queryType ( @Nullable String queryType ) {
return new QueryableEncryptionOptions ( queryType , contentionFactor , rangeOption s) ;
return new QueryableEncryptionOptions ( queryType , contentionFactor , attribute s) ;
}
/ * *
@ -149,89 +153,57 @@ public class EncryptionOptions {
@@ -149,89 +153,57 @@ public class EncryptionOptions {
* @return new instance of { @link QueryableEncryptionOptions } .
* /
public QueryableEncryptionOptions contentionFactor ( @Nullable Long contentionFactor ) {
return new QueryableEncryptionOptions ( queryType , contentionFactor , rangeOption s) ;
return new QueryableEncryptionOptions ( queryType , contentionFactor , attribute s) ;
}
/ * *
* Define the { @code rangeOptions } to be used for queryable document encryption .
*
* @param rangeOption s can be { @literal null } .
* @param attribute s can be { @literal null } .
* @return new instance of { @link QueryableEncryptionOptions } .
* /
public QueryableEncryptionOptions rangeOptions ( @Nullable Document rangeOption s) {
return new QueryableEncryptionOptions ( queryType , contentionFactor , rangeOption s) ;
public QueryableEncryptionOptions attributes ( Map < String , Object > attribute s) {
return new QueryableEncryptionOptions ( queryType , contentionFactor , attribute s) ;
}
/ * *
* Get the { @code queryType } to apply .
*
* @return { @link Optional # empty ( ) } if not set .
* @return { @literal null } if not set .
* /
public Optional < String > getQueryType ( ) {
return Optional . ofNullable ( queryType ) ;
public @Nullable String getQueryType ( ) {
return queryType ;
}
/ * *
* Get the { @code contentionFactor } to apply .
*
* @return { @link Optional # empty ( ) } if not set .
* @return { @literal null } if not set .
* /
public Optional < Long > getContentionFactor ( ) {
return Optional . ofNullable ( contentionFactor ) ;
public @Nullable Long getContentionFactor ( ) {
return contentionFactor ;
}
/ * *
* Get the { @code rangeOptions } to apply .
*
* @return { @link Optional # empty ( ) } if not set .
* @return never { @literal null } .
* /
public Optional < RangeOptions > getRangeOptions ( ) {
if ( rangeOptions = = null ) {
return Optional . empty ( ) ;
}
RangeOptions encryptionRangeOptions = new RangeOptions ( ) ;
if ( rangeOptions . containsKey ( "min" ) ) {
encryptionRangeOptions . min ( BsonUtils . simpleToBsonValue ( rangeOptions . get ( "min" ) ) ) ;
}
if ( rangeOptions . containsKey ( "max" ) ) {
encryptionRangeOptions . max ( BsonUtils . simpleToBsonValue ( rangeOptions . get ( "max" ) ) ) ;
}
if ( rangeOptions . containsKey ( "trimFactor" ) ) {
Object trimFactor = rangeOptions . get ( "trimFactor" ) ;
Assert . isInstanceOf ( Integer . class , trimFactor , ( ) - > String
. format ( "Expected to find a %s but it turned out to be %s." , Integer . class , trimFactor . getClass ( ) ) ) ;
encryptionRangeOptions . trimFactor ( ( Integer ) trimFactor ) ;
}
if ( rangeOptions . containsKey ( "sparsity" ) ) {
Object sparsity = rangeOptions . get ( "sparsity" ) ;
Assert . isInstanceOf ( Number . class , sparsity ,
( ) - > String . format ( "Expected to find a %s but it turned out to be %s." , Long . class , sparsity . getClass ( ) ) ) ;
encryptionRangeOptions . sparsity ( ( ( Number ) sparsity ) . longValue ( ) ) ;
}
if ( rangeOptions . containsKey ( "precision" ) ) {
Object precision = rangeOptions . get ( "precision" ) ;
Assert . isInstanceOf ( Number . class , precision , ( ) - > String
. format ( "Expected to find a %s but it turned out to be %s." , Integer . class , precision . getClass ( ) ) ) ;
encryptionRangeOptions . precision ( ( ( Number ) precision ) . intValue ( ) ) ;
}
return Optional . of ( encryptionRangeOptions ) ;
public Map < String , Object > getAttributes ( ) {
return Map . copyOf ( attributes ) ;
}
/ * *
* @return { @literal true } if no arguments set .
* /
boolean isEmpty ( ) {
return ! Optionals . isAnyPresent ( getQueryType ( ) , getContentionFactor ( ) , getRangeOptions ( ) ) ;
return getQueryType ( ) = = null & & getContentionFactor ( ) = = null & & getAttributes ( ) . isEmpty ( ) ;
}
@Override
public String toString ( ) {
return "QueryableEncryptionOptions{" + "queryType='" + queryType + '\'' + ", contentionFactor=" + contentionFactor
+ ", rangeOptions=" + rangeOption s + '}' ;
+ ", attributes=" + attributes + '}' ;
}
@Override
@ -251,12 +223,12 @@ public class EncryptionOptions {
@@ -251,12 +223,12 @@ public class EncryptionOptions {
if ( ! ObjectUtils . nullSafeEquals ( contentionFactor , that . contentionFactor ) ) {
return false ;
}
return ObjectUtils . nullSafeEquals ( rangeOption s, that . rangeOption s) ;
return ObjectUtils . nullSafeEquals ( attribute s, that . attribute s) ;
}
@Override
public int hashCode ( ) {
return Objects . hash ( queryType , contentionFactor , rangeOption s) ;
return Objects . hash ( queryType , contentionFactor , attribute s) ;
}
}
}