From c84c0c1761cddac08f28b554809580df1f84d59e Mon Sep 17 00:00:00 2001 From: shashikanth Date: Thu, 21 Sep 2017 19:24:34 +0530 Subject: Fix Blocker/Critical sonar issues 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 --- .../sli/adaptors/resource/sql/SqlResource.java | 101 +++++++-------------- 1 file changed, 35 insertions(+), 66 deletions(-) (limited to 'sql-resource/provider/src') diff --git a/sql-resource/provider/src/main/java/org/onap/ccsdk/sli/adaptors/resource/sql/SqlResource.java b/sql-resource/provider/src/main/java/org/onap/ccsdk/sli/adaptors/resource/sql/SqlResource.java index 78d92d09..df3d8132 100644 --- a/sql-resource/provider/src/main/java/org/onap/ccsdk/sli/adaptors/resource/sql/SqlResource.java +++ b/sql-resource/provider/src/main/java/org/onap/ccsdk/sli/adaptors/resource/sql/SqlResource.java @@ -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); -- cgit 1.2.3-korg