Commit 26051712 authored by Sven Franck's avatar Sven Franck

webDav storage POST updated to new API

parent fa7a5f07
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */ /*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global jIO: true, $: true, Base64: true */ /*global jIO: true, $: true, Base64: 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 || {};
var that = my.basicStorage(spec, my), priv = {}, var that, priv, super_serialized;
super_serialized = that.serialized; that = my.basicStorage(spec, my);
priv = {};
super_serialized = that.serialized;
priv.secureDocId = function (string) { priv.secureDocId = function (string) {
var split = string.split('/'), var split = string.split('/'),
...@@ -27,19 +32,17 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -27,19 +32,17 @@ jIO.addStorageType('dav', function (spec, my) {
}; };
// ==================== Attributes ====================
priv.username = spec.username || ''; priv.username = spec.username || '';
priv.secured_username = priv.convertSlashes(priv.username); priv.secured_username = priv.convertSlashes(priv.username);
priv.application_name = spec.application_name || 'untitled'; priv.password = spec.password || '';
priv.secured_application_name = priv.convertSlashes(priv.application_name);
priv.url = spec.url || ''; priv.url = spec.url || '';
priv.password = spec.password || ''; // TODO : is it secured ?
that.serialized = function () { that.serialized = function () {
var o = super_serialized(); var o = super_serialized();
o.username = priv.username; o.username = priv.username;
o.application_name = priv.application_name;
o.url = priv.url; o.url = priv.url;
o.password = priv.password; // TODO : not realy secured... o.password = priv.password;
return o; return o;
}; };
...@@ -81,35 +84,160 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -81,35 +84,160 @@ jIO.addStorageType('dav', function (spec, my) {
return async; return async;
}; };
priv.checkCors = function(){
return $.support.cors;
};
// wedDav methods rfc4918
// PROPFIND
// PROPPATCH
// MCKOL
// GET
// > resource = return content of element xyz.abc
// > collection > allDocs
// > attachment = return content of element xyz_.abc/att.def
// HEAD
// POST
// DELETE
// PUT
// COPY
// MOVE
// LOCK
// UNLOCK
priv.putOrPost = function (command, type) { priv.putOrPost = function (command, type) {
var doc = command.getDocId(),
secured_docid;
var secured_docid = priv.secureDocId(command.getDocId()); // no docId
if (!(typeof doc === "string" && doc !== "")) {
that.error({
"status": 405,
"statusText": "Method Not Allowed",
"error": "method_not_allowed",
"message": "Cannot create document which id is undefined",
"reason": "Document id is undefined"
});
return;
}
// no cross domain ajax
if (priv.checkCors === false) {
that.error({
"status": 405,
"statusText": "Method Not Allowed",
"error": "method_not_allowed",
"message": "Browser does not support cross domain ajax requests",
"reason": "cors is undefined"
});
return;
}
secured_docid = priv.secureDocId(command.getDocId());
url = priv.url + '/' + secured_docid;
// see if the document exists
$.ajax({ $.ajax({
url: priv.url + '/' + priv.secured_username + '/' + url: url + '?_=' + Date.now(),
priv.secured_application_name + '/' + secured_docid + '?_=' + type: "GET",
Date.now(),
// to make url unique and avoid chrome PUT on cache !
type: type,
data: command.getDocContent(),
async: true, async: true,
dataType: 'text', // TODO is it necessary ? dataType: 'text',
headers: { crossdomain : true,
'Authorization': 'Basic ' + Base64.encode(priv.username + ':' + headers : {
priv.password) Authorization: 'Basic ' + Base64.encode(
}, priv.username + ':' + priv.password
// xhrFields: {withCredentials: 'true'}, // cross domain )
success: function () { },
that.success({ // xhrFields: {withCredentials: 'true'},
ok: true, success: function (content) {
id: command.getDocId() if (type === 'POST') {
}); // POST the document already exists
that.error({
"status": 409,
"statusText": "Conflicts",
"error": "conflicts",
"message": "Cannot create a new document",
"reason": "Document already exists"
});
return;
} else {
// PUT update document
// remove first or can webDav overwrite?
$.ajax({
url: url + '?_=' + Date.now(),
type: type,
data: command.getDoc(),
async: true,
crossdomain: true,
headers : {
Authorization: 'Basic ' + Base64.encode(
priv.username + ':' + priv.password
)
},
// xhrFields: {withCredentials: 'true'},
success: function () {
that.success({
ok: true,
id: command.getDocId()
});
},
error: function (type) {
that.error({
"status": 409,
"statusText": "Conflicts",
"error": "conflicts",
"message": "Cannot modify document",
"reason": "Error trying to write to remote storage"
});
return;
}
});
}
}, },
error: function (type) { error: function (err) {
// TODO : make statusText to lower case and add '_' if (err.status === 404) {
type.error = type.statusText; $.ajax({
type.reason = 'Cannot save "' + command.getDocId() + '"'; url: url + '?_=' + Date.now(),
type.message = type.reason + '.'; // must always use put, POST only seems to work on collections
that.retry(type); type: 'PUT',
data: command.getDoc(),
async: true,
crossdomain: true,
headers : {
Authorization: 'Basic ' + Base64.encode(
priv.username + ':' + priv.password
)
},
// xhrFields: {withCredentials: 'true'},
success: function (response) {
that.success({
ok: true,
id: command.getDocId()
});
},
error: function (type) {
that.error({
"status": 409,
"statusText": "Conflicts",
"error": "conflicts",
"message": "Cannot modify document",
"reason": "Error trying to write to remote storage"
});
return;
}
});
} else {
// error accessing remote storage
that.error({
"status": err.status,
"statusText": err.statusText,
"error": "error",
"message": err.message,
"reason": "Failed to access remote storage"
});
return;
}
} }
}); });
}; };
...@@ -130,6 +258,92 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -130,6 +258,92 @@ jIO.addStorageType('dav', function (spec, my) {
* Loads a document from a distant dav storage. * Loads a document from a distant dav storage.
* @method get * @method get
*/ */
that.get = function (command) {
var doc = command.getDocId(),
secured_docid;
// no docId
if (!(typeof doc === "string" && doc !== "")) {
that.error({
"status": 405,
"statusText": "Method Not Allowed",
"error": "method_not_allowed",
"message": "Cannot create document which id is undefined",
"reason": "Document id is undefined"
});
return;
}
// no cors support
if (priv.checkCors === false) {
that.error({
"status": 405,
"statusText": "Method Not Allowed",
"error": "method_not_allowed",
"message": "Browser does not support cross domain ajax requests",
"reason": "cors is undefined"
});
return;
}
secured_docid = priv.secureDocId(command.getDocId());
url = priv.url + '/' + secured_docid;
// get attachment
if (typeof command.getAttachmentId() === "string") {
$.ajax({
url: url + '?_=' + Date.now(),
type: type,
data: command.getDoc(),
async: true,
crossdomain: true,
headers : {
Authorization: 'Basic ' + Base64.encode(
priv.username + ':' + priv.password
)
},
// xhrFields: {withCredentials: 'true'},
success: function (response) {
that.success(response)
},
error: function (type) {
that.error({
"status": 404,
"statusText": "Not Found",
"error": "not_found",
"message": "Cannot find the attachment",
"reason": "Attachment does not exist"
});
}
});
} else {
// get document
$.ajax({
url: url + '?_=' + Date.now(),
type: type,
data: command.getDoc(),
async: true,
crossdomain: true,
headers : {
Authorization: 'Basic ' + Base64.encode(
priv.username + ':' + priv.password
)
},
// xhrFields: {withCredentials: 'true'},
success: function (response) {
that.success(response)
},
error: function (type) {
that.error({
"status": 404,
"statusText": "Not Found",
"error": "not_found",
"message": "Cannot find the document",
"reason": "Document does not exist"
});
}
});
}
};
that.get = function (command) { that.get = function (command) {
var secured_docid = priv.secureDocId(command.getDocId()), var secured_docid = priv.secureDocId(command.getDocId()),
doc = {}, doc = {},
...@@ -214,6 +428,7 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -214,6 +428,7 @@ jIO.addStorageType('dav', function (spec, my) {
}); });
}; };
/** /**
* Gets a document list from a distant dav storage. * Gets a document list from a distant dav storage.
* @method allDocs * @method allDocs
...@@ -389,6 +604,5 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -389,6 +604,5 @@ jIO.addStorageType('dav', function (spec, my) {
} }
}); });
}; };
return that; return that;
}); });
\ No newline at end of file
...@@ -20,15 +20,22 @@ ...@@ -20,15 +20,22 @@
<script type="text/javascript" src="../src/jio.storage/localstorage.js"> <script type="text/javascript" src="../src/jio.storage/localstorage.js">
</script> </script>
<script type="text/javascript" src="../lib/jsSha2/sha2.js"></script> <script type="text/javascript" src="../lib/jsSha2/sha2.js"></script>
<script type="text/javascript" src="../src/jio.storage/revisionstorage.js"> <script type="text/javascript" src="../src/jio.storage/revisionstorage.js">
</script> </script>
<!--
<script type="text/javascript" <script type="text/javascript"
src="../src/jio.storage/replicaterevisionstorage.js"> src="../src/jio.storage/replicaterevisionstorage.js">
</script> </script>
-->
<!-- webDav -->
<script type="text/javascript" src="../lib/jquery/jquery.min.js"></script>
<script type="text/javascript" src="../lib/base64/base64.js"></script>
<script type="text/javascript" src="../src/jio.storage/davstorage.js">
</script>
<script type="text/javascript" src="./jiotests.js"></script> <script type="text/javascript" src="./jiotests.js"></script>
</body> </body>
</html> </html>
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