bea2bc590818a036c9e07acc4566a8e5a6120b91
[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     xreq.multipart = true; /* XXX browser check */
38     xreq.onload = function(evt) {xhr_req.core_handler(evt.target);}
39     xreq.open('POST', OSRF_HTTP_TRANSLATOR, true);
40
41     xreq.setRequestHeader('Content-Type', OSRF_POST_CONTENT_TYPE);
42     xreq.setRequestHeader(OSRF_HTTP_HEADER_MULTIPART, 'true');
43     xreq.setRequestHeader(OSRF_HTTP_HEADER_THREAD, this.args.thread);
44
45     if(this.args.rcpt)
46         xreq.setRequestHeader(OSRF_HTTP_HEADER_TO, this.args.rcpt);
47     else
48         xreq.setRequestHeader(OSRF_HTTP_HEADER_SERVICE, this.args.rcpt_service);
49
50     var post = 'osrf-msg=' + encodeURIComponent(js2JSON([this.message.serialize()]));
51     xreq.send(post);
52
53     return this;
54 }
55
56 OpenSRF.XHRequest.prototype.core_handler = function() {
57     sender = this.xreq.getResponseHeader(OSRF_HTTP_HEADER_FROM);
58     thread = this.xreq.getResponseHeader(OSRF_HTTP_HEADER_THREAD);
59     json = this.xreq.responseText;
60     stat = this.xreq.status;
61
62     var xhr = this;
63     var req = OpenSRF.Stack.push(
64         new OpenSRF.NetMessage(null, sender, thread, json),
65         function(ses, req) {
66             if(req) {
67                 if(req.response_queue.length > 0 && xhr.args.onresponse) 
68                     return xhr.args.onresponse(req);
69                 if(req.complete && xhr.args.oncomplete)
70                     return xhr.args.oncomplete(req);
71             }
72         }
73     );
74 }
75