Merge "Adding Junit"
[so.git] / common / src / main / java / org / openecomp / mso / properties / MsoJavaProperties.java
index 3675dd6..c13b9e0 100644 (file)
@@ -24,7 +24,11 @@ package org.openecomp.mso.properties;
 import java.io.FileReader;
 import java.io.IOException;
 import java.security.GeneralSecurityException;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Properties;
+
+import org.apache.commons.codec.binary.Base64;
 import org.openecomp.mso.utils.CryptoUtils;
 
 public class MsoJavaProperties extends AbstractMsoProperties {
@@ -86,11 +90,43 @@ public class MsoJavaProperties extends AbstractMsoProperties {
 
        }
 
+       /**
+        * @param encryptedAuth: encrypted credentials from properties
+        * @param msoKey: key to use to decrypt from properties
+        * @return base 64 encoded basic auth credentials
+        */
+       public synchronized String getBasicAuth(String encryptedAuth, String msoKey){
+               String encodedString = null;
+               if ((encryptedAuth == null || encryptedAuth.isEmpty()) || (msoKey == null || msoKey.isEmpty()))
+                       return null;
+               try {
+                       String auth = decrypt(encryptedAuth, msoKey);
+                       byte[] encoded = Base64.encodeBase64(auth.getBytes());
+                       encodedString = new String(encoded);
+                       encodedString = "Basic " + encodedString;
+                       
+               } catch (Exception ex) {
+                       LOGGER.debug("Exception while getBasicAuth " + encryptedAuth, ex);
+               }
+               return encodedString;
+       }               
+       
        public synchronized int size() {
                return this.msoProperties.size();
        }
        
-
+       public synchronized String decrypt(String toDecrypt, String msokey){
+               String result = null;
+               try {
+                       result = CryptoUtils.decrypt(toDecrypt, msokey);
+                       
+               }
+               catch (Exception e) {
+                       LOGGER.debug("Failed to decrypt credentials: " + toDecrypt, e);
+               }
+               return result;
+       }       
+       
        @Override
        protected synchronized void reloadPropertiesFile() throws IOException {
                this.loadPropertiesFile(this.propertiesFileName);
@@ -115,7 +151,6 @@ public class MsoJavaProperties extends AbstractMsoProperties {
 
                } finally {
                        this.automaticRefreshInMinutes = this.getIntProperty(RELOAD_TIME_PROPERTY, DEFAULT_RELOAD_TIME_MIN);
-                       // Always close the file
                        try {
                                if (reader != null) {
                                        reader.close();
@@ -155,27 +190,39 @@ public class MsoJavaProperties extends AbstractMsoProperties {
                        return false;
                }
                MsoJavaProperties other = (MsoJavaProperties) obj;
-               if (!msoProperties.equals(other.msoProperties)) {
-                       return false;
-               }
-               return true;
+
+               return msoProperties.equals(other.msoProperties);
        }
 
        @Override
        public String toString() {
 
-               StringBuffer response = new StringBuffer();
-               response.append("Config file " + propertiesFileName + "(Timer:" + automaticRefreshInMinutes + "mins):"
-                               + System.getProperty("line.separator"));
+               StringBuilder response = new StringBuilder();
+               response.append("Config file ")
+                               .append(propertiesFileName)
+                               .append("(Timer:")
+                               .append(automaticRefreshInMinutes)
+                               .append("mins):")
+                               .append(System.lineSeparator());
+
                for (Object key : this.msoProperties.keySet()) {
                        String propertyName = (String) key;
                        response.append(propertyName);
                        response.append("=");
                        response.append(this.msoProperties.getProperty(propertyName));
-                       response.append(System.getProperty("line.separator"));
+                       response.append(System.lineSeparator());
                }
-               response.append(System.getProperty("line.separator"));
-               response.append(System.getProperty("line.separator"));
+               response.append(System.lineSeparator());
+               response.append(System.lineSeparator());
+
                return response.toString();
        }
+        
+    public Map<String, String> asMap() {
+        final Map<String, String> result = new HashMap<>();
+        msoProperties.forEach((key, value) -> result.put(key.toString(), value.toString()));
+        
+        return result;
+    }
+
 }