diff options
author | 2019-05-02 16:41:24 -0700 | |
---|---|---|
committer | 2019-05-03 18:54:13 +0000 | |
commit | daa8dd085c91a3e8aed459aabd01708209e7383c (patch) | |
tree | d8f7580dc6d866be97d5d60069cff1494aa9954e /appc-config/appc-config-generator/provider/src/main | |
parent | 8b0f5d3c68f91970e4fa5f735ac7527628e28567 (diff) |
Add method to escape json strings
All backslash escape characters are being stripped out of strings.
They need to be added back to strings containing json data.
Change-Id: Ic8d9ba95da904b96f65598752133971f8c324694
Signed-off-by: Patrick Brady <patrick.brady@att.com>
Issue-ID: APPC-1576
Diffstat (limited to 'appc-config/appc-config-generator/provider/src/main')
2 files changed, 44 insertions, 0 deletions
diff --git a/appc-config/appc-config-generator/provider/src/main/java/org/onap/sdnc/config/generator/convert/ConvertNode.java b/appc-config/appc-config-generator/provider/src/main/java/org/onap/sdnc/config/generator/convert/ConvertNode.java index 69c5dffc4..f9f00e4f0 100644 --- a/appc-config/appc-config-generator/provider/src/main/java/org/onap/sdnc/config/generator/convert/ConvertNode.java +++ b/appc-config/appc-config-generator/provider/src/main/java/org/onap/sdnc/config/generator/convert/ConvertNode.java @@ -60,6 +60,12 @@ public class ConvertNode implements SvcLogicJavaPlugin { if (StringUtils.isNotBlank(jsonData)) { if (StringUtils.isNotBlank(isEscaped) && "Y".equalsIgnoreCase(isEscaped)) { jsonData = StringEscapeUtils.unescapeJavaScript(jsonData); + //We need to re-escape any json data that might be contained in Strings + try { + jsonData = JSONTool.escapeInternalJson(jsonData); + } catch (Exception e) { + log.error("Exception during JSONTool.escapeInternalJson",e); + } } List<String> blockKeys = new ArrayList<>(); diff --git a/appc-config/appc-config-generator/provider/src/main/java/org/onap/sdnc/config/generator/tool/JSONTool.java b/appc-config/appc-config-generator/provider/src/main/java/org/onap/sdnc/config/generator/tool/JSONTool.java index 20aeacb3b..3c49ec6a9 100644 --- a/appc-config/appc-config-generator/provider/src/main/java/org/onap/sdnc/config/generator/tool/JSONTool.java +++ b/appc-config/appc-config-generator/provider/src/main/java/org/onap/sdnc/config/generator/tool/JSONTool.java @@ -84,6 +84,44 @@ public class JSONTool { return mm; } + //Finds json values which themselves contain String representations of json + //data and adds escape characters to the quotes. Example: + //Input: { "key1": "{"internalKey1": "internalValue1"}" } + //Output: { "key1": "{\"internalKey1\": \"internalValue1\"}" } + public static String escapeInternalJson(String jsonString) throws JSONException + { + StringBuilder sb = new StringBuilder(); + char lastChar = 0; + boolean inJson = false; + for(char c : jsonString.toCharArray()) { + if(c == '{' && lastChar == '"') { + inJson = true; + } + if(inJson) { + if(c == '"' && lastChar != '\\') { + sb.append("\\\""); + } else { + sb.append(c); + } + if(c == '}' && lastChar == '"') { + inJson = false; + } + } else { + sb.append(c); + } + if(!Character.isWhitespace(c)) { + lastChar = c; + } + } + if(inJson == true) { + //We reached the end of the string, but the internal string containing + //the json data to escape never ended. + throw new JSONException("End of json data reached, but end of internal" + + "json string never reached."); + } + return sb.toString(); + } + private static void tryAddBlockKeys(List<String> blockKeys, Map<String, String> mm, String key, Object o) { if (blockKeys != null && blockKeys.contains(key) && o != null) { mm.put("block_" + key, o.toString()); |