Fix sonar issues + merge
[aai/schema-service.git] / aai-schema-service / src / main / java / org / onap / aai / schemaservice / config / PropertyPasswordConfiguration.java
index df1d91c..2f42309 100644 (file)
@@ -7,9 +7,9 @@
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- *
- *    http://www.apache.org/licenses/LICENSE-2.0
- *
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -17,6 +17,7 @@
  * limitations under the License.
  * ============LICENSE_END=========================================================
  */
+
 package org.onap.aai.schemaservice.config;
 
 import java.io.File;
@@ -38,95 +39,73 @@ import org.springframework.core.env.EnumerablePropertySource;
 import org.springframework.core.env.MapPropertySource;
 import org.springframework.core.env.PropertySource;
 
-public class PropertyPasswordConfiguration implements ApplicationContextInitializer<ConfigurableApplicationContext> {
+public class PropertyPasswordConfiguration
+    implements ApplicationContextInitializer<ConfigurableApplicationContext> {
 
     private static final Pattern decodePasswordPattern = Pattern.compile("password\\((.*?)\\)");
     private PasswordDecoder passwordDecoder = new JettyPasswordDecoder();
-    private static final Logger logger = LoggerFactory.getLogger(PropertyPasswordConfiguration.class.getName());
+    private static final Logger logger =
+        LoggerFactory.getLogger(PropertyPasswordConfiguration.class.getName());
 
     @Override
     public void initialize(ConfigurableApplicationContext applicationContext) {
 
         ConfigurableEnvironment environment = applicationContext.getEnvironment();
         String certPath = environment.getProperty("server.certs.location");
-        File passwordFile = null;
-        File passphrasesFile = null;
-        InputStream passwordStream = null;
-        InputStream passphrasesStream = null;
         Map<String, Object> sslProps = new LinkedHashMap<>();
 
         // Override the passwords from application.properties if we find AAF certman files
         if (certPath != null) {
-            try {
-                passwordFile = new File(certPath + ".password");
-                passwordStream = new FileInputStream(passwordFile);
-
-                if (passwordStream != null) {
-                    String keystorePassword = null;
+            File passwordFile = new File(certPath + ".password");
+            try (InputStream passwordStream = new FileInputStream(passwordFile)) {
+                String keystorePassword = null;
 
-                    keystorePassword = IOUtils.toString(passwordStream);
-                    if (keystorePassword != null) {
-                        keystorePassword = keystorePassword.trim();
-                        sslProps.put("server.ssl.key-store-password", keystorePassword);
-                    }
-                    else{
-                        logger.warn("Keystore password is null in AAF Certman password file");
-                    }
+                keystorePassword = IOUtils.toString(passwordStream);
+                if (keystorePassword != null) {
+                    keystorePassword = keystorePassword.trim();
+                    sslProps.put("server.ssl.key-store-password", keystorePassword);
                 } else {
-                    logger.info("Not using AAF Certman password file");
+                    logger.warn("Keystore password is null in AAF Certman password file");
                 }
             } catch (IOException e) {
-                logger.warn("Not using AAF Certman password file " + passwordFile.getName() + " e=" + e.getMessage());
-            } finally {
-                if (passwordStream != null) {
-                    try {
-                        passwordStream.close();
-                    } catch (Exception e) {
-                    }
-                }
+                logger.warn(
+                    "Not using AAF Certman password file " + passwordFile.getName() + " e=" +
+                        e.getMessage());
             }
-            try {
-                passphrasesFile = new File(certPath + ".passphrases");
-                passphrasesStream = new FileInputStream(passphrasesFile);
 
-                if (passphrasesStream != null) {
-                    String truststorePassword = null;
-                    Properties passphrasesProps = new Properties();
-                    passphrasesProps.load(passphrasesStream);
-                    truststorePassword = passphrasesProps.getProperty("cadi_truststore_password");
-                    if (truststorePassword != null) {
-                        truststorePassword = truststorePassword.trim();
-                        sslProps.put("server.ssl.trust-store-password", truststorePassword);
-                    }
-                    else {
-                        logger.warn("Truststore password is null in AAF Certman passphrases file");
-                    }
+            File passphrasesFile = new File(certPath + ".passphrases");
+            try (InputStream passphrasesStream = new FileInputStream(passphrasesFile)) {
+                String truststorePassword = null;
+                Properties passphrasesProps = new Properties();
+                passphrasesProps.load(passphrasesStream);
+                truststorePassword = passphrasesProps.getProperty("cadi_truststore_password");
+                if (truststorePassword != null) {
+                    truststorePassword = truststorePassword.trim();
+                    sslProps.put("server.ssl.trust-store-password", truststorePassword);
                 } else {
-                    logger.info("Not using AAF Certman passphrases file");
+                    logger.warn("Truststore password is null in AAF Certman passphrases file");
                 }
             } catch (IOException e) {
-                logger.warn("Not using AAF Certman passphrases file " +  passphrasesFile.getName() + " e=" + e.getMessage());
-            } finally {
-                if (passphrasesStream != null) {
-                    try {
-                        passphrasesStream.close();
-                    } catch (Exception e) {
-                    }
-                }
+                logger.warn(
+                    "Not using AAF Certman passphrases file " + passphrasesFile.getName() + " e=" +
+                        e.getMessage());
             }
         }
         for (PropertySource<?> propertySource : environment.getPropertySources()) {
             Map<String, Object> propertyOverrides = new LinkedHashMap<>();
             decodePasswords(propertySource, propertyOverrides);
             if (!propertyOverrides.isEmpty()) {
-                PropertySource<?> decodedProperties = new MapPropertySource("decoded "+ propertySource.getName(), propertyOverrides);
-                environment.getPropertySources().addBefore(propertySource.getName(), decodedProperties);
+                PropertySource<?> decodedProperties =
+                    new MapPropertySource("decoded " + propertySource.getName(), propertyOverrides);
+                environment.getPropertySources()
+                    .addBefore(propertySource.getName(), decodedProperties);
             }
 
         }
         if (!sslProps.isEmpty()) {
             logger.info("Using AAF Certman files");
-            PropertySource<?> additionalProperties = new MapPropertySource("additionalProperties", sslProps);
+            PropertySource<?> additionalProperties =
+                new MapPropertySource("additionalProperties", sslProps);
             environment.getPropertySources().addFirst(additionalProperties);
         }
 
@@ -134,7 +113,8 @@ public class PropertyPasswordConfiguration implements ApplicationContextInitiali
 
     private void decodePasswords(PropertySource<?> source, Map<String, Object> propertyOverrides) {
         if (source instanceof EnumerablePropertySource) {
-            EnumerablePropertySource<?> enumerablePropertySource = (EnumerablePropertySource<?>) source;
+            EnumerablePropertySource<?> enumerablePropertySource =
+                (EnumerablePropertySource<?>) source;
             for (String key : enumerablePropertySource.getPropertyNames()) {
                 Object rawValue = source.getProperty(key);
                 if (rawValue instanceof String) {
@@ -146,7 +126,9 @@ public class PropertyPasswordConfiguration implements ApplicationContextInitiali
     }
 
     private String decodePasswordsInString(String input) {
-        if (input == null) return null;
+        if (input == null) {
+            return null;
+        }
         StringBuffer output = new StringBuffer();
         Matcher matcher = decodePasswordPattern.matcher(input);
         while (matcher.find()) {