aboutsummaryrefslogtreecommitdiffstats
path: root/appc-config
diff options
context:
space:
mode:
Diffstat (limited to 'appc-config')
-rw-r--r--appc-config/appc-config-generator/provider/src/main/java/org/onap/sdnc/config/generator/convert/ConvertNode.java6
-rw-r--r--appc-config/appc-config-generator/provider/src/main/java/org/onap/sdnc/config/generator/tool/JSONTool.java38
-rw-r--r--appc-config/appc-config-generator/provider/src/test/java/org/onap/sdnc/config/generator/tool/TestJSONTool.java42
3 files changed, 86 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());
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();
+ }
+ }
+
+}