changeset 250:2d924d7904a7

Add optional arguments to gateway commands client Reviewed-by: jerboaa, neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-September/024839.html
author Chris Lessard <clessard@redhat.com>
date Wed, 06 Sep 2017 13:11:55 -0400
parents f638608b889a
children ed634d83e742
files services/commands/src/main/resources/agent.html services/commands/src/main/resources/agent2.html services/commands/src/main/resources/index.html
diffstat 3 files changed, 61 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/services/commands/src/main/resources/agent.html	Wed Sep 06 10:18:26 2017 -0400
+++ b/services/commands/src/main/resources/agent.html	Wed Sep 06 13:11:55 2017 -0400
@@ -70,10 +70,24 @@
             };
 
             CmdChan.socket.onmessage = function (message) {
-                Console.log('Got: ' + message.data);
+                var data = JSON.parse(message.data);
+                var payload = data.payload;
+
+                var requestArguments = {}
+                for (var key in payload) {
+                    // Check for an actual key, and not a prototype value
+                    if (payload.hasOwnProperty(key)) {
+                        requestArguments[escapeHtml(key)] = escapeHtml(payload[key]);
+                    }
+                }
+
+                // Print message to screen
+                data.payload = requestArguments;
+                Console.log('Got: ' + JSON.stringify(data));
+
                 // parse message
                 Msg.fromRequest(message.data);
-                
+
                 // send the always-ok reply
                 CmdChan.socket.send(Response.asJson(Msg.sequence, 'OK'));
                 Console.log('Info: OK reply sent.');
@@ -104,7 +118,6 @@
 
         CmdChan.initialize();
 
-
         document.addEventListener("DOMContentLoaded", function() {
             // Remove elements with "noscript" class - <noscript> is not allowed in XHTML
             var noscripts = document.getElementsByClassName("noscript");
@@ -113,6 +126,10 @@
             }
         }, false);
 
+        var escapeHtml = function(unsafeStr) {
+            return unsafeStr.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;");
+        }
+
     //]]>
     </script>
 </head>
--- a/services/commands/src/main/resources/agent2.html	Wed Sep 06 10:18:26 2017 -0400
+++ b/services/commands/src/main/resources/agent2.html	Wed Sep 06 13:11:55 2017 -0400
@@ -70,7 +70,21 @@
             };
 
             CmdChan.socket.onmessage = function (message) {
-                Console.log('Got: ' + message.data);
+                var data = JSON.parse(message.data);
+                var payload = data.payload;
+
+                var requestArguments = {}
+                for (var key in payload) {
+                    // Check for an actual key, and not a prototype value
+                    if (payload.hasOwnProperty(key)) {
+                        requestArguments[escapeHtml(key)] = escapeHtml(payload[key]);
+                    }
+                }
+
+                // Print message to screen
+                data.payload = requestArguments;
+                Console.log('Got: ' + JSON.stringify(data));
+
                 // parse message
                 Msg.fromRequest(message.data);
                 
@@ -104,7 +118,6 @@
 
         CmdChan.initialize();
 
-
         document.addEventListener("DOMContentLoaded", function() {
             // Remove elements with "noscript" class - <noscript> is not allowed in XHTML
             var noscripts = document.getElementsByClassName("noscript");
@@ -113,6 +126,10 @@
             }
         }, false);
 
+        var escapeHtml = function(unsafeStr) {
+            return unsafeStr.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;");
+        }
+
     //]]>
     </script>
 </head>
--- a/services/commands/src/main/resources/index.html	Wed Sep 06 10:18:26 2017 -0400
+++ b/services/commands/src/main/resources/index.html	Wed Sep 06 13:11:55 2017 -0400
@@ -76,23 +76,36 @@
         CmdChan.sendMessage = (function() {
             var action = document.getElementById('cmd-chan').value;
             var agent = document.getElementById('agent').value;
+            var argumentKeys = document.getElementsByClassName('optionArgument');
+            var argumentValues = document.getElementsByClassName('optionArgumentVal');
+
             if (action != '' && agent != '') {
                 var clientRequest = {};
                 var url = null;
                 clientRequest.type = 2;
                 clientRequest.payload = {};
+
+                // Parse command arguments
+                for (var i = 0; i < argumentValues.length; i++) {
+                    var key = argumentKeys[i].value.trim();
+                    var val = argumentValues[i].value.trim();
+
+                    if (val != '' && key != '') {
+                        clientRequest.payload[key] = val;
+                    }
+                }
+
                 if (window.location.protocol == 'http:') {
                   url = 'ws://' + window.location.host;
                 } else {
                   url = 'wss://' + window.location.host;
-                } 
+                }
                 url = url + '/commands/v1/actions/' + action + '/systems/ignored_system/agents/' + agent + '/jvms/ignored_jvm/sequence/' + CmdChan.sequence++;
                 CmdChan.connect( url, clientRequest );
                 document.getElementById('cmd-chan').value = '';
             }
         });
 
-
         var Console = {};
 
         Console.log = (function(message) {
@@ -126,8 +139,13 @@
     Thermostat Commands: HTML Client
     </p>
     <p>
-        <input type="text" placeholder="type the action and press enter in order to send a message to the agent below." id="cmd-chan" />
-        <input type="text" placeholder="The agent to send messages to. Eg. 'testAgent' or 'otherAgent'" id="agent" />
+        <input type="text" placeholder="Type the action and press enter in order to send a message to the agent below." id="cmd-chan" />
+        <input type="text" placeholder="The agent to send messages to. Eg. 'testAgent' or 'otherAgent'" id="agent" /> <br>
+        <h3>Arguments: (blank fields will be omitted)</h3><br>
+        <input type="text" placeholder="Argument 1" class="optionArgument"/><input type="text" placeholder="Value 1" class="optionArgumentVal"/><br>
+        <input type="text" placeholder="Argument 2" class="optionArgument"/><input type="text" placeholder="Value 2" class="optionArgumentVal"/><br>
+        <input type="text" placeholder="Argument 3" class="optionArgument"/><input type="text" placeholder="Value 3" class="optionArgumentVal"/><br>
+
     </p>
     <div id="console-container">
         <div id="console"/>