1 var XML_HTTP_GATEWAY = "gateway";
2 var XML_HTTP_SERVER = "";
3 var XML_HTTP_MAX_TRIES = 3;
7 /* This object is thrown when network failures occur */
8 function NetworkFailure(stat, url) {
13 NetworkFailure.prototype.status = function() { return this._status; }
14 NetworkFailure.prototype.url = function() { return this._url; }
15 NetworkFailure.prototype.toString = function() {
16 return "Network Failure: status = " + this.status() +'\n'+this.url();
22 function isXUL() { try { if(IAMXUL) return true;}catch(e){return false;}; }
24 /* some communication exceptions */
25 function EX(message) { this.init(message); }
26 EX.prototype.init = function(message) { this.message = message; }
27 EX.prototype.toString = function() { return "\n *** Exception Occured \n" + this.message; }
28 EXCommunication.prototype = new EX();
29 EXCommunication.prototype.constructor = EXCommunication;
30 EXCommunication.baseClass = EX.prototype.constructor;
31 function EXCommunication(message) { this.classname="EXCommunication"; this.init("EXCommunication: " + message); }
32 /* ------------------------------------------------ */
35 var _allrequests = {};
37 function cleanRemoteRequests() {
38 for( var i in _allrequests )
39 destroyRequest(_allrequests[i]);
42 function abortAllRequests() {
43 for( var i in _allrequests ) {
44 var r = _allrequests[i];
52 function destroyRequest(r) {
56 r.xmlhttp.onreadystatechange = function(){};
62 _allrequests[r.id] = null;
65 /* ----------------------------------------------------------------------- */
67 function RemoteRequest( service, method ) {
70 this.service = service;
75 this.alertEvent = true; /* only used when isXUL is false */
77 this.type = "POST"; /* default */
78 this.id = service + method + Math.random();
79 this.cancelled = false;
81 this.setSecure(false);
82 if(isXUL()) this.setSecure(true);
84 _allrequests[this.id] = this;
89 while(i < arguments.length) {
90 var object = js2JSON(arguments[i++]);
91 this.params += "¶m=" + encodeURIComponent(object);
94 if(!this.params) { this.params = ""; }
95 this.param_string = "service=" + service + "&method=" + method + this.params;
96 if( this.buildXMLRequest() == null ) alert("Browser is not supported!");
101 RemoteRequest.prototype.timeout = function(t) {
104 req.timeoutFunc = setTimeout(
106 if( req && req.xmlhttp ) {
107 req.cancelled = true;
109 if( req.abtCallback ) {
110 req.abtCallback(req);
118 RemoteRequest.prototype.abortCallback = function(func) {
119 this.abtCallback = func;
122 RemoteRequest.prototype.event = function(evt) {
123 if( arguments.length > 0 )
128 RemoteRequest.prototype.abort = function() {
130 /* this has to come before abort() or IE will puke on you */
131 this.xmlhttp.onreadystatechange = function(){};
132 this.xmlhttp.abort();
136 /* constructs our XMLHTTPRequest object */
137 RemoteRequest.prototype.buildXMLRequest = function() {
138 this.xmlhttp = buildXMLRequest();
143 function buildXMLRequest() {
146 x = new ActiveXObject("Msxml2.XMLHTTP");
149 x = new ActiveXObject("Microsoft.XMLHTTP");
155 if (!x && typeof XMLHttpRequest!='undefined') x = new XMLHttpRequest();
158 alert("NEEDS NEWER JAVASCRIPT for XMLHTTPRequest()");
166 function _remoteRequestCallback(id) {
168 var object = _allrequests[id];
169 if(object.cancelled) return;
171 if( object.xmlhttp.readyState == 4 ) {
173 object.callback(object);
176 /* if we receive a communication error, retry the request up
177 to XML_HTTP_MAX_TRIES attempts */
178 if( E && E.classname == "EXCommunication" ) {
180 //try { dump('Communication Error: ' + E ); } catch(e){}
181 alert('Debug: Communication Error: ' + E );
183 if(object.sendCount >= XML_HTTP_MAX_TRIES ) {
184 if(isXUL()) throw object;
185 else alert("Arrrgghh, Matey! Error communicating:\n" + E + "\n" + object.param_string);
187 object.buildXMLRequest();
194 destroyRequest(object);
201 /* define the callback we use when this request has received
203 RemoteRequest.prototype.setCompleteCallback = function(callback) {
204 if(this.cancelled) return;
205 this.callback = callback;
207 this.xmlhttp.onreadystatechange = function() { _remoteRequestCallback(id); }
211 /* http by default. This makes it https. *ONLY works when
212 embedded in a XUL app. */
213 RemoteRequest.prototype.setSecure = function(bool) {
217 RemoteRequest.prototype.send = function(blocking) {
219 if(this.cancelled) return;
223 /* determine the xmlhttp server dynamically */
224 var url = location.protocol + "//" + location.host + "/" + XML_HTTP_GATEWAY;
227 if( XML_HTTP_SERVER )
228 url = 'http://'+XML_HTTP_SERVER+'/'+XML_HTTP_GATEWAY;
230 if( url.match(/^http:/) &&
231 (this.secure || location.href.match(/^https:/)) ) {
232 netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
233 url = url.replace(/^http:/, 'https:');
238 if( this.type == 'GET' ) url += "?" + this.param_string;
240 if(isXUL() && this.secure ) dump('SECURE = true\n');
246 if(blocking) this.xmlhttp.open(this.type, url, false);
247 else this.xmlhttp.open(this.type, url, true);
250 alert("Fatal error opening XMLHTTPRequest for URL:\n" + url + '\n' + E);
254 if( this.type == 'POST' ) {
255 data = this.param_string;
256 this.xmlhttp.setRequestHeader('Content-Type',
257 'application/x-www-form-urlencoded');
261 var ses = cookieManager.read(COOKIE_SES);
262 if(!ses && isXUL()) ses = xulG['authtoken'];
263 if( ses ) this.xmlhttp.setRequestHeader('X-OILS-Authtoken', ses);
267 try{ this.xmlhttp.send( data ); } catch(e){}
273 /* returns the actual response text from the request */
274 RemoteRequest.prototype.getText = function() {
275 return this.xmlhttp.responseText;
278 RemoteRequest.prototype.isReady = function() {
279 return this.xmlhttp.readyState == 4;
283 /* returns the JSON->js result object */
284 RemoteRequest.prototype.getResultObject = function() {
286 if(this.cancelled) return null;
287 if(!this.xmlhttp) return null;
295 dump(this.url + '?' + this.param_string + '\n' +
296 'Returned with \n\tstatus = ' + this.xmlhttp.status +
297 '\n\tpayload= ' + this.xmlhttp.responseText + '\n');
302 status = this.xmlhttp.status;
303 if( status != 200 ) failed = true;
304 } catch(e) { failed = true; }
307 if(!status) status = '<unknown>';
308 try{dump('! NETWORK FAILURE. HTTP STATUS = ' +status+'\n');}catch(e){}
310 throw new NetworkFailure(status, this.param_string);
314 var text = this.xmlhttp.responseText;
316 if(text == "" || text == " " || text == null) return null;
318 var obj = JSON2js(text);
319 if(!obj) return null;
321 if( obj.status != 200 ) {
323 var str = 'A server error occurred. Debug information follows: ' +
324 '\ncode = ' + obj.status +
325 '\ndebug: ' + obj.debug +
326 '\npayload: ' + js2JSON(obj.payload);
338 var payload = obj.payload;
339 if(!payload || payload.length == 0) return null;
340 payload = (payload.length == 1) ? payload[0] : payload;
343 if( checkILSEvent(payload) ) {
345 if( this.alertEvent ) {
346 alertILSEvent(payload);
355 /* adds a new parameter to the request */
356 RemoteRequest.prototype.addParam = function(param) {
357 var string = encodeURIComponent(js2JSON(param));
358 this.param_string += "¶m=" + string;