1 var XML_HTTP_GATEWAY = "gateway";
2 var XML_HTTP_SERVER = "gapines.org";
3 var XML_HTTP_MAX_TRIES = 3;
6 function isXUL() { return IAMXUL; }
10 function cleanRemoteRequests() {
11 for( var i in _allrequests )
12 destroyRequest(_allrequests[i]);
15 function destroyRequest(r) {
17 r.xmlhttp.onreadystatechange = function(){};
21 _allrequests[r.id] = null;
24 /* ----------------------------------------------------------------------- */
26 function RemoteRequest( service, method ) {
29 this.service = service;
35 this.type = "POST"; /* default */
36 this.id = service + method + Math.random();
37 this.cancelled = false;
39 _allrequests[this.id] = this;
44 while(i < arguments.length) {
45 var object = js2JSON(arguments[i++]);
46 this.params += "¶m=" + encodeURIComponent(object);
49 if(!this.params) { this.params = ""; }
50 this.param_string = "service=" + service + "&method=" + method + this.params;
51 if( this.buildXMLRequest() == null ) alert("Browser is not supported!");
54 /* constructs our XMLHTTPRequest object */
55 RemoteRequest.prototype.buildXMLRequest = function() {
59 x = new ActiveXObject("Msxml2.XMLHTTP");
62 x = new ActiveXObject("Microsoft.XMLHTTP");
68 if (!x && typeof XMLHttpRequest!='undefined') x = new XMLHttpRequest();
71 alert("NEEDS NEWER JAVASCRIPT for XMLHTTPRequest()");
80 function _remoteRequestCallback(id) {
82 var object = _allrequests[id];
83 if(object.cancelled) return;
85 if( object.xmlhttp.readyState == 4 ) {
87 object.callback(object);
90 /* if we receive a communication error, retry the request up
91 to XML_HTTP_MAX_TRIES attempts */
92 if( instanceOf(E, EXCommunication) ) {
94 if(object.sendCount >= XML_HTTP_MAX_TRIES ) {
95 if(isXUL()) throw object;
96 else alert("Arrrgghh, Matey! Error communicating:\n" + E + "\n" + object.param_string);
98 object.buildXMLRequest();
105 destroyRequest(object);
112 /* define the callback we use when this request has received
114 RemoteRequest.prototype.setCompleteCallback = function(callback) {
115 if(this.cancelled) return;
116 this.callback = callback;
118 this.xmlhttp.onreadystatechange = function() { _remoteRequestCallback(id); }
122 /* http by default. This makes it https. *ONLY works when
123 embedded in a XUL app. */
124 RemoteRequest.prototype.setSecure = function(bool) {
128 RemoteRequest.prototype.send = function(blocking) {
130 if(this.cancelled) return;
132 /* determine the xmlhttp server dynamically */
133 var url = location.protocol + "//" + location.host + "/" + XML_HTTP_GATEWAY;
137 url = "https://" + XML_HTTP_SERVER + "/" + XML_HTTP_GATEWAY;
139 url = "http://" + XML_HTTP_SERVER + "/" + XML_HTTP_GATEWAY;
144 if( this.type == 'GET' ) {
145 url += "?" + this.param_string;
149 this.xmlhttp.open(this.type, url, false);
151 this.xmlhttp.open(this.type, url, true);
155 if( this.type == 'POST' ) {
156 data = this.param_string;
157 this.xmlhttp.setRequestHeader('Content-Type',
158 'application/x-www-form-urlencoded');
161 this.xmlhttp.send( data );
166 /* returns the actual response text from the request */
167 RemoteRequest.prototype.getText = function() {
168 return this.xmlhttp.responseText;
171 RemoteRequest.prototype.isReady = function() {
172 return this.xmlhttp.readyState == 4;
176 /* returns the JSON->js result object */
177 RemoteRequest.prototype.getResultObject = function() {
178 if(this.cancelled) return null;
180 var text = this.xmlhttp.responseText;
181 var obj = JSON2js(text);
183 if(obj == null) return null;
184 if(obj.is_err) throw new EXCommunication(obj.err_msg);
185 if( obj[0] != null && obj[1] == null ) obj = obj[0];
187 /* these are user level exceptions from the server code */
188 if(instanceOf(obj, ex)) {
189 if(!isXUL()) alert(obj.err_msg());
193 if(instanceOf(obj, perm_ex)) {
194 /* the opac will go ahead and spit out the error msg */
195 if(!isXUL()) alert(obj.err_msg());
202 /* adds a new parameter to the request */
203 RemoteRequest.prototype.addParam = function(param) {
204 var string = encodeURIComponent(js2JSON(param));
205 this.param_string += "¶m=" + string;