From 2f5f9dc5c97749679695ff8147a6bf6b4e337ac5 Mon Sep 17 00:00:00 2001 From: "Smokowski, Kevin (ks6305)" Date: Wed, 2 Sep 2020 13:35:29 -0500 Subject: secure printer changes add method to print properties alphabetically Issue-ID: CCSDK-2721 Signed-off-by: Smokowski, Kevin (ks6305) Change-Id: I3bcb46a1f45dc74a2bc97acff08fa8c2c29d0ba7 --- .../org/onap/ccsdk/sli/core/sli/SecurePrinter.java | 50 ++++++++++++++++++---- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SecurePrinter.java b/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SecurePrinter.java index d12729c42..e25aed9f8 100644 --- a/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SecurePrinter.java +++ b/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SecurePrinter.java @@ -1,9 +1,13 @@ package org.onap.ccsdk.sli.core.sli; +import java.io.File; +import java.io.FileOutputStream; +import java.io.PrintStream; import java.util.HashMap; import java.util.Map.Entry; import java.util.Properties; +import java.util.TreeMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -13,6 +17,9 @@ public class SecurePrinter { private static final String DEFAULT_FILTER = "password,pass,pswd"; private static final String REDACTED = "***REDACTED***"; private static final String FILTER_PROPERTY = "NODE_STRING_FILTER"; + private static final String SEPERATOR = " = "; + private static final String COMMON_ERROR_MESSAGE = "Failed to print properties"; + private static String[] filterArray; public SecurePrinter() { @@ -38,7 +45,7 @@ public class SecurePrinter { if (LOG.isDebugEnabled()) { for (Entry attribute : attributes.entrySet()) { String value = filterValue(attribute.getKey(), attribute.getValue()); - LOG.debug(attribute.getKey() + " = " + value); + LOG.debug(attribute.getKey() + SEPERATOR + value); } } } @@ -48,7 +55,7 @@ public class SecurePrinter { for (Entry attribute : attributes.entrySet()) { if (attribute.getKey().startsWith(subpath)) { String value = filterValue(attribute.getKey(), attribute.getValue()); - LOG.debug(attribute.getKey() + " = " + value); + LOG.debug(attribute.getKey() + SEPERATOR + value); } } } @@ -61,15 +68,15 @@ public class SecurePrinter { String keyString = (String) property.getKey(); String valueString = (String) property.getValue(); String value = filterValue(keyString, valueString); - LOG.debug(keyString + " = " + value); + LOG.debug(keyString + SEPERATOR + value); } } catch (Exception e) { - LOG.error("Failed to print properties", e); + LOG.error(COMMON_ERROR_MESSAGE, e); } } } - public void printProperties(Properties props, String subpath) { + public void printProperties(Properties props, String subpath) { if (LOG.isDebugEnabled()) { try { for (Entry property : props.entrySet()) { @@ -77,15 +84,40 @@ public class SecurePrinter { if (keyString.startsWith(subpath)) { String valueString = (String) property.getValue(); String value = filterValue(keyString, valueString); - LOG.debug(keyString + " = " + value); + LOG.debug(keyString + SEPERATOR + value); } } } catch (Exception e) { - LOG.error("Failed to print properties", e); + LOG.error(COMMON_ERROR_MESSAGE, e); } } } -} - + public void printPropertiesAlphabetically(Properties props) { + if (LOG.isDebugEnabled()) { + TreeMap sortedMap = new TreeMap(props); + try { + for (Entry entry : sortedMap.entrySet()) { + String value = filterValue(entry.getKey(), entry.getValue()); + LOG.debug(entry.getKey() + SEPERATOR + value); + } + } catch (Exception e) { + LOG.error(COMMON_ERROR_MESSAGE, e); + } + } + } + public void printAttributesToFile(HashMap attributes, String fileName) { + try (FileOutputStream fstr = new FileOutputStream(new File(fileName)); + PrintStream pstr = new PrintStream(fstr, true);) { + pstr.println("#######################################"); + for (Entry entry : attributes.entrySet()) { + String value = filterValue(entry.getKey(), entry.getValue()); + pstr.println(entry.getKey() + SEPERATOR + value); + } + } catch (Exception e) { + LOG.error("Cannot write context to file.", e); + } + } + +} \ No newline at end of file -- cgit 1.2.3-korg