]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/javascript/util/RemoteRequest.js
using perl gateway for now
[working/Evergreen.git] / Open-ILS / src / javascript / util / RemoteRequest.js
1 /* dynamically determine the XMLHTTPRequest server based on 
2         where the client is coming from.  Good for SSH tunneling, etc. */
3 /*
4 var _url = "https://" + globalRootURL;
5 if(globalPort != "443")
6         _url = _url + ":" + globalPort;
7 var XML_HTTP_URL = _url + "/gateway";
8 */
9
10 //var XML_HTTP_URL = "https://spacely.georgialibraries.org/gateway";
11 var XML_HTTP_URL = "https://spacely.georgialibraries.org/method/";
12
13
14 /* Request object */
15 function RemoteRequest( service, method ) {
16
17         this.service    = service;
18         this.method             = method;
19         this.xmlhttp    = false;
20         this.name               = null;
21
22         /* give us the ability to ignore responses from cancelled searches */
23         this.cancelled = false; 
24
25         this.type               = "POST"; /* default */
26
27         var i = 2;
28         this.params = ""; 
29         while(arguments[i] != null) {
30                 var object = js2JSON(arguments[i++]);
31                 this.params += "&__param=" + encodeURIComponent(object);
32         }
33
34         if(!this.params) { this.params = ""; }
35         this.param_string = "service=" + service + "&method=" + method + this.params;
36         this.url = XML_HTTP_URL;
37
38         if( ! this.type || ! this.service || ! this.method ) {
39                 alert( "ERROR IN REQUEST PARAMS");
40                 return null;
41         }
42
43         /* try MS */
44         try { 
45                 this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
46         } catch (e) {
47                 try { 
48                         this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
49                 } catch (E) {
50                         this.xmlhttp = false;
51                 }
52         }
53
54         if (!this.xmlhttp && typeof XMLHttpRequest!='undefined') {
55                 this.xmlhttp = new XMLHttpRequest();
56         }
57
58         if(!this.xmlhttp) {
59                 alert("NEEDS NEWER JAVASCRIPT for XMLHTTPRequest()");
60                 return null;
61         }
62
63 }
64
65 /* define the callback we use when this request has received
66         all of its data */
67 RemoteRequest.prototype.setCompleteCallback = function(callback) {
68         var object = this;
69         var obj = this.xmlhttp;
70         this.callback = callback;
71         this.xmlhttp.onreadystatechange = function() {
72                 if( obj.readyState == 4 ) {
73                         if(!this.cancelled)
74                                 callback(object);
75                 }
76         }
77 }
78
79 /** Send the request 
80   * By default, all calls are asynchronous.  if 'blocking' is
81   * set to true, then the call will block until a response
82   * is received.  If blocking, callbacks will not be called.
83   * In other, you can assume the data is avaiable as soon as the
84   * send call returns. 
85   */
86 RemoteRequest.prototype.send = function(blocking) {
87
88         var url = this.url;
89         var data = null;
90
91         if( this.type == 'GET' ) { 
92                 url +=  "?" + this.param_string; 
93         }
94
95         if(blocking) {
96                 this.xmlhttp.open(this.type, url, false);
97         } else {
98                 this.xmlhttp.open(this.type, url, true);
99         }
100
101
102         if( this.type == 'POST' ) {
103                 data = this.param_string;
104                 this.xmlhttp.setRequestHeader('Content-Type',
105                                 'application/x-www-form-urlencoded');
106         }
107
108         if(!this.cancelled)
109                 this.xmlhttp.send( data );
110 }
111
112 RemoteRequest.prototype.getText = function() {
113         return this.xmlhttp.responseText;
114 }
115
116 RemoteRequest.prototype.isReady = function() {
117         debug( "Request state is " + this.xmlhttp.readyState );
118         return this.xmlhttp.readyState == 4;
119 }
120
121
122 RemoteRequest.prototype.getResultObject = function() {
123         var obj = JSON2js( this.xmlhttp.responseText );
124         if(obj == null) {
125                 debug("received null response");
126                 return null;
127         }
128
129         if(obj && obj.is_err) { 
130                 debug("Something's Wrong: " + js2JSON(obj));
131                 throw new EXCommunication(obj.err_msg); 
132         }
133
134         if( obj[0] != null && obj[1] == null ) 
135                 return obj[0];
136
137         return obj;
138 }
139
140 RemoteRequest.prototype.addParam = function(param) {
141         this.param_string += "&__param=" + escape(js2JSON(param));
142 }