View Javadoc

1   /**
2    * Copyright 2010-2012 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  public class EndsWithDecryptProcessor extends DecryptProcessor {
30  
31  	private static final Logger logger = LoggerFactory.getLogger(EndsWithDecryptProcessor.class);
32  
33  	String suffix = Constants.DEFAULT_ENCRYPTED_SUFFIX;
34  	boolean removeEncryptedProperties = true;
35  	Mode propertyOverwriteMode = Constants.DEFAULT_PROPERTY_OVERWRITE_MODE;
36  
37  	public EndsWithDecryptProcessor() {
38  		this(null);
39  	}
40  
41  	public EndsWithDecryptProcessor(TextEncryptor encryptor) {
42  		super(encryptor);
43  	}
44  
45  	@Override
46  	public void process(Properties properties) {
47  		List<String> keys = PropertyUtils.getEndsWithKeys(properties, suffix);
48  		logger.info("Decrypting {} property values", keys.size());
49  		for (String key : keys) {
50  			logger.debug("Decrypting [{}]", key);
51  			String encryptedValue = properties.getProperty(key);
52  			String decryptedValue = decrypt(key, encryptedValue, encryptor);
53  			int endIndex = key.length() - suffix.length();
54  			String newKey = key.substring(0, endIndex);
55  			PropertyUtils.addOrOverrideProperty(properties, newKey, decryptedValue, propertyOverwriteMode);
56  			if (removeEncryptedProperties) {
57  				logger.debug("Removing {}", key);
58  				properties.remove(key);
59  			}
60  		}
61  	}
62  
63  	protected String decrypt(String key, String encryptedValue, TextEncryptor encryptor) {
64  		try {
65  			return encryptor.decrypt(encryptedValue);
66  		} catch (EncryptionOperationNotPossibleException e) {
67  			throw new IllegalStateException("Unexpected error decrypting [" + key + "]=[" + encryptedValue + "]");
68  		}
69  	}
70  
71  	public String getSuffix() {
72  		return suffix;
73  	}
74  
75  	public void setSuffix(String suffix) {
76  		this.suffix = suffix;
77  	}
78  
79  	public boolean isRemoveEncryptedProperties() {
80  		return removeEncryptedProperties;
81  	}
82  
83  	public void setRemoveEncryptedProperties(boolean removeEncryptedProperties) {
84  		this.removeEncryptedProperties = removeEncryptedProperties;
85  	}
86  
87  	public Mode getPropertyOverwriteMode() {
88  		return propertyOverwriteMode;
89  	}
90  
91  	public void setPropertyOverwriteMode(Mode propertyOverwriteMode) {
92  		this.propertyOverwriteMode = propertyOverwriteMode;
93  	}
94  
95  }