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.util.text.TextEncryptor;
022 import org.kuali.common.util.Mode;
023 import org.kuali.common.util.PropertyUtils;
024 import org.kuali.common.util.property.Constants;
025 import org.slf4j.Logger;
026 import org.slf4j.LoggerFactory;
027
028 public class EndsWithEncryptProcessor extends DecryptProcessor {
029
030 private static final Logger logger = LoggerFactory.getLogger(EndsWithEncryptProcessor.class);
031
032 String suffix = Constants.DEFAULT_ENCRYPTED_SUFFIX;
033 boolean removeUnencryptedProperties = true;
034 Mode propertyOverwriteMode = Constants.DEFAULT_PROPERTY_OVERWRITE_MODE;
035
036 public EndsWithEncryptProcessor() {
037 this(null);
038 }
039
040 public EndsWithEncryptProcessor(TextEncryptor encryptor) {
041 super(encryptor);
042 }
043
044 @Override
045 public void process(Properties properties) {
046 List<String> keys = PropertyUtils.getSortedKeys(properties);
047 for (String key : keys) {
048 String decryptedValue = properties.getProperty(key);
049 String encryptedValue = encryptor.encrypt(decryptedValue);
050 String newKey = key + suffix;
051 PropertyUtils.addOrOverrideProperty(properties, newKey, encryptedValue, propertyOverwriteMode);
052 if (removeUnencryptedProperties) {
053 logger.debug("Removing {}", key);
054 properties.remove(key);
055 }
056 }
057 }
058
059 public String getSuffix() {
060 return suffix;
061 }
062
063 public void setSuffix(String suffix) {
064 this.suffix = suffix;
065 }
066
067 public boolean isRemoveUnencryptedProperties() {
068 return removeUnencryptedProperties;
069 }
070
071 public void setRemoveUnencryptedProperties(boolean removeUnencryptedProperties) {
072 this.removeUnencryptedProperties = removeUnencryptedProperties;
073 }
074
075 public Mode getPropertyOverwriteMode() {
076 return propertyOverwriteMode;
077 }
078
079 public void setPropertyOverwriteMode(Mode propertyOverwriteMode) {
080 this.propertyOverwriteMode = propertyOverwriteMode;
081 }
082
083 }