18
18
19
19
import static com .couchbase .client .java .ClusterOptions .clusterOptions ;
20
20
21
- import java .util .Collections ;
21
+ import java .util .ArrayList ;
22
22
import java .util .HashSet ;
23
+ import java .util .List ;
23
24
import java .util .Set ;
24
25
25
26
import org .springframework .beans .factory .config .BeanDefinition ;
26
27
import org .springframework .context .annotation .Bean ;
27
28
import org .springframework .context .annotation .ClassPathScanningCandidateComponentProvider ;
28
29
import org .springframework .context .annotation .Configuration ;
29
30
import org .springframework .context .annotation .Role ;
31
+ import org .springframework .core .convert .converter .GenericConverter ;
30
32
import org .springframework .core .type .filter .AnnotationTypeFilter ;
31
33
import org .springframework .data .convert .CustomConversions ;
32
34
import org .springframework .data .couchbase .CouchbaseClientFactory ;
35
37
import org .springframework .data .couchbase .core .ReactiveCouchbaseTemplate ;
36
38
import org .springframework .data .couchbase .core .convert .CouchbaseCustomConversions ;
37
39
import org .springframework .data .couchbase .core .convert .MappingCouchbaseConverter ;
40
+ import org .springframework .data .couchbase .core .convert .OtherConverters ;
38
41
import org .springframework .data .couchbase .core .convert .translation .JacksonTranslationService ;
39
42
import org .springframework .data .couchbase .core .convert .translation .TranslationService ;
40
43
import org .springframework .data .couchbase .core .mapping .CouchbaseMappingContext ;
@@ -149,7 +152,9 @@ public ClusterEnvironment couchbaseClusterEnvironment() {
149
152
if (!nonShadowedJacksonPresent ()) {
150
153
throw new CouchbaseException ("non-shadowed Jackson not present" );
151
154
}
152
- builder .jsonSerializer (JacksonJsonSerializer .create (couchbaseObjectMapper ()));
155
+ CryptoManager cryptoManager = cryptoManager ();
156
+ builder .jsonSerializer (JacksonJsonSerializer .create (couchbaseObjectMapper (cryptoManager )));
157
+ builder .cryptoManager (cryptoManager );
153
158
configureEnvironment (builder );
154
159
return builder .build ();
155
160
}
@@ -280,8 +285,8 @@ public MappingCouchbaseConverter mappingCouchbaseConverter(CouchbaseMappingConte
280
285
@ Bean
281
286
public TranslationService couchbaseTranslationService () {
282
287
final JacksonTranslationService jacksonTranslationService = new JacksonTranslationService ();
288
+ jacksonTranslationService .setObjectMapper (couchbaseObjectMapper (cryptoManager ()));
283
289
jacksonTranslationService .afterPropertiesSet ();
284
-
285
290
// for sdk3, we need to ask the mapper _it_ uses to ignore extra fields...
286
291
JacksonTransformers .MAPPER .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
287
292
return jacksonTranslationService ;
@@ -308,10 +313,25 @@ public CouchbaseMappingContext couchbaseMappingContext(CustomConversions customC
308
313
*/
309
314
310
315
public ObjectMapper couchbaseObjectMapper () {
311
- ObjectMapper mapper = new ObjectMapper ();
316
+ return couchbaseObjectMapper (cryptoManager ());
317
+ }
318
+
319
+ /**
320
+ * Creates a {@link ObjectMapper} for the jsonSerializer of the ClusterEnvironment
321
+ *
322
+ * @param cryptoManager
323
+ * @return ObjectMapper
324
+ */
325
+
326
+ ObjectMapper mapper ;
327
+
328
+ public ObjectMapper couchbaseObjectMapper (CryptoManager cryptoManager ) {
329
+ if (mapper != null ) {
330
+ return mapper ;
331
+ }
332
+ mapper = new ObjectMapper (); // or use the one from the Java SDK (?) JacksonTransformers.MAPPER
312
333
mapper .configure (com .fasterxml .jackson .databind .DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
313
334
mapper .registerModule (new JsonValueModule ());
314
- CryptoManager cryptoManager = null ;
315
335
if (cryptoManager != null ) {
316
336
mapper .registerModule (new EncryptionModule (cryptoManager ));
317
337
}
@@ -320,7 +340,7 @@ public ObjectMapper couchbaseObjectMapper() {
320
340
321
341
/**
322
342
* The default blocking transaction manager. It is an implementation of CallbackPreferringTransactionManager
323
- * CallbackPreferrringTransactionmanagers do not play well with test-cases that rely
343
+ * CallbackPreferringTransactionManagers do not play well with test-cases that rely
324
344
* on @TestTransaction/@BeforeTransaction/@AfterTransaction
325
345
*
326
346
* @param clientFactory
@@ -341,6 +361,7 @@ CouchbaseCallbackTransactionManager couchbaseTransactionManager(CouchbaseClientF
341
361
TransactionTemplate couchbaseTransactionTemplate (CouchbaseCallbackTransactionManager couchbaseTransactionManager ) {
342
362
return new TransactionTemplate (couchbaseTransactionManager );
343
363
}
364
+
344
365
/**
345
366
* The default TransactionalOperator.
346
367
*
@@ -379,11 +400,28 @@ protected boolean autoIndexCreation() {
379
400
* and {@link #couchbaseMappingContext(CustomConversions)}. Returns an empty {@link CustomConversions} instance by
380
401
* default.
381
402
*
403
+ * @param cryptoManagerOptional optional cryptoManager. Make varargs for backwards compatibility.
382
404
* @return must not be {@literal null}.
383
405
*/
384
406
@ Bean (name = BeanNames .COUCHBASE_CUSTOM_CONVERSIONS )
385
- public CustomConversions customConversions () {
386
- return new CouchbaseCustomConversions (Collections .emptyList ());
407
+ public CustomConversions customConversions (CryptoManager ... cryptoManagerOptional ) {
408
+ assert (cryptoManagerOptional == null || cryptoManagerOptional .length <= 1 );
409
+ CryptoManager cryptoManager = cryptoManagerOptional != null && cryptoManagerOptional .length == 1
410
+ ? cryptoManagerOptional [0 ]
411
+ : null ;
412
+ List <GenericConverter > newConverters = new ArrayList ();
413
+ // the cryptoConverters take an argument, so they cannot be created in the
414
+ // static block of CouchbaseCustomConversions. And they must be set before the super() constructor
415
+ // in CouchbaseCustomerConversions
416
+ if (cryptoManager != null ) {
417
+ newConverters .addAll (OtherConverters .getCryptoConverters (cryptoManager ));
418
+ }
419
+ return new CouchbaseCustomConversions (newConverters );
420
+ }
421
+
422
+ @ Bean
423
+ protected CryptoManager cryptoManager () {
424
+ return null ;
387
425
}
388
426
389
427
/**
0 commit comments