1 var XML_HTTP_GATEWAY = "gateway";
2 var XML_HTTP_SERVER = "gapines.org";
3 var XML_HTTP_MAX_TRIES = 3;
7 function cleanRemoteRequests() {
8 for( var i in _allrequests )
9 destroyRequest(_allrequests[i]);
12 function destroyRequest(r) {
14 r.xmlhttp.onreadystatechange = function(){};
18 _allrequests[r.id] = null;
21 /* ----------------------------------------------------------------------- */
23 function RemoteRequest( service, method ) {
26 this.service = service;
32 this.type = "POST"; /* default */
33 this.id = service + method + Math.random();
34 this.cancelled = false;
36 _allrequests[this.id] = this;
41 while(i < arguments.length) {
42 var object = js2JSON(arguments[i++]);
43 this.params += "¶m=" + encodeURIComponent(object);
46 if(!this.params) { this.params = ""; }
47 this.param_string = "service=" + service + "&method=" + method + this.params;
48 if( this.buildXMLRequest() == null ) alert("Browser is not supported!");
51 /* constructs our XMLHTTPRequest object */
52 RemoteRequest.prototype.buildXMLRequest = function() {
56 x = new ActiveXObject("Msxml2.XMLHTTP");
59 x = new ActiveXObject("Microsoft.XMLHTTP");
65 if (!x && typeof XMLHttpRequest!='undefined') x = new XMLHttpRequest();
68 alert("NEEDS NEWER JAVASCRIPT for XMLHTTPRequest()");
77 function _remoteRequestCallback(id) {
79 var object = _allrequests[id];
80 if(object.cancelled) return;
82 if( object.xmlhttp.readyState == 4 ) {
84 object.callback(object);
87 /* if we receive a communication error, retry the request up
88 to XML_HTTP_MAX_TRIES attempts */
89 if( instanceOf(E, EXCommunication) ) {
91 if(object.sendCount >= XML_HTTP_MAX_TRIES ) {
92 if(isXUL()) throw object;
93 else alert("Arrrgghh, Matey! Error communicating:\n" + E + "\n" + object.param_string);
95 object.buildXMLRequest();
102 destroyRequest(object);
109 /* define the callback we use when this request has received
111 RemoteRequest.prototype.setCompleteCallback = function(callback) {
112 if(this.cancelled) return;
113 this.callback = callback;
115 this.xmlhttp.onreadystatechange = function() { _remoteRequestCallback(id); }
119 /* http by default. This makes it https. *ONLY works when
120 embedded in a XUL app. */
121 RemoteRequest.prototype.setSecure = function(bool) {
125 RemoteRequest.prototype.send = function(blocking) {
127 if(this.cancelled) return;
129 /* determine the xmlhttp server dynamically */
130 var url = location.protocol + "//" + location.host + "/" + XML_HTTP_GATEWAY;
134 url = "https://" + XML_HTTP_SERVER + "/" + XML_HTTP_GATEWAY;
136 url = "http://" + XML_HTTP_SERVER + "/" + XML_HTTP_GATEWAY;
141 if( this.type == 'GET' ) {
142 url += "?" + this.param_string;
146 this.xmlhttp.open(this.type, url, false);
148 this.xmlhttp.open(this.type, url, true);
152 if( this.type == 'POST' ) {
153 data = this.param_string;
154 this.xmlhttp.setRequestHeader('Content-Type',
155 'application/x-www-form-urlencoded');
158 this.xmlhttp.send( data );
163 /* returns the actual response text from the request */
164 RemoteRequest.prototype.getText = function() {
165 return this.xmlhttp.responseText;
168 RemoteRequest.prototype.isReady = function() {
169 return this.xmlhttp.readyState == 4;
173 /* returns the JSON->js result object */
174 RemoteRequest.prototype.getResultObject = function() {
175 if(this.cancelled) return null;
177 var text = this.xmlhttp.responseText;
178 var obj = JSON2js(text);
180 if(obj == null) return null;
181 if(obj.is_err) throw new EXCommunication(obj.err_msg);
182 if( obj[0] != null && obj[1] == null ) obj = obj[0];
184 /* these are user level exceptions from the server code */
185 if(instanceOf(obj, ex)) {
186 if(!isXUL()) alert(obj.err_msg());
190 if(instanceOf(obj, perm_ex)) {
191 /* the opac will go ahead and spit out the error msg */
192 if(!isXUL()) alert(obj.err_msg());
199 /* adds a new parameter to the request */
200 RemoteRequest.prototype.addParam = function(param) {
201 var string = encodeURIComponent(js2JSON(param));
202 this.param_string += "¶m=" + string;