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;
26 var OSRF_MESSAGE_TYPE_REQUEST = 'REQUEST';
27 var OSRF_MESSAGE_TYPE_STATUS = 'STATUS';
28 var OSRF_MESSAGE_TYPE_RESULT = 'RESULT';
29 var OSRF_MESSAGE_TYPE_CONNECT = 'CONNECT';
30 var OSRF_MESSAGE_TYPE_DISCONNECT = 'DISCONNECT';
32 /* message statuses */
33 var OSRF_STATUS_CONTINUE = 100;
34 var OSRF_STATUS_OK = 200;
35 var OSRF_STATUS_ACCEPTED = 202;
36 var OSRF_STATUS_COMPLETE = 205;
37 var OSRF_STATUS_REDIRECTED = 307;
38 var OSRF_STATUS_BADREQUEST = 400;
39 var OSRF_STATUS_UNAUTHORIZED = 401;
40 var OSRF_STATUS_FORBIDDEN = 403;
41 var OSRF_STATUS_NOTFOUND = 404;
42 var OSRF_STATUS_NOTALLOWED = 405;
43 var OSRF_STATUS_TIMEOUT = 408;
44 var OSRF_STATUS_EXPFAILED = 417;
45 var OSRF_STATUS_INTERNALSERVERERROR = 500;
46 var OSRF_STATUS_NOTIMPLEMENTED = 501;
47 var OSRF_STATUS_VERSIONNOTSUPPORTED = 505;
49 /* The following classes map directly to network-serializable opensrf objects */
51 function osrfMessage(hash) {
54 this.hash.locale = OpenSRF.locale || 'en-US';
55 this._encodehash = true;
57 osrfMessage.prototype.threadTrace = function(d) {
58 if(arguments.length == 1)
59 this.hash.threadTrace = d;
60 return this.hash.threadTrace;
62 osrfMessage.prototype.type = function(d) {
63 if(arguments.length == 1)
65 return this.hash.type;
67 osrfMessage.prototype.payload = function(d) {
68 if(arguments.length == 1)
69 this.hash.payload = d;
70 return this.hash.payload;
72 osrfMessage.prototype.locale = function(d) {
73 if(arguments.length == 1)
75 return this.hash.locale;
77 osrfMessage.prototype.serialize = function() {
81 'threadTrace' : this.hash.threadTrace,
82 'type' : this.hash.type,
83 'payload' : (this.hash.payload) ? this.hash.payload.serialize() : 'null',
84 'locale' : this.hash.locale
89 function osrfMethod(hash) {
91 this._encodehash = true;
93 osrfMethod.prototype.method = function(d) {
94 if(arguments.length == 1)
96 return this.hash.method;
98 osrfMethod.prototype.params = function(d) {
99 if(arguments.length == 1)
100 this.hash.params = d;
101 return this.hash.params;
103 osrfMethod.prototype.serialize = function() {
107 'method' : this.hash.method,
108 'params' : this.hash.params
113 function osrfMethodException(hash) {
115 this._encodehash = true;
117 osrfMethodException.prototype.status = function(d) {
118 if(arguments.length == 1)
119 this.hash.status = d;
120 return this.hash.status;
122 osrfMethodException.prototype.statusCode = function(d) {
123 if(arguments.length == 1)
124 this.hash.statusCode = d;
125 return this.hash.statusCode;
127 function osrfConnectStatus(hash) {
129 this._encodehash = true;
131 osrfConnectStatus.prototype.status = function(d) {
132 if(arguments.length == 1)
133 this.hash.status = d;
134 return this.hash.status;
136 osrfConnectStatus.prototype.statusCode = function(d) {
137 if(arguments.length == 1)
138 this.hash.statusCode = d;
139 return this.hash.statusCode;
141 function osrfResult(hash) {
143 this._encodehash = true;
145 osrfResult.prototype.status = function(d) {
146 if(arguments.length == 1)
147 this.hash.status = d;
148 return this.hash.status;
150 osrfResult.prototype.statusCode = function(d) {
151 if(arguments.length == 1)
152 this.hash.statusCode = d;
153 return this.hash.statusCode;
155 osrfResult.prototype.content = function(d) {
156 if(arguments.length == 1)
157 this.hash.content = d;
158 return this.hash.content;
160 function osrfServerError(hash) {
162 this._encodehash = true;
164 osrfServerError.prototype.status = function(d) {
165 if(arguments.length == 1)
166 this.hash.status = d;
167 return this.hash.status;
169 osrfServerError.prototype.statusCode = function(d) {
170 if(arguments.length == 1)
171 this.hash.statusCode = d;
172 return this.hash.statusCode;
174 function osrfContinueStatus(hash) {
176 this._encodehash = true;
178 osrfContinueStatus.prototype.status = function(d) {
179 if(arguments.length == 1)
180 this.hash.status = d;
181 return this.hash.status;
183 osrfContinueStatus.prototype.statusCode = function(d) {
184 if(arguments.length == 1)
185 this.hash.statusCode = d;
186 return this.hash.statusCode;
190 OpenSRF.locale = null;
192 /* makes cls a subclass of pcls */
193 OpenSRF.set_subclass = function(cls, pcls) {
194 var str = cls+'.prototype = new '+pcls+'();';
195 str += cls+'.prototype.constructor = '+cls+';';
196 str += cls+'.baseClass = '+pcls+'.prototype.constructor;';
197 str += cls+'.prototype["super"] = '+pcls+'.prototype;';
202 /* general session superclass */
203 OpenSRF.Session = function() {
204 this.remote_id = null;
205 this.state = OSRF_APP_SESSION_DISCONNECTED;
208 OpenSRF.Session.transport = OSRF_TRANSPORT_TYPE_XHR; /* default to XHR */
209 OpenSRF.Session.cache = {};
210 OpenSRF.Session.find_session = function(thread_trace) {
211 return OpenSRF.Session.cache[thread_trace];
213 OpenSRF.Session.prototype.cleanup = function() {
214 delete OpenSRF.Session.cache[this.thread];
217 OpenSRF.Session.prototype.send = function(osrf_msg, args) {
218 args = (args) ? args : {};
219 switch(OpenSRF.Session.transport) {
220 case OSRF_TRANSPORT_TYPE_XHR:
221 return this.send_xhr(osrf_msg, args);
222 case OSRF_TRANSPORT_TYPE_XMPP:
223 return this.send_xmpp(osrf_msg, args);
227 OpenSRF.Session.prototype.send_xhr = function(osrf_msg, args) {
228 args.thread = this.thread;
229 args.rcpt = this.remote_id;
230 args.rcpt_service = this.service;
231 new OpenSRF.XHRequest(osrf_msg, args).send();
234 OpenSRF.Session.prototype.send_xmpp = function(osrf_msg, args) {
235 alert('xmpp transport not yet implemented');
239 /* client sessions make requests */
240 OpenSRF.ClientSession = function(service) {
241 this.service = service;
242 this.remote_id = null;
243 this.locale = OpenSRF.locale || 'en-US';
245 this.thread = Math.random() + '' + new Date().getTime();
247 this.onconnect = null;
248 OpenSRF.Session.cache[this.thread] = this;
250 OpenSRF.set_subclass('OpenSRF.ClientSession', 'OpenSRF.Session');
253 OpenSRF.ClientSession.prototype.connect = function(args) {
254 args = (args) ? args : {};
255 this.remote_id = null;
258 this.onconnect = args.onconnect;
260 /* if no handler is provided, make this a synchronous call */
262 this.timeout = (args.timeout) ? args.timeout : 5;
264 message = new osrfMessage({
265 'threadTrace' : this.reqid,
266 'type' : OSRF_MESSAGE_TYPE_CONNECT
269 this.send(message, {'timeout' : this.timeout});
271 if(this.onconnect || this.state == OSRF_APP_SESSION_CONNECTED)
276 OpenSRF.ClientSession.prototype.disconnect = function(args) {
279 'threadTrace' : this.reqid,
280 'type' : OSRF_MESSAGE_TYPE_DISCONNECT
283 this.remote_id = null;
287 OpenSRF.ClientSession.prototype.request = function(args) {
289 if(this.state != OSRF_APP_SESSION_CONNECTED)
290 this.remote_id = null;
292 if(typeof args == 'string') {
294 for(var i = 1; i < arguments.length; i++)
295 params.push(arguments[i]);
302 if(typeof args == 'undefined')
306 var req = new OpenSRF.Request(this, this.last_id++, args);
307 this.requests.push(req);
311 OpenSRF.ClientSession.prototype.find_request = function(reqid) {
312 for(var i = 0; i < this.requests.length; i++) {
313 var req = this.requests[i];
314 if(req.reqid == reqid)
320 OpenSRF.Request = function(session, reqid, args) {
321 this.session = session;
325 this.onresponse = args.onresponse;
326 this.oncomplete = args.oncomplete;
327 this.onerror = args.onerror;
328 this.onmethoderror = args.onmethoderror;
329 this.ontransporterror = args.ontransporterror;
331 this.method = args.method;
332 this.params = args.params;
333 this.timeout = args.timeout;
334 this.response_queue = [];
335 this.complete = false;
338 OpenSRF.Request.prototype.peek_last = function(timeout) {
339 if(this.response_queue.length > 0) {
340 var x = this.response_queue.pop();
341 this.response_queue.push(x);
347 OpenSRF.Request.prototype.peek = function(timeout) {
348 if(this.response_queue.length > 0)
349 return this.response_queue[0];
353 OpenSRF.Request.prototype.recv = function(timeout) {
354 if(this.response_queue.length > 0)
355 return this.response_queue.shift();
359 OpenSRF.Request.prototype.send = function() {
360 method = new osrfMethod({'method':this.method, 'params':this.params});
361 message = new osrfMessage({
362 'threadTrace' : this.reqid,
363 'type' : OSRF_MESSAGE_TYPE_REQUEST,
365 'locale' : this.session.locale
368 this.session.send(message, {
369 'timeout' : this.timeout,
370 'onresponse' : this.onresponse,
371 'oncomplete' : this.oncomplete,
372 'onerror' : this.onerror,
373 'onmethoderror' : this.onmethoderror,
374 'ontransporterror' : this.ontransporterror
378 OpenSRF.NetMessage = function(to, from, thread, body) {
381 this.thread = thread;
385 OpenSRF.Stack = function() {
388 // global inbound message queue
389 OpenSRF.Stack.queue = [];
394 dump(msg + '\n'); // xulrunner
400 OpenSRF.Stack.push = function(net_msg, callbacks) {
401 var ses = OpenSRF.Session.find_session(net_msg.thread);
403 ses.remote_id = net_msg.from;
407 osrf_msgs = JSON2js(net_msg.body);
410 log('Error parsing OpenSRF message body as JSON: ' + net_msg.body + '\n' + E);
413 * For unknown reasons, the Content-Type header will occasionally
414 * be included in the XHR.responseText for multipart/mixed messages.
415 * When this happens, strip the header and newlines from the message
418 net_msg.body = net_msg.body.replace(/^.*\n\n/, '');
419 log('Cleaning up and retrying...');
422 osrf_msgs = JSON2js(net_msg.body);
424 log('Unable to clean up message, giving up: ' + net_msg.body);
429 // push the latest responses onto the end of the inbound message queue
430 for(var i = 0; i < osrf_msgs.length; i++)
431 OpenSRF.Stack.queue.push({msg : osrf_msgs[i], callbacks : callbacks, ses : ses});
433 // continue processing responses, oldest to newest
434 while(OpenSRF.Stack.queue.length) {
435 var data = OpenSRF.Stack.queue.shift();
436 OpenSRF.Stack.handle_message(data.ses, data.msg, data.callbacks);
440 OpenSRF.Stack.handle_message = function(ses, osrf_msg, callbacks) {
444 if(osrf_msg.type() == OSRF_MESSAGE_TYPE_STATUS) {
446 var payload = osrf_msg.payload();
447 var status = payload.statusCode();
448 var status_text = payload.status();
450 if(status == OSRF_STATUS_COMPLETE) {
451 req = ses.find_request(osrf_msg.threadTrace());
454 if(callbacks.oncomplete && !req.oncomplete_called) {
455 req.oncomplete_called = true;
456 return callbacks.oncomplete(req);
461 if(status == OSRF_STATUS_OK) {
462 ses.state = OSRF_APP_SESSION_CONNECTED;
464 /* call the connect callback */
465 if(ses.onconnect && !ses.onconnect_called) {
466 ses.onconnect_called = true;
467 return ses.onconnect();
471 if(status == OSRF_STATUS_NOTFOUND || status == OSRF_STATUS_INTERNALSERVERERROR) {
472 req = ses.find_request(osrf_msg.threadTrace());
473 if(callbacks.onmethoderror)
474 return callbacks.onmethoderror(req, status, status_text);
478 if(osrf_msg.type() == OSRF_MESSAGE_TYPE_RESULT) {
479 req = ses.find_request(osrf_msg.threadTrace());
481 req.response_queue.push(osrf_msg.payload());
482 if(callbacks.onresponse)
483 return callbacks.onresponse(req);