From c3754c3921ec4f1786bbd36dbfbc9190d6900d1c Mon Sep 17 00:00:00 2001 From: Pierre Villard Date: Sat, 26 Aug 2017 17:09:15 +0200 Subject: [PATCH] NIFI-4320 - Escape quotes for JSON output in QueryCassandra Signed-off-by: Matthew Burgess This closes #2114 --- .../org/apache/nifi/processors/cassandra/QueryCassandra.java | 3 +++ .../nifi/processors/cassandra/CassandraQueryTestUtil.java | 2 +- .../apache/nifi/processors/cassandra/QueryCassandraTest.java | 4 ++-- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/nifi-nar-bundles/nifi-cassandra-bundle/nifi-cassandra-processors/src/main/java/org/apache/nifi/processors/cassandra/QueryCassandra.java b/nifi-nar-bundles/nifi-cassandra-bundle/nifi-cassandra-processors/src/main/java/org/apache/nifi/processors/cassandra/QueryCassandra.java index 52eb9e020c..b007c7075d 100644 --- a/nifi-nar-bundles/nifi-cassandra-bundle/nifi-cassandra-processors/src/main/java/org/apache/nifi/processors/cassandra/QueryCassandra.java +++ b/nifi-nar-bundles/nifi-cassandra-bundle/nifi-cassandra-processors/src/main/java/org/apache/nifi/processors/cassandra/QueryCassandra.java @@ -33,6 +33,7 @@ import org.apache.avro.generic.GenericData; import org.apache.avro.generic.GenericDatumWriter; import org.apache.avro.generic.GenericRecord; import org.apache.avro.io.DatumWriter; +import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringUtils; import org.apache.nifi.annotation.behavior.EventDriven; import org.apache.nifi.annotation.behavior.InputRequirement; @@ -497,6 +498,8 @@ public class QueryCassandra extends AbstractCassandraProcessor { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ"); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); return "\"" + dateFormat.format((Date) value) + "\""; + } else if (value instanceof String) { + return "\"" + StringEscapeUtils.escapeJson((String) value) + "\""; } else { return "\"" + value.toString() + "\""; } diff --git a/nifi-nar-bundles/nifi-cassandra-bundle/nifi-cassandra-processors/src/test/java/org/apache/nifi/processors/cassandra/CassandraQueryTestUtil.java b/nifi-nar-bundles/nifi-cassandra-bundle/nifi-cassandra-processors/src/test/java/org/apache/nifi/processors/cassandra/CassandraQueryTestUtil.java index dbe2e1ebba..49a6760698 100644 --- a/nifi-nar-bundles/nifi-cassandra-bundle/nifi-cassandra-processors/src/test/java/org/apache/nifi/processors/cassandra/CassandraQueryTestUtil.java +++ b/nifi-nar-bundles/nifi-cassandra-bundle/nifi-cassandra-processors/src/test/java/org/apache/nifi/processors/cassandra/CassandraQueryTestUtil.java @@ -86,7 +86,7 @@ public class CassandraQueryTestUtil { createRow("user1", "Joe", "Smith", Sets.newHashSet("jsmith@notareal.com"), Arrays.asList("New York, NY", "Santa Clara, CA"), new HashMap() {{ - put(aMonthPrior, "Set my alarm for a month from now"); + put(aMonthPrior, "Set my alarm \"for\" a month from now"); }}, false, 1.0f, 2.0), createRow("user2", "Mary", "Jones", Sets.newHashSet("mjones@notareal.com"), Collections.singletonList("Orlando, FL"), diff --git a/nifi-nar-bundles/nifi-cassandra-bundle/nifi-cassandra-processors/src/test/java/org/apache/nifi/processors/cassandra/QueryCassandraTest.java b/nifi-nar-bundles/nifi-cassandra-bundle/nifi-cassandra-processors/src/test/java/org/apache/nifi/processors/cassandra/QueryCassandraTest.java index 83110c3ab6..07c1df0db3 100644 --- a/nifi-nar-bundles/nifi-cassandra-bundle/nifi-cassandra-processors/src/test/java/org/apache/nifi/processors/cassandra/QueryCassandraTest.java +++ b/nifi-nar-bundles/nifi-cassandra-bundle/nifi-cassandra-processors/src/test/java/org/apache/nifi/processors/cassandra/QueryCassandraTest.java @@ -140,7 +140,7 @@ public class QueryCassandraTest { assertEquals("One file should be transferred to success", 1, files.size()); assertEquals("{\"results\":[{\"user_id\":\"user1\",\"first_name\":\"Joe\",\"last_name\":\"Smith\"," + "\"emails\":[\"jsmith@notareal.com\"],\"top_places\":[\"New York, NY\",\"Santa Clara, CA\"]," - + "\"todo\":{\"2016-01-03 05:00:00+0000\":\"Set my alarm for a month from now\"}," + + "\"todo\":{\"2016-01-03 05:00:00+0000\":\"Set my alarm \\\"for\\\" a month from now\"}," + "\"registered\":\"false\",\"scale\":1.0,\"metric\":2.0}," + "{\"user_id\":\"user2\",\"first_name\":\"Mary\",\"last_name\":\"Jones\"," + "\"emails\":[\"mjones@notareal.com\"],\"top_places\":[\"Orlando, FL\"]," @@ -177,7 +177,7 @@ public class QueryCassandraTest { assertEquals("One file should be transferred to success", 1, files.size()); assertEquals("{\"results\":[{\"user_id\":\"user1\",\"first_name\":\"Joe\",\"last_name\":\"Smith\"," + "\"emails\":[\"jsmith@notareal.com\"],\"top_places\":[\"New York, NY\",\"Santa Clara, CA\"]," - + "\"todo\":{\"2016-01-03 05:00:00+0000\":\"Set my alarm for a month from now\"}," + + "\"todo\":{\"2016-01-03 05:00:00+0000\":\"Set my alarm \\\"for\\\" a month from now\"}," + "\"registered\":\"false\",\"scale\":1.0,\"metric\":2.0}," + "{\"user_id\":\"user2\",\"first_name\":\"Mary\",\"last_name\":\"Jones\"," + "\"emails\":[\"mjones@notareal.com\"],\"top_places\":[\"Orlando, FL\"],"