Native Messaging WIP - docs, tweaks
authorBill Erickson <berickxx@gmail.com>
Thu, 10 Nov 2016 21:04:52 +0000 (16:04 -0500)
committerBill Erickson <berickxx@gmail.com>
Thu, 10 Nov 2016 21:04:52 +0000 (16:04 -0500)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
src/org/evergreen_ils/hatch/PrintManager.java
src/org/evergreen_ils/hatch/RequestHandler.java
src/org/evergreen_ils/hatch/TestHatch.java

index ae9709b..841ebd4 100644 (file)
@@ -114,13 +114,13 @@ public class PrintManager {
     public void print(WebEngine engine, JSONObject request) {
 
         JSONObject response = new JSONObject();
-        response.put("msgid", request.get("msgid"));
-        response.put("status", "200");
+        response.put("status", 200);
         response.put("message", "OK");
 
         try {
-            long msgid = request.getLong("msgid");
-            response.put("msgid", msgid);
+
+            response.put("clientid", request.getLong("clientid"));
+            response.put("msgid", request.getLong("msgid"));
 
             boolean showDialog = request.optBoolean("showDialog");
 
@@ -133,7 +133,7 @@ public class PrintManager {
             if (showDialog) {
                 if (!job.showPrintDialog(null)) {
                     job.endJob(); // canceled by user
-                    response.put("status", "200");
+                    response.put("status", 200);
                     response.put("message", "Print job canceled by user");
                     RequestHandler.reply(response);
                     return;
@@ -151,7 +151,7 @@ public class PrintManager {
                 + je.toString() + " : " + request.toString();
 
             logger.warning(error);
-            response.put("status", "400");
+            response.put("status", 400);
             response.put("message", error);
 
         } catch(IllegalArgumentException iae) {
@@ -160,7 +160,7 @@ public class PrintManager {
                 + iae.toString() + " : " + request.toString();
 
             logger.warning(error);
-            response.put("status", "400");
+            response.put("status", 400);
             response.put("message", error);
         }
 
index 4cce19e..eeace1b 100644 (file)
@@ -59,12 +59,10 @@ public class RequestHandler extends Thread {
     private boolean dispatchRequest(
         JSONObject request, JSONObject response) throws JSONException {
 
-        long msgid = request.getLong("msgid");
         String action = request.getString("action");
 
-        response.put("msgid", msgid);
-
-        logger.info("Received message id=" + msgid + " action=" + action);
+        logger.info("Received message id=" + 
+            response.get("msgid") + " action=" + action);
 
         // init must be called first to set the origin info
         if (action.equals("init")) {
@@ -169,12 +167,18 @@ public class RequestHandler extends Thread {
             boolean skipReply = false;
             JSONObject response = new JSONObject();
 
-            // these values are overidden as needed by the dispatch handler.
+            // Status values overidden as needed by the dispatch handler.
             response.put("status", 200); 
             response.put("message", "OK");
 
             try {
-                skipReply = dispatchRequest(io.recvMessage(), response); 
+                JSONObject request = io.recvMessage();
+
+                response.put("clientid", request.getLong("clientid"));
+                response.put("msgid", request.getLong("msgid"));
+
+                skipReply = dispatchRequest(request, response); 
+
             } catch (JSONException je) {
                 response.put("status", 400); 
                 response.put("message", "Bad Request: " + je.toString());
index 08c68c9..616c188 100644 (file)
@@ -29,11 +29,13 @@ public class TestHatch {
     }
 
     public static void doSends() {
-        int msgId = 1;
+        int msgid = 1;
+        int clientid = 1;
 
         // initialize connection to set origin info
         JSONObject obj = new JSONObject();
-        obj.put("msgid", msgId++);
+        obj.put("msgid", msgid++);
+        obj.put("clientid", clientid);
         obj.put("action", "init");
         obj.put("origin", "https://test.hatch.evergreen-ils.org");
         io.sendMessage(obj);
@@ -42,7 +44,8 @@ public class TestHatch {
 
         // get a list of stored keys
         obj = new JSONObject();
-        obj.put("msgid", msgId++);
+        obj.put("msgid", msgid++);
+        obj.put("clientid", clientid);
         obj.put("action", "keys");
         io.sendMessage(obj);
 
@@ -50,7 +53,8 @@ public class TestHatch {
 
         // store a string
         obj = new JSONObject();
-        obj.put("msgid", msgId++);
+        obj.put("msgid", msgid++);
+        obj.put("clientid", clientid);
         obj.put("action", "set");
         obj.put("key", "eg.hatch.test.key1");
         obj.put("content", "Rando content, now with cheese");
@@ -60,14 +64,16 @@ public class TestHatch {
 
         // store a value
         obj = new JSONObject();
-        obj.put("msgid", msgId++);
+        obj.put("msgid", msgid++);
+        obj.put("clientid", clientid);
         obj.put("action", "get");
         obj.put("key", "eg.hatch.test.key1");
         io.sendMessage(obj);
 
         // store an array
         obj = new JSONObject();
-        obj.put("msgid", msgId++);
+        obj.put("msgid", msgid++);
+        obj.put("clientid", clientid);
         obj.put("action", "set");
         obj.put("key", "eg.hatch.test.key2");
         JSONArray arr = new JSONArray();
@@ -79,7 +85,8 @@ public class TestHatch {
         pause();
 
         obj = new JSONObject();
-        obj.put("msgid", msgId++);
+        obj.put("msgid", msgid++);
+        obj.put("clientid", clientid);
         obj.put("action", "get");
         obj.put("key", "eg.hatch.test.key2");
         io.sendMessage(obj);
@@ -87,7 +94,8 @@ public class TestHatch {
         pause();
 
         obj = new JSONObject();
-        obj.put("msgid", msgId++);
+        obj.put("msgid", msgid++);
+        obj.put("clientid", clientid);
         obj.put("action", "keys");
         io.sendMessage(obj);
 
@@ -95,7 +103,8 @@ public class TestHatch {
 
         // get a list of printers
         obj = new JSONObject();
-        obj.put("msgid", msgId++);
+        obj.put("msgid", msgid++);
+        obj.put("clientid", clientid);
         obj.put("action", "printers");
         io.sendMessage(obj);
 
@@ -105,7 +114,8 @@ public class TestHatch {
         // Printing tests
         
         obj = new JSONObject();
-        obj.put("msgid", msgId++);
+        obj.put("msgid", msgid++);
+        obj.put("clientid", clientid);
         obj.put("action", "print");
         obj.put("contentType", "text/plain");
         obj.put("content", "Hello, World!");
@@ -115,7 +125,8 @@ public class TestHatch {
         pause();
 
         obj = new JSONObject();
-        obj.put("msgid", msgId++);
+        obj.put("msgid", msgid++);
+        obj.put("clientid", clientid);
         obj.put("action", "print");
         obj.put("contentType", "text/html");
         obj.put("content", "<html><body><b>HELLO WORLD</b><img src='" +