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
.InvalidKeyIdException
;
8 import org
.whispersystems
.libaxolotl
.state
.SignedPreKeyRecord
;
9 import org
.whispersystems
.libaxolotl
.state
.SignedPreKeyStore
;
11 import java
.io
.IOException
;
12 import java
.util
.HashMap
;
13 import java
.util
.LinkedList
;
14 import java
.util
.List
;
17 class JsonSignedPreKeyStore
implements SignedPreKeyStore
{
19 private final Map
<Integer
, byte[]> store
= new HashMap
<>();
21 public JsonSignedPreKeyStore() {
26 public void addSignedPreKeys(Map
<Integer
, byte[]> preKeys
) {
27 store
.putAll(preKeys
);
31 public SignedPreKeyRecord
loadSignedPreKey(int signedPreKeyId
) throws InvalidKeyIdException
{
33 if (!store
.containsKey(signedPreKeyId
)) {
34 throw new InvalidKeyIdException("No such signedprekeyrecord! " + signedPreKeyId
);
37 return new SignedPreKeyRecord(store
.get(signedPreKeyId
));
38 } catch (IOException e
) {
39 throw new AssertionError(e
);
44 public List
<SignedPreKeyRecord
> loadSignedPreKeys() {
46 List
<SignedPreKeyRecord
> results
= new LinkedList
<>();
48 for (byte[] serialized
: store
.values()) {
49 results
.add(new SignedPreKeyRecord(serialized
));
53 } catch (IOException e
) {
54 throw new AssertionError(e
);
59 public void storeSignedPreKey(int signedPreKeyId
, SignedPreKeyRecord
record) {
60 store
.put(signedPreKeyId
, record.serialize());
64 public boolean containsSignedPreKey(int signedPreKeyId
) {
65 return store
.containsKey(signedPreKeyId
);
69 public void removeSignedPreKey(int signedPreKeyId
) {
70 store
.remove(signedPreKeyId
);
73 public static class JsonSignedPreKeyStoreDeserializer
extends JsonDeserializer
<JsonSignedPreKeyStore
> {
76 public JsonSignedPreKeyStore
deserialize(JsonParser jsonParser
, DeserializationContext deserializationContext
) throws IOException
, JsonProcessingException
{
77 JsonNode node
= jsonParser
.getCodec().readTree(jsonParser
);
80 Map
<Integer
, byte[]> preKeyMap
= new HashMap
<>();
82 for (JsonNode preKey
: node
) {
83 Integer preKeyId
= preKey
.get("id").asInt();
85 preKeyMap
.put(preKeyId
, Base64
.decode(preKey
.get("record").asText()));
86 } catch (IOException e
) {
87 System
.out
.println(String
.format("Error while decoding prekey for: %s", preKeyId
));
92 JsonSignedPreKeyStore keyStore
= new JsonSignedPreKeyStore();
93 keyStore
.addSignedPreKeys(preKeyMap
);
100 public static class JsonSignedPreKeyStoreSerializer
extends JsonSerializer
<JsonSignedPreKeyStore
> {
103 public void serialize(JsonSignedPreKeyStore jsonPreKeyStore
, JsonGenerator json
, SerializerProvider serializerProvider
) throws IOException
, JsonProcessingException
{
104 json
.writeStartArray();
105 for (Map
.Entry
<Integer
, byte[]> signedPreKey
: jsonPreKeyStore
.store
.entrySet()) {
106 json
.writeStartObject();
107 json
.writeNumberField("id", signedPreKey
.getKey());
108 json
.writeStringField("record", Base64
.encodeBytes(signedPreKey
.getValue()));
109 json
.writeEndObject();
111 json
.writeEndArray();