[483] | 1 | define([ |
---|
| 2 | "dojo/_base/declare", |
---|
| 3 | "dojo/_base/kernel", |
---|
| 4 | "dojo/_base/xhr", |
---|
| 5 | "./model" |
---|
| 6 | ], function (declare, kernel, xhrUtil, model){ |
---|
| 7 | |
---|
| 8 | return declare("dojox.atom.io.Connection",null,{ |
---|
| 9 | // summary: |
---|
| 10 | // This object implements a transport layer for working with ATOM feeds and ATOM publishing protocols. |
---|
| 11 | // description: |
---|
| 12 | // This object implements a transport layer for working with ATOM feeds and ATOM publishing protocols. |
---|
| 13 | // Specifically, it provides a mechanism by which feeds can be fetched and entries can be fetched, created |
---|
| 14 | // deleted, and modified. It also provides access to the introspection data. |
---|
| 15 | |
---|
| 16 | constructor: function(/* Boolean */sync, /* Boolean */preventCache){ |
---|
| 17 | // summary: |
---|
| 18 | // initializer |
---|
| 19 | this.sync = sync; |
---|
| 20 | this.preventCache = preventCache; |
---|
| 21 | }, |
---|
| 22 | |
---|
| 23 | preventCache: false, //Flag to denote if the instance should use the xhr prevent cache mechanism |
---|
| 24 | |
---|
| 25 | alertsEnabled: false, //Flag to turn on alerts instead of throwing errors. |
---|
| 26 | |
---|
| 27 | getFeed: function(/*String*/url, /*Function*/callback, /*Function*/errorCallback, scope){ |
---|
| 28 | // summary: |
---|
| 29 | // Function to obtain a s specific ATOM feed from a given ATOM Feed url. |
---|
| 30 | // description: |
---|
| 31 | // This function takes the URL for a specific ATOM feed and returns |
---|
| 32 | // the data from that feed to the caller through the use of a callback |
---|
| 33 | // handler. |
---|
| 34 | // url: String |
---|
| 35 | // The URL of the ATOM feed to fetch. |
---|
| 36 | // callback: |
---|
| 37 | // Function |
---|
| 38 | // A function reference that will handle the feed when it has been retrieved. |
---|
| 39 | // The callback should accept two parameters: The feed object and the original complete DOM object. |
---|
| 40 | // scope: Object |
---|
| 41 | // The scope to use for all callbacks. |
---|
| 42 | // returns: |
---|
| 43 | // Nothing. The return is handled through the callback handler. |
---|
| 44 | this._getXmlDoc(url, "feed", new model.Feed(), model._Constants.ATOM_NS, callback, /*handleDocumentRetrieved,*/ errorCallback, scope); |
---|
| 45 | }, |
---|
| 46 | |
---|
| 47 | getService: function(url, callback, errorCallback, scope){ |
---|
| 48 | // summary: |
---|
| 49 | // Function to retrieve an introspection document from the given URL. |
---|
| 50 | // description: |
---|
| 51 | // This function takes the URL for an ATOM item and feed and returns |
---|
| 52 | // the introspection document. |
---|
| 53 | // url: |
---|
| 54 | // String |
---|
| 55 | // The URL of the ATOM document to obtain the introspection document of. |
---|
| 56 | // callback: |
---|
| 57 | // Function |
---|
| 58 | // A function reference that will handle the introspection document when it has been retrieved. |
---|
| 59 | // The callback should accept two parameters: The introspection document object and the original complete DOM object. |
---|
| 60 | // returns: |
---|
| 61 | // Nothing. The return is handled through the callback handler. |
---|
| 62 | this._getXmlDoc(url, "service", new model.Service(url), model._Constants.APP_NS, callback, errorCallback, scope); |
---|
| 63 | }, |
---|
| 64 | |
---|
| 65 | getEntry: function(url, callback, errorCallback, scope){ |
---|
| 66 | // summary: |
---|
| 67 | // Function to retrieve a single entry from an ATOM feed from the given URL. |
---|
| 68 | // description: |
---|
| 69 | // This function takes the URL for an ATOM entry and returns the constructed dojox.atom.io.model.Entry object through |
---|
| 70 | // the specified callback. |
---|
| 71 | // url: |
---|
| 72 | // String |
---|
| 73 | // The URL of the ATOM Entry document to parse. |
---|
| 74 | // callback: |
---|
| 75 | // Function |
---|
| 76 | // A function reference that will handle the Entry object obtained. |
---|
| 77 | // The callback should accept two parameters, the dojox.atom.io.model.Entry object and the original dom. |
---|
| 78 | // returns: |
---|
| 79 | // Nothing. The return is handled through the callback handler. |
---|
| 80 | this._getXmlDoc(url, "entry", new model.Entry(), model._Constants.ATOM_NS, callback, errorCallback, scope); |
---|
| 81 | }, |
---|
| 82 | |
---|
| 83 | _getXmlDoc: function(url, nodeName, newNode, namespace, callback, errorCallback, scope){ |
---|
| 84 | // summary: |
---|
| 85 | // Internal Function to retrieve an XML document and pass the results to a callback. |
---|
| 86 | // description: |
---|
| 87 | // This internal function takes the URL for an XML document and and passes the |
---|
| 88 | // parsed contents to a specified callback. |
---|
| 89 | // url: |
---|
| 90 | // String |
---|
| 91 | // The URL of the XML document to retrieve |
---|
| 92 | // callback: |
---|
| 93 | // Function |
---|
| 94 | // A function reference that will handle the retrieved XML data. |
---|
| 95 | // The callback should accept one parameter, the DOM of the parsed XML document. |
---|
| 96 | // returns: |
---|
| 97 | // Nothing. The return is handled through the callback handler. |
---|
| 98 | if(!scope){ |
---|
| 99 | scope = kernel.global; |
---|
| 100 | } |
---|
| 101 | var ae = this.alertsEnabled; |
---|
| 102 | var xhrArgs = { |
---|
| 103 | url: url, |
---|
| 104 | handleAs: "xml", |
---|
| 105 | sync: this.sync, |
---|
| 106 | preventCache: this.preventCache, |
---|
| 107 | load: function(data, args){ |
---|
| 108 | var node = null; |
---|
| 109 | var evaldObj = data; |
---|
| 110 | var nodes; |
---|
| 111 | if(evaldObj){ |
---|
| 112 | //find the first node of the appropriate name |
---|
| 113 | if(typeof(evaldObj.getElementsByTagNameNS)!= "undefined"){ |
---|
| 114 | nodes = evaldObj.getElementsByTagNameNS(namespace,nodeName); |
---|
| 115 | if(nodes && nodes.length > 0){ |
---|
| 116 | node = nodes.item(0); |
---|
| 117 | }else if(evaldObj.lastChild){ |
---|
| 118 | // name_spaces can be used without declaration of atom (for example |
---|
| 119 | // gooogle feeds often returns iTunes name_space qualifiers on elements) |
---|
| 120 | // Treat this situation like name_spaces not enabled. |
---|
| 121 | node = evaldObj.lastChild; |
---|
| 122 | } |
---|
| 123 | }else if(typeof(evaldObj.getElementsByTagName)!= "undefined"){ |
---|
| 124 | // Find the first eith the correct tag name and correct namespace. |
---|
| 125 | nodes = evaldObj.getElementsByTagName(nodeName); |
---|
| 126 | if(nodes && nodes.length > 0){ |
---|
| 127 | for(var i=0; i<nodes.length; i++){ |
---|
| 128 | if(nodes[i].namespaceURI == namespace){ |
---|
| 129 | node = nodes[i]; |
---|
| 130 | break; |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | }else if(evaldObj.lastChild){ |
---|
| 134 | node = evaldObj.lastChild; |
---|
| 135 | } |
---|
| 136 | }else if(evaldObj.lastChild){ |
---|
| 137 | node = evaldObj.lastChild; |
---|
| 138 | }else{ |
---|
| 139 | callback.call(scope, null, null, args); |
---|
| 140 | return; |
---|
| 141 | } |
---|
| 142 | newNode.buildFromDom(node); |
---|
| 143 | if(callback){ |
---|
| 144 | callback.call(scope, newNode, evaldObj, args); |
---|
| 145 | }else if(ae){ |
---|
| 146 | throw new Error("The callback value does not exist."); |
---|
| 147 | } |
---|
| 148 | }else{ |
---|
| 149 | callback.call(scope, null, null, args); |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | }; |
---|
| 153 | |
---|
| 154 | if(this.user && this.user !== null){ |
---|
| 155 | xhrArgs.user = this.user; |
---|
| 156 | } |
---|
| 157 | if(this.password && this.password !== null){ |
---|
| 158 | xhrArgs.password = this.password; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | if(errorCallback){ |
---|
| 162 | xhrArgs.error = function(error, args){errorCallback.call(scope, error, args);}; |
---|
| 163 | }else{ |
---|
| 164 | xhrArgs.error = function(){ |
---|
| 165 | throw new Error("The URL requested cannot be accessed"); |
---|
| 166 | }; |
---|
| 167 | } |
---|
| 168 | xhrUtil.get(xhrArgs); |
---|
| 169 | }, |
---|
| 170 | |
---|
| 171 | updateEntry: function(entry, callback, errorCallback, retrieveUpdated, xmethod, scope){ |
---|
| 172 | // summary: |
---|
| 173 | // Function to update a specific ATOM entry by putting the new changes via APP. |
---|
| 174 | // description: |
---|
| 175 | // This function takes a specific dojox.atom.io.model.Entry object and pushes the |
---|
| 176 | // changes back to the provider of the Entry. |
---|
| 177 | // The entry MUST have a link tag with rel="edit" for this to work. |
---|
| 178 | // entry: |
---|
| 179 | // Object |
---|
| 180 | // The dojox.atom.io.model.Entry object to update. |
---|
| 181 | // callback: |
---|
| 182 | // Function |
---|
| 183 | // A function reference that will handle the results from the entry update. |
---|
| 184 | // The callback should accept two parameters: The first is an Entry object, and the second is the URL of that Entry |
---|
| 185 | // Either can be null, depending on the value of retrieveUpdated. |
---|
| 186 | // retrieveUpdated: |
---|
| 187 | // boolean |
---|
| 188 | // A boolean flag denoting if the entry that was updated should then be |
---|
| 189 | // retrieved and returned to the caller via the callback. |
---|
| 190 | // xmethod: |
---|
| 191 | // boolean |
---|
| 192 | // Whether to use POST for PUT/DELETE items and send the X-Method-Override header. |
---|
| 193 | // scope: |
---|
| 194 | // Object |
---|
| 195 | // The scope to use for all callbacks. |
---|
| 196 | // returns: |
---|
| 197 | // Nothing. The return is handled through the callback handler. |
---|
| 198 | if(!scope){ |
---|
| 199 | scope = kernel.global; |
---|
| 200 | } |
---|
| 201 | entry.updated = new Date(); |
---|
| 202 | var url = entry.getEditHref(); |
---|
| 203 | if(!url){ |
---|
| 204 | throw new Error("A URL has not been specified for editing this entry."); |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | var self = this; |
---|
| 208 | var ae = this.alertsEnabled; |
---|
| 209 | var xhrArgs = { |
---|
| 210 | url: url, |
---|
| 211 | handleAs: "text", |
---|
| 212 | contentType: "text/xml", |
---|
| 213 | sync: this.sync, |
---|
| 214 | preventCache: this.preventCache, |
---|
| 215 | load: function(data, args){ |
---|
| 216 | var location = null; |
---|
| 217 | if(retrieveUpdated){ |
---|
| 218 | location = args.xhr.getResponseHeader("Location"); |
---|
| 219 | if(!location){location = url;} |
---|
| 220 | |
---|
| 221 | //Function to handle the callback mapping of a getEntry after an update to return the |
---|
| 222 | //entry and location. |
---|
| 223 | var handleRetrieve = function(entry, dom, args){ |
---|
| 224 | if(callback){ |
---|
| 225 | callback.call(scope, entry, location, args); |
---|
| 226 | }else if(ae){ |
---|
| 227 | throw new Error("The callback value does not exist."); |
---|
| 228 | } |
---|
| 229 | }; |
---|
| 230 | self.getEntry(location,handleRetrieve); |
---|
| 231 | }else{ |
---|
| 232 | if(callback){ |
---|
| 233 | callback.call(scope, entry, args.xhr.getResponseHeader("Location"), args); |
---|
| 234 | }else if(ae){ |
---|
| 235 | throw new Error("The callback value does not exist."); |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | return data; |
---|
| 239 | } |
---|
| 240 | }; |
---|
| 241 | |
---|
| 242 | if(this.user && this.user !== null){ |
---|
| 243 | xhrArgs.user = this.user; |
---|
| 244 | } |
---|
| 245 | if(this.password && this.password !== null){ |
---|
| 246 | xhrArgs.password = this.password; |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | if(errorCallback){ |
---|
| 250 | xhrArgs.error = function(error, args){errorCallback.call(scope, error, args);}; |
---|
| 251 | }else{ |
---|
| 252 | xhrArgs.error = function(){ |
---|
| 253 | throw new Error("The URL requested cannot be accessed"); |
---|
| 254 | }; |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | if(xmethod){ |
---|
| 258 | xhrArgs.postData = entry.toString(true); //Set the content to send. |
---|
| 259 | xhrArgs.headers = {"X-Method-Override": "PUT"}; |
---|
| 260 | xhrUtil.post(xhrArgs); |
---|
| 261 | }else{ |
---|
| 262 | xhrArgs.putData = entry.toString(true); //Set the content to send. |
---|
| 263 | var xhr = xhrUtil.put(xhrArgs); |
---|
| 264 | } |
---|
| 265 | }, |
---|
| 266 | |
---|
| 267 | addEntry: function(entry, url, callback, errorCallback, retrieveEntry, scope){ |
---|
| 268 | // summary: |
---|
| 269 | // Function to add a new ATOM entry by posting the new entry via APP. |
---|
| 270 | // description: |
---|
| 271 | // This function takes a specific dojox.atom.io.model.Entry object and pushes the |
---|
| 272 | // changes back to the provider of the Entry. |
---|
| 273 | // entry: |
---|
| 274 | // Object |
---|
| 275 | // The dojox.atom.io.model.Entry object to publish. |
---|
| 276 | // callback: |
---|
| 277 | // Function |
---|
| 278 | // A function reference that will handle the results from the entry publish. |
---|
| 279 | // The callback should accept two parameters: The first is an dojox.atom.io.model.Entry object, and the second is the location of the entry |
---|
| 280 | // Either can be null, depending on the value of retrieveUpdated. |
---|
| 281 | // retrieveEntry: |
---|
| 282 | // boolean |
---|
| 283 | // A boolean flag denoting if the entry that was created should then be |
---|
| 284 | // retrieved and returned to the caller via the callback. |
---|
| 285 | // scope: |
---|
| 286 | // Object |
---|
| 287 | // The scope to use for all callbacks. |
---|
| 288 | // returns: |
---|
| 289 | // Nothing. The return is handled through the callback handler. |
---|
| 290 | if(!scope){ |
---|
| 291 | scope = kernel.global; |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | entry.published = new Date(); |
---|
| 295 | entry.updated = new Date(); |
---|
| 296 | |
---|
| 297 | var feedUrl = entry.feedUrl; |
---|
| 298 | var ae = this.alertsEnabled; |
---|
| 299 | |
---|
| 300 | //Determine which URL to use for the post. |
---|
| 301 | if(!url && feedUrl){url = feedUrl;} |
---|
| 302 | if(!url){ |
---|
| 303 | if(ae){ |
---|
| 304 | throw new Error("The request cannot be processed because the URL parameter is missing."); |
---|
| 305 | } |
---|
| 306 | return; |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | var self = this; |
---|
| 310 | var xhrArgs = { |
---|
| 311 | url: url, |
---|
| 312 | handleAs: "text", |
---|
| 313 | contentType: "text/xml", |
---|
| 314 | sync: this.sync, |
---|
| 315 | preventCache: this.preventCache, |
---|
| 316 | postData: entry.toString(true), |
---|
| 317 | load: function(data, args){ |
---|
| 318 | var location = args.xhr.getResponseHeader("Location"); |
---|
| 319 | if(!location){ |
---|
| 320 | location = url; |
---|
| 321 | } |
---|
| 322 | if(!args.retrieveEntry){ |
---|
| 323 | if(callback){ |
---|
| 324 | callback.call(scope, entry, location, args); |
---|
| 325 | }else if(ae){ |
---|
| 326 | throw new Error("The callback value does not exist."); |
---|
| 327 | } |
---|
| 328 | }else{ |
---|
| 329 | //Function to handle the callback mapping of a getEntry after an update to return the |
---|
| 330 | //entry and location. |
---|
| 331 | var handleRetrieve = function(entry, dom, args){ |
---|
| 332 | if(callback){ |
---|
| 333 | callback.call(scope, entry, location, args); |
---|
| 334 | }else if(ae){ |
---|
| 335 | throw new Error("The callback value does not exist."); |
---|
| 336 | } |
---|
| 337 | }; |
---|
| 338 | self.getEntry(location,handleRetrieve); |
---|
| 339 | } |
---|
| 340 | return data; |
---|
| 341 | } |
---|
| 342 | }; |
---|
| 343 | |
---|
| 344 | if(this.user && this.user !== null){ |
---|
| 345 | xhrArgs.user = this.user; |
---|
| 346 | } |
---|
| 347 | if(this.password && this.password !== null){ |
---|
| 348 | xhrArgs.password = this.password; |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | if(errorCallback){ |
---|
| 352 | xhrArgs.error = function(error, args){errorCallback.call(scope, error, args);}; |
---|
| 353 | }else{ |
---|
| 354 | xhrArgs.error = function(){ |
---|
| 355 | throw new Error("The URL requested cannot be accessed"); |
---|
| 356 | }; |
---|
| 357 | } |
---|
| 358 | xhrUtil.post(xhrArgs); |
---|
| 359 | }, |
---|
| 360 | |
---|
| 361 | deleteEntry: function(entry,callback,errorCallback,xmethod,scope){ |
---|
| 362 | // summary: |
---|
| 363 | // Function to delete a specific ATOM entry via APP. |
---|
| 364 | // description: |
---|
| 365 | // This function takes a specific dojox.atom.io.model.Entry object and calls for a delete on the |
---|
| 366 | // service housing the ATOM Entry database. |
---|
| 367 | // The entry MUST have a link tag with rel="edit" for this to work. |
---|
| 368 | // entry: |
---|
| 369 | // Object |
---|
| 370 | // The dojox.atom.io.model.Entry object to delete. |
---|
| 371 | // callback: |
---|
| 372 | // Function |
---|
| 373 | // A function reference that will handle the results from the entry delete. |
---|
| 374 | // The callback is called only if the delete is successful. |
---|
| 375 | // returns: |
---|
| 376 | // Nothing. The return is handled through the callback handler. |
---|
| 377 | if(!scope){ |
---|
| 378 | scope = kernel.global; |
---|
| 379 | } |
---|
| 380 | |
---|
| 381 | var url = null; |
---|
| 382 | if(typeof(entry) == "string"){ |
---|
| 383 | url = entry; |
---|
| 384 | }else{ |
---|
| 385 | url = entry.getEditHref(); |
---|
| 386 | } |
---|
| 387 | if(!url){ |
---|
| 388 | callback.call(scope, false, null); |
---|
| 389 | throw new Error("The request cannot be processed because the URL parameter is missing."); |
---|
| 390 | } |
---|
| 391 | |
---|
| 392 | var xhrArgs = { |
---|
| 393 | url: url, |
---|
| 394 | handleAs: "text", |
---|
| 395 | sync: this.sync, |
---|
| 396 | preventCache: this.preventCache, |
---|
| 397 | load: function(data, args){ |
---|
| 398 | callback.call(scope, args); |
---|
| 399 | return data; |
---|
| 400 | } |
---|
| 401 | }; |
---|
| 402 | |
---|
| 403 | if(this.user && this.user !== null){ |
---|
| 404 | xhrArgs.user = this.user; |
---|
| 405 | } |
---|
| 406 | if(this.password && this.password !== null){ |
---|
| 407 | xhrArgs.password = this.password; |
---|
| 408 | } |
---|
| 409 | |
---|
| 410 | if(errorCallback){ |
---|
| 411 | xhrArgs.error = function(error, args){errorCallback.call(scope, error, args);}; |
---|
| 412 | }else{ |
---|
| 413 | xhrArgs.error = function(){ |
---|
| 414 | throw new Error("The URL requested cannot be accessed"); |
---|
| 415 | }; |
---|
| 416 | } |
---|
| 417 | if(xmethod){ |
---|
| 418 | xhrArgs.headers = {"X-Method-Override": "DELETE"}; |
---|
| 419 | dhxr.post(xhrArgs); |
---|
| 420 | }else{ |
---|
| 421 | xhrUtil.del(xhrArgs); |
---|
| 422 | } |
---|
| 423 | } |
---|
| 424 | }); |
---|
| 425 | }); |
---|