3 import org
.json
.JSONArray
;
4 import org
.json
.JSONObject
;
5 import org
.whispersystems
.libaxolotl
.AxolotlAddress
;
6 import org
.whispersystems
.libaxolotl
.state
.SessionRecord
;
7 import org
.whispersystems
.libaxolotl
.state
.SessionStore
;
9 import java
.io
.IOException
;
12 class JsonSessionStore
implements SessionStore
{
14 private final Map
<AxolotlAddress
, byte[]> sessions
= new HashMap
<>();
16 public JsonSessionStore() {
20 public JsonSessionStore(JSONArray list
) {
21 for (int i
= 0; i
< list
.length(); i
++) {
22 JSONObject k
= list
.getJSONObject(i
);
24 sessions
.put(new AxolotlAddress(k
.getString("name"), k
.getInt("deviceId")), Base64
.decode(k
.getString("record")));
25 } catch (IOException e
) {
26 System
.out
.println("Error while decoding prekey for: " + k
.getString("name"));
31 public JSONArray
getJson() {
32 JSONArray result
= new JSONArray();
33 for (AxolotlAddress address
: sessions
.keySet()) {
34 result
.put(new JSONObject().put("name", address
.getName()).
35 put("deviceId", address
.getDeviceId()).
36 put("record", Base64
.encodeBytes(sessions
.get(address
))));
42 public synchronized SessionRecord
loadSession(AxolotlAddress remoteAddress
) {
44 if (containsSession(remoteAddress
)) {
45 return new SessionRecord(sessions
.get(remoteAddress
));
47 return new SessionRecord();
49 } catch (IOException e
) {
50 throw new AssertionError(e
);
55 public synchronized List
<Integer
> getSubDeviceSessions(String name
) {
56 List
<Integer
> deviceIds
= new LinkedList
<>();
58 for (AxolotlAddress key
: sessions
.keySet()) {
59 if (key
.getName().equals(name
) &&
60 key
.getDeviceId() != 1) {
61 deviceIds
.add(key
.getDeviceId());
69 public synchronized void storeSession(AxolotlAddress address
, SessionRecord
record) {
70 sessions
.put(address
, record.serialize());
74 public synchronized boolean containsSession(AxolotlAddress address
) {
75 return sessions
.containsKey(address
);
79 public synchronized void deleteSession(AxolotlAddress address
) {
80 sessions
.remove(address
);
84 public synchronized void deleteAllSessions(String name
) {
85 for (AxolotlAddress key
: new ArrayList
<>(sessions
.keySet())) {
86 if (key
.getName().equals(name
)) {