1 var XML_HTTP_GATEWAY = "gateway";
2 var XML_HTTP_SERVER = "spacely.georgialibraries.org:8080";
3 var XML_HTTP_MAX_TRIES = 3;
6 function isXUL() { return IAMXUL; }
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 destroyRequest(r) {
28 r.xmlhttp.onreadystatechange = function(){};
32 _allrequests[r.id] = null;
35 /* ----------------------------------------------------------------------- */
37 function RemoteRequest( service, method ) {
40 this.service = service;
46 this.type = "POST"; /* default */
47 this.id = service + method + Math.random();
48 this.cancelled = false;
50 _allrequests[this.id] = this;
55 while(i < arguments.length) {
56 var object = js2JSON(arguments[i++]);
57 this.params += "¶m=" + encodeURIComponent(object);
60 if(!this.params) { this.params = ""; }
61 this.param_string = "service=" + service + "&method=" + method + this.params;
62 if( this.buildXMLRequest() == null ) alert("Browser is not supported!");
65 /* constructs our XMLHTTPRequest object */
66 RemoteRequest.prototype.buildXMLRequest = function() {
70 x = new ActiveXObject("Msxml2.XMLHTTP");
73 x = new ActiveXObject("Microsoft.XMLHTTP");
79 if (!x && typeof XMLHttpRequest!='undefined') x = new XMLHttpRequest();
82 alert("NEEDS NEWER JAVASCRIPT for XMLHTTPRequest()");
91 function _remoteRequestCallback(id) {
93 var object = _allrequests[id];
94 if(object.cancelled) return;
96 if( object.xmlhttp.readyState == 4 ) {
98 object.callback(object);
101 /* if we receive a communication error, retry the request up
102 to XML_HTTP_MAX_TRIES attempts */
103 if( E && E.classname == "EXCommunication" ) {
105 if(object.sendCount >= XML_HTTP_MAX_TRIES ) {
106 if(isXUL()) throw object;
107 else alert("Arrrgghh, Matey! Error communicating:\n" + E + "\n" + object.param_string);
109 object.buildXMLRequest();
116 destroyRequest(object);
123 /* define the callback we use when this request has received
125 RemoteRequest.prototype.setCompleteCallback = function(callback) {
126 if(this.cancelled) return;
127 this.callback = callback;
129 this.xmlhttp.onreadystatechange = function() { _remoteRequestCallback(id); }
133 /* http by default. This makes it https. *ONLY works when
134 embedded in a XUL app. */
135 RemoteRequest.prototype.setSecure = function(bool) {
139 RemoteRequest.prototype.send = function(blocking) {
141 if(this.cancelled) return;
143 /* determine the xmlhttp server dynamically */
144 var url = location.protocol + "//" + location.host + "/" + XML_HTTP_GATEWAY;
148 url = "https://" + XML_HTTP_SERVER + "/" + XML_HTTP_GATEWAY;
150 url = "http://" + XML_HTTP_SERVER + "/" + XML_HTTP_GATEWAY;
155 if( this.type == 'GET' ) {
156 url += "?" + this.param_string;
160 this.xmlhttp.open(this.type, url, false);
162 this.xmlhttp.open(this.type, url, true);
166 if( this.type == 'POST' ) {
167 data = this.param_string;
168 this.xmlhttp.setRequestHeader('Content-Type',
169 'application/x-www-form-urlencoded');
172 this.xmlhttp.send( data );
177 /* returns the actual response text from the request */
178 RemoteRequest.prototype.getText = function() {
179 return this.xmlhttp.responseText;
182 RemoteRequest.prototype.isReady = function() {
183 return this.xmlhttp.readyState == 4;
187 /* returns the JSON->js result object */
188 RemoteRequest.prototype.getResultObject = function() {
189 if(this.cancelled) return null;
191 var text = this.xmlhttp.responseText;
192 var obj = JSON2js(text);
194 if(obj == null) return null;
195 if(obj.is_err) throw new EXCommunication(obj.err_msg);
196 if( obj[0] != null && obj[1] == null ) obj = obj[0];
198 /* these are user level exceptions from the server code */
199 if(obj.__isfieldmapper && obj.classname == "ex") {
200 if(!isXUL()) alert(obj.err_msg());
204 if(obj.__isfieldmapper && obj.classname == "perm_ex") {
205 /* the opac will go ahead and spit out the error msg */
206 if(!isXUL()) alert(obj.err_msg());
213 /* adds a new parameter to the request */
214 RemoteRequest.prototype.addParam = function(param) {
215 var string = encodeURIComponent(js2JSON(param));
216 this.param_string += "¶m=" + string;