1 var XML_HTTP_GATEWAY = "gateway";
2 var XML_HTTP_SERVER = "gapines.org";
3 var XML_HTTP_MAX_TRIES = 3;
5 var _allrequests = new Array();
7 function cleanRemoteRequests() {
8 for( var i = 0; i!= _allrequests.length; i++ ) {
9 var r = _allrequests[i];
10 r.xmlhttp.onreadystatechange = function(){};
14 _allrequests[i] = null;
18 /* ----------------------------------------------------------------------- */
20 function RemoteRequest( service, method ) {
22 _allrequests.push(this);
24 this.service = service;
30 this.type = "POST"; /* default */
31 this.id = service + method + Math.random();
32 this.cancelled = false;
37 while(i < arguments.length) {
38 var object = js2JSON(arguments[i++]);
39 this.params += "¶m=" + encodeURIComponent(object);
42 if(!this.params) { this.params = ""; }
43 this.param_string = "service=" + service + "&method=" + method + this.params;
45 if( ! this.type || ! this.service || ! this.method ) {
46 alert( "ERROR IN REQUEST PARAMS");
50 if( this.buildXMLRequest() == null )
51 alert("NEWER BROWSER");
54 /* constructs our XMLHTTPRequest object */
55 RemoteRequest.prototype.buildXMLRequest = function() {
58 this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
61 this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
67 if (!this.xmlhttp && typeof XMLHttpRequest!='undefined') {
68 this.xmlhttp = new XMLHttpRequest();
72 alert("NEEDS NEWER JAVASCRIPT for XMLHTTPRequest()");
80 /* define the callback we use when this request has received
82 RemoteRequest.prototype.setCompleteCallback = function(callback) {
84 if(this.cancelled) return;
86 var xml = this.xmlhttp;
88 xml.onreadystatechange = function() {
89 if( xml.readyState == 4 ) {
92 if(object.cancelled) return;
97 /* if we receive a communication error, retry the request up
98 to XML_HTTP_MAX_TRIES attempts */
99 if( instanceOf(E, EXCommunication) ) {
101 if(object.sendCount >= XML_HTTP_MAX_TRIES ) {
105 alert("Arrrgghh, Matey! Error communicating:\n" +
106 E + "\n" + object.param_string);
109 object.buildXMLRequest();
114 } finally { object = null; }
120 /* http by default. This makes it https. *ONLY works when
121 embedded in a XUL app. */
122 RemoteRequest.prototype.setSecure = function(bool) {
127 * By default, all calls are asynchronous. if 'blocking' is
128 * set to true, then the call will block until a response
129 * is received. If blocking, callbacks will not be called.
130 * In other words, you can assume the data is avaiable
131 * (getResponseObject()) as soon as the send call returns.
133 RemoteRequest.prototype.send = function(blocking) {
135 if(this.cancelled) return;
137 /* determine the xmlhttp server dynamically */
138 var url = location.protocol + "//" + location.host + "/" + XML_HTTP_GATEWAY;
142 url = "https://" + XML_HTTP_SERVER + "/" + XML_HTTP_GATEWAY;
144 url = "http://" + XML_HTTP_SERVER + "/" + XML_HTTP_GATEWAY;
149 if( this.type == 'GET' ) {
150 url += "?" + this.param_string;
154 this.xmlhttp.open(this.type, url, false);
156 this.xmlhttp.open(this.type, url, true);
160 if( this.type == 'POST' ) {
161 data = this.param_string;
162 this.xmlhttp.setRequestHeader('Content-Type',
163 'application/x-www-form-urlencoded');
166 this.xmlhttp.send( data );
171 /* returns the actual response text from the request */
172 RemoteRequest.prototype.getText = function() {
173 return this.xmlhttp.responseText;
176 RemoteRequest.prototype.isReady = function() {
177 return this.xmlhttp.readyState == 4;
181 /* returns the JSON->js result object */
182 RemoteRequest.prototype.getResultObject = function() {
183 //this.callback = null;
184 if(this.cancelled) return null;
186 var text = this.xmlhttp.responseText;
187 var obj = JSON2js(text);
194 throw new EXCommunication(obj.err_msg);
197 if( obj[0] != null && obj[1] == null )
200 /* these are user level exceptions from the server code */
201 if(instanceOf(obj, ex)) {
202 /* the opac will go ahead and spit out the error msg */
203 if(!isXUL()) alert(obj.err_msg());
207 if(instanceOf(obj, perm_ex)) {
208 /* the opac will go ahead and spit out the error msg */
209 if(!isXUL()) alert(obj.err_msg());
216 /* adds a new parameter to the request */
217 RemoteRequest.prototype.addParam = function(param) {
218 var string = encodeURIComponent(js2JSON(param));
219 this.param_string += "¶m=" + string;