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 ;
34
+ import org .springframework .data .convert .PropertyValueConverterRegistrar ;
35
+ import org .springframework .data .convert .SimplePropertyValueConversions ;
32
36
import org .springframework .data .couchbase .CouchbaseClientFactory ;
33
37
import org .springframework .data .couchbase .SimpleCouchbaseClientFactory ;
34
38
import org .springframework .data .couchbase .core .CouchbaseTemplate ;
35
39
import org .springframework .data .couchbase .core .ReactiveCouchbaseTemplate ;
36
40
import org .springframework .data .couchbase .core .convert .CouchbaseCustomConversions ;
41
+ import org .springframework .data .couchbase .core .convert .CouchbasePropertyValueConverterFactory ;
42
+ import org .springframework .data .couchbase .core .convert .CryptoConverter ;
37
43
import org .springframework .data .couchbase .core .convert .MappingCouchbaseConverter ;
38
44
import org .springframework .data .couchbase .core .convert .translation .JacksonTranslationService ;
39
45
import org .springframework .data .couchbase .core .convert .translation .TranslationService ;
@@ -149,7 +155,9 @@ public ClusterEnvironment couchbaseClusterEnvironment() {
149
155
if (!nonShadowedJacksonPresent ()) {
150
156
throw new CouchbaseException ("non-shadowed Jackson not present" );
151
157
}
152
- builder .jsonSerializer (JacksonJsonSerializer .create (couchbaseObjectMapper ()));
158
+ CryptoManager cryptoManager = cryptoManager ();
159
+ builder .jsonSerializer (JacksonJsonSerializer .create (couchbaseObjectMapper (cryptoManager )));
160
+ builder .cryptoManager (cryptoManager );
153
161
configureEnvironment (builder );
154
162
return builder .build ();
155
163
}
@@ -160,7 +168,6 @@ public ClusterEnvironment couchbaseClusterEnvironment() {
160
168
* @param builder the builder that can be customized.
161
169
*/
162
170
protected void configureEnvironment (final ClusterEnvironment .Builder builder ) {
163
-
164
171
}
165
172
166
173
@ Bean (name = BeanNames .COUCHBASE_TEMPLATE )
@@ -269,6 +276,7 @@ public MappingCouchbaseConverter mappingCouchbaseConverter(CouchbaseMappingConte
269
276
CouchbaseCustomConversions couchbaseCustomConversions ) {
270
277
MappingCouchbaseConverter converter = new MappingCouchbaseConverter (couchbaseMappingContext , typeKey ());
271
278
converter .setCustomConversions (couchbaseCustomConversions );
279
+ couchbaseMappingContext .setSimpleTypeHolder (couchbaseCustomConversions .getSimpleTypeHolder ());
272
280
return converter ;
273
281
}
274
282
@@ -280,8 +288,8 @@ public MappingCouchbaseConverter mappingCouchbaseConverter(CouchbaseMappingConte
280
288
@ Bean
281
289
public TranslationService couchbaseTranslationService () {
282
290
final JacksonTranslationService jacksonTranslationService = new JacksonTranslationService ();
291
+ jacksonTranslationService .setObjectMapper (couchbaseObjectMapper (cryptoManager ()));
283
292
jacksonTranslationService .afterPropertiesSet ();
284
-
285
293
// for sdk3, we need to ask the mapper _it_ uses to ignore extra fields...
286
294
JacksonTransformers .MAPPER .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
287
295
return jacksonTranslationService ;
@@ -306,12 +314,26 @@ public CouchbaseMappingContext couchbaseMappingContext(CustomConversions customC
306
314
*
307
315
* @return ObjectMapper
308
316
*/
317
+ private ObjectMapper couchbaseObjectMapper () {
318
+ return couchbaseObjectMapper (cryptoManager ());
319
+ }
320
+
321
+ /**
322
+ * Creates a {@link ObjectMapper} for the jsonSerializer of the ClusterEnvironment
323
+ *
324
+ * @param cryptoManager
325
+ * @return ObjectMapper
326
+ */
309
327
310
- public ObjectMapper couchbaseObjectMapper () {
311
- ObjectMapper mapper = new ObjectMapper ();
328
+ ObjectMapper mapper ;
329
+
330
+ public ObjectMapper couchbaseObjectMapper (CryptoManager cryptoManager ) {
331
+ if (mapper != null ) {
332
+ return mapper ;
333
+ }
334
+ mapper = new ObjectMapper (); // or use the one from the Java SDK (?) JacksonTransformers.MAPPER
312
335
mapper .configure (com .fasterxml .jackson .databind .DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
313
336
mapper .registerModule (new JsonValueModule ());
314
- CryptoManager cryptoManager = null ;
315
337
if (cryptoManager != null ) {
316
338
mapper .registerModule (new EncryptionModule (cryptoManager ));
317
339
}
@@ -320,7 +342,7 @@ public ObjectMapper couchbaseObjectMapper() {
320
342
321
343
/**
322
344
* The default blocking transaction manager. It is an implementation of CallbackPreferringTransactionManager
323
- * CallbackPreferrringTransactionmanagers do not play well with test-cases that rely
345
+ * CallbackPreferringTransactionManagers do not play well with test-cases that rely
324
346
* on @TestTransaction/@BeforeTransaction/@AfterTransaction
325
347
*
326
348
* @param clientFactory
@@ -341,6 +363,7 @@ CouchbaseCallbackTransactionManager couchbaseTransactionManager(CouchbaseClientF
341
363
TransactionTemplate couchbaseTransactionTemplate (CouchbaseCallbackTransactionManager couchbaseTransactionManager ) {
342
364
return new TransactionTemplate (couchbaseTransactionManager );
343
365
}
366
+
344
367
/**
345
368
* The default TransactionalOperator.
346
369
*
@@ -376,14 +399,43 @@ protected boolean autoIndexCreation() {
376
399
/**
377
400
* Register custom Converters in a {@link CustomConversions} object if required. These {@link CustomConversions} will
378
401
* be registered with the {@link #mappingCouchbaseConverter(CouchbaseMappingContext, CouchbaseCustomConversions)} )}
379
- * and {@link #couchbaseMappingContext(CustomConversions)}. Returns an empty {@link CustomConversions} instance by
380
- * default.
402
+ * and {@link #couchbaseMappingContext(CustomConversions)}.
381
403
*
382
404
* @return must not be {@literal null}.
383
405
*/
384
406
@ Bean (name = BeanNames .COUCHBASE_CUSTOM_CONVERSIONS )
385
407
public CustomConversions customConversions () {
386
- return new CouchbaseCustomConversions (Collections .emptyList ());
408
+ return customConversions (cryptoManager ());
409
+ }
410
+
411
+ /**
412
+ * Register custom Converters in a {@link CustomConversions} object if required. These {@link CustomConversions} will
413
+ * be registered with the {@link #mappingCouchbaseConverter(CouchbaseMappingContext, CouchbaseCustomConversions)} )}
414
+ * and {@link #couchbaseMappingContext(CustomConversions)}.
415
+ *
416
+ * @param cryptoManager
417
+ * @return must not be {@literal null}.
418
+ */
419
+ public CustomConversions customConversions (CryptoManager cryptoManager ) {
420
+ List <GenericConverter > newConverters = new ArrayList ();
421
+ CustomConversions customConversions = CouchbaseCustomConversions .create (configurationAdapter -> {
422
+ SimplePropertyValueConversions valueConversions = new SimplePropertyValueConversions ();
423
+ valueConversions .setConverterFactory (new CouchbasePropertyValueConverterFactory (cryptoManager ));
424
+ valueConversions .setValueConverterRegistry (new PropertyValueConverterRegistrar ().buildRegistry ());
425
+ configurationAdapter .setPropertyValueConversions (valueConversions );
426
+ configurationAdapter .registerConverters (newConverters );
427
+ });
428
+ return customConversions ;
429
+ }
430
+
431
+ @ Bean
432
+ protected CryptoManager cryptoManager () {
433
+ return null ;
434
+ }
435
+
436
+ @ Bean
437
+ protected CryptoConverter cryptoConverter (CryptoManager cryptoManager ) {
438
+ return cryptoManager == null ? null : new CryptoConverter (cryptoManager );
387
439
}
388
440
389
441
/**
0 commit comments