2 var XML_HTTP_GATEWAY = "gateway";
3 var XML_HTTP_SERVER = "spacely.georgialibraries.org";
4 var XML_HTTP_MAX_TRIES = 3;
7 /* ----------------------------------------------------------------------- */
10 /* keeping all requests in a global cache allows us to manage request
11 resends effectively */
13 /* Array of globally pending requests */
14 RemoteRequest.pending = new Array();
16 /* cleans requests (and null entries) from the pending array */
17 RemoteRequest.prunePending = function(id) {
18 var tmpArray = new Array();
19 for( var x in RemoteRequest.pending ) {
20 if( RemoteRequest.pending[x] != null ) {
21 if( RemoteRequest.pending[x].id != id )
22 tmpArray.push(RemoteRequest.pending[x]);
25 RemoteRequest.pending = tmpArray;
28 /* returns the number of pending requests */
29 RemoteRequest.numPending = function() {
30 return RemoteRequest.pending.length;
34 /* ----------------------------------------------------------------------- */
35 /* Generic request manager */
36 function RequestBatch() {
37 this.requests = new Array();
39 RequestBatch.prototype.add = function(request) {
40 this.requests.push(request);
43 RequestBatch.prototype.remove = function(request) {
44 var newArray = new Array();
45 for( var i in this.requests ) {
46 if( this.requests[i] != null &&
47 this.requests[i].id != request.id )
48 newArray.push(this.requests[i]);
50 this.requests = newArray;
53 RequestBatch.prototype.pending = function() {
54 return this.requests.length;
57 /* ----------------------------------------------------------------------- */
59 function RemoteRequest( service, method ) {
61 this.service = service;
67 this.type = "POST"; /* default */
68 this.id = service + method + Math.random();
72 while(arguments[i] != null) {
73 var object = js2JSON(arguments[i++]);
74 this.params += "&__param=" + encodeURIComponent(object);
77 if(!this.params) { this.params = ""; }
78 this.param_string = "service=" + service + "&method=" + method + this.params;
80 if( ! this.type || ! this.service || ! this.method ) {
81 alert( "ERROR IN REQUEST PARAMS");
85 if( this.buildXMLRequest() == null )
86 alert("NEWER BROWSER");
89 /* constructs our XMLHTTPRequest object */
90 RemoteRequest.prototype.buildXMLRequest = function() {
93 this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
96 this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
102 if (!this.xmlhttp && typeof XMLHttpRequest!='undefined') {
103 this.xmlhttp = new XMLHttpRequest();
107 alert("NEEDS NEWER JAVASCRIPT for XMLHTTPRequest()");
112 this.setCompleteCallback( this.callback );
118 /* define the callback we use when this request has received
120 RemoteRequest.prototype.setCompleteCallback = function(callback) {
123 var obj = this.xmlhttp;
124 this.callback = callback;
126 this.xmlhttp.onreadystatechange = function() {
127 if( obj.readyState == 4 ) {
133 debug("Processing Error in complete callback: [" + E + "]");
135 /* if we receive a communication error, retry the request up
136 to XML_HTTP_MAX_TRIES attempts */
137 if( instanceOf(E, EXCommunication) ) {
139 debug("Communication Error: [" + E + "]");
140 if(object.sendCount >= XML_HTTP_MAX_TRIES ) {
141 alert("Arrrgghh, Matey! Error communicating:\n" +
142 E + "\n" + object.param_string);
144 object.buildXMLRequest();
149 /* any other exception is alerted for now */
150 RemoteRequest.prunePending(object.id);
151 //alert("Exception: " + E);
156 /* on success, remove the request from the pending cache */
157 RemoteRequest.prunePending(object.id);
163 /* http by default. This makes it https. *ONLY works when
164 embedded in a XUL app. */
165 RemoteRequest.prototype.setSecure = function(bool) {
170 * By default, all calls are asynchronous. if 'blocking' is
171 * set to true, then the call will block until a response
172 * is received. If blocking, callbacks will not be called.
173 * In other words, you can assume the data is avaiable
174 * (getResponseObject()) as soon as the send call returns.
176 RemoteRequest.prototype.send = function(blocking) {
178 if( this.sendCount == 0)
179 RemoteRequest.pending.push(this);
181 debug("Resending request with id " + this.id
182 + " and send count " + this.sendCount);
184 /* determine the xmlhttp server dynamically */
185 var url = location.protocol + "//" + location.host + "/" + XML_HTTP_GATEWAY;
187 var XUL = true; /*will be global, needs to be set by the parent XUL app when around */
190 url = "https://" + XML_HTTP_SERVER + "/" + XML_HTTP_GATEWAY;
192 url = "http://" + XML_HTTP_SERVER + "/" + XML_HTTP_GATEWAY;
197 if( this.type == 'GET' ) {
198 url += "?" + this.param_string;
202 this.xmlhttp.open(this.type, url, false);
204 this.xmlhttp.open(this.type, url, true);
208 if( this.type == 'POST' ) {
209 data = this.param_string;
210 this.xmlhttp.setRequestHeader('Content-Type',
211 'application/x-www-form-urlencoded');
214 this.xmlhttp.send( data );
219 /* returns the actual response text from the request */
220 RemoteRequest.prototype.getText = function() {
221 return this.xmlhttp.responseText;
224 RemoteRequest.prototype.isReady = function() {
225 return this.xmlhttp.readyState == 4;
229 /* returns the JSON->js result object */
230 RemoteRequest.prototype.getResultObject = function() {
231 var text = this.xmlhttp.responseText;
232 var obj = JSON2js(text);
234 debug("received null response");
238 if(obj && obj.is_err) {
239 debug("Something's Wrong: " + js2JSON(obj));
240 throw new EXCommunication(obj.err_msg);
243 if( obj[0] != null && obj[1] == null )
249 /* adds a new parameter to the request */
250 RemoteRequest.prototype.addParam = function(param) {
251 var string = encodeURIComponent(js2JSON(param));
252 this.param_string += "&__param=" + string;