use of org.eclipse.californium.elements.MapBasedEndpointContext in project leshan by eclipse.
the class EndpointContextSerDes method deserialize.
public static EndpointContext deserialize(JsonObject peer) {
String address = peer.get(KEY_ADDRESS).asString();
int port = peer.get(KEY_PORT).asInt();
InetSocketAddress socketAddress = new InetSocketAddress(address, port);
Principal principal = null;
JsonValue value = peer.get(KEY_ID);
if (value != null) {
principal = new PreSharedKeyIdentity(value.asString());
} else if ((value = peer.get(KEY_RPK)) != null) {
try {
byte[] rpk = Hex.decodeHex(value.asString().toCharArray());
X509EncodedKeySpec spec = new X509EncodedKeySpec(rpk);
PublicKey publicKey = KeyFactory.getInstance("EC").generatePublic(spec);
principal = new RawPublicKeyIdentity(publicKey);
} catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
throw new IllegalStateException("Invalid security info content", e);
}
} else if ((value = peer.get(KEY_DN)) != null) {
principal = new X500Principal(value.asString());
}
EndpointContext endpointContext;
value = peer.get(KEY_ATTRIBUTES);
if (value == null) {
endpointContext = new AddressEndpointContext(socketAddress, principal);
} else {
int index = 0;
String[] attributes = new String[value.asObject().size() * 2];
for (Member member : value.asObject()) {
attributes[index++] = member.getName();
attributes[index++] = member.getValue().asString();
}
endpointContext = new MapBasedEndpointContext(socketAddress, principal, attributes);
}
return endpointContext;
}
Aggregations