View Javadoc
1   /**
2    * Copyright 2010-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util.property.processor;
17  
18  import java.util.List;
19  import java.util.Properties;
20  
21  import org.jasypt.exceptions.EncryptionOperationNotPossibleException;
22  import org.jasypt.util.text.TextEncryptor;
23  import org.kuali.common.util.Mode;
24  import org.kuali.common.util.PropertyUtils;
25  import org.kuali.common.util.property.Constants;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  /**
30   * @deprecated
31   */
32  @Deprecated
33  public class EndsWithDecryptProcessor extends DecryptProcessor {
34  
35  	private static final Logger logger = LoggerFactory.getLogger(EndsWithDecryptProcessor.class);
36  
37  	String suffix = Constants.DEFAULT_ENCRYPTED_SUFFIX;
38  	boolean removeEncryptedProperties = true;
39  	Mode propertyOverwriteMode = Constants.DEFAULT_PROPERTY_OVERWRITE_MODE;
40  
41  	public EndsWithDecryptProcessor() {
42  		this(null);
43  	}
44  
45  	public EndsWithDecryptProcessor(TextEncryptor encryptor) {
46  		super(encryptor);
47  	}
48  
49  	@Override
50  	public void process(Properties properties) {
51  		List<String> keys = PropertyUtils.getEndsWithKeys(properties, suffix);
52  		logger.info("Decrypting {} property values", keys.size());
53  		for (String key : keys) {
54  			logger.debug("Decrypting [{}]", key);
55  			String encryptedValue = properties.getProperty(key);
56  			String decryptedValue = decrypt(key, encryptedValue, encryptor);
57  			int endIndex = key.length() - suffix.length();
58  			String newKey = key.substring(0, endIndex);
59  			PropertyUtils.addOrOverrideProperty(properties, newKey, decryptedValue, propertyOverwriteMode);
60  			if (removeEncryptedProperties) {
61  				logger.debug("Removing {}", key);
62  				properties.remove(key);
63  			}
64  		}
65  	}
66  
67  	protected String decrypt(String key, String encryptedValue, TextEncryptor encryptor) {
68  		try {
69  			return encryptor.decrypt(encryptedValue);
70  		} catch (EncryptionOperationNotPossibleException e) {
71  			throw new IllegalStateException("Unexpected error decrypting [" + key + "]=[" + encryptedValue + "]");
72  		}
73  	}
74  
75  	public String getSuffix() {
76  		return suffix;
77  	}
78  
79  	public void setSuffix(String suffix) {
80  		this.suffix = suffix;
81  	}
82  
83  	public boolean isRemoveEncryptedProperties() {
84  		return removeEncryptedProperties;
85  	}
86  
87  	public void setRemoveEncryptedProperties(boolean removeEncryptedProperties) {
88  		this.removeEncryptedProperties = removeEncryptedProperties;
89  	}
90  
91  	public Mode getPropertyOverwriteMode() {
92  		return propertyOverwriteMode;
93  	}
94  
95  	public void setPropertyOverwriteMode(Mode propertyOverwriteMode) {
96  		this.propertyOverwriteMode = propertyOverwriteMode;
97  	}
98  
99  }