1 var XML_HTTP_GATEWAY = "gateway";
2 var XML_HTTP_SERVER = "gapines.org";
3 var XML_HTTP_MAX_TRIES = 3;
5 /* ----------------------------------------------------------------------- */
7 function RemoteRequest( service, method ) {
9 this.service = service;
15 this.type = "POST"; /* default */
16 this.id = service + method + Math.random();
17 this.cancelled = false;
22 while(i < arguments.length) {
23 var object = js2JSON(arguments[i++]);
24 this.params += "¶m=" + encodeURIComponent(object);
27 if(!this.params) { this.params = ""; }
28 this.param_string = "service=" + service + "&method=" + method + this.params;
30 if( ! this.type || ! this.service || ! this.method ) {
31 alert( "ERROR IN REQUEST PARAMS");
35 if( this.buildXMLRequest() == null )
36 alert("NEWER BROWSER");
39 /* constructs our XMLHTTPRequest object */
40 RemoteRequest.prototype.buildXMLRequest = function() {
43 this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
46 this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
52 if (!this.xmlhttp && typeof XMLHttpRequest!='undefined') {
53 this.xmlhttp = new XMLHttpRequest();
57 alert("NEEDS NEWER JAVASCRIPT for XMLHTTPRequest()");
65 /* define the callback we use when this request has received
67 RemoteRequest.prototype.setCompleteCallback = function(callback) {
69 if(this.cancelled) return;
71 var xml = this.xmlhttp;
73 xml.onreadystatechange = function() {
74 if( xml.readyState == 4 ) {
77 if(object.cancelled) return;
82 /* if we receive a communication error, retry the request up
83 to XML_HTTP_MAX_TRIES attempts */
84 if( instanceOf(E, EXCommunication) ) {
86 if(object.sendCount >= XML_HTTP_MAX_TRIES ) {
90 alert("Arrrgghh, Matey! Error communicating:\n" +
91 E + "\n" + object.param_string);
94 object.buildXMLRequest();
99 /* any other exception is alerted for now */
100 //RemoteRequest.prunePending(object.id);
101 //alert("Exception: " + E);
107 object.callback = null;
108 object.xmlhttp.onreadystatechange = function(){};
109 object.xmlhttp = null;
110 object.params = null;
111 object.param_string = null;
118 /* http by default. This makes it https. *ONLY works when
119 embedded in a XUL app. */
120 RemoteRequest.prototype.setSecure = function(bool) {
125 * By default, all calls are asynchronous. if 'blocking' is
126 * set to true, then the call will block until a response
127 * is received. If blocking, callbacks will not be called.
128 * In other words, you can assume the data is avaiable
129 * (getResponseObject()) as soon as the send call returns.
131 RemoteRequest.prototype.send = function(blocking) {
133 if(this.cancelled) return;
135 /* determine the xmlhttp server dynamically */
136 var url = location.protocol + "//" + location.host + "/" + XML_HTTP_GATEWAY;
140 url = "https://" + XML_HTTP_SERVER + "/" + XML_HTTP_GATEWAY;
142 url = "http://" + XML_HTTP_SERVER + "/" + XML_HTTP_GATEWAY;
147 if( this.type == 'GET' ) {
148 url += "?" + this.param_string;
152 this.xmlhttp.open(this.type, url, false);
154 this.xmlhttp.open(this.type, url, true);
158 if( this.type == 'POST' ) {
159 data = this.param_string;
160 this.xmlhttp.setRequestHeader('Content-Type',
161 'application/x-www-form-urlencoded');
164 this.xmlhttp.send( data );
169 /* returns the actual response text from the request */
170 RemoteRequest.prototype.getText = function() {
171 return this.xmlhttp.responseText;
174 RemoteRequest.prototype.isReady = function() {
175 return this.xmlhttp.readyState == 4;
179 /* returns the JSON->js result object */
180 RemoteRequest.prototype.getResultObject = function() {
181 //this.callback = null;
182 if(this.cancelled) return null;
184 var text = this.xmlhttp.responseText;
185 var obj = JSON2js(text);
192 throw new EXCommunication(obj.err_msg);
195 if( obj[0] != null && obj[1] == null )
198 /* these are user level exceptions from the server code */
199 if(instanceOf(obj, ex)) {
200 /* the opac will go ahead and spit out the error msg */
201 if(!isXUL()) alert(obj.err_msg());
205 if(instanceOf(obj, perm_ex)) {
206 /* the opac will go ahead and spit out the error msg */
207 if(!isXUL()) alert(obj.err_msg());
214 /* adds a new parameter to the request */
215 RemoteRequest.prototype.addParam = function(param) {
216 var string = encodeURIComponent(js2JSON(param));
217 this.param_string += "¶m=" + string;