// Invocation Sending a json String and removal of key elements from the json String
String logString = json;
logString = maskedLog(logString);
logger.info("Response Data : "+logString);
public static String maskedLog(String logString){
for (String s : sensitiveKeys){
try {
logString= removeField(s, logString);
} catch (IOException e) {
logger.severe("Exception in Util maskedLog ");
}
}
return logString;
}
public static String removeField(String key, String jsonString) throws NullPointerException, IOException {
JSONArtifact artifact = (JSONArtifact) JSON.parse(jsonString);
removeField(key, artifact);
return artifact.toString();
}
private static void removeField(String key, Object json) {
if (json instanceof JSONObject) {
JSONObject o = (JSONObject) json;
o.remove(key);
Collection values = o.values();
traverseCollection(key, values);
} else if (json instanceof JSONArray) {
JSONArray array = (JSONArray) json;
traverseCollection(key, array);
}
}
private static void traverseCollection(String key, Collection collection) {
for (Object o : collection) {
removeField(key, o);
}
}
Comments
Post a Comment