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 ) {
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 // user_auth used to be a parameter for oilsMessage
225 //if( user_auth ) { this.add( user_auth ); }
229 /** Builds a new oilsMessage from raw xml.
230 * Checks are taken to make sure the xml is supposed to be an oilsMessage.
231 * If not, an oils_ex_dom exception is throw.
233 oilsMessage.newFromXML = function( xml ) {
237 if( ! xml || xml == "" ) { throw 1; }
239 var doc = new DOMParser().parseFromString( xml, "text/xml" );
240 if( ! doc ) { throw 1; }
242 var root = doc.documentElement;
243 if( ! root ) { throw 1; }
245 // -----------------------------------------------------------------------------
246 // There are two options here. One is that we were provided the full message
247 // xml (i.e. contains the <oils:root> tag. The other option is that we were
248 // provided just the message xml portion (top level element is the
249 // <oils:domainObject>
250 // -----------------------------------------------------------------------------
253 if( root.nodeName == "oils:root" ) {
255 element = root.firstChild;
256 if( ! element ) { throw 1; }
260 if( root.nodeName == "oils:domainObject" ) {
268 if( element.nodeName != "oils:domainObject" ) { throw 1; }
269 if( element.getAttribute( "name" ) != "oilsMessage" ) { throw 1; }
271 return new oilsMessage().replaceNode( element );
275 if( E && E.message ) {
276 throw new oils_ex_dom( "Bogus XML for creating oilsMessage: " + E.message + "\n" + xml );
279 throw new oils_ex_dom( "Bogus XML for creating oilsMessage:\n" + xml );
288 /** Adds a copy of the given DomainObject to the message */
289 oilsMessage.prototype.addPayload = function( new_DomainObject ) {
290 this.add( new_DomainObject );
294 /** Returns the top level DomainObject contained in this message
296 * Note: The object retuturned will be the actual object, not just a
297 * generic DomainObject - e.g. oilsException.
299 oilsMessage.prototype.getPayload = function() {
301 var nodes = this.element.childNodes;
303 var node = nodes.item(0);
305 while( node != null ) {
307 if( node.nodeName == "oils:domainObject" ) {
309 new Logger().debug( "Building oilsMessage payload from\n" +
310 new XMLSerializer().serializeToString( node ), Logger.DEBUG );
311 return DomainObject.newFromNode( node );
313 node = nodes.item(++x);
319 oilsMessage.prototype.getUserAuth = function() {
321 var nodes = this.element.getElementsByTagName( "oils:userAuth" );
322 if( ! nodes ) { return null; }
324 var auth_elem = nodes.item(0); // should only be one
325 if ( ! auth_elem ) { return null; }
327 return new userAuth().replaceNode( auth_elem );
331 /** Returns the type of the message */
332 oilsMessage.prototype.getType = function() {
333 return this.getAttr( "type" );
336 /** Returns the message protocol */
337 oilsMessage.prototype.getProtocol = function() {
338 return this.getAttr( "protocol" );
341 /** Returns the threadTrace */
342 oilsMessage.prototype.getThreadTrace = function() {
343 return this.getAttr( "threadTrace" );
346 /** Sets the thread trace - MUST be an integer */
347 oilsMessage.prototype.setThreadTrace = function( trace ) {
348 this.setAttr( "threadTrace", trace );
351 /** Increments the thread trace by 1 */
352 oilsMessage.prototype.updateThreadTrace = function() {
353 var tt = this.getThreadTrace();
354 return this.setThreadTrace( ++tt );
361 // -----------------------------------------------------------------------------
365 // -----------------------------------------------------------------------------
367 // -----------------------------------------------------------------------------
370 * Below are the various response statuses
372 oilsResponse.STATUS_CONTINUE = 100
374 oilsResponse.STATUS_OK = 200
375 oilsResponse.STATUS_ACCEPTED = 202
376 oilsResponse.STATUS_COMPLETE = 205
378 oilsResponse.STATUS_REDIRECTED = 307
380 oilsResponse.STATUS_BADREQUEST = 400
381 oilsResponse.STATUS_UNAUTHORIZED = 401
382 oilsResponse.STATUS_FORBIDDEN = 403
383 oilsResponse.STATUS_NOTFOUND = 404
384 oilsResponse.STATUS_NOTALLOWED = 405
385 oilsResponse.STATUS_TIMEOUT = 408
386 oilsResponse.STATUS_EXPFAILED = 417
387 oilsResponse.STATUS_INTERNALSERVERERROR = 500
388 oilsResponse.STATUS_NOTIMPLEMENTED = 501
389 oilsResponse.STATUS_VERSIONNOTSUPPORTED = 505
392 oilsResponse.prototype = new DomainObject();
393 oilsResponse.prototype.constructor = oilsResponse;
394 oilsResponse.prototype.baseClass = DomainObject.prototype.constructor;
396 /** Constructor. status is the text describing the message status and
397 * statusCode must correspond to one of the oilsResponse status code numbers
399 function oilsResponse( name, status, statusCode ) {
400 if( name && status && statusCode ) {
401 this._initResponse( name, status, statusCode );
405 /** Initializes the reponse XML */
406 oilsResponse.prototype._initResponse = function( name, status, statusCode ) {
408 this._init_domainObject( name );
409 this.add( new domainObjectAttr( "status", status ));
410 this.add( new domainObjectAttr( "statusCode", statusCode ) );
415 /** Returns the status of the response */
416 oilsResponse.prototype.getStatus = function() {
417 return this.getAttr( "status" );
420 /** Returns the statusCode of the response */
421 oilsResponse.prototype.getStatusCode = function() {
422 return this.getAttr("statusCode");
426 oilsConnectStatus.prototype = new oilsResponse();
427 oilsConnectStatus.prototype.constructor = oilsConnectStatus;
428 oilsConnectStatus.prototype.baseClass = oilsResponse.prototype.constructor;
430 /** Constructor. These give us info on our connection attempts **/
431 function oilsConnectStatus( status, statusCode ) {
432 this._initResponse( "oilsConnectStatus", status, statusCode );
436 oilsResult.prototype = new oilsResponse();
437 oilsResult.prototype.constructor = oilsResult;
438 oilsResult.prototype.baseClass = oilsResponse.prototype.constructor;
441 /** Constructor. These usually carry REQUEST responses */
442 function oilsResult( status, statusCode ) {
443 if( status && statusCode ) {
444 this._initResponse( "oilsResult", status, statusCode );
448 /** Returns the top level DomainObject within this result message.
449 * Note: The object retuturned will be the actual object, not just a
450 * generic DomainObject - e.g. oilsException.
452 oilsResult.prototype.getContent = function() {
454 var nodes = this.element.childNodes;
455 if( ! nodes || nodes.length < 1 ) {
456 throw new oils_ex_dom("Content node for oilsResult is invalid\n" + this.toString() );
459 var node = nodes.item(x);
460 while( node != null ) {
462 if( node.nodeName == "oils:domainObject" ||
463 node.nodeName == "oils:domainObjectCollection" ) {
465 //return DomainObject.newFromNode( node );
466 return JSON2js( node.textContent );
468 node = nodes.item(++x);
471 throw new oils_ex_dom("Content node for oilsResult is invalid\n" + this.toString() );
475 oilsException.prototype = new oilsResponse();
476 oilsException.prototype.constructor = oilsException;
477 oilsException.prototype.baseClass = oilsResponse.prototype.constructor;
479 /** Top level exception */
480 function oilsException( status, statusCode ) {
481 if( status && statusCode ) {
482 this._initResponse( "oilsException", status, statusCode );
487 oilsMethodException.prototype = new oilsException();
488 oilsMethodException.prototype.constructor = oilsMethodException;
489 oilsMethodException.prototype.baseClass = oilsException.prototype.constructor;
491 /** Method exception */
492 function oilsMethodException( status, statusCode ) {
493 if( status && statusCode ) {
494 this._initResponse( "oilsMethodException", status, statusCode );
498 oilsBrokenSession.prototype = new oilsException();
499 oilsBrokenSession.prototype.constructor = oilsBrokenSession;
500 oilsBrokenSession.prototype.baseClass = oilsException.prototype.constructor;
502 /** broken session exception */
503 function oilsBrokenSession( status, statusCode ) {
504 if( status && statusCode ) {
505 this._initResponse( "oilsBrokenSession", status, statusCode );
510 // -----------------------------------------------------------------------------
514 oilsScalar.prototype = new DomainObject();
515 oilsScalar.prototype.constructor = oilsScalar;
516 oilsScalar.prototype.baseClass = DomainObject.prototype.constructor;
518 /** Contains a single JSON item as its text content */
519 function oilsScalar( value ) {
520 this._init_domainObject( "oilsScalar" );
521 this.element.appendChild( this.doc.createTextNode( value ) );
524 /** Returns the contained item as a Javascript object */
525 oilsScalar.prototype.getValue = function() {
526 return JSON2js( this.element.textContent );
530 // -----------------------------------------------------------------------------
534 oilsPair.prototype = new DomainObject()
535 oilsPair.prototype.constructor = oilsPair;
536 oilsPair.prototype.baseClass = DomainObject.prototype.constructor;
538 function oilsPair( key, value ) {
540 this._init_domainObject( "oilsPair" );
541 this.element.appendChild( this.doc.createTextNode( value ) );
542 this.element.setAttribute( "key", key );
545 oilsPair.prototype.getKey = function() {
546 return this.element.getAttribute( "key" );
549 oilsPair.prototype.getValue = function() {
550 return this.element.textContent;
555 // -----------------------------------------------------------------------------
556 // oilsArray - is a domainObjectCollection that stores a list of domainObject's
557 // or domainObjectCollections.
558 // -----------------------------------------------------------------------------
560 oilsArray.prototype = new domainObjectCollection()
561 oilsArray.prototype.constructor = oilsArray;
562 oilsArray.prototype.baseClass = domainObjectCollection.prototype.constructor;
564 function oilsArray( obj_list ) {
565 this._initCollection( "oilsArray" );
566 if(obj_list) { this.initArray( obj_list ); }
569 // -----------------------------------------------------------------------------
570 // Adds the array of objects to the array, these should be actual objects, not
571 // DOM nodes/elements, etc.
572 // -----------------------------------------------------------------------------
573 oilsArray.prototype.initArray = function( obj_list ) {
574 if( obj_array != null && obj_array.length > 0 ) {
575 for( var i= 0; i!= obj_array.length; i++ ) {
576 this.add( obj_array[i] );
581 // -----------------------------------------------------------------------------
582 // Returns an array of DomainObjects or domainObjectCollections. The objects
583 // returned will already be 'cast' into the correct object. i.e. you will not
584 // receieve an array of DomainObjects, but instead an array of oilsScalar's or
585 // an array of oilsHashe's, etc.
586 // -----------------------------------------------------------------------------
587 oilsArray.prototype.getObjects = function() {
589 var obj_list = new Array();
590 var nodes = this.element.childNodes;
592 var node = nodes.item(i);
594 while( node != null ) {
596 if( node.nodeName == "oils:domainObject" ||
597 node.nodeName == "oils:domainObjectCollection" ) {
598 obj_list[i++] = DomainObject.newFromNode( node );
600 node = nodes.item(i);
607 // -----------------------------------------------------------------------------
608 // oilsHash - an oilsHash is an oilsArray except it only stores oilsPair's
609 // -----------------------------------------------------------------------------
611 oilsHash.prototype = new oilsArray();
612 oilsHash.prototype.constructor = oilsHash;
613 oilsHash.prototype.baseClass = oilsArray.prototype.constructor;
615 function oilsHash( pair_array ) {
616 this._initCollection("oilsHash");
617 if(pair_array) { this.initArray( pair_array ); }
620 // -----------------------------------------------------------------------------
621 // Returns the array of oilsPairs objects that this hash contains
622 // -----------------------------------------------------------------------------
623 oilsHash.prototype.getPairs = function() {
624 return this.getObjects();