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 ) {
62 /* dojo is currently only available in the OPAC */
64 this.locale = dojo.config.locale;
69 this.service = service;
73 this.alertEvent = true; /* only used when isXUL is false */
75 this.type = "POST"; /* default */
77 this.cancelled = false;
79 this.setSecure(false);
80 if(isXUL()) this.setSecure(true);
82 _allrequests[this.id] = this;
87 while(i < arguments.length) {
88 var object = js2JSON(arguments[i++]);
89 this.params += "¶m=" + encodeURIComponent(object);
92 if(!this.params) { this.params = ""; }
94 this.param_string = "service=" + service + "&method=" + method + this.params;
95 if (this.locale != null) {
96 this.param_string = this.param_string + "&locale=" + this.locale;
98 if( this.buildXMLRequest() == null ) alert("Browser is not supported!");
103 RemoteRequest.prototype.timeout = function(t) {
106 req.timeoutFunc = setTimeout(
108 if( req && req.xmlhttp ) {
109 req.cancelled = true;
111 if( req.abtCallback ) {
112 req.abtCallback(req);
120 RemoteRequest.prototype.abortCallback = function(func) {
121 this.abtCallback = func;
124 RemoteRequest.prototype.event = function(evt) {
125 if( arguments.length > 0 )
130 RemoteRequest.prototype.abort = function() {
132 /* this has to come before abort() or IE will puke on you */
133 this.xmlhttp.onreadystatechange = function(){};
134 this.xmlhttp.abort();
138 /* constructs our XMLHTTPRequest object */
139 RemoteRequest.prototype.buildXMLRequest = function() {
140 this.xmlhttp = buildXMLRequest();
145 function buildXMLRequest() {
147 return new XMLHttpRequest();
150 return new ActiveXObject("Msxml2.XMLHTTP");
153 return new ActiveXObject("Microsoft.XMLHTTP");
155 alert("NEEDS NEWER JAVASCRIPT for XMLHTTPRequest()");
163 function _remoteRequestCallback(id) {
165 var object = _allrequests[id];
166 if(object.cancelled) return;
168 if( object.xmlhttp.readyState == 4 ) {
171 object.duration = new Date().getTime() - object.sendTime;
172 dump('request ' + object.id + ': duration = ' + object.duration + ' ms\n');
176 object.callback(object);
180 destroyRequest(object);
187 /* define the callback we use when this request has received
189 RemoteRequest.prototype.setCompleteCallback = function(callback) {
190 if(this.cancelled) return;
191 this.callback = callback;
193 this.xmlhttp.onreadystatechange = function() { _remoteRequestCallback(id); }
197 /* http by default. This makes it https. *ONLY works when
198 embedded in a XUL app. */
199 RemoteRequest.prototype.setSecure = function(bool) {
203 RemoteRequest.prototype.send = function(blocking) {
205 if(this.cancelled) return;
207 /* determine the xmlhttp server dynamically */
208 var url = location.protocol + "//" + location.host + "/" + XML_HTTP_GATEWAY;
211 if( XML_HTTP_SERVER )
212 url = 'http://'+XML_HTTP_SERVER+'/'+XML_HTTP_GATEWAY;
214 if( url.match(/^http:/) &&
215 (this.secure || location.href.match(/^https:/)) ) {
216 netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
217 url = url.replace(/^http:/, 'https:');
222 if( this.type == 'GET' ) url += "?" + this.param_string;
226 //if( isXUL() ) dump('request URL = ' + url + '?' + this.param_string + '\n');
230 if(blocking) this.xmlhttp.open(this.type, url, false);
231 else this.xmlhttp.open(this.type, url, true);
234 alert("Fatal error opening XMLHTTPRequest for URL:\n" + url + '\n' + E);
238 if( this.type == 'POST' ) {
239 data = this.param_string;
240 this.xmlhttp.setRequestHeader('Content-Type',
241 'application/x-www-form-urlencoded');
246 try { auth = cookieManager.read(COOKIE_SES) } catch(ee) {}
247 if( isXUL() ) auth = fetchXULStash().session.key;
249 this.xmlhttp.setRequestHeader('X-OILS-Authtoken', auth);
253 if(data && data.match(/param=undefined/)) {
254 /* we get a bogus param .. replace with NULL */
255 try{dump('!+! UNDEFINED PARAM IN QUERY: ' + this.service + ' : ' + this.method+'\n');}catch(r){}
256 data = data.replace(/param=undefined/g,'param=null');
260 this.sendTime = new Date().getTime();
261 try{ this.xmlhttp.send( data ); } catch(e){}
266 /* returns the actual response text from the request */
267 RemoteRequest.prototype.getText = function() {
268 return this.xmlhttp.responseText;
271 RemoteRequest.prototype.isReady = function() {
272 return this.xmlhttp.readyState == 4;
276 /* returns the JSON->js result object */
277 RemoteRequest.prototype.getResultObject = function() {
279 if(this.cancelled) return null;
280 if(!this.xmlhttp) return null;
288 dump(this.url + '?' + this.param_string + '\n' +
289 'Returned with \n\tstatus = ' + this.xmlhttp.status +
290 '\n\tpayload= ' + this.xmlhttp.responseText + '\n');
295 status = this.xmlhttp.status;
296 if( status != 200 ) failed = true;
297 } catch(e) { failed = true; }
300 if(!status) status = '<unknown>';
301 try{dump('! NETWORK FAILURE. HTTP STATUS = ' +status+'\n'+this.param_string+'\n');}catch(e){}
303 throw new NetworkFailure(status, this.param_string);
307 var text = this.xmlhttp.responseText;
309 //try{if(getDebug()) _debug('response: ' + text + '\n')}catch(e){}
311 if(text == "" || text == " " || text == null) {
312 try { dump('dbg: Request returned no text!\n'); } catch(E) {}
314 throw new NetworkFailure(status, this.param_string);
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;
361 function fetchXULStash() {
364 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
365 var __OILS = new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
366 var data_cache = new __OILS( );
367 return data_cache.wrappedJSObject.OpenILS.prototype.data;
370 _debug('Error in OpenILS.data._debug_stash(): ' + js2JSON(E) );