1 var XML_HTTP_GATEWAY = "gateway";
2 var XML_HTTP_SERVER = "";
3 var XML_HTTP_MAX_TRIES = 3;
6 function isXUL() { try { if(IAMXUL) return true;}catch(e){return false;}; }
8 /* some communication exceptions */
9 function EX(message) { this.init(message); }
10 EX.prototype.init = function(message) { this.message = message; }
11 EX.prototype.toString = function() { return "\n *** Exception Occured \n" + this.message; }
12 EXCommunication.prototype = new EX();
13 EXCommunication.prototype.constructor = EXCommunication;
14 EXCommunication.baseClass = EX.prototype.constructor;
15 function EXCommunication(message) { this.classname="EXCommunication"; this.init("EXCommunication: " + message); }
16 /* ------------------------------------------------ */
19 var _allrequests = {};
21 function cleanRemoteRequests() {
22 for( var i in _allrequests )
23 destroyRequest(_allrequests[i]);
26 function abortAllRequests() {
27 for( var i in _allrequests ) {
28 var r = _allrequests[i];
36 function destroyRequest(r) {
40 r.xmlhttp.onreadystatechange = function(){};
46 _allrequests[r.id] = null;
49 /* ----------------------------------------------------------------------- */
51 function RemoteRequest( service, method ) {
54 this.service = service;
59 this.alertEvent = true; /* only used when isXUL is false */
61 this.type = "POST"; /* default */
62 this.id = service + method + Math.random();
63 this.cancelled = false;
65 _allrequests[this.id] = this;
70 while(i < arguments.length) {
71 var object = js2JSON(arguments[i++]);
72 this.params += "¶m=" + encodeURIComponent(object);
75 if(!this.params) { this.params = ""; }
76 this.param_string = "service=" + service + "&method=" + method + this.params;
77 if( this.buildXMLRequest() == null ) alert("Browser is not supported!");
82 RemoteRequest.prototype.timeout = function(t) {
85 req.timeoutFunc = setTimeout(
87 if( req && req.xmlhttp ) {
90 if( req.abtCallback ) {
99 RemoteRequest.prototype.abortCallback = function(func) {
100 this.abtCallback = func;
103 RemoteRequest.prototype.event = function(evt) {
104 if( arguments.length > 0 )
109 RemoteRequest.prototype.abort = function() {
111 /* this has to come before abort() or IE will puke on you */
112 this.xmlhttp.onreadystatechange = function(){};
113 this.xmlhttp.abort();
117 /* constructs our XMLHTTPRequest object */
118 RemoteRequest.prototype.buildXMLRequest = function() {
119 this.xmlhttp = buildXMLRequest();
124 function buildXMLRequest() {
127 x = new ActiveXObject("Msxml2.XMLHTTP");
130 x = new ActiveXObject("Microsoft.XMLHTTP");
136 if (!x && typeof XMLHttpRequest!='undefined') x = new XMLHttpRequest();
139 alert("NEEDS NEWER JAVASCRIPT for XMLHTTPRequest()");
147 function _remoteRequestCallback(id) {
149 var object = _allrequests[id];
150 if(object.cancelled) return;
152 if( object.xmlhttp.readyState == 4 ) {
154 object.callback(object);
157 /* if we receive a communication error, retry the request up
158 to XML_HTTP_MAX_TRIES attempts */
159 if( E && E.classname == "EXCommunication" ) {
161 //try { dump('Communication Error: ' + E ); } catch(e){}
162 alert('Debug: Communication Error: ' + E );
164 if(object.sendCount >= XML_HTTP_MAX_TRIES ) {
165 if(isXUL()) throw object;
166 else alert("Arrrgghh, Matey! Error communicating:\n" + E + "\n" + object.param_string);
168 object.buildXMLRequest();
175 destroyRequest(object);
182 /* define the callback we use when this request has received
184 RemoteRequest.prototype.setCompleteCallback = function(callback) {
185 if(this.cancelled) return;
186 this.callback = callback;
188 this.xmlhttp.onreadystatechange = function() { _remoteRequestCallback(id); }
192 /* http by default. This makes it https. *ONLY works when
193 embedded in a XUL app. */
194 RemoteRequest.prototype.setSecure = function(bool) {
198 RemoteRequest.prototype.send = function(blocking) {
200 if(this.cancelled) return;
202 /* determine the xmlhttp server dynamically */
203 var url = location.protocol + "//" + location.host + "/" + XML_HTTP_GATEWAY;
205 if(isXUL() && XML_HTTP_SERVER) {
206 if(this.secure || url.match(/^https:/) )
207 url = "https://" + XML_HTTP_SERVER + "/" + XML_HTTP_GATEWAY;
209 url = "http://" + XML_HTTP_SERVER + "/" + XML_HTTP_GATEWAY;
212 /* this number could be a lot higher.. */
213 var whole = url + this.param_string + 1;
217 /* IE will cache this, so don't use it if IE.
218 useful for debugging */
219 if( whole.length < 256 ) this.type = 'GET';
223 if( this.type == 'GET' ) url += "?" + this.param_string;
227 if(blocking) this.xmlhttp.open(this.type, url, false);
228 else this.xmlhttp.open(this.type, url, true);
231 alert("Fatal error opening XMLHTTPRequest for URL:\n" + url + '\n');
236 if( this.type == 'POST' ) {
237 data = this.param_string;
238 this.xmlhttp.setRequestHeader('Content-Type',
239 'application/x-www-form-urlencoded');
242 try{ this.xmlhttp.send( data ); } catch(e){}
248 /* returns the actual response text from the request */
249 RemoteRequest.prototype.getText = function() {
250 return this.xmlhttp.responseText;
253 RemoteRequest.prototype.isReady = function() {
254 return this.xmlhttp.readyState == 4;
258 /* returns the JSON->js result object */
259 RemoteRequest.prototype.getResultObject = function() {
261 if(this.cancelled) return null;
262 if(!this.xmlhttp) return null;
266 var text = this.xmlhttp.responseText;
267 if(text == "" || text == " " || text == null) null;
269 var obj = JSON2js(text);
270 if(!obj) return null;
272 if( obj.status != 200 ) {
274 var str = 'A server error occurred. Debug information follows: ' +
275 '\ncode = ' + obj.status +
276 '\ndebug: ' + obj.debug +
277 '\npayload: ' + js2JSON(obj.payload);
289 var payload = obj.payload;
290 if(!payload || payload.length == 0) return null;
291 payload = (payload.length == 1) ? payload[0] : payload;
294 if( checkILSEvent(payload) ) {
296 if( this.alertEvent )
297 alertILSEvent(payload);
305 /* adds a new parameter to the request */
306 RemoteRequest.prototype.addParam = function(param) {
307 var string = encodeURIComponent(js2JSON(param));
308 this.param_string += "¶m=" + string;