Fix Blocker/Critical sonar issues 61/14261/2
authorshashikanth <shashikanth.vh@huawei.com>
Thu, 21 Sep 2017 13:54:34 +0000 (19:24 +0530)
committerShashikanth VH <shashikanth.vh@huawei.com>
Thu, 21 Sep 2017 14:02:50 +0000 (14:02 +0000)
Fix Blocker/Critical sonar issues in ccsdk/sli/adaptors module
https://sonar.onap.org/component_issues?id=org.onap.ccsdk.sli.adaptors%3Accsdk-sli-adaptors#resolved=false|severities=BLOCKER

Fixed Close "PreparedStatement".

Issue-Id: CCSDK-67
Change-Id: I2d4b42067f3286806f7bca8821ce17328d72091f
Signed-off-by: shashikanth.vh <shashikanth.vh@huawei.com>
sql-resource/provider/src/main/java/org/onap/ccsdk/sli/adaptors/resource/sql/SqlResource.java

index 78d92d0..df3d813 100644 (file)
@@ -382,72 +382,41 @@ public class SqlResource implements SvcLogicResource, SvcLogicJavaPlugin {
                return (executeSqlWrite(key, ctx));
        }
 
-       private String decryptColumn(String tableName, String colName, byte[] colValue, DbLibService dblibSvc) {
-               String strValue = new String(colValue);
-
-               if (StringUtils.isAsciiPrintable(strValue)) {
-
-                       // If printable, not encrypted
-                       return (strValue);
-               } else {
-                       PreparedStatement stmt = null;
-                       Connection conn = null;
-                       ResultSet results = null;
-                       try {
-                               // CachedRowSet results =
-                               // dblibSvc.getData("SELECT
-                               // CAST(AES_DECRYPT('"+strValue+"','"+CRYPT_KEY+"') AS CHAR(50))
-                               // FROM DUAL",
-                               // null, null);
-                               conn = ((DBResourceManager) dblibSvc).getConnection();
-
-                               stmt = conn.prepareStatement("SELECT CAST(AES_DECRYPT(?, ?) AS CHAR(50)) FROM DUAL");
-
-                               stmt.setBytes(1, colValue);
-                               stmt.setString(2, getCryptKey());
-
-                               results = stmt.executeQuery();
-
-                               if ((results != null) && results.next()) {
-                                       strValue = results.getString(1);
-                                       LOG.debug("Decrypted value is " + strValue);
-                               } else {
-                                       LOG.warn("Cannot decrypt " + tableName + "." + colName);
-                               }
-                       } catch (Exception e) {
-                               LOG.error("Caught exception trying to decrypt " + tableName + "." + colName, e);
-                       } finally {
-                               try {
-                                       if (results != null) {
-                                               results.close();
-                                               results = null;
-                                       }
-                               } catch (Exception exc) {
-
-                               }
-
-                               try {
-                                       if (stmt != null) {
-                                               stmt.close();
-                                               stmt = null;
-                                       }
-                               } catch (Exception exc) {
-
-                               }
-
-                               try {
-                                       if (conn != null) {
-                                               conn.close();
-                                               conn = null;
-                                       }
-                               } catch (Exception exc) {
-
-                               }
-
-                       }
-               }
-               return (strValue);
-       }
+    private String decryptColumn(String tableName, String colName, byte[] colValue, DbLibService dblibSvc) {
+        String strValue = new String(colValue);
+
+        if (StringUtils.isAsciiPrintable(strValue)) {
+
+            // If printable, not encrypted
+            return (strValue);
+        } else {
+            ResultSet results = null;
+            try (Connection conn = ((DBResourceManager) dblibSvc).getConnection();
+               PreparedStatement stmt = conn.prepareStatement("SELECT CAST(AES_DECRYPT(?, ?) AS CHAR(50)) FROM DUAL")) {
+
+                stmt.setBytes(1, colValue);
+                stmt.setString(2, getCryptKey());
+                results = stmt.executeQuery();
+
+                if ((results != null) && results.next()) {
+                    strValue = results.getString(1);
+                    LOG.debug("Decrypted value is " + strValue);
+                } else {
+                    LOG.warn("Cannot decrypt " + tableName + "." + colName);
+                }
+            } catch (Exception e) {
+                if (results != null) {
+                    try {
+                        results.close();
+                    } catch (SQLException ignored) {
+
+                    }
+                }
+                LOG.error("Caught exception trying to decrypt " + tableName + "." + colName, e);
+            }
+        }
+        return (strValue);
+    }
 
        public static String getCryptKey() {
                return (CRYPT_KEY);