hedera-web/js/vn/json-connection.js

322 lines
7.2 KiB
JavaScript
Raw Normal View History

2016-09-26 09:28:47 +00:00
2022-06-06 16:02:17 +00:00
var VnObject = require('./object');
2022-05-05 13:56:17 +00:00
var JsonException = require('./json-exception');
2016-09-26 09:28:47 +00:00
2016-09-23 22:47:34 +00:00
/**
* Handler for JSON rest connections.
2022-05-26 06:08:31 +00:00
*/
2022-05-05 13:56:17 +00:00
module.exports = new Class({
2022-06-06 16:02:17 +00:00
Extends: VnObject
2016-09-23 22:47:34 +00:00
,_connected: false
,_requestsCount: 0
,token: null
2016-09-23 22:47:34 +00:00
/**
* Initilizes the connection object.
2022-05-26 06:08:31 +00:00
*/
2022-05-05 13:56:17 +00:00
,initialize: function() {
2022-06-06 16:02:17 +00:00
VnObject.prototype.initialize.call(this);
2022-05-05 13:56:17 +00:00
this.fetchToken();
2016-10-04 15:27:49 +00:00
}
2022-05-05 13:56:17 +00:00
,fetchToken: function() {
var token = null;
2022-05-05 13:56:17 +00:00
if (sessionStorage.getItem('vnToken'))
token = sessionStorage.getItem('vnToken');
if (localStorage.getItem('vnToken'))
token = localStorage.getItem('vnToken');
this.token = token;
}
2022-05-05 13:56:17 +00:00
,clearToken: function() {
this.token = null;
2022-05-05 13:56:17 +00:00
localStorage.removeItem('vnToken');
sessionStorage.removeItem('vnToken');
2016-09-23 22:47:34 +00:00
}
/**
* Opens the connection to the REST service.
*
2016-10-04 15:27:49 +00:00
* @param {String} user The user name
2016-09-23 22:47:34 +00:00
* @param {String} password The user password
* @param {Boolean} remember Specifies if the user should be remembered
* @param {Function} openCallback The function to call when operation is done
2022-05-26 06:08:31 +00:00
*/
2022-05-05 13:56:17 +00:00
,open: function(user, pass, remember, callback) {
if (user !== null && user !== undefined) {
2016-09-23 22:47:34 +00:00
var params = {
user: user
,password: pass
,remember: remember
2016-09-23 22:47:34 +00:00
};
2022-05-05 13:56:17 +00:00
} else
2016-09-23 22:47:34 +00:00
var params = null;
2022-05-05 13:56:17 +00:00
this.lbSend('POST', 'Accounts/login', params,
this._onOpen.bind(this, callback, remember));
2016-09-23 22:47:34 +00:00
}
/*
* Called when open operation is done.
*/
2022-05-05 13:56:17 +00:00
,_onOpen: function(callback, remember, json, error) {
if (json) {
2016-09-23 22:47:34 +00:00
this._connected = true;
this.token = json.token;
2016-09-23 22:47:34 +00:00
var storage = remember ? localStorage : sessionStorage;
2022-05-05 13:56:17 +00:00
storage.setItem('vnToken', this.token);
2016-09-23 22:47:34 +00:00
2022-05-30 01:30:33 +00:00
this.emit('openned');
2022-05-05 13:56:17 +00:00
} else
this._closeClient();
2016-09-23 22:47:34 +00:00
if (callback)
2022-05-05 13:56:17 +00:00
callback(this, this._connected, error);
2016-09-23 22:47:34 +00:00
}
/**
* Closes the connection to the REST service.
*
* @param {Function} callback The function to call when operation is done
2022-05-26 06:08:31 +00:00
*/
2022-05-05 13:56:17 +00:00
,close: function(callback) {
this.lbSend('POST', 'Accounts/logout', null,
this._onClose.bind(this, callback));
this._closeClient();
2016-09-23 22:47:34 +00:00
}
/*
* Called when close operation is done.
*/
2022-05-05 13:56:17 +00:00
,_onClose: function(callback, json, error) {
2022-05-30 01:30:33 +00:00
this.emit('closed');
2016-09-23 22:47:34 +00:00
if (callback)
2022-05-05 13:56:17 +00:00
callback(this, null, error);
2016-09-23 22:47:34 +00:00
}
2016-09-24 14:32:31 +00:00
2022-05-05 13:56:17 +00:00
,_closeClient: function() {
2016-09-24 14:32:31 +00:00
this._connected = false;
2022-05-05 13:56:17 +00:00
this.clearToken();
2016-09-24 14:32:31 +00:00
}
2016-09-23 22:47:34 +00:00
2016-10-04 15:27:49 +00:00
/**
2022-05-05 13:56:17 +00:00
* Supplants another user.
2016-10-04 15:27:49 +00:00
*
* @param {String} user The user name
* @param {Function} callback The callback function
2022-05-26 06:08:31 +00:00
*/
2022-05-05 13:56:17 +00:00
,supplantUser: function(user, callback) {
var params = {supplantUser: user};
2022-05-05 13:56:17 +00:00
this.send('client/supplant', params,
this._onUserSupplant.bind(this, callback));
2016-10-04 15:27:49 +00:00
}
2022-10-04 13:12:16 +00:00
2022-05-26 08:00:19 +00:00
,_onUserSupplant: function(callback, json, err) {
2016-10-04 15:27:49 +00:00
if (json)
this.token = json;
2016-10-04 15:27:49 +00:00
if (callback)
2022-05-26 08:00:19 +00:00
callback(err == null, err);
2016-10-04 15:27:49 +00:00
}
/**
* Ends the user supplanting and restores the last login.
2022-05-26 06:08:31 +00:00
*/
2022-05-05 13:56:17 +00:00
,supplantEnd: function() {
2022-05-09 13:52:29 +00:00
this.lbSend('POST', 'Accounts/logout');
2022-05-05 13:56:17 +00:00
this.fetchToken();
2016-10-04 15:27:49 +00:00
}
2016-09-23 22:47:34 +00:00
/**
* Executes the specified REST service with the given params and calls
* the callback when response is received.
*
* @param {String} restService The service path
* @param {Map} params The params to pass to the service
* @param {Function} callback The response callback
2022-05-26 06:08:31 +00:00
*/
2022-05-05 13:56:17 +00:00
,send: function(restService, params, callback) {
2016-09-23 22:47:34 +00:00
if (!params)
params = {};
params.srv = 'json:'+ restService;
2016-09-23 22:47:34 +00:00
2022-05-05 13:56:17 +00:00
this.sendWithUrl(params, callback, 'POST', '.');
2016-09-23 22:47:34 +00:00
}
2022-05-05 13:56:17 +00:00
,sendForm: function(form, callback) {
2016-09-23 22:47:34 +00:00
var params = {};
var elements = form.elements;
for (var i = 0; i < elements.length; i++)
if (elements[i].name)
params[elements[i].name] = elements[i].value;
2022-05-05 13:56:17 +00:00
this.sendWithUrl(params, callback, 'POST', form.action);
2016-09-23 22:47:34 +00:00
}
2022-05-05 13:56:17 +00:00
,sendFormMultipart: function(form, callback) {
var formData = new FormData(form);
2016-09-23 22:47:34 +00:00
2022-05-05 13:56:17 +00:00
var request = new XMLHttpRequest();
request.open('POST', form.action, true);
if (this.token)
2022-05-05 13:56:17 +00:00
request.setRequestHeader('Authorization', this.token);
2016-09-23 22:47:34 +00:00
request.onreadystatechange =
2022-05-05 13:56:17 +00:00
this._onStateChange.bind(this, request, callback);
request.send(formData);
2016-09-23 22:47:34 +00:00
2022-05-05 13:56:17 +00:00
this._addRequest();
2016-09-23 22:47:34 +00:00
}
2022-05-05 13:56:17 +00:00
,sendFormData: function(formData, callback) {
var request = new XMLHttpRequest();
request.open('POST', '', true);
2017-05-08 15:54:35 +00:00
if (this.token)
2022-05-05 13:56:17 +00:00
request.setRequestHeader('Authorization', this.token);
2017-05-08 15:54:35 +00:00
request.onreadystatechange =
2022-05-05 13:56:17 +00:00
this._onStateChange.bind(this, request, callback);
request.send(formData);
2017-05-08 15:54:35 +00:00
2022-05-05 13:56:17 +00:00
this._addRequest();
2017-05-08 15:54:35 +00:00
}
2016-09-23 22:47:34 +00:00
/*
* Called when REST response is received.
*/
2022-05-05 13:56:17 +00:00
,sendWithUrl: function(params, callback, method, url) {
var request = new XMLHttpRequest();
request.open(method, url, true);
request.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
if (this.token)
2022-05-05 13:56:17 +00:00
request.setRequestHeader('Authorization', this.token);
request.onreadystatechange =
this._onStateChange.bind(this, request, callback);
request.send(Vn.Url.makeUri(params));
2016-09-23 22:47:34 +00:00
2022-05-05 13:56:17 +00:00
this._addRequest();
}
,lbSend: function(method, url, params, callback) {
var request = new XMLHttpRequest();
request.open(method, `api/${url}`, true);
request.setRequestHeader('Content-Type',
'application/json;charset=utf-8');
if (this.token)
request.setRequestHeader('Authorization', this.token);
2016-09-23 22:47:34 +00:00
request.onreadystatechange =
2022-05-05 13:56:17 +00:00
this._onStateChange.bind(this, request, callback);
request.send(params && JSON.stringify(params));
2016-09-23 22:47:34 +00:00
2022-05-05 13:56:17 +00:00
this._addRequest();
2016-09-23 22:47:34 +00:00
}
2022-05-05 13:56:17 +00:00
,_addRequest: function() {
2016-09-23 22:47:34 +00:00
this._requestsCount++;
if (this._requestsCount === 1)
2022-05-30 01:30:33 +00:00
this.emit('loading-changed', true);
2016-09-23 22:47:34 +00:00
}
2022-05-05 13:56:17 +00:00
,_onStateChange: function(request, callback) {
2016-09-23 22:47:34 +00:00
if (request.readyState !== 4)
return;
this._requestsCount--;
if (this._requestsCount === 0)
2022-05-30 01:30:33 +00:00
this.emit('loading-changed', false);
2016-09-23 22:47:34 +00:00
var data = null;
var error = null;
try {
2022-05-05 13:56:17 +00:00
if (request.status == 0) {
var ex = new JsonException();
ex.message = _('The server does not respond, please check your Internet connection');
2016-09-23 22:47:34 +00:00
throw ex;
}
var contentType = null;
try {
contentType = request
2022-05-05 13:56:17 +00:00
.getResponseHeader('Content-Type')
.split(';')[0]
.trim();
} catch (err) {
console.warn(err);
2016-09-23 22:47:34 +00:00
}
2022-05-05 13:56:17 +00:00
if (contentType != 'application/json') {
var ex = new JsonException();
2016-09-23 22:47:34 +00:00
ex.message = request.statusText;
ex.code = request.status;
throw ex;
}
2022-05-05 13:56:17 +00:00
var json;
var jsData;
if (request.responseText)
json = JSON.parse(request.responseText);
if (json)
jsData = json.data || json;
if (request.status >= 200 && request.status < 300) {
2016-09-23 22:47:34 +00:00
data = jsData;
2022-05-05 13:56:17 +00:00
} else {
var exception = jsData.exception;
2022-05-05 13:56:17 +00:00
var error = jsData.error;
2022-05-05 13:56:17 +00:00
if (exception) {
exception = exception
2022-05-05 13:56:17 +00:00
.replace(/\\/g, '.')
.replace(/Exception$/, '')
.replace(/^Vn\.Web\./, '');
var ex = new JsonException();
ex.exception = exception;
ex.message = jsData.message;
ex.code = jsData.code;
ex.file = jsData.file;
ex.line = jsData.line;
ex.trace = jsData.trace;
} else if (error) {
var ex = new Error();
ex.name = error.name;
ex.message = error.message;
ex.code = error.code;
ex.statusCode = request.status;
}
2016-09-23 22:47:34 +00:00
throw ex;
}
2022-05-05 13:56:17 +00:00
} catch (e) {
2016-09-23 22:47:34 +00:00
data = null;
error = e;
}
if (callback)
2016-10-11 14:45:10 +00:00
try {
2022-05-05 13:56:17 +00:00
callback(data, error);
2016-10-11 14:45:10 +00:00
error = null;
2022-05-05 13:56:17 +00:00
} catch (e) {
2016-10-11 14:45:10 +00:00
error = e;
}
2022-05-05 13:56:17 +00:00
if (error) {
if (error.exception == 'SessionExpired')
2022-05-05 13:56:17 +00:00
this.clearToken();
2022-05-26 08:00:19 +00:00
2022-05-30 01:30:33 +00:00
this.emit('error', error);
}
2016-09-23 22:47:34 +00:00
}
});