Skip to content

Commit f0697db

Browse files
committed
Polishing.
Reformat code, replace known unsupported constructor with UnsupportedOperationException. See #4432 Original pull request: #4439
1 parent 2cc5e42 commit f0697db

File tree

5 files changed

+66
-94
lines changed

5 files changed

+66
-94
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/encryption/MongoEncryptionConverter.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.bson.BsonDocument;
2727
import org.bson.BsonValue;
2828
import org.bson.Document;
29-
import org.bson.conversions.Bson;
3029
import org.bson.types.Binary;
3130
import org.springframework.core.CollectionFactory;
3231
import org.springframework.data.mongodb.core.convert.MongoConversionContext;
@@ -96,7 +95,7 @@ public Object decrypt(Object encryptedValue, EncryptionContext context) {
9695
if (!persistentProperty.isEntity()) {
9796
Collection<Object> collection = CollectionFactory.createCollection(persistentProperty.getType(), size);
9897
iterable.forEach(it -> {
99-
if(it instanceof BsonValue bsonValue) {
98+
if (it instanceof BsonValue bsonValue) {
10099
collection.add(BsonUtils.toJavaType(bsonValue));
101100
} else {
102101
collection.add(context.read(it, persistentProperty.getActualType()));
@@ -107,7 +106,7 @@ public Object decrypt(Object encryptedValue, EncryptionContext context) {
107106
} else {
108107
Collection<Object> collection = CollectionFactory.createCollection(persistentProperty.getType(), size);
109108
iterable.forEach(it -> {
110-
if(it instanceof BsonValue bsonValue) {
109+
if (it instanceof BsonValue bsonValue) {
111110
collection.add(context.read(BsonUtils.toJavaType(bsonValue), persistentProperty.getActualType()));
112111
} else {
113112
collection.add(context.read(it, persistentProperty.getActualType()));
@@ -118,14 +117,14 @@ public Object decrypt(Object encryptedValue, EncryptionContext context) {
118117
}
119118

120119
if (!persistentProperty.isEntity() && persistentProperty.isMap()) {
121-
if(persistentProperty.getType() != Document.class) {
122-
if(decryptedValue instanceof BsonValue bsonValue) {
120+
if (persistentProperty.getType() != Document.class) {
121+
if (decryptedValue instanceof BsonValue bsonValue) {
123122
return new LinkedHashMap<>((Document) BsonUtils.toJavaType(bsonValue));
124123
}
125-
if(decryptedValue instanceof Document document) {
124+
if (decryptedValue instanceof Document document) {
126125
return new LinkedHashMap<>(document);
127126
}
128-
if(decryptedValue instanceof Map map) {
127+
if (decryptedValue instanceof Map map) {
129128
return map;
130129
}
131130
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/util/BsonUtils.java

+27-41
Original file line numberDiff line numberDiff line change
@@ -287,36 +287,22 @@ static void removeFrom(Bson bson, String key) {
287287
*/
288288
public static Object toJavaType(BsonValue value) {
289289

290-
switch (value.getBsonType()) {
291-
case INT32:
292-
return value.asInt32().getValue();
293-
case INT64:
294-
return value.asInt64().getValue();
295-
case STRING:
296-
return value.asString().getValue();
297-
case DECIMAL128:
298-
return value.asDecimal128().doubleValue();
299-
case DOUBLE:
300-
return value.asDouble().getValue();
301-
case BOOLEAN:
302-
return value.asBoolean().getValue();
303-
case OBJECT_ID:
304-
return value.asObjectId().getValue();
305-
case DB_POINTER:
306-
return new DBRef(value.asDBPointer().getNamespace(), value.asDBPointer().getId());
307-
case BINARY:
308-
return value.asBinary().getData();
309-
case DATE_TIME:
310-
return new Date(value.asDateTime().getValue());
311-
case SYMBOL:
312-
return value.asSymbol().getSymbol();
313-
case ARRAY:
314-
return value.asArray().toArray();
315-
case DOCUMENT:
316-
return Document.parse(value.asDocument().toJson());
317-
default:
318-
return value;
319-
}
290+
return switch (value.getBsonType()) {
291+
case INT32 -> value.asInt32().getValue();
292+
case INT64 -> value.asInt64().getValue();
293+
case STRING -> value.asString().getValue();
294+
case DECIMAL128 -> value.asDecimal128().doubleValue();
295+
case DOUBLE -> value.asDouble().getValue();
296+
case BOOLEAN -> value.asBoolean().getValue();
297+
case OBJECT_ID -> value.asObjectId().getValue();
298+
case DB_POINTER -> new DBRef(value.asDBPointer().getNamespace(), value.asDBPointer().getId());
299+
case BINARY -> value.asBinary().getData();
300+
case DATE_TIME -> new Date(value.asDateTime().getValue());
301+
case SYMBOL -> value.asSymbol().getSymbol();
302+
case ARRAY -> value.asArray().toArray();
303+
case DOCUMENT -> Document.parse(value.asDocument().toJson());
304+
default -> value;
305+
};
320306
}
321307

322308
/**
@@ -341,6 +327,7 @@ public static BsonValue simpleToBsonValue(Object source) {
341327
* @throws IllegalArgumentException if {@literal source} does not correspond to a {@link BsonValue} type.
342328
* @since 4.2
343329
*/
330+
@SuppressWarnings("unchecked")
344331
public static BsonValue simpleToBsonValue(Object source, CodecRegistry codecRegistry) {
345332

346333
if (source instanceof BsonValue bsonValue) {
@@ -407,7 +394,7 @@ public static BsonValue simpleToBsonValue(Object source, CodecRegistry codecRegi
407394

408395
/**
409396
* Merge the given {@link Document documents} into on in the given order. Keys contained within multiple documents are
410-
* overwritten by their follow ups.
397+
* overwritten by their follow-ups.
411398
*
412399
* @param documents must not be {@literal null}. Can be empty.
413400
* @return the document containing all key value pairs.
@@ -730,8 +717,9 @@ private static String serializeValue(@Nullable Object value) {
730717

731718
private static String toString(Map<?, ?> source) {
732719

720+
// Avoid String.format for performance
733721
return iterableToDelimitedString(source.entrySet(), "{ ", " }",
734-
entry -> String.format("\"%s\" : %s", entry.getKey(), toJson(entry.getValue())));
722+
entry -> "\"" + entry.getKey() + "\" : " + toJson(entry.getValue()));
735723
}
736724

737725
private static String toString(Collection<?> source) {
@@ -748,12 +736,13 @@ private static <T> String iterableToDelimitedString(Iterable<T> source, String p
748736
return joiner.toString();
749737
}
750738

751-
private static class BsonCapturingWriter extends AbstractBsonWriter {
739+
static class BsonCapturingWriter extends AbstractBsonWriter {
752740

753-
List<BsonValue> values = new ArrayList<>(0);
741+
private final List<BsonValue> values = new ArrayList<>(0);
754742

755743
public BsonCapturingWriter(Class<?> type) {
756744
super(new BsonWriterSettings());
745+
757746
if (ClassUtils.isAssignable(Map.class, type)) {
758747
setContext(new Context(null, BsonContextType.DOCUMENT));
759748
} else if (ClassUtils.isAssignable(List.class, type) || type.isArray()) {
@@ -763,6 +752,7 @@ public BsonCapturingWriter(Class<?> type) {
763752
}
764753
}
765754

755+
@Nullable
766756
BsonValue getCapturedValue() {
767757

768758
if (values.isEmpty()) {
@@ -852,18 +842,14 @@ protected void doWriteJavaScript(String value) {
852842

853843
@Override
854844
protected void doWriteJavaScriptWithScope(String value) {
855-
values.add(new BsonJavaScriptWithScope(value, null));
845+
throw new UnsupportedOperationException("Cannot capture JavaScriptWith");
856846
}
857847

858848
@Override
859-
protected void doWriteMaxKey() {
860-
861-
}
849+
protected void doWriteMaxKey() {}
862850

863851
@Override
864-
protected void doWriteMinKey() {
865-
866-
}
852+
protected void doWriteMinKey() {}
867853

868854
@Override
869855
protected void doWriteNull() {

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/encryption/AbstractEncryptionTestBase.java

+19-28
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.time.Month;
2626
import java.util.Arrays;
2727
import java.util.Collections;
28-
import java.util.HashMap;
2928
import java.util.List;
3029
import java.util.Map;
3130
import java.util.Objects;
@@ -34,14 +33,6 @@
3433
import java.util.function.Function;
3534
import java.util.function.Supplier;
3635

37-
import com.mongodb.ClientEncryptionSettings;
38-
import com.mongodb.ConnectionString;
39-
import com.mongodb.MongoClientSettings;
40-
import com.mongodb.client.MongoCollection;
41-
import com.mongodb.client.model.Filters;
42-
import com.mongodb.client.model.IndexOptions;
43-
import com.mongodb.client.model.Indexes;
44-
import com.mongodb.client.vault.ClientEncryptions;
4536
import org.assertj.core.api.Assertions;
4637
import org.bson.BsonBinary;
4738
import org.bson.Document;
@@ -61,13 +52,21 @@
6152
import org.springframework.data.mongodb.core.convert.encryption.MongoEncryptionConverter;
6253
import org.springframework.data.mongodb.core.mapping.ExplicitEncrypted;
6354
import org.springframework.data.mongodb.core.query.Update;
55+
import org.springframework.data.util.Lazy;
6456

57+
import com.mongodb.ClientEncryptionSettings;
58+
import com.mongodb.ConnectionString;
59+
import com.mongodb.MongoClientSettings;
6560
import com.mongodb.MongoNamespace;
6661
import com.mongodb.client.MongoClient;
6762
import com.mongodb.client.MongoClients;
63+
import com.mongodb.client.MongoCollection;
64+
import com.mongodb.client.model.Filters;
65+
import com.mongodb.client.model.IndexOptions;
66+
import com.mongodb.client.model.Indexes;
6867
import com.mongodb.client.model.vault.DataKeyOptions;
6968
import com.mongodb.client.vault.ClientEncryption;
70-
import org.springframework.data.util.Lazy;
69+
import com.mongodb.client.vault.ClientEncryptions;
7170

7271
/**
7372
* @author Christoph Strobl
@@ -87,7 +86,7 @@ void encryptAndDecryptSimpleValue() {
8786

8887
verifyThat(source) //
8988
.identifiedBy(Person::getId) //
90-
.wasSavedMatching(it -> assertThat(it.get("ssn")).isInstanceOf(Binary.class)) //
89+
.wasSavedMatching(it -> assertThat(it.get("ssn")).isInstanceOf(Binary.class)) //
9190
.loadedIsEqualToSource();
9291
}
9392

@@ -119,7 +118,7 @@ void encryptAndDecryptComplexValue() {
119118

120119
verifyThat(source) //
121120
.identifiedBy(Person::getId) //
122-
.wasSavedMatching(it -> assertThat(it.get("address")).isInstanceOf(Binary.class)) //
121+
.wasSavedMatching(it -> assertThat(it.get("address")).isInstanceOf(Binary.class)) //
123122
.loadedIsEqualToSource();
124123
}
125124

@@ -483,24 +482,17 @@ ClientEncryptionSettings encryptionSettings(MongoClient mongoClient) {
483482
MongoCollection<Document> collection = mongoClient.getDatabase(getDatabaseName()).getCollection("test");
484483
collection.drop(); // Clear old data
485484

486-
final byte[] localMasterKey = new byte[96];
485+
byte[] localMasterKey = new byte[96];
487486
new SecureRandom().nextBytes(localMasterKey);
488-
Map<String, Map<String, Object>> kmsProviders = new HashMap<>() {
489-
{
490-
put("local", new HashMap<>() {
491-
{
492-
put("key", localMasterKey);
493-
}
494-
});
495-
}
496-
};
487+
Map<String, Map<String, Object>> kmsProviders = Map.of("local", Map.of("key", localMasterKey));
497488

498489
// Create the ClientEncryption instance
499-
ClientEncryptionSettings clientEncryptionSettings = ClientEncryptionSettings.builder()
490+
return ClientEncryptionSettings.builder() //
500491
.keyVaultMongoClientSettings(
501-
MongoClientSettings.builder().applyConnectionString(new ConnectionString("mongodb://localhost")).build())
502-
.keyVaultNamespace(keyVaultNamespace.getFullName()).kmsProviders(kmsProviders).build();
503-
return clientEncryptionSettings;
492+
MongoClientSettings.builder().applyConnectionString(new ConnectionString("mongodb://localhost")).build()) //
493+
.keyVaultNamespace(keyVaultNamespace.getFullName()) //
494+
.kmsProviders(kmsProviders) //
495+
.build();
504496
}
505497
}
506498

@@ -693,8 +685,7 @@ public String toString() {
693685
+ ", wallet=" + this.getWallet() + ", address=" + this.getAddress() + ", encryptedZip="
694686
+ this.getEncryptedZip() + ", listOfString=" + this.getListOfString() + ", listOfComplex="
695687
+ this.getListOfComplex() + ", viaAltKeyNameField=" + this.getViaAltKeyNameField() + ", mapOfString="
696-
+ this.getMapOfString() + ", mapOfComplex=" + this.getMapOfComplex()
697-
+ ", today=" + this.getToday() + ")";
688+
+ this.getMapOfString() + ", mapOfComplex=" + this.getMapOfComplex() + ", today=" + this.getToday() + ")";
698689
}
699690
}
700691

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/encryption/BypassAutoEncryptionTest.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ protected void configureClientSettings(Builder builder) {
6868
ClientEncryptionSettings clientEncryptionSettings = encryptionSettings(mongoClient);
6969
mongoClient.close();
7070

71-
builder.autoEncryptionSettings(
72-
AutoEncryptionSettings.builder().kmsProviders(clientEncryptionSettings.getKmsProviders())
73-
.keyVaultNamespace(clientEncryptionSettings.getKeyVaultNamespace()).bypassAutoEncryption(true).build());
71+
builder.autoEncryptionSettings(AutoEncryptionSettings.builder() //
72+
.kmsProviders(clientEncryptionSettings.getKmsProviders()) //
73+
.keyVaultNamespace(clientEncryptionSettings.getKeyVaultNamespace()) //
74+
.bypassAutoEncryption(true).build());
7475
}
7576

7677
@Override
@@ -81,6 +82,7 @@ protected void configureConverters(MongoConverterConfigurationAdapter converterC
8182
}
8283

8384
@Bean
85+
@Override
8486
MongoEncryptionConverter encryptingConverter(MongoClientEncryption mongoClientEncryption) {
8587

8688
Lazy<BsonBinary> dataKey = Lazy.of(() -> mongoClientEncryption.getClientEncryption().createDataKey("local",
@@ -91,6 +93,7 @@ MongoEncryptionConverter encryptingConverter(MongoClientEncryption mongoClientEn
9193
}
9294

9395
@Bean
96+
@Override
9497
CachingMongoClientEncryption clientEncryption(ClientEncryptionSettings encryptionSettings) {
9598
return new CachingMongoClientEncryption(() -> ClientEncryptions.create(encryptionSettings));
9699
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/encryption/EncryptionTests.java

+8-15
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import java.security.SecureRandom;
1919
import java.util.Collections;
20-
import java.util.HashMap;
2120
import java.util.Map;
2221

2322
import org.bson.BsonBinary;
@@ -66,6 +65,7 @@ protected String getDatabaseName() {
6665
}
6766

6867
@Bean
68+
@Override
6969
public MongoClient mongoClient() {
7070
return super.mongoClient();
7171
}
@@ -106,24 +106,17 @@ ClientEncryptionSettings encryptionSettings(MongoClient mongoClient) {
106106
MongoCollection<Document> collection = mongoClient.getDatabase(getDatabaseName()).getCollection("test");
107107
collection.drop(); // Clear old data
108108

109-
final byte[] localMasterKey = new byte[96];
109+
byte[] localMasterKey = new byte[96];
110110
new SecureRandom().nextBytes(localMasterKey);
111-
Map<String, Map<String, Object>> kmsProviders = new HashMap<>() {
112-
{
113-
put("local", new HashMap<>() {
114-
{
115-
put("key", localMasterKey);
116-
}
117-
});
118-
}
119-
};
111+
Map<String, Map<String, Object>> kmsProviders = Map.of("local", Map.of("key", localMasterKey));
120112

121113
// Create the ClientEncryption instance
122-
ClientEncryptionSettings clientEncryptionSettings = ClientEncryptionSettings.builder()
114+
return ClientEncryptionSettings.builder()
123115
.keyVaultMongoClientSettings(
124-
MongoClientSettings.builder().applyConnectionString(new ConnectionString("mongodb://localhost")).build())
125-
.keyVaultNamespace(keyVaultNamespace.getFullName()).kmsProviders(kmsProviders).build();
126-
return clientEncryptionSettings;
116+
MongoClientSettings.builder().applyConnectionString(new ConnectionString("mongodb://localhost")).build()) //
117+
.keyVaultNamespace(keyVaultNamespace.getFullName()) //
118+
.kmsProviders(kmsProviders) //
119+
.build();
127120
}
128121
}
129122
}

0 commit comments

Comments
 (0)