Search in sources :

Example 6 with ApplicationException

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;
}
Also used : ApplicationException(org.orcid.core.exception.ApplicationException) Address(org.orcid.jaxb.model.record_v2.Address) SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity) AddressEntity(org.orcid.persistence.jpa.entities.AddressEntity) ApplicationException(org.orcid.core.exception.ApplicationException) OrcidDuplicatedElementException(org.orcid.core.exception.OrcidDuplicatedElementException) Date(java.util.Date) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity)

Example 7 with ApplicationException

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);
    }
}
Also used : ApplicationException(org.orcid.core.exception.ApplicationException) GeneralSecurityException(java.security.GeneralSecurityException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 8 with ApplicationException

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);
    }
}
Also used : ApplicationException(org.orcid.core.exception.ApplicationException) GeneralSecurityException(java.security.GeneralSecurityException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 9 with ApplicationException

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);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) ApplicationException(org.orcid.core.exception.ApplicationException) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Aggregations

ApplicationException (org.orcid.core.exception.ApplicationException)9 Date (java.util.Date)5 OrcidDuplicatedElementException (org.orcid.core.exception.OrcidDuplicatedElementException)5 ProfileEntity (org.orcid.persistence.jpa.entities.ProfileEntity)4 SourceEntity (org.orcid.persistence.jpa.entities.SourceEntity)4 GeneralSecurityException (java.security.GeneralSecurityException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 AddressEntity (org.orcid.persistence.jpa.entities.AddressEntity)2 ExternalIdentifierEntity (org.orcid.persistence.jpa.entities.ExternalIdentifierEntity)2 OtherNameEntity (org.orcid.persistence.jpa.entities.OtherNameEntity)2 ProfileKeywordEntity (org.orcid.persistence.jpa.entities.ProfileKeywordEntity)2 ResearcherUrlEntity (org.orcid.persistence.jpa.entities.ResearcherUrlEntity)2 Transactional (org.springframework.transaction.annotation.Transactional)2 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)1 KeySpec (java.security.spec.KeySpec)1 SecretKey (javax.crypto.SecretKey)1 PBEKeySpec (javax.crypto.spec.PBEKeySpec)1 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)1 Transactional (javax.transaction.Transactional)1 CreditName (org.orcid.jaxb.model.common_v2.CreditName)1