3 import com
.fasterxml
.jackson
.core
.JsonGenerator
;
4 import com
.fasterxml
.jackson
.core
.JsonParser
;
5 import com
.fasterxml
.jackson
.core
.JsonProcessingException
;
6 import com
.fasterxml
.jackson
.databind
.*;
7 import org
.whispersystems
.libaxolotl
.AxolotlAddress
;
8 import org
.whispersystems
.libaxolotl
.state
.SessionRecord
;
9 import org
.whispersystems
.libaxolotl
.state
.SessionStore
;
11 import java
.io
.IOException
;
14 class JsonSessionStore
implements SessionStore
{
16 private final Map
<AxolotlAddress
, byte[]> sessions
= new HashMap
<>();
18 public JsonSessionStore() {
22 public void addSessions(Map
<AxolotlAddress
, byte[]> sessions
) {
23 this.sessions
.putAll(sessions
);
28 public synchronized SessionRecord
loadSession(AxolotlAddress remoteAddress
) {
30 if (containsSession(remoteAddress
)) {
31 return new SessionRecord(sessions
.get(remoteAddress
));
33 return new SessionRecord();
35 } catch (IOException e
) {
36 throw new AssertionError(e
);
41 public synchronized List
<Integer
> getSubDeviceSessions(String name
) {
42 List
<Integer
> deviceIds
= new LinkedList
<>();
44 for (AxolotlAddress key
: sessions
.keySet()) {
45 if (key
.getName().equals(name
) &&
46 key
.getDeviceId() != 1) {
47 deviceIds
.add(key
.getDeviceId());
55 public synchronized void storeSession(AxolotlAddress address
, SessionRecord
record) {
56 sessions
.put(address
, record.serialize());
60 public synchronized boolean containsSession(AxolotlAddress address
) {
61 return sessions
.containsKey(address
);
65 public synchronized void deleteSession(AxolotlAddress address
) {
66 sessions
.remove(address
);
70 public synchronized void deleteAllSessions(String name
) {
71 for (AxolotlAddress key
: new ArrayList
<>(sessions
.keySet())) {
72 if (key
.getName().equals(name
)) {
78 public static class JsonSessionStoreDeserializer
extends JsonDeserializer
<JsonSessionStore
> {
81 public JsonSessionStore
deserialize(JsonParser jsonParser
, DeserializationContext deserializationContext
) throws IOException
, JsonProcessingException
{
82 JsonNode node
= jsonParser
.getCodec().readTree(jsonParser
);
84 Map
<AxolotlAddress
, byte[]> sessionMap
= new HashMap
<>();
86 for (JsonNode session
: node
) {
87 String sessionName
= session
.get("name").asText();
89 sessionMap
.put(new AxolotlAddress(sessionName
, session
.get("deviceId").asInt()), Base64
.decode(session
.get("record").asText()));
90 } catch (IOException e
) {
91 System
.out
.println(String
.format("Error while decoding session for: %s", sessionName
));
96 JsonSessionStore sessionStore
= new JsonSessionStore();
97 sessionStore
.addSessions(sessionMap
);
104 public static class JsonPreKeyStoreSerializer
extends JsonSerializer
<JsonSessionStore
> {
107 public void serialize(JsonSessionStore jsonSessionStore
, JsonGenerator json
, SerializerProvider serializerProvider
) throws IOException
, JsonProcessingException
{
108 json
.writeStartArray();
109 for (Map
.Entry
<AxolotlAddress
, byte[]> preKey
: jsonSessionStore
.sessions
.entrySet()) {
110 json
.writeStartObject();
111 json
.writeStringField("name", preKey
.getKey().getName());
112 json
.writeNumberField("deviceId", preKey
.getKey().getDeviceId());
113 json
.writeStringField("record", Base64
.encodeBytes(preKey
.getValue()));
114 json
.writeEndObject();
116 json
.writeEndArray();