Repository: Vantiq/vantiq-extension-sources (language: python)

Issue:
Camel Component: Json output puts strings in quotes



Discussion / hints:

Fix patch:
diff --git a/camelComponent/src/main/java/io/vantiq/extsrc/camel/VantiqConsumer.java b/camelComponent/src/main/java/io/vantiq/extsrc/camel/VantiqConsumer.java
index afeddf8a..6145dc10 100644
--- a/camelComponent/src/main/java/io/vantiq/extsrc/camel/VantiqConsumer.java
+++ b/camelComponent/src/main/java/io/vantiq/extsrc/camel/VantiqConsumer.java
@@ -32,7 +32,6 @@
 @Slf4j
 public class VantiqConsumer extends DefaultConsumer {
     private final VantiqEndpoint endpoint;
-    private ExtensionWebSocketClient vantiqClient;
 
     private ExecutorService executorService;
     
@@ -47,7 +46,7 @@ public VantiqConsumer(VantiqEndpoint endpoint, Processor processor) {
     protected void doStart() throws Exception {
         super.doStart();
         endpoint.startup();
-        vantiqClient = endpoint.getVantiqClient();
+        ExtensionWebSocketClient vantiqClient = endpoint.getVantiqClient();
         vantiqClient.setPublishHandler(publishHandler);
         vantiqClient.setQueryHandler(queryHandler);
 
@@ -64,9 +63,6 @@ protected void doStop() throws Exception {
 
         // shutdown the thread pool gracefully
         getEndpoint().getCamelContext().getExecutorServiceManager().shutdownGraceful(executorService);
-        if (vantiqClient != null) {
-            vantiqClient.close();
-        }
     }
     
     /**
@@ -118,10 +114,15 @@ private void processMessage(Object msg, ExchangePattern pattern) {
                 msgBody = camelBody;
                 log.debug("Structured message -- hdrs: {}, message: {}", camelHdrs, camelBody);
             }
+            Object output = msgBody;
             if (endpoint.isConsumerOutputJson()) {
                 // Convert to JSON output
-               JsonNode jnode =  mapper.convertValue(msgBody, JsonNode.class);
-               msgBody = jnode.toPrettyString();
+                JsonNode jnode =  mapper.convertValue(msgBody, JsonNode.class);
+                output = jnode;
+                if (log.isDebugEnabled()) {
+                    Object resValueDbg = jnode.isTextual() ? jnode.asText() : jnode;
+                    log.debug("ConsumerOutputJson: msgBody out: {}", resValueDbg);
+                }
             }
     
             // Create an exchange to move our message along.  In the publish case,
@@ -130,7 +131,7 @@ private void processMessage(Object msg, ExchangePattern pattern) {
             // result, so we'll deal with those separately.
             final Exchange exchange = createExchange(false);
             exchange.setPattern(pattern);
-            exchange.getIn().setBody(msgBody);
+            exchange.getIn().setBody(output);
             if (camelHdrs != null) {
                 exchange.getIn().setHeaders(camelHdrs);
             }


Test patch:
diff --git a/camelComponent/src/test/java/io/vantiq/extsrc/camel/VantiqComponentTest.java b/camelComponent/src/test/java/io/vantiq/extsrc/camel/VantiqComponentTest.java
index 95d4e9e0..52a35f06 100644
--- a/camelComponent/src/test/java/io/vantiq/extsrc/camel/VantiqComponentTest.java
+++ b/camelComponent/src/test/java/io/vantiq/extsrc/camel/VantiqComponentTest.java
@@ -17,7 +17,10 @@
 
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.BaseJsonNode;
 import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.fasterxml.jackson.databind.node.TextNode;
 import io.vantiq.extjsdk.ExtensionServiceMessage;
 import io.vantiq.extjsdk.FalseClient;
 import io.vantiq.extjsdk.FalseWebSocket;
@@ -414,20 +417,22 @@ public void testVantiqConsumerOutputJson() throws Exception {
             assertNotNull("Null exchange body",  exchangeBody);
             // Verify that we got JSON -- which will manifest here as a String, but we'll verify that we can convert it.
             assertTrue("Vantiq Consumer Output wrong type: " + exchangeBody.getClass().getName(),
-                exchangeBody instanceof String);
+                exchangeBody instanceof BaseJsonNode);
             msgsReceived.addAndGet(1);
             Object msg;
             try {
-                msg = mapper.readValue((String) exchangeBody, Map.class);
-            } catch (JsonProcessingException e) {
-                // Maybe we asked for the wrong type
+                msg = mapper.convertValue(exchangeBody, Map.class); // readValue((ObjectNode) exchangeBody, Map.class);
+            } catch (Exception trySomethingNew) {
                 try {
-                    msg = mapper.readValue((String) exchangeBody, String.class);
-                } catch (JsonProcessingException ex) {
+                    msg = mapper.convertValue(exchangeBody, String.class);
+                } catch (Exception tryAgain) {
                     try {
-                        msg = mapper.readValue((String) exchangeBody, List.class);
-                    } catch (JsonProcessingException exc) {
-                        throw new RuntimeException(exc);
+                        msg = mapper.convertValue(exchangeBody, List.class);
+                    } catch (Exception e) {
+                        log.error("Trapped exception: ", e);
+                        msg = e;
+                        assertNull("Trapped exception: " + e.getMessage(), e);
+                        // about it
                     }
                 }
             }
@@ -515,19 +520,20 @@ public void testVantiqConsumerOutputJsonStructured() throws Exception {
             assertNotNull("Null exchange body",  exchangeBody);
             // Verify that we got JSON -- which will manifest here as a String, but we'll verify that we can convert it.
             assertTrue("Vantiq Consumer Output wrong type: " + exchangeBody.getClass().getName(),
-                       exchangeBody instanceof String);
+                       exchangeBody instanceof BaseJsonNode);
             msgsReceived.addAndGet(1);
             Object msg;
             try {
-                msg = mapper.readValue((String) exchangeBody, Map.class);
-            } catch (JsonProcessingException e) {
+                msg = mapper.convertValue(exchangeBody, Map.class); //readValue((String) exchangeBody, Map.class);
+            } catch (Exception e) {
                 // Maybe we asked for the wrong type
                 try {
-                    msg = mapper.readValue((String) exchangeBody, String.class);
-                } catch (JsonProcessingException ex) {
+                    msg = mapper.convertValue(exchangeBody, String.class);
+                } catch (Exception ex) {
                     try {
-                        msg = mapper.readValue((String) exchangeBody, List.class);
-                    } catch (JsonProcessingException exc) {
+                        msg = mapper.convertValue(exchangeBody, List.class);
+                        // readValue((String) exchangeBody,List.class);
+                    } catch (Exception exc) {
                         throw new RuntimeException(exc);
                     }
                 }
@@ -690,11 +696,11 @@ public void testVantiqConsumerOutputJsonQuery() throws Exception {
             Object exchBody = exchange.getIn().getBody();
             assertNotNull("Exchange boddy is null", exchBody);
             assertTrue("Exchange body wrong type: " + exchBody.getClass().getName(),
-                       exchange.getIn().getBody() instanceof String);
+                       exchange.getIn().getBody() instanceof ObjectNode);
             Map<?,?> msg;
             try {
-                msg = mapper.readValue((String) exchBody, Map.class);
-            } catch (JsonProcessingException e) {
+                msg = mapper.convertValue(exchBody, Map.class);
+            } catch (Exception e) {
                 throw new RuntimeException(e);
             }
             assertNotNull("Deserialized JSON message is null", msg);