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,8 @@ 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
+ builder .jsonSerializer (JacksonJsonSerializer .create (couchbaseObjectMapper (cryptoManager ())));
156
+ builder .cryptoManager ();
153
157
configureEnvironment (builder );
154
158
return builder .build ();
155
159
}
@@ -280,8 +284,8 @@ public MappingCouchbaseConverter mappingCouchbaseConverter(CouchbaseMappingConte
280
284
@ Bean
281
285
public TranslationService couchbaseTranslationService () {
282
286
final JacksonTranslationService jacksonTranslationService = new JacksonTranslationService ();
287
+ jacksonTranslationService .setObjectMapper (couchbaseObjectMapper (cryptoManager ()));
283
288
jacksonTranslationService .afterPropertiesSet ();
284
-
285
289
// for sdk3, we need to ask the mapper _it_ uses to ignore extra fields...
286
290
JacksonTransformers .MAPPER .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
287
291
return jacksonTranslationService ;
@@ -308,10 +312,25 @@ public CouchbaseMappingContext couchbaseMappingContext(CustomConversions customC
308
312
*/
309
313
310
314
public ObjectMapper couchbaseObjectMapper () {
311
- ObjectMapper mapper = new ObjectMapper ();
315
+ return couchbaseObjectMapper (cryptoManager ());
316
+ }
317
+
318
+ /**
319
+ * Creates a {@link ObjectMapper} for the jsonSerializer of the ClusterEnvironment
320
+ *
321
+ * @param cryptoManager
322
+ * @return ObjectMapper
323
+ */
324
+
325
+ ObjectMapper mapper ;
326
+
327
+ public ObjectMapper couchbaseObjectMapper (CryptoManager cryptoManager ) {
328
+ if (mapper != null ) {
329
+ return mapper ;
330
+ }
331
+ mapper = new ObjectMapper (); // or use the one from the Java SDK (?) JacksonTransformers.MAPPER
312
332
mapper .configure (com .fasterxml .jackson .databind .DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
313
333
mapper .registerModule (new JsonValueModule ());
314
- CryptoManager cryptoManager = null ;
315
334
if (cryptoManager != null ) {
316
335
mapper .registerModule (new EncryptionModule (cryptoManager ));
317
336
}
@@ -320,7 +339,7 @@ public ObjectMapper couchbaseObjectMapper() {
320
339
321
340
/**
322
341
* The default blocking transaction manager. It is an implementation of CallbackPreferringTransactionManager
323
- * CallbackPreferrringTransactionmanagers do not play well with test-cases that rely
342
+ * CallbackPreferringTransactionManagers do not play well with test-cases that rely
324
343
* on @TestTransaction/@BeforeTransaction/@AfterTransaction
325
344
*
326
345
* @param clientFactory
@@ -341,6 +360,7 @@ CouchbaseCallbackTransactionManager couchbaseTransactionManager(CouchbaseClientF
341
360
TransactionTemplate couchbaseTransactionTemplate (CouchbaseCallbackTransactionManager couchbaseTransactionManager ) {
342
361
return new TransactionTemplate (couchbaseTransactionManager );
343
362
}
363
+
344
364
/**
345
365
* The default TransactionalOperator.
346
366
*
@@ -379,11 +399,28 @@ protected boolean autoIndexCreation() {
379
399
* and {@link #couchbaseMappingContext(CustomConversions)}. Returns an empty {@link CustomConversions} instance by
380
400
* default.
381
401
*
402
+ * @param cryptoManagerOptional optional cryptoManager. Make varargs for backwards compatibility.
382
403
* @return must not be {@literal null}.
383
404
*/
384
405
@ Bean (name = BeanNames .COUCHBASE_CUSTOM_CONVERSIONS )
385
- public CustomConversions customConversions () {
386
- return new CouchbaseCustomConversions (Collections .emptyList ());
406
+ public CustomConversions customConversions (CryptoManager ... cryptoManagerOptional ) {
407
+ assert (cryptoManagerOptional == null || cryptoManagerOptional .length <= 1 );
408
+ CryptoManager cryptoManager = cryptoManagerOptional != null && cryptoManagerOptional .length == 1
409
+ ? cryptoManagerOptional [0 ]
410
+ : null ;
411
+ List <GenericConverter > newConverters = new ArrayList ();
412
+ // the cryptoConverters take an argument, so they cannot be created in the
413
+ // static block of CouchbaseCustomConversions. And they must be set before the super() constructor
414
+ // in CouchbaseCustomerConversions
415
+ if (cryptoManager != null ) {
416
+ newConverters .addAll (OtherConverters .getCryptoConverters (cryptoManager ));
417
+ }
418
+ return new CouchbaseCustomConversions (newConverters );
419
+ }
420
+
421
+ @ Bean
422
+ protected CryptoManager cryptoManager () {
423
+ return null ;
387
424
}
388
425
389
426
/**
0 commit comments