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;
64 this.locale = dojo.config.locale;
68 this.alertEvent = true; /* only used when isXUL is false */
70 this.type = "POST"; /* default */
72 this.cancelled = false;
74 this.setSecure(false);
75 if(isXUL()) this.setSecure(true);
77 _allrequests[this.id] = this;
82 while(i < arguments.length) {
83 var object = js2JSON(arguments[i++]);
84 this.params += "¶m=" + encodeURIComponent(object);
87 if(!this.params) { this.params = ""; }
88 this.param_string = "service=" + service + "&locale=" + locale + "&method=" + method + this.params;
89 if( this.buildXMLRequest() == null ) alert("Browser is not supported!");
94 RemoteRequest.prototype.timeout = function(t) {
97 req.timeoutFunc = setTimeout(
99 if( req && req.xmlhttp ) {
100 req.cancelled = true;
102 if( req.abtCallback ) {
103 req.abtCallback(req);
111 RemoteRequest.prototype.abortCallback = function(func) {
112 this.abtCallback = func;
115 RemoteRequest.prototype.event = function(evt) {
116 if( arguments.length > 0 )
121 RemoteRequest.prototype.abort = function() {
123 /* this has to come before abort() or IE will puke on you */
124 this.xmlhttp.onreadystatechange = function(){};
125 this.xmlhttp.abort();
129 /* constructs our XMLHTTPRequest object */
130 RemoteRequest.prototype.buildXMLRequest = function() {
131 this.xmlhttp = buildXMLRequest();
136 function buildXMLRequest() {
138 return new XMLHttpRequest();
141 return new ActiveXObject("Msxml2.XMLHTTP");
144 return new ActiveXObject("Microsoft.XMLHTTP");
146 alert("NEEDS NEWER JAVASCRIPT for XMLHTTPRequest()");
154 function _remoteRequestCallback(id) {
156 var object = _allrequests[id];
157 if(object.cancelled) return;
159 if( object.xmlhttp.readyState == 4 ) {
162 object.duration = new Date().getTime() - object.sendTime;
163 dump('request ' + object.id + ': duration = ' + object.duration + ' ms\n');
167 object.callback(object);
171 destroyRequest(object);
178 /* define the callback we use when this request has received
180 RemoteRequest.prototype.setCompleteCallback = function(callback) {
181 if(this.cancelled) return;
182 this.callback = callback;
184 this.xmlhttp.onreadystatechange = function() { _remoteRequestCallback(id); }
188 /* http by default. This makes it https. *ONLY works when
189 embedded in a XUL app. */
190 RemoteRequest.prototype.setSecure = function(bool) {
194 RemoteRequest.prototype.send = function(blocking) {
196 if(this.cancelled) return;
198 /* determine the xmlhttp server dynamically */
199 var url = location.protocol + "//" + location.host + "/" + XML_HTTP_GATEWAY;
202 if( XML_HTTP_SERVER )
203 url = 'http://'+XML_HTTP_SERVER+'/'+XML_HTTP_GATEWAY;
205 if( url.match(/^http:/) &&
206 (this.secure || location.href.match(/^https:/)) ) {
207 netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
208 url = url.replace(/^http:/, 'https:');
213 if( this.type == 'GET' ) url += "?" + this.param_string;
217 //if( isXUL() ) dump('request URL = ' + url + '?' + this.param_string + '\n');
221 if(blocking) this.xmlhttp.open(this.type, url, false);
222 else this.xmlhttp.open(this.type, url, true);
225 alert("Fatal error opening XMLHTTPRequest for URL:\n" + url + '\n' + E);
229 if( this.type == 'POST' ) {
230 data = this.param_string;
231 this.xmlhttp.setRequestHeader('Content-Type',
232 'application/x-www-form-urlencoded');
237 try { auth = cookieManager.read(COOKIE_SES) } catch(ee) {}
238 if( isXUL() ) auth = fetchXULStash().session.key;
240 this.xmlhttp.setRequestHeader('X-OILS-Authtoken', auth);
244 if(data && data.match(/param=undefined/)) {
245 /* we get a bogus param .. replace with NULL */
246 try{dump('!+! UNDEFINED PARAM IN QUERY: ' + this.service + ' : ' + this.method+'\n');}catch(r){}
247 data = data.replace(/param=undefined/g,'param=null');
251 this.sendTime = new Date().getTime();
252 try{ this.xmlhttp.send( data ); } catch(e){}
257 /* returns the actual response text from the request */
258 RemoteRequest.prototype.getText = function() {
259 return this.xmlhttp.responseText;
262 RemoteRequest.prototype.isReady = function() {
263 return this.xmlhttp.readyState == 4;
267 /* returns the JSON->js result object */
268 RemoteRequest.prototype.getResultObject = function() {
270 if(this.cancelled) return null;
271 if(!this.xmlhttp) return null;
279 dump(this.url + '?' + this.param_string + '\n' +
280 'Returned with \n\tstatus = ' + this.xmlhttp.status +
281 '\n\tpayload= ' + this.xmlhttp.responseText + '\n');
286 status = this.xmlhttp.status;
287 if( status != 200 ) failed = true;
288 } catch(e) { failed = true; }
291 if(!status) status = '<unknown>';
292 try{dump('! NETWORK FAILURE. HTTP STATUS = ' +status+'\n'+this.param_string+'\n');}catch(e){}
294 throw new NetworkFailure(status, this.param_string);
298 var text = this.xmlhttp.responseText;
300 //try{if(getDebug()) _debug('response: ' + text + '\n')}catch(e){}
302 if(text == "" || text == " " || text == null) {
303 try { dump('dbg: Request returned no text!\n'); } catch(E) {}
305 throw new NetworkFailure(status, this.param_string);
309 var obj = JSON2js(text);
310 if(!obj) return null;
312 if( obj.status != 200 ) {
314 var str = 'A server error occurred. Debug information follows: ' +
315 '\ncode = ' + obj.status +
316 '\ndebug: ' + obj.debug +
317 '\npayload: ' + js2JSON(obj.payload);
329 var payload = obj.payload;
330 if(!payload || payload.length == 0) return null;
331 payload = (payload.length == 1) ? payload[0] : payload;
334 if( checkILSEvent(payload) ) {
336 if( this.alertEvent ) {
337 alertILSEvent(payload);
346 /* adds a new parameter to the request */
347 RemoteRequest.prototype.addParam = function(param) {
348 var string = encodeURIComponent(js2JSON(param));
349 this.param_string += "¶m=" + string;
352 function fetchXULStash() {
355 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
356 var __OILS = new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
357 var data_cache = new __OILS( );
358 return data_cache.wrappedJSObject.OpenILS.prototype.data;
361 _debug('Error in OpenILS.data._debug_stash(): ' + js2JSON(E) );