package de.superx.spring; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.security.NoSuchAlgorithmException; import java.security.InvalidKeyException; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.List; import java.util.Properties; import org.apache.log4j.Logger; import de.superx.util.PathAndFileUtils; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; import javax.crypto.Cipher; import javax.crypto.BadPaddingException; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.SecretKeySpec; import java.util.Set; public class CustomDatabases{ private static Logger logger = Logger.getLogger(CustomDatabases.class); private static SecretKeySpec KEY; private static Cipher desCipher; public static Map> getCustomConnections() throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException{ Map> connections = new HashMap<>(); //loop over all properties and map them to connections //automatically compute dbconnections from the name property Properties props = fetchProperties(); List dbconnections = getDBConnectionsFromNameProperty(props); final Set propertyNames = props.stringPropertyNames(); for (String s : dbconnections) { Map properties = new HashMap<>(); logger.info(String.format("%s connection retrieved from HISinOne", s)); List keys = propertyNames.stream().filter(it -> it.startsWith("db." + s + ".")).collect(Collectors.toList()); for (String key : keys) { String value = props.getProperty(key); if (key.endsWith("password")) { value = decryptDbPassword(value.trim()); } properties.put(key.substring(key.lastIndexOf('.') + 1), value); } connections.put(s, properties); } return connections; } private static List getPropertyValuesByRegex(Properties properties, String regex) { List values = new ArrayList<>(); Pattern pattern = Pattern.compile(regex); for (String key : properties.stringPropertyNames()) { Matcher matcher = pattern.matcher(key); if (matcher.matches()) { values.add(properties.getProperty(key)); } } return values; } public static List getDBConnectionsFromNameProperty(Properties props){ ArrayList connection_names = new ArrayList(); String regex = "db.*.name"; connection_names = (ArrayList) getPropertyValuesByRegex(props, regex); //check for duplicate names Set seen = new HashSet<>(); for (String db_name : connection_names) { if(!seen.add(db_name)) { logger.warn("Found atleast two databases with the name" + db_name); } } return connection_names; } private static String decryptDbPassword(String password) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { if(password == null || password.equals("")) { return null; }else if(!password.startsWith("sx_des")) { return password; } if(desCipher == null) { desCipher = getDesCipher(); } String[] stext = password.substring(7).split("#"); byte[] bstext = new byte[stext.length]; for(int i = 0; i