1 /* -----------------------------------------------------------------------
2 * Copyright (C) 2008 Georgia Public Library Service
3 * Bill Erickson <erickson@esilibrary.com>
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.
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 * ----------------------------------------------------------------------- */
17 var OSRF_APP_SESSION_CONNECTED = 0;
18 var OSRF_APP_SESSION_CONNECTING = 1;
19 var OSRF_APP_SESSION_DISCONNECTED = 2;
21 /* types of transport layers */
22 var OSRF_TRANSPORT_TYPE_XHR = 1;
23 var OSRF_TRANSPORT_TYPE_XMPP = 2;
24 var OSRF_TRANSPORT_TYPE_WS = 3;
27 var OSRF_MESSAGE_TYPE_REQUEST = 'REQUEST';
28 var OSRF_MESSAGE_TYPE_STATUS = 'STATUS';
29 var OSRF_MESSAGE_TYPE_RESULT = 'RESULT';
30 var OSRF_MESSAGE_TYPE_CONNECT = 'CONNECT';
31 var OSRF_MESSAGE_TYPE_DISCONNECT = 'DISCONNECT';
33 /* message statuses */
34 var OSRF_STATUS_CONTINUE = 100;
35 var OSRF_STATUS_OK = 200;
36 var OSRF_STATUS_ACCEPTED = 202;
37 var OSRF_STATUS_COMPLETE = 205;
38 var OSRF_STATUS_REDIRECTED = 307;
39 var OSRF_STATUS_BADREQUEST = 400;
40 var OSRF_STATUS_UNAUTHORIZED = 401;
41 var OSRF_STATUS_FORBIDDEN = 403;
42 var OSRF_STATUS_NOTFOUND = 404;
43 var OSRF_STATUS_NOTALLOWED = 405;
44 var OSRF_STATUS_TIMEOUT = 408;
45 var OSRF_STATUS_EXPFAILED = 417;
46 var OSRF_STATUS_INTERNALSERVERERROR = 500;
47 var OSRF_STATUS_NOTIMPLEMENTED = 501;
48 var OSRF_STATUS_VERSIONNOTSUPPORTED = 505;
50 /* The following classes map directly to network-serializable opensrf objects */
52 function osrfMessage(hash) {
55 this.hash.locale = OpenSRF.locale || 'en-US';
56 this._encodehash = true;
58 osrfMessage.prototype.threadTrace = function(d) {
59 if(arguments.length == 1)
60 this.hash.threadTrace = d;
61 return this.hash.threadTrace;
63 osrfMessage.prototype.type = function(d) {
64 if(arguments.length == 1)
66 return this.hash.type;
68 osrfMessage.prototype.payload = function(d) {
69 if(arguments.length == 1)
70 this.hash.payload = d;
71 return this.hash.payload;
73 osrfMessage.prototype.locale = function(d) {
74 if(arguments.length == 1)
76 return this.hash.locale;
78 osrfMessage.prototype.serialize = function() {
82 'threadTrace' : this.hash.threadTrace,
83 'type' : this.hash.type,
84 'payload' : (this.hash.payload) ? this.hash.payload.serialize() : 'null',
85 'locale' : this.hash.locale
90 function osrfMethod(hash) {
92 this._encodehash = true;
94 osrfMethod.prototype.method = function(d) {
95 if(arguments.length == 1)
97 return this.hash.method;
99 osrfMethod.prototype.params = function(d) {
100 if(arguments.length == 1)
101 this.hash.params = d;
102 return this.hash.params;
104 osrfMethod.prototype.serialize = function() {
108 'method' : this.hash.method,
109 'params' : this.hash.params
114 function osrfMethodException(hash) {
116 this._encodehash = true;
118 osrfMethodException.prototype.status = function(d) {
119 if(arguments.length == 1)
120 this.hash.status = d;
121 return this.hash.status;
123 osrfMethodException.prototype.statusCode = function(d) {
124 if(arguments.length == 1)
125 this.hash.statusCode = d;
126 return this.hash.statusCode;
128 function osrfConnectStatus(hash) {
130 this._encodehash = true;
132 osrfConnectStatus.prototype.status = function(d) {
133 if(arguments.length == 1)
134 this.hash.status = d;
135 return this.hash.status;
137 osrfConnectStatus.prototype.statusCode = function(d) {
138 if(arguments.length == 1)
139 this.hash.statusCode = d;
140 return this.hash.statusCode;
142 function osrfResult(hash) {
144 this._encodehash = true;
146 osrfResult.prototype.status = function(d) {
147 if(arguments.length == 1)
148 this.hash.status = d;
149 return this.hash.status;
151 osrfResult.prototype.statusCode = function(d) {
152 if(arguments.length == 1)
153 this.hash.statusCode = d;
154 return this.hash.statusCode;
156 osrfResult.prototype.content = function(d) {
157 if(arguments.length == 1)
158 this.hash.content = d;
159 return this.hash.content;
161 function osrfServerError(hash) {
163 this._encodehash = true;
165 osrfServerError.prototype.status = function(d) {
166 if(arguments.length == 1)
167 this.hash.status = d;
168 return this.hash.status;
170 osrfServerError.prototype.statusCode = function(d) {
171 if(arguments.length == 1)
172 this.hash.statusCode = d;
173 return this.hash.statusCode;
175 function osrfContinueStatus(hash) {
177 this._encodehash = true;
179 osrfContinueStatus.prototype.status = function(d) {
180 if(arguments.length == 1)
181 this.hash.status = d;
182 return this.hash.status;
184 osrfContinueStatus.prototype.statusCode = function(d) {
185 if(arguments.length == 1)
186 this.hash.statusCode = d;
187 return this.hash.statusCode;
191 OpenSRF.locale = null;
193 /* makes cls a subclass of pcls */
194 OpenSRF.set_subclass = function(cls, pcls) {
195 var str = cls+'.prototype = new '+pcls+'();';
196 str += cls+'.prototype.constructor = '+cls+';';
197 str += cls+'.baseClass = '+pcls+'.prototype.constructor;';
198 str += cls+'.prototype["super"] = '+pcls+'.prototype;';
203 /* general session superclass */
204 OpenSRF.Session = function() {
205 this.remote_id = null;
206 this.state = OSRF_APP_SESSION_DISCONNECTED;
209 OpenSRF.Session.transport = OSRF_TRANSPORT_TYPE_WS;
210 if (true || typeof WebSocket == 'undefined')
211 OpenSRF.Session.transport = OSRF_TRANSPORT_TYPE_XHR;
213 OpenSRF.Session.cache = {};
214 OpenSRF.Session.find_session = function(thread_trace) {
215 return OpenSRF.Session.cache[thread_trace];
217 OpenSRF.Session.prototype.cleanup = function() {
218 delete OpenSRF.Session.cache[this.thread];
221 OpenSRF.Session.prototype.send = function(osrf_msg, args) {
222 args = (args) ? args : {};
223 switch(OpenSRF.Session.transport) {
224 case OSRF_TRANSPORT_TYPE_WS:
225 return this.send_ws(osrf_msg, args);
226 case OSRF_TRANSPORT_TYPE_XHR:
227 return this.send_xhr(osrf_msg, args);
228 case OSRF_TRANSPORT_TYPE_XMPP:
229 return this.send_xmpp(osrf_msg, args);
233 OpenSRF.Session.prototype.send_xhr = function(osrf_msg, args) {
234 args.thread = this.thread;
235 args.rcpt = this.remote_id;
236 args.rcpt_service = this.service;
237 new OpenSRF.XHRequest(osrf_msg, args).send();
240 OpenSRF.Session.prototype.send_ws = function(osrf_msg, args) {
242 if (this.websocket) {
243 this.websocket.args = args; // callbacks
244 this.websocket.send(osrf_msg);
246 this.websocket = new OpenSRF.WSRequest(
247 this, args, function(wsreq) {
248 wsreq.send(osrf_msg);
254 OpenSRF.Session.prototype.send_xmpp = function(osrf_msg, args) {
255 alert('xmpp transport not implemented');
259 /* client sessions make requests */
260 OpenSRF.ClientSession = function(service) {
261 this.service = service;
262 this.remote_id = null;
263 this.locale = OpenSRF.locale || 'en-US';
265 this.thread = Math.random() + '' + new Date().getTime();
267 this.onconnect = null;
268 OpenSRF.Session.cache[this.thread] = this;
270 OpenSRF.set_subclass('OpenSRF.ClientSession', 'OpenSRF.Session');
273 OpenSRF.ClientSession.prototype.connect = function(args) {
274 args = (args) ? args : {};
275 this.remote_id = null;
277 if (this.state == OSRF_APP_SESSION_CONNECTED) {
278 if (args.onconnect) args.onconnect();
283 this.onconnect = args.onconnect;
286 /* if no handler is provided, make this a synchronous call */
287 this.timeout = (args.timeout) ? args.timeout : 5;
290 message = new osrfMessage({
291 'threadTrace' : this.last_id++,
292 'type' : OSRF_MESSAGE_TYPE_CONNECT
295 this.send(message, {'timeout' : this.timeout});
297 if(this.onconnect || this.state == OSRF_APP_SESSION_CONNECTED)
303 OpenSRF.ClientSession.prototype.disconnect = function(args) {
305 if (this.state == OSRF_APP_SESSION_CONNECTED) {
308 'threadTrace' : this.last_id++,
309 'type' : OSRF_MESSAGE_TYPE_DISCONNECT
314 this.remote_id = null;
315 this.state = OSRF_APP_SESSION_DISCONNECTED;
317 if (this.websocket) {
318 this.websocket.close();
319 delete this.websocket;
324 OpenSRF.ClientSession.prototype.request = function(args) {
326 if(this.state != OSRF_APP_SESSION_CONNECTED)
327 this.remote_id = null;
329 if(typeof args == 'string') {
331 for(var i = 1; i < arguments.length; i++)
332 params.push(arguments[i]);
339 if(typeof args == 'undefined')
343 var req = new OpenSRF.Request(this, this.last_id++, args);
344 this.requests.push(req);
348 OpenSRF.ClientSession.prototype.find_request = function(reqid) {
349 for(var i = 0; i < this.requests.length; i++) {
350 var req = this.requests[i];
351 if(req.reqid == reqid)
357 OpenSRF.Request = function(session, reqid, args) {
358 this.session = session;
362 this.onresponse = args.onresponse;
363 this.oncomplete = args.oncomplete;
364 this.onerror = args.onerror;
365 this.onmethoderror = args.onmethoderror;
366 this.ontransporterror = args.ontransporterror;
368 this.method = args.method;
369 this.params = args.params;
370 this.timeout = args.timeout;
371 this.response_queue = [];
372 this.complete = false;
375 OpenSRF.Request.prototype.peek_last = function(timeout) {
376 if(this.response_queue.length > 0) {
377 var x = this.response_queue.pop();
378 this.response_queue.push(x);
384 OpenSRF.Request.prototype.peek = function(timeout) {
385 if(this.response_queue.length > 0)
386 return this.response_queue[0];
390 OpenSRF.Request.prototype.recv = function(timeout) {
391 if(this.response_queue.length > 0)
392 return this.response_queue.shift();
396 OpenSRF.Request.prototype.send = function() {
397 method = new osrfMethod({'method':this.method, 'params':this.params});
398 message = new osrfMessage({
399 'threadTrace' : this.reqid,
400 'type' : OSRF_MESSAGE_TYPE_REQUEST,
402 'locale' : this.session.locale
405 this.session.send(message, {
406 'timeout' : this.timeout,
407 'onresponse' : this.onresponse,
408 'oncomplete' : this.oncomplete,
409 'onerror' : this.onerror,
410 'onmethoderror' : this.onmethoderror,
411 'ontransporterror' : this.ontransporterror
415 OpenSRF.NetMessage = function(to, from, thread, body) {
418 this.thread = thread;
422 OpenSRF.Stack = function() {
425 // global inbound message queue
426 OpenSRF.Stack.queue = [];
431 dump(msg + '\n'); // xulrunner
437 // ses may be passed to us by the network handler
438 OpenSRF.Stack.push = function(net_msg, callbacks, ses) {
439 if (!ses) ses = OpenSRF.Session.find_session(net_msg.thread);
441 ses.remote_id = net_msg.from;
445 osrf_msgs = JSON2js(net_msg.body);
448 log('Error parsing OpenSRF message body as JSON: ' + net_msg.body + '\n' + E);
451 * For unknown reasons, the Content-Type header will occasionally
452 * be included in the XHR.responseText for multipart/mixed messages.
453 * When this happens, strip the header and newlines from the message
456 net_msg.body = net_msg.body.replace(/^.*\n\n/, '');
457 log('Cleaning up and retrying...');
460 osrf_msgs = JSON2js(net_msg.body);
462 log('Unable to clean up message, giving up: ' + net_msg.body);
467 // push the latest responses onto the end of the inbound message queue
468 for(var i = 0; i < osrf_msgs.length; i++)
469 OpenSRF.Stack.queue.push({msg : osrf_msgs[i], callbacks : callbacks, ses : ses});
471 // continue processing responses, oldest to newest
472 while(OpenSRF.Stack.queue.length) {
473 var data = OpenSRF.Stack.queue.shift();
474 OpenSRF.Stack.handle_message(data.ses, data.msg, data.callbacks);
478 OpenSRF.Stack.handle_message = function(ses, osrf_msg, callbacks) {
482 if(osrf_msg.type() == OSRF_MESSAGE_TYPE_STATUS) {
484 var payload = osrf_msg.payload();
485 var status = payload.statusCode();
486 var status_text = payload.status();
488 if(status == OSRF_STATUS_COMPLETE) {
489 req = ses.find_request(osrf_msg.threadTrace());
492 if(callbacks.oncomplete && !req.oncomplete_called) {
493 req.oncomplete_called = true;
494 return callbacks.oncomplete(req);
499 if(status == OSRF_STATUS_OK) {
500 ses.state = OSRF_APP_SESSION_CONNECTED;
502 /* call the connect callback */
503 if(ses.onconnect && !ses.onconnect_called) {
504 ses.onconnect_called = true;
505 return ses.onconnect();
509 if(status == OSRF_STATUS_NOTFOUND || status == OSRF_STATUS_INTERNALSERVERERROR) {
510 req = ses.find_request(osrf_msg.threadTrace());
511 if(callbacks.onmethoderror)
512 return callbacks.onmethoderror(req, status, status_text);
516 if(osrf_msg.type() == OSRF_MESSAGE_TYPE_RESULT) {
517 req = ses.find_request(osrf_msg.threadTrace());
519 req.response_queue.push(osrf_msg.payload());
520 if(callbacks.onresponse)
521 return callbacks.onresponse(req);