Add method to escape json strings 78/86878/2
authorPatrick Brady <patrick.brady@att.com>
Thu, 2 May 2019 23:41:24 +0000 (16:41 -0700)
committerPatrick Brady <patrick.brady@att.com>
Thu, 2 May 2019 23:48:27 +0000 (16:48 -0700)
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

appc-config/appc-config-generator/provider/src/main/java/org/onap/sdnc/config/generator/convert/ConvertNode.java
appc-config/appc-config-generator/provider/src/main/java/org/onap/sdnc/config/generator/tool/JSONTool.java
appc-config/appc-config-generator/provider/src/test/java/org/onap/sdnc/config/generator/tool/TestJSONTool.java [new file with mode: 0644]

index 69c5dff..f9f00e4 100644 (file)
@@ -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<>();
index 20aeacb..3c49ec6 100644 (file)
@@ -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());
diff --git a/appc-config/appc-config-generator/provider/src/test/java/org/onap/sdnc/config/generator/tool/TestJSONTool.java b/appc-config/appc-config-generator/provider/src/test/java/org/onap/sdnc/config/generator/tool/TestJSONTool.java
new file mode 100644 (file)
index 0000000..fcc0c7c
--- /dev/null
@@ -0,0 +1,42 @@
+package org.onap.sdnc.config.generator.tool;
+
+import org.codehaus.jettison.json.JSONException;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestJSONTool {
+
+    @Test
+    public void testEscapeInternalJson() {
+        String testData = "{\"test1\":\"value1\",\"test2\":\"{\"key1\":\"value\"}\"}";
+        String expectedOutput = "{\"test1\":\"value1\",\"test2\":\"{\\\"key1\\\":\\\"value\\\"}\"}";
+        try {
+            Assert.assertEquals(expectedOutput, JSONTool.escapeInternalJson(testData));
+        } catch (JSONException e) {
+            Assert.fail();
+        }
+    }
+
+    @Test
+    public void testEscapeInternalJson_alreadyEscaped() {
+        String testData = "{\"test1\":\"value1\",\"test2\":\"{\\\"key1\\\":\\\"value\\\"}\"}";
+        String expectedOutput = "{\"test1\":\"value1\",\"test2\":\"{\\\"key1\\\":\\\"value\\\"}\"}";
+        try {
+            Assert.assertEquals(expectedOutput, JSONTool.escapeInternalJson(testData));
+        } catch (JSONException e) {
+            Assert.fail();
+        }
+    }
+
+    @Test
+    public void testEscapeInternalJson_withNewLines() {
+        String testData = "{\"test1\":\"value1\",\"test2\":\"\n{\"key1\":\"value\"\n}\"}";
+        String expectedOutput = "{\"test1\":\"value1\",\"test2\":\"\n{\\\"key1\\\":\\\"value\\\"\n}\"}";
+        try {
+            Assert.assertEquals(expectedOutput, JSONTool.escapeInternalJson(testData));
+        } catch (JSONException e) {
+            Assert.fail();
+        }
+    }
+
+}