1
2 function connect(sessionName, password) {
3 doStopUpdates = false;
4 var req = "<xmlBlaster>\n" +
5 " <connect>\n" +
6 " <qos>\n" +
7 " <securityService type='htpasswd' version='1.0'><![CDATA[\n" +
8 " <user>michele</user>\n" +
9 " <passwd>secret</passwd>\n" +
10 " ]]></securityService>\n" +
11 " <session name='michele/1' />\n" +
12 " <queue relating='connection'><address pingInterval='0' retries='-1' delay='2000' /></queue>\n" +
13 " <queue relating='callback' maxEntries='1000' maxEntriesCache='2000'><callback pingInterval='0' retries='-1' delay='1000' /></queue>\n" +
14 " </qos>\n" +
15 " </connect>\n" +
16 "</xmlBlaster>\n";
17
18 var parameters = "ActionType=xmlScript";
19 parameters += "&xmlScriptPlain=" + encodeURIComponent(req);
20 processUpdates(url, parameters, false);
21 }
22
23 function subscribeAndStartListen(topic) {
24 req = "<xmlBlaster>\n" +
25 " <subscribe><key oid='" + topic + "'></key><qos><persistent>false</persistent></qos></subscribe>\n" +
26 "</xmlBlaster>\n";
27
28 parameters = "ActionType=xmlScript";
29 parameters += "&xmlScriptPlain=" + encodeURIComponent(req);
30 processUpdates(url, parameters, false);
31
32 parameters = "ActionType=updatePollBlocking&onlyContent=true";
33 processUpdates(url, parameters, true);
34 }
35
36 function stopUpdates(topic) {
37 var req = "<xmlBlaster>\n" +
38 " <unSubscribe><key oid='" + topic + "'></key></unSubscribe>\n" +
39 " <wait delay='200' />\n" +
40 "</xmlBlaster>\n";
41
42 var parameters = "ActionType=xmlScript";
43 parameters += "&xmlScriptPlain=" + encodeURIComponent(req);
44 processUpdates(url, parameters, false);
45 doStopUpdates = true;
46 }
47
48 function processContent(content) {
49 if (content == null)
50 return;
51 var name = content.tagName;
52 if (name == 'modifyAttribute') {
53 var id = content.getAttribute('id');
54 var attrName = content.getAttribute('name');
55 var attrNewValue = content.getAttribute('value');
56 var el = otherWindow.document.getElementById(id);
57 if (el != null) {
58 el.setAttribute(attrName, attrNewValue);
59 }
60 else {
61 alert("No id='" + id + "' found");
62 otherWindow.status = "No id='" + id + "' found";
63 }
64 }
65 //
66 // will add an element to the element specified with the id. if replace is specified,
67 // then the current element is replaced.
68 //
69 else if (name == 'addElement' || name == 'replaceElement') {
70 var id = content.getAttribute('id');
71 var replace = false;
72 if (name == "replaceElement")
73 replace = true;
74 var el = document.getElementById(id);
75 if (el != null) {
76 var allChilds = content.childNodes;
77 for (var i=0; i < allChilds.length; i++) {
78 var tmpNode = allChilds.item(i);
79 if (tmpNode.nodeType == 1) {
80 addRecursiveChilds(document, el, tmpNode, replace, null);
81 }
82 }
83 }
84 else {
85 alert("Element with id='" + id + "' not found in this document");
86 }
87 }
88 else if (name == "removeElement") {
89 // TODO implement
90 var id = content.getAttribute('id');
91 var elToDelete = document.getElementById(id);
92 if (elToDelete != null) {
93 var parent = elToDelete.parentNode;
94 if (parent != null)
95 parent.deleteChild(elToDelete);
96 }
97 }
98 else if (name == "removeAttribute") {
99 // TODO implement
100 }
101
102 else {
103 alert("Operation '" + name + "' not known");
104 otherWindow.status = "Operation '" + name + "' not known";
105 }
106 }
107
108 function updateText(responseText, status, statusTxt) {
109 alert("Response is not xml: " + responseText);
110 // updateXML(null, responseText, status, statusTxt);
111
112
113 function updateXML(responseXML, responseText, status, statusTxt) {
114 if (responseText == "<void/>")
115 return;
116 var data = responseXML.getElementsByTagName('xmlBlasterResponse');
117 if (data != null && data.length > 0) {
118 data = data[0].getElementsByTagName('update');
119 if (data != null && data.length > 0) {
120 for (var j=0; j < data.length; j++) {
121 if (data[j].nodeType == 1) {
122 var childs = data[j].childNodes;
123 if (childs != null) {
124 for (var i=0; i < childs.length; i++) {
125 var content = childs.item(i);
126 if (content.nodeType == 1)
127 processContent(content);
128 }
129 }
130 }
131 }
132 }
133 }
134 else
135 alert("response '" + responseText + "' does not start with tag 'xmlBlasterResponse'");
136 }
137
138
139 function updateError(status, statusTxt) {
140 alert("updateError: statusTxt='" + statusTxt + "'");
141 }
142
143
144 // These should not be changed by the user
145 var base = "../ajax";
146 var doStopUpdates = false;
147 var time = null;
148 var url = base;
149 var otherWindow = null;
150
151 function processOneEvent(xmlHttpC, url, parameters, doPoll) {
152 if (xmlHttpC.readyState == 4 || xmlHttpC.readyState == "complete") {
153 //alert("Connect returned: " + xmlHttpC.responseText);
154 }
155 else {
156 // alert("Connect failed: " + xmlHttpC.readyState);
157 return;
158 }
159 var status = xmlHttpC.status;
160 var statusTxt = xmlHttpC.statusTxt;
161 if (xmlHttpC.responseXML != null) {
162 updateXML(xmlHttpC.responseXML, xmlHttpC.responseText, status, statusTxt);
163 }
164 else if (xmlHttpC.responseText != null) {
165 updateText(xmlHttpC.responseText, status, statusTxt);
166 }
167 else
168 updateError(status, statusTxt);
169 if (!doStopUpdates && doPoll) {
170 var txt = "processUpdates(\'" + url + "\',\'" + parameters + "\',true)";
171 // alert(txt);
172 time = setTimeout(txt, 10);
173 }
174 }
175
176 function processUpdates(url, parameters, doPoll) {
177 if (doStopUpdates) {
178 document.getElementById('request').value = "The updates are stopped";
179 return;
180 }
181
182 var async = doPoll;
183 var xmlHttpC = GetXmlHttpObject();
184 if (async) {
185 xmlHttpC.onreadystatechange = function() {
186 // alert("Starting async");
187 processOneEvent(xmlHttpC, url, parameters, doPoll);
188 }
189 }
190
191 xmlHttpC.open("POST", url, async);
192 xmlHttpC.setRequestHeader("content-type","application/x-www-form-urlencoded;charset=UTF-8");
193 xmlHttpC.setRequestHeader("Content-length", parameters.length);
194 xmlHttpC.setRequestHeader("Connection", "close");
195 xmlHttpC.send(parameters);
196
197 if (!async) {
198 processOneEvent(xmlHttpC, url, parameters, doPoll);
199 }
200 }
201
202 function GetXmlHttpObject() {
203 var objXMLHttp = null;
204 if (window.XMLHttpRequest)
205 objXMLHttp = new XMLHttpRequest();
206 else if (window.ActiveXObject)
207 objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
208 if (objXMLHttp == null)
209 alert("Your browser does not support Ajax");
210 return objXMLHttp;
211 }
212
213
214
215 const unsigned short ELEMENT_NODE = 1
216 const unsigned short ATTRIBUTE_NODE = 2
217 const unsigned short TEXT_NODE = 3
218 const unsigned short CDATA_SECTION_NODE = 4
219 const unsigned short ENTITY_REFERENCE_NODE = 5
220 const unsigned short ENTITY_NODE = 6
221 const unsigned short PROCESSING_INSTRUCTION_NODE = 7
222 const unsigned short COMMENT_NODE = 8
223 const unsigned short DOCUMENT_NODE = 9
224 const unsigned short DOCUMENT_TYPE_NODE = 10
225 const unsigned short DOCUMENT_FRAGMENT_NODE = 11
226 const unsigned short NOTATION_NODE = 12
227 */
228
229 function addRecursiveChilds(doc, father, node, replace, contentAsText) {
230 if (node == null || father == null || doc == null)
231 return contentAsText;
232 // make a copy of the node
233 var child = null;
234 if (node.nodeType == 1) { // element
235 child = doc.createElement(node.tagName);
236 if (contentAsText != null)
237 contentAsText += "<" + child.tagName + " ";
238 // add the attributes since they are not detected as attribute nodes
239 var attributes = node.attributes;
240 for (var i=0; i < attributes.length; i++) {
241 var attr = attributes.item(i);
242 child.setAttribute(attr.name, attr.value);
243 if (contentAsText != null)
244 contentAsText += attr.name + "='" + attr.value + "' ";
245 }
246 if (contentAsText != null)
247 contentAsText += ">\n";
248 }
249 else if (node.nodeType == 2) { // attribute
250 child = doc.createAttribute(node.nodeName);
251 child.value = node.nodeValue;
252 alert(node.nodeName + "='" + child.value + "'");
253 }
254 else if (node.nodeType == 3) {
255 child = doc.createTextNode(node.data);
256 if (contentAsText != null)
257 contentAsText += node.data;
258 }
259 else { // not implemented (do not add it)
260 if (contentAsText != null)
261 contentAsText = contentAsText + "</" + child.tagName + ">\n";
262 return contentAsText;
263 }
264 if (node.hasChildNodes()) {
265 // add all childs recursively
266 var allChilds = node.childNodes;
267 var tmp = "";
268 for (var i=0; i < allChilds.length; i++) {
269 tmp += addRecursiveChilds(doc, child, allChilds.item(i), false, contentAsText);
270 }
271 if (contentAsText != null)
272 contentAsText += tmp;
273 }
274 if (!replace)
275 father.appendChild(child);
276 else {
277 var grandParent = father.parentNode;
278 if (grandParent != null) {
279 dump("The father and the child " + father + " " + child);
280 grandParent.replaceChild(child, father);
281 }
282 else
283 alert("The grandparent is null");
284 }
285 if (contentAsText != null)
286 contentAsText = contentAsText + "</" + child.tagName + ">\n";
287 return contentAsText;
288 }
syntax highlighted by Code2HTML, v. 0.9.1