From 402e6365b8d8ab1efef0b55d51d6793dc3bacf9c Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Thu, 10 Nov 2016 16:04:52 -0500 Subject: [PATCH] Native Messaging WIP - docs, tweaks Signed-off-by: Bill Erickson --- src/org/evergreen_ils/hatch/PrintManager.java | 14 ++++---- .../evergreen_ils/hatch/RequestHandler.java | 16 +++++---- src/org/evergreen_ils/hatch/TestHatch.java | 33 ++++++++++++------- 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/src/org/evergreen_ils/hatch/PrintManager.java b/src/org/evergreen_ils/hatch/PrintManager.java index ae9709bd86..841ebd4cdd 100644 --- a/src/org/evergreen_ils/hatch/PrintManager.java +++ b/src/org/evergreen_ils/hatch/PrintManager.java @@ -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); } diff --git a/src/org/evergreen_ils/hatch/RequestHandler.java b/src/org/evergreen_ils/hatch/RequestHandler.java index 4cce19ee66..eeace1b918 100644 --- a/src/org/evergreen_ils/hatch/RequestHandler.java +++ b/src/org/evergreen_ils/hatch/RequestHandler.java @@ -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()); diff --git a/src/org/evergreen_ils/hatch/TestHatch.java b/src/org/evergreen_ils/hatch/TestHatch.java index 08c68c9064..616c188e2d 100644 --- a/src/org/evergreen_ils/hatch/TestHatch.java +++ b/src/org/evergreen_ils/hatch/TestHatch.java @@ -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", "HELLO WORLD