]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - org.argeo.slc.webapp/src/main/webapp/argeo-ria-src/class/org/argeo/ria/remote/JmsClient.js
git-svn-id: https://svn.argeo.org/slc/trunk@2380 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc
[gpl/argeo-slc.git] / org.argeo.slc.webapp / src / main / webapp / argeo-ria-src / class / org / argeo / ria / remote / JmsClient.js
index bcf6e3a6d90d3e46fd431e84ed25b295c584760d..63711e74b724420a9a454e8ea0e333d6826e0230 100644 (file)
@@ -1,3 +1,7 @@
+/**\r
+ * A standard client for sending/receiving JMS message.\r
+ * It is based on ActiveMQ Ajax implementation.\r
+ */\r
 qx.Class.define("org.argeo.ria.remote.JmsClient", {\r
 \r
        type : "singleton",\r
@@ -8,7 +12,7 @@ qx.Class.define("org.argeo.ria.remote.JmsClient", {
        },\r
        members : {\r
                // The URI of the MessageListenerServlet\r
-               uri : '../amqMessage',          \r
+               uri : '../amq',         \r
 \r
                // Polling. Set to true (default) if waiting poll for messages is needed\r
                poll : true,\r
@@ -20,9 +24,17 @@ qx.Class.define("org.argeo.ria.remote.JmsClient", {
                _pollDelay : 0,\r
 \r
                _first : true,\r
+               /**\r
+                * Trigger at each poll event.\r
+                * @param first {Boolean} Whether it is the first event to be triggered. \r
+                */\r
                _pollEvent : function(first) {},\r
                _handlers : new Array(),\r
 \r
+               /**\r
+                * Parses the XML response to a message POST.\r
+                * @param response {qx.io.remote.Response} The query response\r
+                */\r
                _messageHandler : function(response) {\r
                        var doc = response.getContent();                        \r
                        var messages = org.argeo.ria.util.Element.selectNodes(doc, "//response");\r
@@ -34,6 +46,10 @@ qx.Class.define("org.argeo.ria.remote.JmsClient", {
                        }\r
                },\r
                \r
+               /**\r
+                * Parses the empty response of a poll GET query.\r
+                * @param response {qx.io.remote.Response} The query response\r
+                */\r
                _pollHandler : function(response) {\r
                        try {\r
                                this._messageHandler(response);\r
@@ -49,6 +65,10 @@ qx.Class.define("org.argeo.ria.remote.JmsClient", {
                                this._sendPoll();\r
                },\r
 \r
+               /**\r
+                * Send a poll query : GET query and no paramter at all. \r
+                * @param request {qx.io.remote.Request} A request object\r
+                */\r
                _sendPoll : function(request) {\r
                        if(this.interrupt) return;\r
                        var request = new qx.io.remote.Request(this.uri, "GET", "application/xml");\r
@@ -57,9 +77,13 @@ qx.Class.define("org.argeo.ria.remote.JmsClient", {
                        request.send();\r
                },\r
 \r
-               // Add a function that gets called on every poll response, after all received\r
-               // messages have been handled.  The poll handler is past a boolean that indicates\r
-               // if this is the first poll for the page.\r
+               /**\r
+                * Add a function that gets called on every poll response, after all received\r
+                * messages have been handled.  The poll handler is past a boolean that indicates\r
+                * if this is the first poll for the page.\r
+                * \r
+                * @param func {Function} The handler to be called. \r
+                */\r
                addPollHandler : function(func) {\r
                        var old = this._pollEvent;\r
                        this._pollEvent = function(first) {\r
@@ -68,38 +92,86 @@ qx.Class.define("org.argeo.ria.remote.JmsClient", {
                        }\r
                },\r
 \r
-               // Send a JMS message to a destination (eg topic://MY.TOPIC).  Message should be xml or encoded\r
-               // xml content.\r
+               /**\r
+                * Send a JMS message to a destination (eg topic://MY.TOPIC).  \r
+                * Message should be xml or encoded xml content.\r
+                * \r
+                * @param destination {String} The topic destination\r
+                * @param message {String} XML encoded message\r
+                * @param properties {Map} A map of additional parameters to add to the query.\r
+                */\r
                sendMessage : function(destination, message, properties) {\r
                        this._sendMessage(destination, message, 'send', properties);\r
                },\r
 \r
-               // Listen on a channel or topic.   handler must be a function taking a message arguement\r
+               /**\r
+                * Listen on a channel or topic.   handler must be a function taking a message arguement\r
+                * @param id {String} A unique identifier for this handler\r
+                * @param destination {String} The topic to listen to (topic://MY.TOPIC) \r
+                * @param handler {Function} The handler to trigger when receiving a message \r
+                * @param context {Object} An object to bind on the handler.\r
+                */\r
                addListener : function(id, destination, handler, context) {\r
                        this._handlers[id] = qx.lang.Function.bind(handler, context);\r
                        this._sendMessage(destination, id, 'listen');\r
                },\r
 \r
-               // remove Listener from channel or topic.\r
+               /**\r
+                * Remove Listener from channel or topic.\r
+                * @param id {String} identifier of the handler to remove.\r
+                * @param destination {String} The topic to listen to (topic://MY.TOPIC) \r
+                */ \r
                removeListener : function(id, destination) {\r
                        this._handlers[id] = null;\r
                        this._sendMessage(destination, id, 'unlisten');\r
                },\r
-\r
+               \r
+               /**\r
+                * Send a message of a given type.\r
+                * @param destination {String} The topic to listen to (topic://MY.TOPIC) \r
+                * @param message {String} XML encoded message\r
+                * @param type {String} The JMS-Type of message (listen, unlisten, send).\r
+                * @param properties {Map} A map of additional parameters to add to the query.\r
+                */\r
                _sendMessage : function(destination, message, type, properties) {\r
                        var req = new qx.io.remote.Request(this.uri, "POST", "text/plain");\r
-                       req.setParameter("destination", destination);\r
-                       req.setParameter("message", message);\r
-                       req.setParameter("type", type);\r
-                       if(properties){\r
-                               for(var key in properties){\r
-                                       req.setParameter(key, properties[key]);\r
-                               }\r
-                       }\r
+                       if(!properties) properties = {};                        \r
+                       properties["destination"] = destination;\r
+                       properties["message"] = message;\r
+                       properties["type"] = type;\r
+               var vParametersList = [];\r
+       \r
+               for (var vId in properties)\r
+               {\r
+                 var value = properties[vId];\r
+                 if (value instanceof Array)\r
+                 {\r
+                   for (var i=0; i<value.length; i++)\r
+                   {\r
+                     vParametersList.push(encodeURIComponent(vId) +\r
+                                          "=" +\r
+                                          encodeURIComponent(value[i]));\r
+                   }\r
+                 }\r
+                 else\r
+                 {\r
+                   vParametersList.push(encodeURIComponent(vId) +\r
+                                        "=" +\r
+                                        encodeURIComponent(value));\r
+                 }\r
+               }       \r
+               if (vParametersList.length > 0)\r
+               {\r
+                 req.setData(vParametersList.join("&"));\r
+               }\r
+                       \r
                        //req.addListener("completed", this.endBatch, this);\r
                        req.send();\r
                },\r
 \r
+               /**\r
+                * Starts a poll on the JMS server.\r
+                */\r
                startPolling : function() {\r
                        if (this.poll){\r
                                this.interrupt = false;\r
@@ -110,6 +182,9 @@ qx.Class.define("org.argeo.ria.remote.JmsClient", {
                        }\r
                },\r
                \r
+               /**\r
+                * Stops polling the JMS server.\r
+                */\r
                stopPolling : function(){\r
                        this.interrupt = true;\r
                }\r