-
Notifications
You must be signed in to change notification settings - Fork 214
/
ConnectivityMongoEventAdapter.java
101 lines (91 loc) · 5.52 KB
/
ConnectivityMongoEventAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.connectivity.service.messaging.persistence;
import java.util.HashMap;
import java.util.Map;
import org.apache.pekko.actor.ExtendedActorSystem;
import org.eclipse.ditto.base.model.signals.JsonParsable;
import org.eclipse.ditto.base.model.signals.events.Event;
import org.eclipse.ditto.base.model.signals.events.EventJsonDeserializer;
import org.eclipse.ditto.base.model.signals.events.EventRegistry;
import org.eclipse.ditto.base.model.signals.events.GlobalEventRegistry;
import org.eclipse.ditto.base.service.config.DittoServiceConfig;
import org.eclipse.ditto.connectivity.model.Connection;
import org.eclipse.ditto.connectivity.model.ConnectivityConstants;
import org.eclipse.ditto.connectivity.model.signals.events.ConnectionCreated;
import org.eclipse.ditto.connectivity.model.signals.events.ConnectionModified;
import org.eclipse.ditto.connectivity.model.signals.events.ConnectivityEvent;
import org.eclipse.ditto.connectivity.service.config.DefaultConnectionConfig;
import org.eclipse.ditto.internal.utils.config.DefaultScopedConfig;
import org.eclipse.ditto.internal.utils.persistence.mongo.AbstractMongoEventAdapter;
import org.eclipse.ditto.connectivity.service.config.DittoConnectivityConfig;
import org.eclipse.ditto.connectivity.service.config.FieldsEncryptionConfig;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonObjectBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* EventAdapter for {@link ConnectivityEvent}s persisted into
* pekko-persistence event-journal. Converts Events to MongoDB BSON objects and vice versa.
*/
public final class ConnectivityMongoEventAdapter extends AbstractMongoEventAdapter<ConnectivityEvent<?>> {
private final FieldsEncryptionConfig encryptionConfig;
private final Logger logger;
public ConnectivityMongoEventAdapter(final ExtendedActorSystem system) {
super(system, createEventRegistry(), DefaultConnectionConfig.of(
DittoServiceConfig.of(DefaultScopedConfig.dittoScoped(system.settings().config()), "connectivity"))
.getEventConfig());
logger = LoggerFactory.getLogger(ConnectivityMongoEventAdapter.class);
final DittoConnectivityConfig connectivityConfig = DittoConnectivityConfig.of(
DefaultScopedConfig.dittoScoped(system.settings().config()));
encryptionConfig = connectivityConfig.getConnectionConfig().getFieldsEncryptionConfig();
logger.info("Connections fields encryption: {}", encryptionConfig.isEncryptionEnabled());
if (encryptionConfig.isEncryptionEnabled()) {
logger.debug("Connections fields that will be encryption: {}", encryptionConfig.getJsonPointers());
}
}
@Override
protected JsonObjectBuilder performToJournalMigration(final Event<?> event, final JsonObject jsonObject) {
if (encryptionConfig.isEncryptionEnabled()) {
final JsonObject superObject = super.performToJournalMigration(event, jsonObject).build();
return JsonFieldsEncryptor.encrypt(superObject, ConnectivityConstants.ENTITY_TYPE.toString(),
encryptionConfig.getJsonPointers(), encryptionConfig.getSymmetricalKey())
.toBuilder();
}
return super.performToJournalMigration(event, jsonObject);
}
@Override
protected JsonObject performFromJournalMigration(final JsonObject jsonObject) {
return JsonFieldsEncryptor.decrypt(jsonObject, ConnectivityConstants.ENTITY_TYPE.toString(),
encryptionConfig.getJsonPointers(), encryptionConfig.getSymmetricalKey());
}
private static EventRegistry<ConnectivityEvent<?>> createEventRegistry() {
final Map<String, JsonParsable<ConnectivityEvent<?>>> parseStrategies = new HashMap<>();
parseStrategies.put(ConnectionCreated.TYPE, (jsonObject, dittoHeaders) ->
new EventJsonDeserializer<ConnectionCreated>(ConnectionCreated.TYPE, jsonObject)
.deserialize((revision, timestamp, metadata) -> {
final Connection connection = ConnectionMigrationUtil.connectionFromJsonWithMigration(
jsonObject.getValueOrThrow(ConnectivityEvent.JsonFields.CONNECTION));
return ConnectionCreated.of(connection, revision, timestamp, dittoHeaders, metadata);
}));
parseStrategies.put(ConnectionModified.TYPE, (jsonObject, dittoHeaders) ->
new EventJsonDeserializer<ConnectionModified>(ConnectionModified.TYPE, jsonObject)
.deserialize((revision, timestamp, metadata) -> {
final Connection connection = ConnectionMigrationUtil.connectionFromJsonWithMigration(
jsonObject.getValueOrThrow(ConnectivityEvent.JsonFields.CONNECTION));
return ConnectionModified.of(connection, revision, timestamp, dittoHeaders, metadata);
}));
final GlobalEventRegistry<ConnectivityEvent<?>> globalEventRegistry = GlobalEventRegistry.getInstance();
return globalEventRegistry.customize(parseStrategies);
}
}