1 // -----------------------------------------------------------------------------
2 // This houses all of the domain object code.
3 // -----------------------------------------------------------------------------
8 // -----------------------------------------------------------------------------
11 DomainObject.prototype = new domainObject();
12 DomainObject.prototype.constructor = DomainObject;
13 DomainObject.prototype.baseClass = domainObject.prototype.constructor;
15 /** Top level DomainObject class. This most provides convience methods
16 * and a shared superclass
18 function DomainObject( name ) {
19 if( name ) { this._init_domainObject( name ); }
22 /** Returns the actual element of the given domainObjectAttr. */
23 DomainObject.prototype._findAttr = function ( name ) {
25 var nodes = this.element.childNodes;
27 if( ! nodes || nodes.length < 1 ) {
28 throw new oils_ex_dom( "Invalid xml object in _findAttr: " + this.toString() );
32 var node = nodes.item(i);
34 while( node != null ) {
36 if( node.nodeName == "oils:domainObjectAttr" &&
37 node.getAttribute("name") == name ) {
41 node = nodes.item(++i);
50 /** Returns the value stored in the given attribute */
51 DomainObject.prototype.getAttr = function ( name ) {
53 var node = this._findAttr( name );
54 if( node ) { return node.getAttribute( "value" ); }
56 throw new oils_ex_dom( "getAttr(); Getting nonexistent attribute: " + name );
60 /** Updates the value held by the given attribute */
61 DomainObject.prototype.setAttr = function ( name, value ) {
63 var node = this._findAttr( name );
65 node.setAttribute( "value", value );
67 throw new oils_ex_dom( "setAttr(); Setting nonexistent attribute: " + name );
71 /** This takes a raw DOM Element node and creates the DomainObject that the node
72 * embodies and returns the object. All new DomainObjects should be added to
73 * this list if they require this type of dynamic functionality.
74 * NOTE Much of this will be deprecated as move to a more JSON-centric wire protocol
76 DomainObject.newFromNode = function( node ) {
78 switch( node.getAttribute("name") ) {
81 return new oilsMethod().replaceNode( node );
84 return new oilsMessage().replaceNode( node );
87 return new oilsResponse().replaceNode( node );
90 return new oilsResult().replaceNode( node );
92 case "oilsConnectStatus":
93 return new oilsConnectStatus().replaceNode( node );
96 return new oilsException().replaceNode( node );
98 case "oilsMethodException":
99 return new oilsMethodException().replaceNode( node );
101 case "oilsBrokenSession":
102 return new oilsBrokenSession().replaceNode( node );
105 return new oilsScalar().replaceNode( node );
108 return new oilsPair().replaceNode( node );
111 return new oilsArray().replaceNode( node );
114 return new oilsHash().replaceNode( node );
123 // -----------------------------------------------------------------------------
126 oilsMethod.prototype = new DomainObject();
127 oilsMethod.prototype.constructor = oilsMethod;
128 oilsMethod.prototype.baseClass = DomainObject.prototype.constructor;
131 * oilsMethod Constructor
133 * @param method_name The name of the method your are sending
134 * to the remote server
135 * @param params_array A Javascript array of the params (any
136 * Javascript data type)
139 function oilsMethod( method_name, params_array ) {
140 this._init_domainObject( "oilsMethod" );
141 this.add( new domainObjectAttr( "method", method_name ) );
144 var params = this.doc.createElement( "oils:params" );
145 this.element.appendChild( params );
146 params.appendChild( this.doc.createTextNode(
147 js2JSON( params_array ) ) );
152 /** Locates the params node of this method */
153 oilsMethod.prototype._getParamsNode = function() {
155 var nodes = this.element.childNodes;
156 if( ! nodes || nodes.length < 1 ) { return null; }
158 var node = nodes.item(x);
159 while( node != null ) {
160 if( node.nodeName == "oils:params" ) {
163 node = nodes.item(++x);
170 /** Returns an array of param objects */
171 oilsMethod.prototype.getParams = function() {
172 var node = this._getParamsNode();
174 return JSON2js( node.textContent );
180 // -----------------------------------------------------------------------------
183 // -----------------------------------------------------------------------------
184 // oilsMessage message types
185 // -----------------------------------------------------------------------------
186 /** CONNECT Message type */
187 oilsMessage.CONNECT = 'CONNECT';
188 /** DISCONNECT Message type */
189 oilsMessage.DISCONNECT = 'DISCONNECT';
190 /** STATUS Message type */
191 oilsMessage.STATUS = 'STATUS';
192 /** REQUEST Message type */
193 oilsMessage.REQUEST = 'REQUEST';
194 /** RESULT Message type */
195 oilsMessage.RESULT = 'RESULT';
198 oilsMessage.prototype = new DomainObject();
199 oilsMessage.prototype.constructor = oilsMessage;
200 oilsMessage.prototype.baseClass = DomainObject.prototype.constructor;
202 /** Core XML object for message passing */
203 function oilsMessage( type, protocol, user_auth ) {
205 if( !( type && protocol) ) {
206 type = oilsMessage.CONNECT;
210 if( ! ( type == oilsMessage.CONNECT ||
211 type == oilsMessage.DISCONNECT ||
212 type == oilsMessage.STATUS ||
213 type == oilsMessage.REQUEST ||
214 type == oilsMessage.RESULT ) ) {
215 throw new oils_ex_message( "Attempt to create oilsMessage with incorrect type: " + type );
218 this._init_domainObject( "oilsMessage" );
220 this.add( new domainObjectAttr( "type", type ) );
221 this.add( new domainObjectAttr( "threadTrace", 0 ) );
222 this.add( new domainObjectAttr( "protocol", protocol ) );
224 if( user_auth ) { this.add( user_auth ); }
228 /** Builds a new oilsMessage from raw xml.
229 * Checks are taken to make sure the xml is supposed to be an oilsMessage.
230 * If not, an oils_ex_dom exception is throw.
232 oilsMessage.newFromXML = function( xml ) {
236 if( ! xml || xml == "" ) { throw 1; }
238 var doc = new DOMParser().parseFromString( xml, "text/xml" );
239 if( ! doc ) { throw 1; }
241 var root = doc.documentElement;
242 if( ! root ) { throw 1; }
244 // -----------------------------------------------------------------------------
245 // There are two options here. One is that we were provided the full message
246 // xml (i.e. contains the <oils:root> tag. The other option is that we were
247 // provided just the message xml portion (top level element is the
248 // <oils:domainObject>
249 // -----------------------------------------------------------------------------
252 if( root.nodeName == "oils:root" ) {
254 element = root.firstChild;
255 if( ! element ) { throw 1; }
259 if( root.nodeName == "oils:domainObject" ) {
267 if( element.nodeName != "oils:domainObject" ) { throw 1; }
268 if( element.getAttribute( "name" ) != "oilsMessage" ) { throw 1; }
270 return new oilsMessage().replaceNode( element );
274 if( E && E.message ) {
275 throw new oils_ex_dom( "Bogus XML for creating oilsMessage: " + E.message + "\n" + xml );
278 throw new oils_ex_dom( "Bogus XML for creating oilsMessage:\n" + xml );
287 /** Adds a copy of the given DomainObject to the message */
288 oilsMessage.prototype.addPayload = function( new_DomainObject ) {
289 this.add( new_DomainObject );
293 /** Returns the top level DomainObject contained in this message
295 * Note: The object retuturned will be the actual object, not just a
296 * generic DomainObject - e.g. oilsException.
298 oilsMessage.prototype.getPayload = function() {
300 var nodes = this.element.childNodes;
302 var node = nodes.item(0);
304 while( node != null ) {
306 if( node.nodeName == "oils:domainObject" ) {
308 new Logger().debug( "Building oilsMessage payload from\n" +
309 new XMLSerializer().serializeToString( node ), Logger.DEBUG );
310 return DomainObject.newFromNode( node );
312 node = nodes.item(++x);
318 oilsMessage.prototype.getUserAuth = function() {
320 var nodes = this.element.getElementsByTagName( "oils:userAuth" );
321 if( ! nodes ) { return null; }
323 var auth_elem = nodes.item(0); // should only be one
324 if ( ! auth_elem ) { return null; }
326 return new userAuth().replaceNode( auth_elem );
330 /** Returns the type of the message */
331 oilsMessage.prototype.getType = function() {
332 return this.getAttr( "type" );
335 /** Returns the message protocol */
336 oilsMessage.prototype.getProtocol = function() {
337 return this.getAttr( "protocol" );
340 /** Returns the threadTrace */
341 oilsMessage.prototype.getThreadTrace = function() {
342 return this.getAttr( "threadTrace" );
345 /** Sets the thread trace - MUST be an integer */
346 oilsMessage.prototype.setThreadTrace = function( trace ) {
347 this.setAttr( "threadTrace", trace );
350 /** Increments the thread trace by 1 */
351 oilsMessage.prototype.updateThreadTrace = function() {
352 var tt = this.getThreadTrace();
353 return this.setThreadTrace( ++tt );
360 // -----------------------------------------------------------------------------
364 // -----------------------------------------------------------------------------
366 // -----------------------------------------------------------------------------
369 * Below are the various response statuses
371 oilsResponse.STATUS_CONTINUE = 100
373 oilsResponse.STATUS_OK = 200
374 oilsResponse.STATUS_ACCEPTED = 202
375 oilsResponse.STATUS_COMPLETE = 205
377 oilsResponse.STATUS_REDIRECTED = 307
379 oilsResponse.STATUS_BADREQUEST = 400
380 oilsResponse.STATUS_UNAUTHORIZED = 401
381 oilsResponse.STATUS_FORBIDDEN = 403
382 oilsResponse.STATUS_NOTFOUND = 404
383 oilsResponse.STATUS_NOTALLOWED = 405
384 oilsResponse.STATUS_TIMEOUT = 408
385 oilsResponse.STATUS_EXPFAILED = 417
386 oilsResponse.STATUS_INTERNALSERVERERROR = 500
387 oilsResponse.STATUS_NOTIMPLEMENTED = 501
388 oilsResponse.STATUS_VERSIONNOTSUPPORTED = 505
391 oilsResponse.prototype = new DomainObject();
392 oilsResponse.prototype.constructor = oilsResponse;
393 oilsResponse.prototype.baseClass = DomainObject.prototype.constructor;
395 /** Constructor. status is the text describing the message status and
396 * statusCode must correspond to one of the oilsResponse status code numbers
398 function oilsResponse( name, status, statusCode ) {
399 if( name && status && statusCode ) {
400 this._initResponse( name, status, statusCode );
404 /** Initializes the reponse XML */
405 oilsResponse.prototype._initResponse = function( name, status, statusCode ) {
407 this._init_domainObject( name );
408 this.add( new domainObjectAttr( "status", status ));
409 this.add( new domainObjectAttr( "statusCode", statusCode ) );
414 /** Returns the status of the response */
415 oilsResponse.prototype.getStatus = function() {
416 return this.getAttr( "status" );
419 /** Returns the statusCode of the response */
420 oilsResponse.prototype.getStatusCode = function() {
421 return this.getAttr("statusCode");
425 oilsConnectStatus.prototype = new oilsResponse();
426 oilsConnectStatus.prototype.constructor = oilsConnectStatus;
427 oilsConnectStatus.prototype.baseClass = oilsResponse.prototype.constructor;
429 /** Constructor. These give us info on our connection attempts **/
430 function oilsConnectStatus( status, statusCode ) {
431 this._initResponse( "oilsConnectStatus", status, statusCode );
435 oilsResult.prototype = new oilsResponse();
436 oilsResult.prototype.constructor = oilsResult;
437 oilsResult.prototype.baseClass = oilsResponse.prototype.constructor;
440 /** Constructor. These usually carry REQUEST responses */
441 function oilsResult( status, statusCode ) {
442 if( status && statusCode ) {
443 this._initResponse( "oilsResult", status, statusCode );
447 /** Returns the top level DomainObject within this result message.
448 * Note: The object retuturned will be the actual object, not just a
449 * generic DomainObject - e.g. oilsException.
451 oilsResult.prototype.getContent = function() {
453 var nodes = this.element.childNodes;
454 if( ! nodes || nodes.length < 1 ) {
455 throw new oils_ex_dom("Content node for oilsResult is invalid\n" + this.toString() );
458 var node = nodes.item(x);
459 while( node != null ) {
461 if( node.nodeName == "oils:domainObject" ||
462 node.nodeName == "oils:domainObjectCollection" ) {
464 return DomainObject.newFromNode( node );
466 node = nodes.item(++x);
469 throw new oils_ex_dom("Content node for oilsResult is invalid\n" + this.toString() );
473 oilsException.prototype = new oilsResponse();
474 oilsException.prototype.constructor = oilsException;
475 oilsException.prototype.baseClass = oilsResponse.prototype.constructor;
477 /** Top level exception */
478 function oilsException( status, statusCode ) {
479 if( status && statusCode ) {
480 this._initResponse( "oilsException", status, statusCode );
485 oilsMethodException.prototype = new oilsException();
486 oilsMethodException.prototype.constructor = oilsMethodException;
487 oilsMethodException.prototype.baseClass = oilsException.prototype.constructor;
489 /** Method exception */
490 function oilsMethodException( status, statusCode ) {
491 if( status && statusCode ) {
492 this._initResponse( "oilsMethodException", status, statusCode );
496 oilsBrokenSession.prototype = new oilsException();
497 oilsBrokenSession.prototype.constructor = oilsBrokenSession;
498 oilsBrokenSession.prototype.baseClass = oilsException.prototype.constructor;
500 /** broken session exception */
501 function oilsBrokenSession( status, statusCode ) {
502 if( status && statusCode ) {
503 this._initResponse( "oilsBrokenSession", status, statusCode );
508 // -----------------------------------------------------------------------------
512 oilsScalar.prototype = new DomainObject();
513 oilsScalar.prototype.constructor = oilsScalar;
514 oilsScalar.prototype.baseClass = DomainObject.prototype.constructor;
516 /** Contains a single JSON item as its text content */
517 function oilsScalar( value ) {
518 this._init_domainObject( "oilsScalar" );
519 this.element.appendChild( this.doc.createTextNode( value ) );
522 /** Returns the contained item as a Javascript object */
523 oilsScalar.prototype.getValue = function() {
524 return JSON2js( this.element.textContent );
528 // -----------------------------------------------------------------------------
532 oilsPair.prototype = new DomainObject()
533 oilsPair.prototype.constructor = oilsPair;
534 oilsPair.prototype.baseClass = DomainObject.prototype.constructor;
536 function oilsPair( key, value ) {
538 this._init_domainObject( "oilsPair" );
539 this.element.appendChild( this.doc.createTextNode( value ) );
540 this.element.setAttribute( "key", key );
543 oilsPair.prototype.getKey = function() {
544 return this.element.getAttribute( "key" );
547 oilsPair.prototype.getValue = function() {
548 return this.element.textContent;
553 // -----------------------------------------------------------------------------
554 // oilsArray - is a domainObjectCollection that stores a list of domainObject's
555 // or domainObjectCollections.
556 // -----------------------------------------------------------------------------
558 oilsArray.prototype = new domainObjectCollection()
559 oilsArray.prototype.constructor = oilsArray;
560 oilsArray.prototype.baseClass = domainObjectCollection.prototype.constructor;
562 function oilsArray( obj_list ) {
563 this._initCollection( "oilsArray" );
564 if(obj_list) { this.initArray( obj_list ); }
567 // -----------------------------------------------------------------------------
568 // Adds the array of objects to the array, these should be actual objects, not
569 // DOM nodes/elements, etc.
570 // -----------------------------------------------------------------------------
571 oilsArray.prototype.initArray = function( obj_list ) {
572 if( obj_array != null && obj_array.length > 0 ) {
573 for( var i= 0; i!= obj_array.length; i++ ) {
574 this.add( obj_array[i] );
579 // -----------------------------------------------------------------------------
580 // Returns an array of DomainObjects or domainObjectCollections. The objects
581 // returned will already be 'cast' into the correct object. i.e. you will not
582 // receieve an array of DomainObjects, but instead an array of oilsScalar's or
583 // an array of oilsHashe's, etc.
584 // -----------------------------------------------------------------------------
585 oilsArray.prototype.getObjects = function() {
587 var obj_list = new Array();
588 var nodes = this.element.childNodes;
590 var node = nodes.item(i);
592 while( node != null ) {
594 if( node.nodeName == "oils:domainObject" ||
595 node.nodeName == "oils:domainObjectCollection" ) {
596 obj_list[i++] = DomainObject.newFromNode( node );
598 node = nodes.item(i);
605 // -----------------------------------------------------------------------------
606 // oilsHash - an oilsHash is an oilsArray except it only stores oilsPair's
607 // -----------------------------------------------------------------------------
609 oilsHash.prototype = new oilsArray();
610 oilsHash.prototype.constructor = oilsHash;
611 oilsHash.prototype.baseClass = oilsArray.prototype.constructor;
613 function oilsHash( pair_array ) {
614 this._initCollection("oilsHash");
615 if(pair_array) { this.initArray( pair_array ); }
618 // -----------------------------------------------------------------------------
619 // Returns the array of oilsPairs objects that this hash contains
620 // -----------------------------------------------------------------------------
621 oilsHash.prototype.getPairs = function() {
622 return this.getObjects();