001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.data.jpa.converters;
017
018import javax.persistence.AttributeConverter;
019import javax.persistence.Converter;
020
021import org.apache.commons.lang.StringUtils;
022import org.kuali.rice.core.api.CoreApiServiceLocator;
023
024/**
025 * Calls the core service to encrypt values going to the database and decrypt values coming back from the database.
026 *
027 * @author Kuali Rice Team (rice.collab@kuali.org)
028 */
029@Converter
030public class EncryptionConverter implements AttributeConverter<String, String> {
031
032    /**
033     * {@inheritDoc}
034     *
035     * This implementation encrypts the value going to the database.
036     */
037        @Override
038        public String convertToDatabaseColumn(String objectValue) {
039                // don't attempt to encrypt nulls or empty strings
040                if (objectValue == null) {
041                        return null;
042                }
043                if (StringUtils.isEmpty(objectValue.toString())) {
044                        return "";
045                }
046                try {
047                        // check if the encryption service is enable before using it
048                        if (CoreApiServiceLocator.getEncryptionService().isEnabled()) {
049                                return CoreApiServiceLocator.getEncryptionService().encrypt(objectValue);
050                        }
051                } catch (Exception e) {
052                        throw new RuntimeException("Exception while attempting to encrypt value for DB: ", e);
053                }
054                return objectValue;
055        }
056
057    /**
058     * {@inheritDoc}
059     *
060     * This implementation decrypts the value coming from the database.
061     */
062        @Override
063        public String convertToEntityAttribute(String dataValue) {
064                // don't attempt to decrypt nulls or empty strings
065                if (dataValue == null) {
066                        return null;
067                }
068                if (StringUtils.isEmpty(dataValue.toString())) {
069                        return "";
070                }
071                try {
072                        // check if the encryption service is enable before using it
073                        if (CoreApiServiceLocator.getEncryptionService().isEnabled()) {
074                                return CoreApiServiceLocator.getEncryptionService().decrypt(dataValue.toString());
075                        }
076                } catch (Exception e) {
077                        throw new RuntimeException("Exception while attempting to decrypt value from DB: ", e);
078                }
079                return dataValue;
080        }
081
082}