summaryrefslogtreecommitdiff
path: root/js/dojo-1.7.2/dojox/atom/io/Connection.js
blob: da0c2a31c02f2d0d979fb4b661ad1aa6edf6d4ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//>>built
define("dojox/atom/io/Connection", [
	"dojo/_base/kernel",
	"dojo/_base/xhr",
	"dojo/_base/window",
	"./model",
	"dojo/_base/declare"], function (dojo, xhrUtil, windowUtil, model) {
return dojo.declare("dojox.atom.io.Connection",null,{
	// summary: This object implements a transport layer for working with ATOM feeds and ATOM publishing protocols.
	// description: This object implements a transport layer for working with ATOM feeds and ATOM publishing protocols.
	//   Specifically, it provides a mechanism by which feeds can be fetched and entries can be fetched, created
	//   deleted, and modified.  It also provides access to the introspection data.

	constructor: function(/* Boolean */sync, /* Boolean */preventCache){
		// 	summary:
		//		initializer
		this.sync = sync;
		this.preventCache = preventCache;
	},

	preventCache: false, //Flag to denote if the instance should use the xhr prevent cache mechanism

	alertsEnabled: false, //Flag to turn on alerts instead of throwing errors.

	getFeed: function(/*String*/url, /*Function*/callback, /*Function*/errorCallback, scope){
		// 	summary:
		//		Function to obtain a s specific ATOM feed from a given ATOM Feed url.
		//	description:
		//		This function takes the URL for a specific ATOM feed and returns
		//		the data from that feed to the caller through the use of a callback
		//		handler.
		//
		// 	url: String
		//		The URL of the ATOM feed to fetch.
		//	callback:
		//		Function
		//		A function reference that will handle the feed when it has been retrieved.
		//		The callback should accept two parameters:  The feed object and the original complete DOM object.
		//	scope: Object
		//		The scope to use for all callbacks.
		//
		//	returns:
		//		Nothing. The return is handled through the callback handler.
		this._getXmlDoc(url, "feed", new model.Feed(), model._Constants.ATOM_NS, callback, /*handleDocumentRetrieved,*/ errorCallback, scope);
	},
	
	getService: function(url, callback, errorCallback, scope){
		//	summary:
		//		Function to retrieve an introspection document from the given URL.
		// 	description:
		//		This function takes the URL for an ATOM item and feed and returns
		//		the introspection document.
		//
		//	url:
		//		String
		//		The URL of the ATOM document to obtain the introspection document of.
		//	callback:
		//		Function
		//		A function reference that will handle the introspection document when it has been retrieved.
		//		The callback should accept two parameters:  The introspection document object and the original complete DOM object.
		//
		//	returns:
		//		Nothing. The return is handled through the callback handler.
		this._getXmlDoc(url, "service", new model.Service(url), model._Constants.APP_NS, callback, errorCallback, scope);
	},
	
	getEntry: function(url, callback, errorCallback, scope){
		//	summary:
		//		Function to retrieve a single entry from an ATOM feed from the given URL.
		//	description:
		//		This function takes the URL for an ATOM entry and returns the constructed dojox.atom.io.model.Entry object through
		//		the specified callback.
		//
		//	url:
		//		String
		//		The URL of the ATOM Entry document to parse.
		//	callback:
		//		Function
		//		A function reference that will handle the Entry object obtained.
		//		The callback should accept two parameters, the dojox.atom.io.model.Entry object and the original dom.
		//
		//	returns:
		//		Nothing. The return is handled through the callback handler.
		this._getXmlDoc(url, "entry", new model.Entry(), model._Constants.ATOM_NS, callback, errorCallback, scope);
	},

	_getXmlDoc: function(url, nodeName, newNode, namespace, callback, errorCallback, scope){
		//	summary:
		//		Internal Function to retrieve an XML document and pass the results to a callback.
		//	description:
		//		This internal function takes the URL for an XML document and and passes the
		//		parsed contents to a specified callback.
		//
		//	url:
		//		String
		//		The URL of the XML document to retrieve
		//	callback:
		//		Function
		//		A function reference that will handle the retrieved XML data.
		//		The callback should accept one parameter, the DOM of the parsed XML document.
		//
		//	returns:
		//		Nothing. The return is handled through the callback handler.
		if(!scope){
			scope = windowUtil.global;
		}
		var ae = this.alertsEnabled;
		var xhrArgs = {
			url: url,
			handleAs: "xml",
			sync: this.sync,
			preventCache: this.preventCache,
			load: function(data, args){
				var node	 = null;
				var evaldObj = data;
				var nodes;
				if(evaldObj){
					//find the first node of the appropriate name
					if(typeof(evaldObj.getElementsByTagNameNS)!= "undefined"){
						nodes = evaldObj.getElementsByTagNameNS(namespace,nodeName);
						if(nodes && nodes.length > 0){
							node = nodes.item(0);
						}else if(evaldObj.lastChild){
							// name_spaces can be used without declaration of atom (for example
							// gooogle feeds often returns iTunes name_space qualifiers on elements)
							// Treat this situation like name_spaces not enabled.
							node = evaldObj.lastChild;
						}
					}else if(typeof(evaldObj.getElementsByTagName)!= "undefined"){
						// Find the first eith the correct tag name and correct namespace.
						nodes = evaldObj.getElementsByTagName(nodeName);
						if(nodes && nodes.length > 0){
							for(var i=0; i<nodes.length; i++){
								if(nodes[i].namespaceURI == namespace){
									node = nodes[i];
									break;
								}
							}
						}else if(evaldObj.lastChild){
							node = evaldObj.lastChild;
						}
					}else if(evaldObj.lastChild){
						node = evaldObj.lastChild;
					}else{
						callback.call(scope, null, null, args);
						return;
					}
					newNode.buildFromDom(node);
					if(callback){
						callback.call(scope, newNode, evaldObj, args);
					}else if(ae){
						throw new Error("The callback value does not exist.");
					}
				}else{
					callback.call(scope, null, null, args);
				}
			}
		};

		if(this.user && this.user !== null){
			xhrArgs.user = this.user;
		}
		if(this.password && this.password !== null){
			xhrArgs.password = this.password;
		}

		if(errorCallback){
			xhrArgs.error = function(error, args){errorCallback.call(scope, error, args);};
		}else{
			xhrArgs.error = function(){
				throw new Error("The URL requested cannot be accessed");
			};
		}
		xhrUtil.get(xhrArgs);
	},

	updateEntry: function(entry, callback, errorCallback, retrieveUpdated, xmethod, scope){
		//	summary:
		//		Function to update a specific ATOM entry by putting the new changes via APP.
		//	description:
		//		This function takes a specific dojox.atom.io.model.Entry object and pushes the
		//		changes back to the provider of the Entry.
		//		The entry MUST have a link tag with rel="edit" for this to work.
		//
		//	entry:
		//		Object
		//		The dojox.atom.io.model.Entry object to update.
		//	callback:
		//		Function
		//		A function reference that will handle the results from the entry update.
		//		The callback should accept two parameters:  The first is an Entry object, and the second is the URL of that Entry
		//		Either can be null, depending on the value of retrieveUpdated.
		//	retrieveUpdated:
		//		boolean
		//		A boolean flag denoting if the entry that was updated should then be
		//		retrieved and returned to the caller via the callback.
		//	xmethod:
		//		boolean
		//		Whether to use POST for PUT/DELETE items and send the X-Method-Override header.
		//	scope:
		//		Object
		//		The scope to use for all callbacks.
		//
		//	returns:
		//		Nothing. The return is handled through the callback handler.
		if(!scope){
			scope = windowUtil.global;
		}
		entry.updated = new Date();
		var url = entry.getEditHref();
		if(!url){
			throw new Error("A URL has not been specified for editing this entry.");
		}

		var self = this;
		var ae = this.alertsEnabled;
		var xhrArgs = {
			url: url,
			handleAs: "text",
			contentType: "text/xml",
			sync: this.sync,
			preventCache: this.preventCache,
			load: function(data, args){
				var location = null;
				if(retrieveUpdated){
					location = args.xhr.getResponseHeader("Location");
					if(!location){location = url;}

					//Function to handle the callback mapping of a getEntry after an update to return the
					//entry and location.
					var handleRetrieve = function(entry, dom, args){
						if(callback){
							callback.call(scope, entry, location, args);
						}else if(ae){
							throw new Error("The callback value does not exist.");
						}
					};
					self.getEntry(location,handleRetrieve);
				}else{
					if(callback){
						callback.call(scope, entry, args.xhr.getResponseHeader("Location"), args);
					}else if(ae){
						throw new Error("The callback value does not exist.");
					}
				}
				return data;
			}
		};
		
		if(this.user && this.user !== null){
			xhrArgs.user = this.user;
		}
		if(this.password && this.password !== null){
			xhrArgs.password = this.password;
		}

		if(errorCallback){
			xhrArgs.error = function(error, args){errorCallback.call(scope, error, args);};
		}else{
			xhrArgs.error = function(){
				throw new Error("The URL requested cannot be accessed");
			};
		}

		if(xmethod){
			xhrArgs.postData = entry.toString(true); //Set the content to send.
			xhrArgs.headers = {"X-Method-Override": "PUT"};
			xhrUtil.post(xhrArgs);
		}else{
			xhrArgs.putData = entry.toString(true); //Set the content to send.
			var xhr = xhrUtil.put(xhrArgs);
		}
	},

	addEntry: function(entry, url, callback, errorCallback, retrieveEntry, scope){
		//	summary:
		//		Function to add a new ATOM entry by posting the new entry via APP.
		//	description:
		//		This function takes a specific dojox.atom.io.model.Entry object and pushes the
		//		changes back to the provider of the Entry.
		//
		//	entry:
		//		Object
		//		The dojox.atom.io.model.Entry object to publish.
		//	callback:
		//		Function
		//		A function reference that will handle the results from the entry publish.
		//		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
		//		Either can be null, depending on the value of retrieveUpdated.
		//	retrieveEntry:
		//		boolean
		//		A boolean flag denoting if the entry that was created should then be
		//		retrieved and returned to the caller via the callback.
		//	scope:
		//		Object
		//	 	The scope to use for all callbacks.
		//
		//	returns:
		//		Nothing. The return is handled through the callback handler.
		if(!scope){
			scope = windowUtil.global;
		}

		entry.published = new Date();
		entry.updated = new Date();

		var feedUrl = entry.feedUrl;
		var ae = this.alertsEnabled;

		//Determine which URL to use for the post.
		if(!url && feedUrl){url = feedUrl;}
		if(!url){
			if(ae){
				throw new Error("The request cannot be processed because the URL parameter is missing.");
			}
			return;
		}

		var self = this;
		var xhrArgs = {
			url: url,
			handleAs: "text",
			contentType: "text/xml",
			sync: this.sync,
			preventCache: this.preventCache,
			postData: entry.toString(true),
			load: function(data, args){
				var location = args.xhr.getResponseHeader("Location");
				if(!location){
					location = url;
				}
				if(!args.retrieveEntry){
					if(callback){
						callback.call(scope, entry, location, args);
					}else if(ae){
						throw new Error("The callback value does not exist.");
					}
				}else{
					//Function to handle the callback mapping of a getEntry after an update to return the
					//entry and location.
					var handleRetrieve = function(entry, dom, args){
						if(callback){
							callback.call(scope, entry, location, args);
						}else if(ae){
							throw new Error("The callback value does not exist.");
						}
					};
					self.getEntry(location,handleRetrieve);
				}
				return data;
			}
		};

		if(this.user && this.user !== null){
			xhrArgs.user = this.user;
		}
		if(this.password && this.password !== null){
			xhrArgs.password = this.password;
		}

		if(errorCallback){
			xhrArgs.error = function(error, args){errorCallback.call(scope, error, args);};
		}else{
			xhrArgs.error = function(){
				throw new Error("The URL requested cannot be accessed");
			};
		}
		xhrUtil.post(xhrArgs);
	},

	deleteEntry: function(entry,callback,errorCallback,xmethod,scope){
		//	summary:
		//		Function to delete a specific ATOM entry via APP.
		//	description:
		//		This function takes a specific dojox.atom.io.model.Entry object and calls for a delete on the
		//		service housing the ATOM Entry database.
		//		The entry MUST have a link tag with rel="edit" for this to work.
		//
		//	entry:
		//		Object
		//		The dojox.atom.io.model.Entry object to delete.
		//	callback:
		//		Function
		//		A function reference that will handle the results from the entry delete.
		//		The callback is called only if the delete is successful.
		//
		//	returns:
		//		Nothing. The return is handled through the callback handler.
		if(!scope){
			scope = windowUtil.global;
		}

		var url = null;
		if(typeof(entry) == "string"){
			url = entry;
		}else{
			url = entry.getEditHref();
		}
		if(!url){
			callback.call(scope, false, null);
			throw new Error("The request cannot be processed because the URL parameter is missing.");
		}

		var xhrArgs = {
			url: url,
			handleAs: "text",
			sync: this.sync,
			preventCache: this.preventCache,
			load: function(data, args){
				callback.call(scope, args);
				return data;
			}
		};

		if(this.user && this.user !== null){
			xhrArgs.user = this.user;
		}
		if(this.password && this.password !== null){
			xhrArgs.password = this.password;
		}

		if(errorCallback){
			xhrArgs.error = function(error, args){errorCallback.call(scope, error, args);};
		}else{
			xhrArgs.error = function(){
				throw new Error("The URL requested cannot be accessed");
			};
		}
		if(xmethod){
			xhrArgs.headers = {"X-Method-Override": "DELETE"};
			dhxr.post(xhrArgs);
		}else{
			xhrUtil.del(xhrArgs);
		}
	}
});
});