Commit 06571b79 authored by Sven Franck's avatar Sven Franck

davStorage: bundled all ajax calls into single function

parent abe4d766
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */ /*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global jIO: true, $: true, btoa: true */ /*global jIO: true, $: true, btoa: true */
// test here: http://enable-cors.org/
//http://metajack.im/2010/01/19/crossdomain-ajax-for-xmpp-http-binding-made-easy
jIO.addStorageType('dav', function (spec, my) { jIO.addStorageType('dav', function (spec, my) {
spec = spec || {}; spec = spec || {};
...@@ -47,7 +45,6 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -47,7 +45,6 @@ jIO.addStorageType('dav', function (spec, my) {
return true; return true;
}; };
// ==================== Attributes ====================
priv.username = spec.username || ''; priv.username = spec.username || '';
priv.secured_username = priv.convertSlashes(priv.username); priv.secured_username = priv.convertSlashes(priv.username);
priv.password = spec.password || ''; priv.password = spec.password || '';
...@@ -118,6 +115,32 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -118,6 +115,32 @@ jIO.addStorageType('dav', function (spec, my) {
return clean_url; return clean_url;
}; };
/**
* Runs all ajax requests for davStorage
* @method ajax
* @param {object} ajax_object The request parameters
*/
priv.ajax = function (ajax_object) {
$.ajax({
url: ajax_object.url,
type: ajax_object.type,
async: true,
dataType: ajax_object.dataType || null,
data: ajax_object.data || null,
crossdomain : true,
headers : {
Authorization: 'Basic ' + btoa(
priv.username + ':' + priv.password
),
Depth: ajax_object.headers === undefined ? null :
ajax_object.headers.depth
},
// xhrFields: {withCredentials: 'true'},
success: ajax_object.success,
error: ajax_object.error
});
};
/** /**
* Creates error objects for this storage * Creates error objects for this storage
* @method createError * @method createError
...@@ -155,6 +178,27 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -155,6 +178,27 @@ jIO.addStorageType('dav', function (spec, my) {
return error; return error;
}; };
/**
* Check if method can be run on browser
* @method support
*/
priv.support = function (docid) {
// no docId
if (!(typeof docid === "string" && docid !== "")) {
that.error(priv.createError(405, "Can't create document without id",
"Document id is undefined"
));
return true;
}
// no cross domain ajax
if (priv.checkCors === false) {
that.error(priv.createError(405,
"Browser does not support cross domain ajax", "CORS is undefined"
));
return true;
}
};
// wedDav methods rfc4918 (short summary) // wedDav methods rfc4918 (short summary)
// COPY Reproduces single resources (files) and collections (directory // COPY Reproduces single resources (files) and collections (directory
// trees). Will overwrite files (if specified by request) but will // trees). Will overwrite files (if specified by request) but will
...@@ -182,40 +226,19 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -182,40 +226,19 @@ jIO.addStorageType('dav', function (spec, my) {
priv.putOrPost = function (command, type) { priv.putOrPost = function (command, type) {
var docid = command.getDocId(), var docid = command.getDocId(), secured_docid, url, ajax_object;
secured_docid,
url;
// no docId if (priv.support(docid)) {
if (!(typeof docid === "string" && docid !== "")) {
that.error(priv.createError(405, "Can't create document without id",
"Document id is undefined"
));
return;
}
// no cross domain ajax
if (priv.checkCors === false) {
that.error(priv.createError(405,
"Browser does not support cross domain ajax", "CORS is undefined"
));
return; return;
} }
secured_docid = priv.secureDocId(command.getDocId()); secured_docid = priv.secureDocId(command.getDocId());
url = priv.url + '/' + priv.underscoreFileExtenisons(secured_docid); url = priv.url + '/' + priv.underscoreFileExtenisons(secured_docid);
// see if the document exists
$.ajax({ ajax_object = {
url: url + '?_=' + Date.now(), url: url + '?_=' + Date.now(),
type: "GET", type: "GET",
async: true, dataType: "text",
dataType: 'text',
crossdomain : true,
headers : {
Authorization: 'Basic ' + btoa(
priv.username + ':' + priv.password
)
},
// xhrFields: {withCredentials: 'true'},
success: function () { success: function () {
if (type === 'POST') { if (type === 'POST') {
// POST the document already exists // POST the document already exists
...@@ -224,19 +247,10 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -224,19 +247,10 @@ jIO.addStorageType('dav', function (spec, my) {
)); ));
return; return;
} }
// PUT update document ajax_object = {
$.ajax({
url: url, url: url,
type: type, type: type,
data: JSON.stringify(command.getDoc()), data: JSON.stringify(command.getDoc()),
async: true,
crossdomain: true,
headers : {
Authorization: 'Basic ' + btoa(
priv.username + ':' + priv.password
)
},
// xhrFields: {withCredentials: 'true'},
success: function () { success: function () {
that.success({ that.success({
ok: true, ok: true,
...@@ -248,23 +262,16 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -248,23 +262,16 @@ jIO.addStorageType('dav', function (spec, my) {
"Error writing to remote storage" "Error writing to remote storage"
)); ));
} }
}); };
priv.ajax(ajax_object);
}, },
error: function (err) { error: function (err) {
// Firefox returns 0 instead of 404 on CORS? // Firefox returns 0 instead of 404 on CORS?
if (err.status === 404 || err.status === 0) { if (err.status === 404 || err.status === 0) {
$.ajax({ ajax_object = {
url: url, url: url,
type: 'PUT', type: "PUT",
data: JSON.stringify(command.getDoc()), data: JSON.stringify(command.getDoc()),
async: true,
crossdomain: true,
headers : {
Authorization: 'Basic ' + btoa(
priv.username + ':' + priv.password
)
},
// xhrFields: {withCredentials: 'true'},
success: function () { success: function () {
that.success({ that.success({
ok: true, ok: true,
...@@ -276,8 +283,8 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -276,8 +283,8 @@ jIO.addStorageType('dav', function (spec, my) {
"Cannot modify document", "Error writing to remote storage" "Cannot modify document", "Error writing to remote storage"
)); ));
} }
}); };
priv.ajax(ajax_object);
} else { } else {
// error accessing remote storage // error accessing remote storage
that.error({ that.error({
...@@ -289,7 +296,8 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -289,7 +296,8 @@ jIO.addStorageType('dav', function (spec, my) {
}); });
} }
} }
}); };
priv.ajax(ajax_object);
}; };
/** /**
...@@ -321,36 +329,18 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -321,36 +329,18 @@ jIO.addStorageType('dav', function (spec, my) {
url, url,
secured_docid, secured_docid,
secured_attachmentid, secured_attachmentid,
attachment_url; attachment_url,
ajax_object;
if (!(typeof docid === "string" && docid !== "")) { priv.support(docid);
that.error(priv.createError(405,
"Can't create document without id", "Document id is undefined"
));
return;
}
if (priv.checkCors === false) {
that.error(priv.createError(405,
"Browser does not support cross domain ajax", "CORS is undefined"
));
return;
}
secured_docid = priv.secureDocId(docid); secured_docid = priv.secureDocId(docid);
url = priv.url + '/' + priv.underscoreFileExtenisons(secured_docid); url = priv.url + '/' + priv.underscoreFileExtenisons(secured_docid);
// see if the underlying document exists ajax_object = {
$.ajax({
url: url + '?_=' + Date.now(), url: url + '?_=' + Date.now(),
type: 'GET', type: 'GET',
async: true,
dataType: 'text', dataType: 'text',
crossdomain : true,
headers : {
Authorization: 'Basic ' + btoa(
priv.username + ':' + priv.password
)
},
success: function (response) { success: function (response) {
doc = JSON.parse(response); doc = JSON.parse(response);
...@@ -362,34 +352,18 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -362,34 +352,18 @@ jIO.addStorageType('dav', function (spec, my) {
"length": command.getAttachmentLength() "length": command.getAttachmentLength()
}; };
// put updated document data // put updated document data
$.ajax({ ajax_object = {
url: url + '?_=' + Date.now(), url: url + '?_=' + Date.now(),
type: 'PUT', type: 'PUT',
data: JSON.stringify(doc), data: JSON.stringify(doc),
async: true,
crossdomain: true,
headers : {
Authorization: 'Basic ' + btoa(
priv.username + ':' + priv.password
)
},
// xhrFields: {withCredentials: 'true'},
success: function () { success: function () {
secured_attachmentid = priv.secureDocId(command.getAttachmentId()); secured_attachmentid = priv.secureDocId(command.getAttachmentId());
attachment_url = url + '.' + attachment_url = url + '.' +
priv.underscoreFileExtenisons(secured_attachmentid); priv.underscoreFileExtenisons(secured_attachmentid);
$.ajax({ ajax_object = {
url: attachment_url + '?_=' + Date.now(), url: attachment_url + '?_=' + Date.now(),
type: 'PUT', type: 'PUT',
data: JSON.stringify(command.getDoc()), data: JSON.stringify(command.getDoc()),
async: true,
crossdomain: true,
headers : {
Authorization: 'Basic ' + btoa(
priv.username + ':' + priv.password
)
},
// xhrFields: {withCredentials: 'true'},
success: function () { success: function () {
that.success({ that.success({
ok: true, ok: true,
...@@ -402,7 +376,8 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -402,7 +376,8 @@ jIO.addStorageType('dav', function (spec, my) {
)); ));
return; return;
} }
}); };
priv.ajax(ajax_object);
}, },
error: function () { error: function () {
that.error(priv.createError(409, that.error(priv.createError(409,
...@@ -410,7 +385,8 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -410,7 +385,8 @@ jIO.addStorageType('dav', function (spec, my) {
)); ));
return; return;
} }
}); };
priv.ajax(ajax_object);
}, },
error: function () { error: function () {
// the document does not exist // the document does not exist
...@@ -419,7 +395,9 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -419,7 +395,9 @@ jIO.addStorageType('dav', function (spec, my) {
)); ));
return; return;
} }
}); };
// see if the underlying document exists
priv.ajax(ajax_object);
}; };
/** /**
...@@ -437,20 +415,13 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -437,20 +415,13 @@ jIO.addStorageType('dav', function (spec, my) {
url, url,
secured_docid, secured_docid,
secured_attachmentid, secured_attachmentid,
attachment_url; attachment_url,
ajax_object;
if (!(typeof docid === "string" && docid !== "")) { if (priv.support(docid)) {
that.error(priv.createError(405,
"Can't create document without id", "Document id is undefined"
));
return;
}
if (priv.checkCors === false) {
that.error(priv.createError(405,
"Browser does not support cross domain ajax", "CORS is undefined"
));
return; return;
} }
secured_docid = priv.secureDocId(command.getDocId()); secured_docid = priv.secureDocId(command.getDocId());
url = priv.url + '/' + priv.underscoreFileExtenisons(secured_docid); url = priv.url + '/' + priv.underscoreFileExtenisons(secured_docid);
...@@ -459,19 +430,11 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -459,19 +430,11 @@ jIO.addStorageType('dav', function (spec, my) {
attachment_url = url + '.' + priv.underscoreFileExtenisons( attachment_url = url + '.' + priv.underscoreFileExtenisons(
secured_attachmentid secured_attachmentid
); );
// get attachment // get attachment
$.ajax({ ajax_object = {
url: attachment_url + '?_=' + Date.now(), url: attachment_url + '?_=' + Date.now(),
type: 'GET', type: "GET",
async: true, dataType: "text",
dataType: 'text',
crossdomain : true,
headers : {
Authorization: 'Basic ' + btoa(
priv.username + ':' + priv.password
)
},
success: function (response) { success: function (response) {
doc = JSON.parse(response); doc = JSON.parse(response);
that.success(doc); that.success(doc);
...@@ -481,21 +444,14 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -481,21 +444,14 @@ jIO.addStorageType('dav', function (spec, my) {
"Cannot find the attachment", "Attachment does not exist" "Cannot find the attachment", "Attachment does not exist"
)); ));
} }
}); };
priv.ajax(ajax_object);
} else { } else {
// get document // get document
$.ajax({ ajax_object = {
url: url + '?_=' + Date.now(), url: url + '?_=' + Date.now(),
type: 'GET', type: "GET",
async: true, dataType: "text",
dataType: 'text',
crossdomain : true,
headers : {
Authorization: 'Basic ' + btoa(
priv.username + ':' + priv.password
)
},
success: function (response) { success: function (response) {
// metadata_only should not be handled by jIO, as it is a // metadata_only should not be handled by jIO, as it is a
// webDav only option, shouldn't it? // webDav only option, shouldn't it?
...@@ -508,7 +464,8 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -508,7 +464,8 @@ jIO.addStorageType('dav', function (spec, my) {
"Cannot find the document", "Document does not exist" "Cannot find the document", "Document does not exist"
)); ));
} }
}); };
priv.ajax(ajax_object);
} }
}; };
...@@ -520,18 +477,9 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -520,18 +477,9 @@ jIO.addStorageType('dav', function (spec, my) {
that.remove = function (command) { that.remove = function (command) {
var docid = command.getDocId(), doc, url, var docid = command.getDocId(), doc, url,
secured_docid, secured_attachmentid, attachment_url, secured_docid, secured_attachmentid, attachment_url,
attachment_list = [], i, j, k = 1, deleteAttachment; attachment_list = [], i, j, k = 1, deleteAttachment, ajax_object;
if (!(typeof docid === "string" && docid !== "")) { if (priv.support(docid)) {
that.error(priv.createError(405,
"Can't create document without id", "Document id is undefined"
));
return;
}
if (priv.checkCors === false) {
that.error(priv.createError(405,
"Browser does not support cross domain ajax", "CORS is undefined"
));
return; return;
} }
...@@ -544,30 +492,15 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -544,30 +492,15 @@ jIO.addStorageType('dav', function (spec, my) {
attachment_url = url + '.' + priv.underscoreFileExtenisons( attachment_url = url + '.' + priv.underscoreFileExtenisons(
secured_attachmentid secured_attachmentid
); );
ajax_object = {
$.ajax({
url: attachment_url + '?_=' + Date.now(), url: attachment_url + '?_=' + Date.now(),
type: 'DELETE', type: "DELETE",
async: true,
crossdomain : true,
headers : {
Authorization: 'Basic ' + btoa(
priv.username + ':' + priv.password
)
},
success: function () { success: function () {
// retrieve underlying document // retrieve underlying document
$.ajax({ ajax_object = {
url: url + '?_=' + Date.now(), url: url + '?_=' + Date.now(),
type: 'GET', type: "GET",
async: true, dataType: "text",
dataType: 'text',
crossdomain : true,
headers : {
Authorization: 'Basic ' + btoa(
priv.username + ':' + priv.password
)
},
success: function (response) { success: function (response) {
// underlying document // underlying document
doc = JSON.parse(response); doc = JSON.parse(response);
...@@ -581,18 +514,10 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -581,18 +514,10 @@ jIO.addStorageType('dav', function (spec, my) {
delete doc._attachments; delete doc._attachments;
} }
// PUT back to server // PUT back to server
$.ajax({ ajax_object = {
url: url + '?_=' + Date.now(), url: url + '?_=' + Date.now(),
type: 'PUT', type: 'PUT',
data: JSON.stringify(doc), data: JSON.stringify(doc),
async: true,
crossdomain: true,
headers : {
Authorization: 'Basic ' + btoa(
priv.username + ':' + priv.password
)
},
// xhrFields: {withCredentials: 'true'},
success: function () { success: function () {
that.success({ that.success({
"ok": true, "ok": true,
...@@ -605,7 +530,8 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -605,7 +530,8 @@ jIO.addStorageType('dav', function (spec, my) {
"Cannot modify document", "Error saving attachment" "Cannot modify document", "Error saving attachment"
)); ));
} }
}); };
priv.ajax(ajax_object);
} else { } else {
// sure this if-else is needed? // sure this if-else is needed?
that.error(priv.createError(404, that.error(priv.createError(404,
...@@ -625,29 +551,22 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -625,29 +551,22 @@ jIO.addStorageType('dav', function (spec, my) {
"Cannot find the document", "Document does not exist" "Cannot find the document", "Document does not exist"
)); ));
} }
}); };
priv.ajax(ajax_object);
}, },
error: function () { error: function () {
that.error(priv.createError(404, that.error(priv.createError(404,
"Cannot find the attachment", "Error removing attachment" "Cannot find the attachment", "Error removing attachment"
)); ));
} }
}); };
// remove document priv.ajax(ajax_object);
// remove document incl. all attachments
} else { } else {
ajax_object = {
// get document to also remove all attachments
$.ajax({
url: url + '?_=' + Date.now(), url: url + '?_=' + Date.now(),
type: 'GET', type: 'GET',
async: true,
dataType: 'text', dataType: 'text',
crossdomain : true,
headers : {
Authorization: 'Basic ' + btoa(
priv.username + ':' + priv.password
)
},
success: function (response) { success: function (response) {
var x; var x;
doc = JSON.parse(response); doc = JSON.parse(response);
...@@ -661,16 +580,9 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -661,16 +580,9 @@ jIO.addStorageType('dav', function (spec, my) {
} }
} }
// delete document // delete document
$.ajax({ ajax_object = {
url: url + '?_=' + Date.now(), url: url + '?_=' + Date.now(),
type: 'DELETE', type: 'DELETE',
async: true,
crossdomain : true,
headers : {
Authorization: 'Basic ' + btoa(
priv.username + ':' + priv.password
)
},
success: function () { success: function () {
j = attachment_list.length; j = attachment_list.length;
// no attachments, done // no attachments, done
...@@ -681,16 +593,9 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -681,16 +593,9 @@ jIO.addStorageType('dav', function (spec, my) {
}); });
} else { } else {
deleteAttachment = function (attachment_url, j, k) { deleteAttachment = function (attachment_url, j, k) {
$.ajax({ ajax_object = {
url: attachment_url + '?_=' + Date.now(), url: attachment_url + '?_=' + Date.now(),
type: 'DELETE', type: 'DELETE',
async: true,
crossdomain : true,
headers : {
Authorization: 'Basic ' + btoa(
priv.username + ':' + priv.password
)
},
success: function () { success: function () {
// all deleted, return response, need k as async couter // all deleted, return response, need k as async couter
if (j === k) { if (j === k) {
...@@ -707,7 +612,8 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -707,7 +612,8 @@ jIO.addStorageType('dav', function (spec, my) {
"Cannot find attachment", "Error removing attachment" "Cannot find attachment", "Error removing attachment"
)); ));
} }
}); };
priv.ajax(ajax_object);
}; };
for (i = 0; i < j; i += 1) { for (i = 0; i < j; i += 1) {
secured_attachmentid = priv.secureDocId(attachment_list[i]); secured_attachmentid = priv.secureDocId(attachment_list[i]);
...@@ -723,14 +629,16 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -723,14 +629,16 @@ jIO.addStorageType('dav', function (spec, my) {
"Cannot find the document", "Error removing document" "Cannot find the document", "Error removing document"
)); ));
} }
}); };
priv.ajax(ajax_object);
}, },
error: function () { error: function () {
that.error(priv.createError(404, that.error(priv.createError(404,
"Cannot find the document", "Document does not exist" "Cannot find the document", "Document does not exist"
)); ));
} }
}); };
priv.ajax(ajax_object);
} }
}; };
...@@ -756,22 +664,16 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -756,22 +664,16 @@ jIO.addStorageType('dav', function (spec, my) {
that.allDocs = function (command) { that.allDocs = function (command) {
var rows = [], url, var rows = [], url,
am = priv.newAsyncModule(), am = priv.newAsyncModule(),
o = {}; o = {},
ajax_object;
o.getContent = function (file) { o.getContent = function (file) {
var docid = priv.secureDocId(file.id), var docid = priv.secureDocId(file.id),
url = priv.url + '/' + docid; url = priv.url + '/' + docid;
ajax_object = {
$.ajax({
url: url + '?_=' + Date.now(), url: url + '?_=' + Date.now(),
type: 'GET', type: 'GET',
async: true,
dataType: 'text', dataType: 'text',
headers: {
'Authorization': 'Basic ' + btoa(
priv.username + ':' + priv.password
)
},
success: function (content) { success: function (content) {
file.doc = JSON.parse(content); file.doc = JSON.parse(content);
rows.push(file); rows.push(file);
...@@ -783,23 +685,16 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -783,23 +685,16 @@ jIO.addStorageType('dav', function (spec, my) {
)); ));
am.call(o, 'error', [type]); am.call(o, 'error', [type]);
} }
}); };
priv.ajax(ajax_object);
}; };
o.getDocumentList = function () { o.getDocumentList = function () {
url = priv.url + '/'; url = priv.url + '/';
$.ajax({ ajax_object = {
url: url + '?_=' + Date.now(), url: url + '?_=' + Date.now(),
type: 'PROPFIND', type: "PROPFIND",
async: true,
dataType: "xml", dataType: "xml",
crossdomain : true, headers : { depth: '1' },
headers : {
Authorization: 'Basic ' + btoa(
priv.username + ':' + priv.password
),
Depth: '1'
},
success: function (xml) { success: function (xml) {
var response = $(xml).find('D\\:response, response'), var response = $(xml).find('D\\:response, response'),
len = response.length; len = response.length;
...@@ -834,7 +729,8 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -834,7 +729,8 @@ jIO.addStorageType('dav', function (spec, my) {
)); ));
am.call(o, 'retry', [type]); am.call(o, 'retry', [type]);
} }
}); };
priv.ajax(ajax_object);
}; };
o.retry = function (error) { o.retry = function (error) {
am.neverCall(o, 'retry'); am.neverCall(o, 'retry');
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment