Fix NPE in toJsonString()
[ccsdk/sli/core.git] / sli / common / src / main / java / org / onap / ccsdk / sli / core / sli / SecurePrinter.java
1
2 package org.onap.ccsdk.sli.core.sli;
3
4 import java.io.File;
5 import java.io.FileOutputStream;
6 import java.io.PrintStream;
7 import java.util.HashMap;
8 import java.util.Map.Entry;
9 import java.util.Properties;
10 import java.util.TreeMap;
11
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 public class SecurePrinter {
16     private static final Logger LOG = LoggerFactory.getLogger(SecurePrinter.class);
17     private static final String DEFAULT_FILTER = "password,pass,pswd";
18     private static final String REDACTED = "***REDACTED***";
19     private static final String FILTER_PROPERTY = "NODE_STRING_FILTER";
20     private static final String SEPERATOR = " = ";
21     private static final String COMMON_ERROR_MESSAGE = "Failed to print properties";
22
23     private static String[] filterArray;
24
25     public SecurePrinter() {
26         String filterProperty = System.getProperty(FILTER_PROPERTY);
27         if (filterProperty != null && !filterProperty.isEmpty() && filterProperty.contains(",")) {
28             filterArray = filterProperty.split(",");
29         } else {
30             filterArray = DEFAULT_FILTER.split(",");
31         }
32     }
33
34     private String filterValue(String key, String value) {
35         String normalizedKey = key.toLowerCase();
36         for (String restrictedKey : filterArray) {
37             if (normalizedKey.contains(restrictedKey)) {
38                 return REDACTED;
39             }
40         }
41         return value;
42     }
43
44     public void printAttributes(HashMap<String, String> attributes) {
45         if (LOG.isDebugEnabled()) {
46             for (Entry<String, String> attribute : attributes.entrySet()) {
47                 String value = filterValue(attribute.getKey(), attribute.getValue());
48                 LOG.debug(attribute.getKey() + SEPERATOR + value);
49             }
50         }
51     }
52
53     public void printAttributes(HashMap<String, String> attributes, String subpath) {
54         if (LOG.isDebugEnabled()) {
55             for (Entry<String, String> attribute : attributes.entrySet()) {
56                 if (attribute.getKey().startsWith(subpath)) {
57                     String value = filterValue(attribute.getKey(), attribute.getValue());
58                     LOG.debug(attribute.getKey() + SEPERATOR + value);
59                 }
60             }
61         }
62     }
63
64     public void printProperties(Properties props) {
65         if (LOG.isDebugEnabled()) {
66             try {
67                 for (Entry<Object, Object> property : props.entrySet()) {
68                     String keyString = (String) property.getKey();
69                     String valueString = (String) property.getValue();
70                     String value = filterValue(keyString, valueString);
71                     LOG.debug(keyString + SEPERATOR + value);
72                 }
73             } catch (Exception e) {
74                 LOG.error(COMMON_ERROR_MESSAGE, e);
75             }
76         }
77     }
78
79     public void printProperties(Properties props, String subpath) {       
80         if (LOG.isDebugEnabled()) {
81             try {
82                 for (Entry<Object, Object> property : props.entrySet()) {
83                     String keyString = (String) property.getKey();
84                     if (keyString.startsWith(subpath)) {
85                         String valueString = (String) property.getValue();
86                         String value = filterValue(keyString, valueString);
87                         LOG.debug(keyString + SEPERATOR + value);
88                     }
89                 }
90             } catch (Exception e) {
91                 LOG.error(COMMON_ERROR_MESSAGE, e);
92             }
93         }
94     }
95     
96     public void printPropertiesAlphabetically(Properties props) {
97         if (LOG.isDebugEnabled()) {
98             TreeMap<String, String> sortedMap = new TreeMap(props);
99             try {
100                 for (Entry<String, String> entry : sortedMap.entrySet()) {
101                     String value = filterValue(entry.getKey(), entry.getValue());
102                     LOG.debug(entry.getKey() + SEPERATOR + value);
103                 }
104             } catch (Exception e) {
105                 LOG.error(COMMON_ERROR_MESSAGE, e);
106             }
107         }
108     }
109
110     public void printAttributesToFile(HashMap<String, String> attributes, String fileName) {
111         try (FileOutputStream fstr = new FileOutputStream(new File(fileName));
112                 PrintStream pstr = new PrintStream(fstr, true);) {
113             pstr.println("#######################################");
114             for (Entry<String, String> entry : attributes.entrySet()) {
115                 String value = filterValue(entry.getKey(), entry.getValue());
116                 pstr.println(entry.getKey() + SEPERATOR + value);
117             }
118         } catch (Exception e) {
119             LOG.error("Cannot write context to file.", e);
120         }
121     }
122     
123 }