diff --git a/api-data-store-server-javascript/src/js/api/server-data-store-facade.js b/api-data-store-server-javascript/src/js/api/server-data-store-facade.js index aefaafd22f3096ddb9964374a7b3de31591959aa..5eedbb4f65f76bb9937fae375413e5dc5bb812a4 100644 --- a/api-data-store-server-javascript/src/js/api/server-data-store-facade.js +++ b/api-data-store-server-javascript/src/js/api/server-data-store-facade.js @@ -45,43 +45,51 @@ _DataStoreServerInternal.prototype.jsonRequestData = function(params) { return JSON.stringify(params); } -_DataStoreServerInternal.prototype.sendHttpRequest = function(httpMethod, contentType, url, data, callback) { +_DataStoreServerInternal.prototype.sendHttpRequest = function(httpMethod, contentType, url, data) { const xhr = new XMLHttpRequest(); xhr.open(httpMethod, url); xhr.responseType = "blob"; - xhr.onreadystatechange = function() { - if (xhr.readyState === XMLHttpRequest.DONE) { - const status = xhr.status; - if (status >= 200 && status < 300) { - const response = xhr.response - const contentType = this.getResponseHeader('content-type'); - - switch (contentType) { - case 'text/plain': - // Fall through. - case 'application/json': - response.text().then((blobResponse) => callback(blobResponse)) - .catch((error) => alert(error)); - break; - case 'application/octet-stream': - callback(response); - break; - default: - throw new Error("Client error HTTP response. Unsupported content-type received."); + return new Promise((resolve, reject) => { + xhr.onreadystatechange = function() { + if (xhr.readyState === XMLHttpRequest.DONE) { + const status = xhr.status; + const response = xhr.response; + + if (status >= 200 && status < 300) { + const contentType = this.getResponseHeader('content-type'); + + switch (contentType) { + case 'text/plain': + // Fall through. + case'application/json': { + response.text().then((blobResponse) => resolve(blobResponse)) + .catch((error) => reject(error)); + break; + } + case 'application/octet-stream': { + resolve(response); + break; + } + default: { + reject("Client error HTTP response. Unsupported content-type received."); + break; + } + } + } else if (status >= 400 && status < 600) { + if (response.size > 0) { + response.text().then((blobResponse) => reject(JSON.parse(blobResponse).error[1].message)) + .catch((error) => reject(error)); + } else { + reject(xhr.statusText); + } + } else { + reject("ERROR: " + xhr.responseText); } - } else if(status >= 400 && status < 500) { - let response = JSON.parse(xhr.responseText); - alert(response.error[1].message); - } else if(status >= 500 && status < 600) { - let response = JSON.parse(xhr.responseText); - alert(response.error[1].message); - } else { - alert("ERROR: " + xhr.responseText); } - } - }; - xhr.send(data); + }; + xhr.send(data); + }); } @@ -155,13 +163,15 @@ _DataStoreServerInternal.prototype.parseUri = function(str) { /** Helper method for checking response from DSS server */ -function parseJsonResponse(rawResponse, action) { - let response = JSON.parse(rawResponse); - if(response.error){ - alert(response.error[1].message); - }else{ - action(response); - } +function parseJsonResponse(rawResponse) { + return new Promise((resolve, reject) => { + let response = JSON.parse(rawResponse); + if (response.error) { + reject(response.error[1].message); + } else { + resolve(response); + } + }); } @@ -286,25 +296,22 @@ const encodeParams = p => Object.entries(p).map(kv => kv.map(encodeURIComponent * * @method */ -DataStoreServer.prototype.login = function(userId, userPassword, action) { +DataStoreServer.prototype.login = function(userId, userPassword) { var datastoreObj = this const data = this.fillCommonParameters({ "method": "login", "userId": userId, "password": userPassword }); - this._internal.sendHttpRequest( + return this._internal.sendHttpRequest( "POST", "application/octet-stream", this._internal.datastoreUrl, - encodeParams(data), - function(loginResponse) { - datastoreObj._internal.sessionToken = loginResponse; - datastoreObj.rememberSession(); - action(loginResponse); - } - ); - + encodeParams(data) + ).then((loginResponse) => { + datastoreObj._internal.sessionToken = loginResponse; + datastoreObj.rememberSession(); + }); } @@ -312,32 +319,33 @@ DataStoreServer.prototype.login = function(userId, userPassword, action) { * Checks whether the current session is still active. * */ -DataStoreServer.prototype.isSessionValid = function(action) { - if(this.getSession()){ - const data = this.fillCommonParameters({"method":"isSessionValid"}); - this._internal.sendHttpRequest( - "GET", - "application/octet-stream", - this._internal.datastoreUrl, - encodeParams(data), - (response) => parseJsonResponse(response, action) - ); - }else{ - action({ result : false }) - } +DataStoreServer.prototype.isSessionValid = function() { + return new Promise((resolve, reject) => { + if (this.getSession()) { + const data = this.fillCommonParameters({"method":"isSessionValid"}); + this._internal.sendHttpRequest( + "GET", + "application/octet-stream", + this._internal.datastoreUrl, + encodeParams(data) + ).then((response) => parseJsonResponse(response).then((value) => resolve(value)) + .catch((reason) => reject(reason))); + } else { + resolve({ result : false }) + } + }); } /** - * Restores the current session from a cookie and executes - * the specified action if the session is still active. + * Restores the current session from a cookie. * * @see restoreSession() * @see isSessionActive() * @method */ -DataStoreServer.prototype.ifRestoredSessionActive = function(action) { +DataStoreServer.prototype.ifRestoredSessionActive = function() { this.restoreSession(); - this.isSessionValid(function(data) { if (data.result) action(data) }); + return this.isSessionValid(); } /** @@ -345,21 +353,23 @@ DataStoreServer.prototype.ifRestoredSessionActive = function(action) { * * @method */ -DataStoreServer.prototype.logout = function(action) { - this.forgetSession(); - - if(this.getSession()){ - const data = this.fillCommonParameters({"method":"logout"}); - this._internal.sendHttpRequest( - "POST", - "application/octet-stream", - this._internal.datastoreUrl, - encodeParams(data), - (response) => parseJsonResponse(response, action) - ); - }else if(action){ - action({ result : null }); - } +DataStoreServer.prototype.logout = function() { + return new Promise((resolve, reject) => { + this.forgetSession(); + + if (this.getSession()) { + const data = this.fillCommonParameters({"method": "logout"}); + this._internal.sendHttpRequest( + "POST", + "application/octet-stream", + this._internal.datastoreUrl, + encodeParams(data) + ).then((response) => parseJsonResponse(response).then((value) => resolve(value)) + .catch((reason) => reject(reason))); + } else { + resolve({result: null}); + } + }); } @@ -372,20 +382,19 @@ DataStoreServer.prototype.logout = function(action) { /** * List files in the DSS for given owner and source */ -DataStoreServer.prototype.list = function(owner, source, recursively, action){ +DataStoreServer.prototype.list = function(owner, source, recursively){ const data = this.fillCommonParameters({ "method": "list", "owner" : owner, "source": source, "recursively": recursively }); - this._internal.sendHttpRequest( + return this._internal.sendHttpRequest( "GET", "application/octet-stream", this._internal.buildGetUrl(data), - {}, - (response) => parseJsonResponse(response, action) - ); + {} + ).then((response) => parseJsonResponse(response)); } /** @@ -394,9 +403,8 @@ DataStoreServer.prototype.list = function(owner, source, recursively, action){ * @param {str} source path to file * @param {int} offset offset from whoch to start reading * @param {int} limit how many characters to read - * @param {*} action post-processing action */ -DataStoreServer.prototype.read = function(owner, source, offset, limit, action){ +DataStoreServer.prototype.read = function(owner, source, offset, limit){ const data = this.fillCommonParameters({ "method": "read", "owner" : owner, @@ -404,12 +412,11 @@ DataStoreServer.prototype.read = function(owner, source, offset, limit, action){ "offset": offset, "limit": limit }); - this._internal.sendHttpRequest( + return this._internal.sendHttpRequest( "GET", "application/octet-stream", this._internal.buildGetUrl(data), - {}, - (response) => action(response) + {} ); } @@ -428,9 +435,8 @@ function hex2a(hexx) { * @param {str} source path to file * @param {int} offset offset from which to start writing * @param {str} data data to write - * @param {*} action post-processing action */ -DataStoreServer.prototype.write = function(owner, source, offset, data, action){ +DataStoreServer.prototype.write = function(owner, source, offset, data){ const params = this.fillCommonParameters({ "method": "write", "owner" : owner, @@ -440,12 +446,11 @@ DataStoreServer.prototype.write = function(owner, source, offset, data, action){ "md5Hash": btoa(hex2a(md5(data))), }); - this._internal.sendHttpRequest( + return this._internal.sendHttpRequest( "POST", "application/octet-stream", this._internal.datastoreUrl, - encodeParams(params), - (response) => action(response) + encodeParams(params) ); } @@ -453,27 +458,25 @@ DataStoreServer.prototype.write = function(owner, source, offset, data, action){ * Delete file from the DSS * @param {str} owner owner of the file * @param {str} source path to file - * @param {*} action post-processing action */ -DataStoreServer.prototype.delete = function(owner, source, action){ +DataStoreServer.prototype.delete = function(owner, source){ const data = this.fillCommonParameters({ "method": "delete", "owner" : owner, "source": source }); - this._internal.sendHttpRequest( + return this._internal.sendHttpRequest( "DELETE", "application/octet-stream", this._internal.datastoreUrl, - encodeParams(data), - (response) => action(response) + encodeParams(data) ); } /** * Copy file within DSS */ -DataStoreServer.prototype.copy = function(sourceOwner, source, targetOwner, target, action){ +DataStoreServer.prototype.copy = function(sourceOwner, source, targetOwner, target){ const data = this.fillCommonParameters({ "method": "copy", "sourceOwner" : sourceOwner, @@ -481,19 +484,18 @@ DataStoreServer.prototype.copy = function(sourceOwner, source, targetOwner, targ "targetOwner": targetOwner, "target" : target }); - this._internal.sendHttpRequest( + return this._internal.sendHttpRequest( "POST", "application/octet-stream", this._internal.datastoreUrl, - encodeParams(data), - (response) => action(response) + encodeParams(data) ); } /** * Move file within DSS */ -DataStoreServer.prototype.move = function(sourceOwner, source, targetOwner, target, action){ +DataStoreServer.prototype.move = function(sourceOwner, source, targetOwner, target){ const data = this.fillCommonParameters({ "method": "move", "sourceOwner" : sourceOwner, @@ -501,32 +503,29 @@ DataStoreServer.prototype.move = function(sourceOwner, source, targetOwner, targ "targetOwner": targetOwner, "target" : target }); - this._internal.sendHttpRequest( + return this._internal.sendHttpRequest( "POST", "application/octet-stream", this._internal.datastoreUrl, - encodeParams(data), - (response) => action(response) + encodeParams(data) ); - } /** * Create a file/directory within DSS */ -DataStoreServer.prototype.create = function(owner, source, directory, action){ +DataStoreServer.prototype.create = function(owner, source, directory){ const data = this.fillCommonParameters({ "method": "create", "owner" : owner, "source": source, "directory": directory }); - this._internal.sendHttpRequest( + return this._internal.sendHttpRequest( "POST", "application/octet-stream", this._internal.datastoreUrl, - encodeParams(data), - (response) => action(response) + encodeParams(data) ); } @@ -537,74 +536,65 @@ DataStoreServer.prototype.create = function(owner, source, directory, action){ * ================================================================================== */ -DataStoreServer.prototype.begin = function(transactionId, action){ +DataStoreServer.prototype.begin = function(transactionId){ const data = this.fillCommonParameters({ "method": "begin", "transactionId" : transactionId }); - this._internal.sendHttpRequest( + return this._internal.sendHttpRequest( "POST", "application/octet-stream", this._internal.datastoreUrl, - encodeParams(data), - (response) => action(response) + encodeParams(data) ); - } -DataStoreServer.prototype.prepare = function(action){ +DataStoreServer.prototype.prepare = function(){ const data = this.fillCommonParameters({ "method": "prepare" }); - this._internal.sendHttpRequest( + return this._internal.sendHttpRequest( "POST", "application/octet-stream", this._internal.datastoreUrl, - encodeParams(data), - (response) => action(response) + encodeParams(data) ); - } -DataStoreServer.prototype.commit = function(action){ +DataStoreServer.prototype.commit = function(){ const data = this.fillCommonParameters({ "method": "commit" }); - this._internal.sendHttpRequest( + return this._internal.sendHttpRequest( "POST", "application/octet-stream", this._internal.datastoreUrl, - encodeParams(data), - (response) => action(response) + encodeParams(data) ); - } -DataStoreServer.prototype.rollback = function(action){ +DataStoreServer.prototype.rollback = function(){ const data = this.fillCommonParameters({ "method": "rollback" }); - this._internal.sendHttpRequest( + return this._internal.sendHttpRequest( "POST", "application/octet-stream", this._internal.datastoreUrl, - encodeParams(data), - (response) => action(response) + encodeParams(data) ); } -DataStoreServer.prototype.recover = function(action){ +DataStoreServer.prototype.recover = function(){ const data = this.fillCommonParameters({ "method": "recover" }); - this._internal.sendHttpRequest( + return this._internal.sendHttpRequest( "POST", "application/octet-stream", this._internal.datastoreUrl, - encodeParams(data), - (response) => action(response) + encodeParams(data) ); - } diff --git a/api-data-store-server-javascript/src/js/demo/server-data-store-client.js b/api-data-store-server-javascript/src/js/demo/server-data-store-client.js index 655d5e3a43287e3a5a0af3129a83f8cb85292929..8528af8c83a03683698d36d1f8703b02ced69250 100644 --- a/api-data-store-server-javascript/src/js/demo/server-data-store-client.js +++ b/api-data-store-server-javascript/src/js/demo/server-data-store-client.js @@ -12,19 +12,19 @@ datastoreServer = new DataStoreServer('http://localhost:8085', HTTP_SERVER_URI); /** Creates open button for reading the file from dss */ function createOpenButton(row, filePath, fileSize) { readButton = document.createElement("button"); - readButton.innerText = "open"; - readButton.dataset.filepath = filePath; - readButton.dataset.filesize = fileSize; - readButton.onclick = (function() { - datastoreServer.read(owner, this.dataset.filepath, 0, this.dataset.filesize, (responseData => { - const blob = responseData - const link = document.createElement('a') - link.href = window.URL.createObjectURL(blob) - link.download = filePath.substring(filePath.lastIndexOf('/') + 1) - link.click() - })); - }); - row.appendChild(readButton); + readButton.innerText = "open"; + readButton.dataset.filepath = filePath; + readButton.dataset.filesize = fileSize; + readButton.onclick = (function() { + datastoreServer.read(owner, this.dataset.filepath, 0, this.dataset.filesize).then((responseData) => { + const blob = responseData + const link = document.createElement('a') + link.href = window.URL.createObjectURL(blob) + link.download = filePath.substring(filePath.lastIndexOf('/') + 1) + link.click() + }); + }); + row.appendChild(readButton); } /** creates button for deleteing a file from the dss */ @@ -33,10 +33,8 @@ function createDeleteButton(row, filePath) { deleteButton.innerText = "delete"; deleteButton.dataset.filepath = filePath; deleteButton.onclick = (function() { - if (confirm("Do you want to delete "+filePath+" ?") == true) { - datastoreServer.delete(owner, this.dataset.filepath, (responseData => { - showEntries(); - })); + if (confirm("Do you want to delete " + filePath + "?")) { + datastoreServer.delete(owner, this.dataset.filepath).then(() => showEntries()); } }); row.appendChild(deleteButton); @@ -147,7 +145,7 @@ function showEntries() while (table.firstChild) { table.firstChild.remove() } - datastoreServer.list(owner, source, "true", displayReturnedFiles); + datastoreServer.list(owner, source, "true").then(displayReturnedFiles); } @@ -193,43 +191,36 @@ window.onload = function() { document.getElementById("write-submit").onclick = function() { if(isWriteValid()) { datastoreServer.write(owner, - document.getElementById("fpath").value.trim(), - parseInt(document.getElementById("foffset").value.trim()), - document.getElementById("write-text").value.trim(), - (_ => { - showEntries(); - })); + document.getElementById("fpath").value.trim(), + parseInt(document.getElementById("foffset").value.trim()), + document.getElementById("write-text").value.trim()) + .then(() => showEntries()); } }; document.getElementById("copy-submit").onclick = function() { - if(isCopyValid()) { - datastoreServer.copy(owner, document.getElementById("copy-from-path").value.trim(), owner, document.getElementById("copy-to-path").value.trim(), - (_ => { - showEntries(); - })); + if (isCopyValid()) { + datastoreServer.copy(owner, document.getElementById("copy-from-path").value.trim(), owner, + document.getElementById("copy-to-path").value.trim()) + .then(() => showEntries()); } }; document.getElementById("move-submit").onclick = function() { - if(isMoveValid()) { + if (isMoveValid()) { datastoreServer.move(owner, - document.getElementById("move-from-path").value.trim(), - owner, - document.getElementById("move-to-path").value.trim(), - (_ => { - showEntries(); - })); + document.getElementById("move-from-path").value.trim(), + owner, + document.getElementById("move-to-path").value.trim()) + .then(() => showEntries()); } }; document.getElementById("create-submit").onclick = function() { datastoreServer.create(owner, document.getElementById("create-path").value.trim(), - document.getElementById("create-directory").checked, - (_ => { - showEntries(); - })); + document.getElementById("create-directory").checked) + .then(() => showEntries()); }; } diff --git a/api-data-store-server-javascript/src/js/demo/server-data-store-login.js b/api-data-store-server-javascript/src/js/demo/server-data-store-login.js index ee6039ac7bb63447be43f3c207e870da528fe684..6e954f7bafe08265422cea54088b936c7d31e331 100644 --- a/api-data-store-server-javascript/src/js/demo/server-data-store-login.js +++ b/api-data-store-server-javascript/src/js/demo/server-data-store-login.js @@ -51,27 +51,25 @@ dssClientLoginPage.prototype.configure = function(){ } document.getElementById("logout-button").onclick = function() { - loginPage.datastore.logout(function(data) { + loginPage.datastore.logout().then(() => { document.getElementById("login-form-div").style.display = "block"; document.getElementById("main").style.display = "none"; document.getElementById("openbis-logo").style.height = "100px"; document.getElementById("username").focus(); }); - }; document.getElementById("login-form").onsubmit = function() { - loginPage.datastore.login(document.getElementById("username").value.trim(), - document.getElementById("password").value.trim(), - function(data) { - document.getElementById("username").value = ''; - document.getElementById("password").value = ''; - loginPage.onLogin(data); - }) + loginPage.datastore.login(document.getElementById("username").value.trim(), + document.getElementById("password").value.trim()).then((data) => { + document.getElementById("username").value = ''; + document.getElementById("password").value = ''; + loginPage.onLogin(data); + }) }; - loginPage.datastore.ifRestoredSessionActive(function(data) { loginPage.onLogin(data) }); + loginPage.datastore.ifRestoredSessionActive().then((data) => loginPage.onLogin(data)); document.onkeydown=function(evt){