IOException, InvalidKeyException,
InvalidAlgorithmParameterException, NoSuchAlgorithmException,
InvalidKeySpecException, NoSuchPaddingException,
IllegalBlockSizeException, BadPaddingException {
State state = agentHost.getStateFactory().get(agentId);
String conns = state.get(CONNKEY,String.class);
ArrayNode newConns;
if (conns != null) {
newConns = (ArrayNode) JOM.getInstance().readTree(conns);
} else {
newConns = JOM.createArrayNode();
}
ObjectNode params = JOM.createObjectNode();
params.put("username", EncryptionUtil.encrypt(username));
params.put("password", EncryptionUtil.encrypt(password));
if (resource != null && !resource.isEmpty()) {
params.put("resource", EncryptionUtil.encrypt(resource));
}
for (JsonNode item : newConns) {
if (item.get("username").equals(params.get("username"))) {
return;
}
}
newConns.add(params);
if (!state.putIfUnchanged(CONNKEY, JOM.getInstance()
.writeValueAsString(newConns), conns)) {
// recursive retry
storeConnection(agentId, username, password, resource);
}
}