From daa8dd085c91a3e8aed459aabd01708209e7383c Mon Sep 17 00:00:00 2001 From: Patrick Brady Date: Thu, 2 May 2019 16:41:24 -0700 Subject: 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 Issue-ID: APPC-1576 --- .../sdnc/config/generator/convert/ConvertNode.java | 6 ++++ .../onap/sdnc/config/generator/tool/JSONTool.java | 38 ++++++++++++++++++++ .../sdnc/config/generator/tool/TestJSONTool.java | 42 ++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 appc-config/appc-config-generator/provider/src/test/java/org/onap/sdnc/config/generator/tool/TestJSONTool.java 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 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 blockKeys, Map 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 index 000000000..fcc0c7c27 --- /dev/null +++ b/appc-config/appc-config-generator/provider/src/test/java/org/onap/sdnc/config/generator/tool/TestJSONTool.java @@ -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(); + } + } + +} -- cgit 1.2.3-korg