001    /**
002     * Copyright 2010-2012 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     */
016    package org.kuali.common.util.property.processor;
017    
018    import java.util.List;
019    import java.util.Properties;
020    
021    import org.jasypt.exceptions.EncryptionOperationNotPossibleException;
022    import org.jasypt.util.text.TextEncryptor;
023    import org.kuali.common.util.Mode;
024    import org.kuali.common.util.PropertyUtils;
025    import org.kuali.common.util.property.Constants;
026    import org.slf4j.Logger;
027    import org.slf4j.LoggerFactory;
028    
029    public class EndsWithDecryptProcessor extends DecryptProcessor {
030    
031            private static final Logger logger = LoggerFactory.getLogger(EndsWithDecryptProcessor.class);
032    
033            String suffix = Constants.DEFAULT_ENCRYPTED_SUFFIX;
034            boolean removeEncryptedProperties = true;
035            Mode propertyOverwriteMode = Constants.DEFAULT_PROPERTY_OVERWRITE_MODE;
036    
037            public EndsWithDecryptProcessor() {
038                    this(null);
039            }
040    
041            public EndsWithDecryptProcessor(TextEncryptor encryptor) {
042                    super(encryptor);
043            }
044    
045            @Override
046            public void process(Properties properties) {
047                    List<String> keys = PropertyUtils.getEndsWithKeys(properties, suffix);
048                    logger.info("Decrypting {} property values", keys.size());
049                    for (String key : keys) {
050                            logger.debug("Decrypting [{}]", key);
051                            String encryptedValue = properties.getProperty(key);
052                            String decryptedValue = decrypt(key, encryptedValue, encryptor);
053                            int endIndex = key.length() - suffix.length();
054                            String newKey = key.substring(0, endIndex);
055                            PropertyUtils.addOrOverrideProperty(properties, newKey, decryptedValue, propertyOverwriteMode);
056                            if (removeEncryptedProperties) {
057                                    logger.debug("Removing {}", key);
058                                    properties.remove(key);
059                            }
060                    }
061            }
062    
063            protected String decrypt(String key, String encryptedValue, TextEncryptor encryptor) {
064                    try {
065                            return encryptor.decrypt(encryptedValue);
066                    } catch (EncryptionOperationNotPossibleException e) {
067                            throw new IllegalStateException("Unexpected error decrypting [" + key + "]=[" + encryptedValue + "]");
068                    }
069            }
070    
071            public String getSuffix() {
072                    return suffix;
073            }
074    
075            public void setSuffix(String suffix) {
076                    this.suffix = suffix;
077            }
078    
079            public boolean isRemoveEncryptedProperties() {
080                    return removeEncryptedProperties;
081            }
082    
083            public void setRemoveEncryptedProperties(boolean removeEncryptedProperties) {
084                    this.removeEncryptedProperties = removeEncryptedProperties;
085            }
086    
087            public Mode getPropertyOverwriteMode() {
088                    return propertyOverwriteMode;
089            }
090    
091            public void setPropertyOverwriteMode(Mode propertyOverwriteMode) {
092                    this.propertyOverwriteMode = propertyOverwriteMode;
093            }
094    
095    }