]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/javascript/opensrf_xhr.js
implemented onerror, onmethoderror and ontransporterror. moved the callbacks into...
[OpenSRF.git] / src / javascript / opensrf_xhr.js
1 /* -----------------------------------------------------------------------
2  * Copyright (C) 2008  Georgia Public Library Service
3  * Bill Erickson <erickson@esilibrary.com>
4  *  
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * ----------------------------------------------------------------------- */
15
16 var OSRF_HTTP_HEADER_TO = 'X-OpenSRF-to';
17 var OSRF_HTTP_HEADER_XID = 'X-OpenSRF-thread';
18 var OSRF_HTTP_HEADER_FROM = 'X-OpenSRF-from';
19 var OSRF_HTTP_HEADER_THREAD = 'X-OpenSRF-thread';
20 var OSRF_HTTP_HEADER_TIMEOUT = 'X-OpenSRF-timeout';
21 var OSRF_HTTP_HEADER_SERVICE = 'X-OpenSRF-service';
22 var OSRF_HTTP_HEADER_MULTIPART = 'X-OpenSRF-multipart';
23 var OSRF_HTTP_TRANSLATOR = '/osrf-http-translator'; /* XXX config */
24 var OSRF_POST_CONTENT_TYPE = 'application/x-www-form-urlencoded';
25
26
27 OpenSRF.XHRequest = function(osrf_msg, args) {
28     this.message = osrf_msg;
29     this.args = args;
30     this.xreq = new XMLHttpRequest(); /* XXX browser check */
31 }
32
33 OpenSRF.XHRequest.prototype.send = function() {
34     var xhr_req = this;
35     var xreq = this.xreq
36
37     if(this.args.timeout) {
38         /* this is a standard blocking (non-multipart) call */
39         xreq.open('POST', OSRF_HTTP_TRANSLATOR, false);
40
41     } else {
42
43         if( /* XXX browser != mozilla */ false ) {
44
45             /* standard asynchronous call */
46             xreq.onreadystatechange = function() {
47                 if(xreq.readyState == 4)
48                     xhr_req.core_handler();
49             }
50             xreq.open('POST', OSRF_HTTP_TRANSLATOR, true);
51
52         } else {
53
54             /* asynchronous multipart call */
55             xreq.multipart = true;
56             xreq.onload = function(evt) {xhr_req.core_handler();}
57             xreq.open('POST', OSRF_HTTP_TRANSLATOR, true);
58             xreq.setRequestHeader(OSRF_HTTP_HEADER_MULTIPART, 'true');
59         }
60     }
61
62     xreq.setRequestHeader('Content-Type', OSRF_POST_CONTENT_TYPE);
63     xreq.setRequestHeader(OSRF_HTTP_HEADER_THREAD, this.args.thread);
64     if(this.args.rcpt)
65         xreq.setRequestHeader(OSRF_HTTP_HEADER_TO, this.args.rcpt);
66     else
67         xreq.setRequestHeader(OSRF_HTTP_HEADER_SERVICE, this.args.rcpt_service);
68
69     var post = 'osrf-msg=' + encodeURIComponent(js2JSON([this.message.serialize()]));
70     xreq.send(post);
71
72     if(this.args.timeout) /* this was a blocking call, manually run the handler */
73         this.core_handler()
74
75     return this;
76 }
77
78 OpenSRF.XHRequest.prototype.core_handler = function() {
79     sender = this.xreq.getResponseHeader(OSRF_HTTP_HEADER_FROM);
80     thread = this.xreq.getResponseHeader(OSRF_HTTP_HEADER_THREAD);
81     json = this.xreq.responseText;
82     stat = this.xreq.status;
83
84     if(stat >= 400) 
85         return this.transport_error_handler();
86
87     OpenSRF.Stack.push(
88         new OpenSRF.NetMessage(null, sender, thread, json),
89         {
90             onresponse : this.args.onresponse,
91             oncomplete : this.args.oncomplete,
92             onerror : this.args.onerror,
93             onmethoderror : this.method_error_handler()
94         }
95     );
96 }
97
98
99 OpenSRF.XHRequest.prototype.method_error_handler = function() {
100     var xhr = this;
101     return function(req, status, status_text) {
102         if(xhr.args.onmethoderror) 
103             xhr.args.onmethoderror(req, status, status_text);
104         if(xhr.args.onerror)  
105             xhr.args.onerror(xhr.message, xhr.args.rcpt || xhr.args.rcpt_service, xhr.args.thread);
106     }
107 }
108
109 OpenSRF.XHRequest.prototype.transport_error_handler = function() {
110     if(this.args.ontransporterror) 
111         this.args.ontransporterror(this.xreq);
112     if(this.args.onerror) 
113         this.args.onerror(this.message, this.args.rcpt || this.args.rcpt_service, this.args.thread);
114 }
115
116