use of org.orcid.core.exception.ApplicationException in project ORCID-Source by ORCID.
the class AddressManagerImpl method updateAddresses.
@Override
public Addresses updateAddresses(String orcid, Addresses addresses) {
List<AddressEntity> existingAddressList = addressDao.getAddresses(orcid, getLastModified(orcid));
//Delete the deleted ones
for (AddressEntity existingAddress : existingAddressList) {
boolean deleteMe = true;
if (addresses.getAddress() != null) {
for (Address updatedOrNew : addresses.getAddress()) {
if (existingAddress.getId().equals(updatedOrNew.getPutCode())) {
deleteMe = false;
break;
}
}
}
if (deleteMe) {
try {
addressDao.deleteAddress(orcid, existingAddress.getId());
} catch (Exception e) {
throw new ApplicationException("Unable to delete address " + existingAddress.getId(), e);
}
}
}
if (addresses != null && addresses.getAddress() != null) {
for (Address updatedOrNew : addresses.getAddress()) {
if (updatedOrNew.getPutCode() != null) {
//Update the existing ones
for (AddressEntity existingAddress : existingAddressList) {
if (existingAddress.getId().equals(updatedOrNew.getPutCode())) {
existingAddress.setLastModified(new Date());
existingAddress.setVisibility(updatedOrNew.getVisibility());
existingAddress.setIso2Country(updatedOrNew.getCountry().getValue());
existingAddress.setDisplayIndex(updatedOrNew.getDisplayIndex());
addressDao.merge(existingAddress);
}
}
} else {
//Add the new ones
AddressEntity newAddress = adapter.toAddressEntity(updatedOrNew);
SourceEntity sourceEntity = sourceManager.retrieveSourceEntity();
ProfileEntity profile = new ProfileEntity(orcid);
newAddress.setUser(profile);
newAddress.setDateCreated(new Date());
//Set the source id
if (sourceEntity.getSourceProfile() != null) {
newAddress.setSourceId(sourceEntity.getSourceProfile().getId());
}
if (sourceEntity.getSourceClient() != null) {
newAddress.setClientSourceId(sourceEntity.getSourceClient().getId());
}
newAddress.setVisibility(updatedOrNew.getVisibility());
newAddress.setDisplayIndex(updatedOrNew.getDisplayIndex());
addressDao.persist(newAddress);
}
}
}
return addresses;
}
use of org.orcid.core.exception.ApplicationException in project ORCID-Source by ORCID.
the class DesEncrypter method encrypt.
public String encrypt(final String str) {
try {
// Encode the string into bytes using utf-8
byte[] utf8 = str.getBytes("UTF8");
// Encrypt
byte[] enc = ecipher.doFinal(utf8);
// Encode bytes to base64 to get a string
return Base64.encodeBase64String(enc);
} catch (UnsupportedEncodingException e) {
LOGGER.trace("DesEncrypter unsupported encoding exception", e);
throw new ApplicationException("DesEncrypter failed - UnsupportedEncodingException ", e);
} catch (GeneralSecurityException e) {
LOGGER.trace("DesEncrypter encryption failed", e);
throw new ApplicationException("DesEncrypter encryption failed - GeneralSecurityException", e);
}
}
use of org.orcid.core.exception.ApplicationException in project ORCID-Source by ORCID.
the class DesEncrypter method decrypt.
public String decrypt(final String str) {
try {
// Decode base64 to get bytes
byte[] dec = Base64.decodeBase64(str);
// Decrypt
byte[] utf8 = dcipher.doFinal(dec);
// Decode using utf-8
return new String(utf8, "UTF8");
} catch (GeneralSecurityException e) {
LOGGER.trace("DesEncrypter.decryptionfailed", e);
throw new ApplicationException("DesEncrypter decryption failed - GeneralSecurityException", e);
} catch (UnsupportedEncodingException e) {
LOGGER.trace("DesEncrypter.decryptionfailed", e);
throw new ApplicationException("DesEncrypter decryption failed - UnsupportedEncodingException", e);
}
}
use of org.orcid.core.exception.ApplicationException in project ORCID-Source by ORCID.
the class DesEncrypter method initDesEncrypter.
private void initDesEncrypter(final String passPhrase) {
try {
// Create the key
KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
ecipher = Cipher.getInstance(key.getAlgorithm());
dcipher = Cipher.getInstance(key.getAlgorithm());
// Prepare the parameter to the ciphers
AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
// Create the ciphers
ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
} catch (GeneralSecurityException e) {
LOGGER.trace("DesEncrypter.creation failed", e);
throw new ApplicationException("DesEncrypter creation failed", e);
}
}
Aggregations