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;
49 if( ! this.type || ! this.service || ! this.method ) {
50 alert( "ERROR IN REQUEST PARAMS");
54 if( this.buildXMLRequest() == null )
55 alert("NEWER BROWSER");
58 /* constructs our XMLHTTPRequest object */
59 RemoteRequest.prototype.buildXMLRequest = function() {
62 this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
65 this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
71 if (!this.xmlhttp && typeof XMLHttpRequest!='undefined') {
72 this.xmlhttp = new XMLHttpRequest();
76 alert("NEEDS NEWER JAVASCRIPT for XMLHTTPRequest()");
84 function _remoteRequestCallback(id) {
86 var object = _allrequests[id];
87 if(object.cancelled) return;
89 if( object.xmlhttp.readyState == 4 ) {
91 object.callback(object);
94 /* if we receive a communication error, retry the request up
95 to XML_HTTP_MAX_TRIES attempts */
96 if( instanceOf(E, EXCommunication) ) {
98 if(object.sendCount >= XML_HTTP_MAX_TRIES ) {
99 if(isXUL()) throw object;
100 else alert("Arrrgghh, Matey! Error communicating:\n" + E + "\n" + object.param_string);
102 object.buildXMLRequest();
109 destroyRequest(object);
116 /* define the callback we use when this request has received
118 RemoteRequest.prototype.setCompleteCallback = function(callback) {
119 if(this.cancelled) return;
120 this.callback = callback;
122 this.xmlhttp.onreadystatechange = function() { _remoteRequestCallback(id); }
126 /* http by default. This makes it https. *ONLY works when
127 embedded in a XUL app. */
128 RemoteRequest.prototype.setSecure = function(bool) {
133 * By default, all calls are asynchronous. if 'blocking' is
134 * set to true, then the call will block until a response
135 * is received. If blocking, callbacks will not be called.
136 * In other words, you can assume the data is avaiable
137 * (getResponseObject()) as soon as the send call returns.
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 //this.callback = null;
190 if(this.cancelled) return null;
192 var text = this.xmlhttp.responseText;
193 var obj = JSON2js(text);
200 throw new EXCommunication(obj.err_msg);
203 if( obj[0] != null && obj[1] == null )
206 /* these are user level exceptions from the server code */
207 if(instanceOf(obj, ex)) {
208 /* the opac will go ahead and spit out the error msg */
209 if(!isXUL()) alert(obj.err_msg());
213 if(instanceOf(obj, perm_ex)) {
214 /* the opac will go ahead and spit out the error msg */
215 if(!isXUL()) alert(obj.err_msg());
222 /* adds a new parameter to the request */
223 RemoteRequest.prototype.addParam = function(param) {
224 var string = encodeURIComponent(js2JSON(param));
225 this.param_string += "¶m=" + string;