1 var XML_HTTP_GATEWAY = "osrf-gateway-v1";
2 var XML_HTTP_SERVER = "";
5 /* This object is thrown when network failures occur */
6 function NetworkFailure(stat, url) {
11 NetworkFailure.prototype.status = function() { return this._status; }
12 NetworkFailure.prototype.url = function() { return this._url; }
13 NetworkFailure.prototype.toString = function() {
14 return "Network Failure: status = " + this.status() +'\n'+this.url();
19 function isXUL() { try { if(IAMXUL) return true;}catch(e){return false;}; }
20 var _allrequests = {};
22 // If the legacy JSON gateway is needed by the staff client, uncomment this
25 XML_HTTP_GATEWAY = 'gateway';
29 function cleanRemoteRequests() {
30 for( var i in _allrequests )
31 destroyRequest(_allrequests[i]);
34 function abortAllRequests() {
35 for( var i in _allrequests ) {
36 var r = _allrequests[i];
44 function destroyRequest(r) {
48 r.xmlhttp.onreadystatechange = function(){};
54 _allrequests[r.id] = null;
57 /* ----------------------------------------------------------------------- */
60 function RemoteRequest( service, method ) {
63 this.service = service;
67 this.alertEvent = true; /* only used when isXUL is false */
69 this.type = "POST"; /* default */
71 this.cancelled = false;
73 this.setSecure(false);
74 if(isXUL()) this.setSecure(true);
76 _allrequests[this.id] = this;
81 while(i < arguments.length) {
82 var object = js2JSON(arguments[i++]);
83 this.params += "¶m=" + encodeURIComponent(object);
86 if(!this.params) { this.params = ""; }
87 this.param_string = "service=" + service + "&method=" + method + this.params;
88 if( this.buildXMLRequest() == null ) alert("Browser is not supported!");
93 RemoteRequest.prototype.timeout = function(t) {
96 req.timeoutFunc = setTimeout(
98 if( req && req.xmlhttp ) {
101 if( req.abtCallback ) {
102 req.abtCallback(req);
110 RemoteRequest.prototype.abortCallback = function(func) {
111 this.abtCallback = func;
114 RemoteRequest.prototype.event = function(evt) {
115 if( arguments.length > 0 )
120 RemoteRequest.prototype.abort = function() {
122 /* this has to come before abort() or IE will puke on you */
123 this.xmlhttp.onreadystatechange = function(){};
124 this.xmlhttp.abort();
128 /* constructs our XMLHTTPRequest object */
129 RemoteRequest.prototype.buildXMLRequest = function() {
130 this.xmlhttp = buildXMLRequest();
135 function buildXMLRequest() {
137 return new XMLHttpRequest();
140 return new ActiveXObject("Msxml2.XMLHTTP");
143 return new ActiveXObject("Microsoft.XMLHTTP");
145 alert("NEEDS NEWER JAVASCRIPT for XMLHTTPRequest()");
153 function _remoteRequestCallback(id) {
155 var object = _allrequests[id];
156 if(object.cancelled) return;
158 if( object.xmlhttp.readyState == 4 ) {
161 object.duration = new Date().getTime() - object.sendTime;
162 dump('request ' + object.id + ': duration = ' + object.duration + ' ms\n');
166 object.callback(object);
170 destroyRequest(object);
177 /* define the callback we use when this request has received
179 RemoteRequest.prototype.setCompleteCallback = function(callback) {
180 if(this.cancelled) return;
181 this.callback = callback;
183 this.xmlhttp.onreadystatechange = function() { _remoteRequestCallback(id); }
187 /* http by default. This makes it https. *ONLY works when
188 embedded in a XUL app. */
189 RemoteRequest.prototype.setSecure = function(bool) {
193 RemoteRequest.prototype.send = function(blocking) {
195 if(this.cancelled) return;
197 /* determine the xmlhttp server dynamically */
198 var url = location.protocol + "//" + location.host + "/" + XML_HTTP_GATEWAY;
201 if( XML_HTTP_SERVER )
202 url = 'http://'+XML_HTTP_SERVER+'/'+XML_HTTP_GATEWAY;
204 if( url.match(/^http:/) &&
205 (this.secure || location.href.match(/^https:/)) ) {
206 netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
207 url = url.replace(/^http:/, 'https:');
212 if( this.type == 'GET' ) url += "?" + this.param_string;
216 //if( isXUL() ) dump('request URL = ' + url + '?' + this.param_string + '\n');
220 if(blocking) this.xmlhttp.open(this.type, url, false);
221 else this.xmlhttp.open(this.type, url, true);
224 alert("Fatal error opening XMLHTTPRequest for URL:\n" + url + '\n' + E);
228 if( this.type == 'POST' ) {
229 data = this.param_string;
230 this.xmlhttp.setRequestHeader('Content-Type',
231 'application/x-www-form-urlencoded');
236 try { auth = cookieManager.read(COOKIE_SES) } catch(ee) {}
237 if( isXUL() ) auth = fetchXULStash().session.key;
239 this.xmlhttp.setRequestHeader('X-OILS-Authtoken', auth);
243 if(data && data.match(/param=undefined/)) {
244 /* we get a bogus param .. replace with NULL */
245 try{dump('!+! UNDEFINED PARAM IN QUERY: ' + this.service + ' : ' + this.method+'\n');}catch(r){}
246 data = data.replace(/param=undefined/g,'param=null');
250 this.sendTime = new Date().getTime();
251 try{ this.xmlhttp.send( data ); } catch(e){}
256 /* returns the actual response text from the request */
257 RemoteRequest.prototype.getText = function() {
258 return this.xmlhttp.responseText;
261 RemoteRequest.prototype.isReady = function() {
262 return this.xmlhttp.readyState == 4;
266 /* returns the JSON->js result object */
267 RemoteRequest.prototype.getResultObject = function() {
269 if(this.cancelled) return null;
270 if(!this.xmlhttp) return null;
278 dump(this.url + '?' + this.param_string + '\n' +
279 'Returned with \n\tstatus = ' + this.xmlhttp.status +
280 '\n\tpayload= ' + this.xmlhttp.responseText + '\n');
285 status = this.xmlhttp.status;
286 if( status != 200 ) failed = true;
287 } catch(e) { failed = true; }
290 if(!status) status = '<unknown>';
291 try{dump('! NETWORK FAILURE. HTTP STATUS = ' +status+'\n'+this.param_string+'\n');}catch(e){}
293 throw new NetworkFailure(status, this.param_string);
297 var text = this.xmlhttp.responseText;
299 //try{if(getDebug()) _debug('response: ' + text + '\n')}catch(e){}
301 if(text == "" || text == " " || text == null) {
302 try { dump('dbg: Request returned no text!\n'); } catch(E) {}
304 throw new NetworkFailure(status, this.param_string);
308 var obj = JSON2js(text);
309 if(!obj) return null;
311 if( obj.status != 200 ) {
313 var str = 'A server error occurred. Debug information follows: ' +
314 '\ncode = ' + obj.status +
315 '\ndebug: ' + obj.debug +
316 '\npayload: ' + js2JSON(obj.payload);
328 var payload = obj.payload;
329 if(!payload || payload.length == 0) return null;
330 payload = (payload.length == 1) ? payload[0] : payload;
333 if( checkILSEvent(payload) ) {
335 if( this.alertEvent ) {
336 alertILSEvent(payload);
345 /* adds a new parameter to the request */
346 RemoteRequest.prototype.addParam = function(param) {
347 var string = encodeURIComponent(js2JSON(param));
348 this.param_string += "¶m=" + string;
351 function fetchXULStash() {
354 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
355 var __OILS = new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
356 var data_cache = new __OILS( );
357 return data_cache.wrappedJSObject.OpenILS.prototype.data;
360 _debug('Error in OpenILS.data._debug_stash(): ' + js2JSON(E) );