package org.xdi.util.security;

import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xdi.util.StringHelper;
import org.xdi.util.security.StringEncrypter.EncryptionException;

/**
 * Decrypt encripted properties
 * 
 * @author Yuriy Movchan Date: 04/24/2013
 */
public class PropertiesDecrypter {

	private static final Logger log = LoggerFactory.getLogger(PropertiesDecrypter.class);

	public static final String bindPassword = "bindPassword";

	public static Properties decryptProperties(Properties properties, String encryptionKey) throws EncryptionException {
		return decryptProperties(StringEncrypter.defaultInstance(), properties, encryptionKey);
	}

	public static Properties decryptProperties(StringEncrypter stringEncrypter, Properties properties, String encryptionKey) {
		if (properties == null) {
			return properties;
		}

		Properties clondedProperties = (Properties) properties.clone();

		String encryptedPassword = clondedProperties.getProperty(PropertiesDecrypter.bindPassword);
		if (StringHelper.isEmpty(encryptedPassword)) {
			return properties;
		}

		try {
			clondedProperties.put(PropertiesDecrypter.bindPassword, stringEncrypter.decrypt(properties.getProperty(PropertiesDecrypter.bindPassword), encryptionKey));
		} catch (EncryptionException ex) {
			log.error(String.format("Failed to decript '%s' property", PropertiesDecrypter.bindPassword), ex);
		}

		return clondedProperties;
	}

	public static String decryptProperty(String encryptedValue, boolean returnSource, String encryptionKey) {
		if (encryptedValue == null) {
			return encryptedValue;
		}

		String resultValue;
		if (returnSource) {
			resultValue = encryptedValue;
		} else {
			resultValue = null;
		}

		try {
			resultValue = StringEncrypter.defaultInstance().decrypt(encryptedValue, encryptionKey);
		} catch (Exception ex) {
			if (!returnSource) {
				log.error(String.format("Failed to decrypt value: '%s'", encryptedValue, ex));
			}
		}

		return resultValue;
	}

}

@@ -691,24 +691,193 @@
 onKey);%0A%09%7D%0A%0A
+%09public static Properties decryptProperties(StringEncrypter stringEncrypter, Properties properties) %7B%0A%09%09return decryptProperties(stringEncrypter, properties, null);%0A%09%7D%0A%0A
 %09public stat
@@ -984,24 +984,24 @@
 ptionKey) %7B%0A
-
 %09%09if (proper
@@ -1295,160 +1295,424 @@
 %0A%09%09%09
-clondedProperties.put(PropertiesDecrypter.bindPassword, stringEncrypter.decrypt(properties.getProperty(PropertiesDecrypter.bindPassword), encryptionKey)
+String decryptedProperty;%0A%09%09%09if (StringHelper.isEmpty(encryptionKey)) %7B%0A%09%09%09%09decryptedProperty = stringEncrypter.decrypt(properties.getProperty(PropertiesDecrypter.bindPassword));%0A%09%09%09%7D else %7B%0A%09%09%09%09decryptedProperty = StringEncrypter.defaultInstance().decrypt(properties.getProperty(PropertiesDecrypter.bindPassword), encryptionKey);%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09clondedProperties.put(PropertiesDecrypter.bindPassword, decryptedProperty
 );%0A%09

Use specified stringDecrypter to decrypt passwords