2016-09-26 09:28:47 +00:00
|
|
|
|
2017-04-08 11:42:27 +00:00
|
|
|
var VnObject = require ('./object');
|
2016-09-26 09:28:47 +00:00
|
|
|
var JsonException = require ('./json-exception');
|
|
|
|
|
2016-09-23 22:47:34 +00:00
|
|
|
/**
|
|
|
|
* Handler for JSON rest connections.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2016-09-26 09:28:47 +00:00
|
|
|
module.exports = new Class
|
2016-09-23 22:47:34 +00:00
|
|
|
({
|
2017-04-08 11:42:27 +00:00
|
|
|
Extends: VnObject
|
2016-09-23 22:47:34 +00:00
|
|
|
|
|
|
|
,_connected: false
|
|
|
|
,_requestsCount: 0
|
2016-10-13 15:07:48 +00:00
|
|
|
,token: null
|
2016-09-23 22:47:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initilizes the connection object.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2016-09-23 22:47:34 +00:00
|
|
|
,initialize: function ()
|
|
|
|
{
|
|
|
|
this.parent ();
|
2016-10-20 08:37:08 +00:00
|
|
|
this.fetchToken ();
|
2016-10-04 15:27:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
,fetchToken: function ()
|
|
|
|
{
|
2016-10-20 08:37:08 +00:00
|
|
|
var token = null;
|
|
|
|
|
2016-09-24 14:32:31 +00:00
|
|
|
if (sessionStorage.getItem ('vnToken'))
|
2016-10-20 08:37:08 +00:00
|
|
|
token = sessionStorage.getItem ('vnToken');
|
2016-10-04 15:27:49 +00:00
|
|
|
if (localStorage.getItem ('vnToken'))
|
2016-10-20 08:37:08 +00:00
|
|
|
token = localStorage.getItem ('vnToken');
|
|
|
|
|
|
|
|
this.token = token;
|
|
|
|
}
|
|
|
|
|
|
|
|
,clearToken: function ()
|
|
|
|
{
|
|
|
|
this.token = null;
|
|
|
|
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
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2016-09-23 22:47:34 +00:00
|
|
|
,open: function (user, pass, remember, callback)
|
|
|
|
{
|
2016-10-16 14:16:08 +00:00
|
|
|
if (user !== null && user !== undefined)
|
2016-09-23 22:47:34 +00:00
|
|
|
{
|
|
|
|
var params = {
|
2017-04-08 11:42:27 +00:00
|
|
|
user: user,
|
|
|
|
password: pass,
|
|
|
|
remember: remember
|
2016-09-23 22:47:34 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
var params = null;
|
|
|
|
|
|
|
|
this.send ('core/login', params,
|
|
|
|
this._onOpen.bind (this, callback, remember));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called when open operation is done.
|
|
|
|
*/
|
|
|
|
,_onOpen: function (callback, remember, json, error)
|
|
|
|
{
|
|
|
|
if (json && json.login)
|
|
|
|
{
|
|
|
|
this._connected = true;
|
2016-10-13 15:07:48 +00:00
|
|
|
this.token = json.token;
|
2016-09-23 22:47:34 +00:00
|
|
|
|
|
|
|
var storage = remember ? localStorage : sessionStorage;
|
2016-10-13 15:07:48 +00:00
|
|
|
storage.setItem ('vnToken', this.token);
|
2016-09-23 22:47:34 +00:00
|
|
|
|
2017-04-19 06:16:37 +00:00
|
|
|
this.emit ('openned');
|
2016-09-23 22:47:34 +00:00
|
|
|
}
|
2016-09-24 14:32:31 +00:00
|
|
|
else
|
|
|
|
this._closeClient ();
|
2016-09-23 22:47:34 +00:00
|
|
|
|
|
|
|
if (callback)
|
|
|
|
callback (this, this._connected, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes the connection to the REST service.
|
|
|
|
*
|
|
|
|
* @param {Function} callback The function to call when operation is done
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2016-09-23 22:47:34 +00:00
|
|
|
,close: function (callback)
|
|
|
|
{
|
2016-09-24 14:32:31 +00:00
|
|
|
this._closeClient ();
|
2016-09-23 22:47:34 +00:00
|
|
|
this.send ('core/logout', null,
|
|
|
|
this._onClose.bind (this, callback));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called when close operation is done.
|
|
|
|
*/
|
|
|
|
,_onClose: function (callback, json, error)
|
|
|
|
{
|
2017-04-19 06:16:37 +00:00
|
|
|
this.emit ('closed');
|
2016-09-23 22:47:34 +00:00
|
|
|
|
|
|
|
if (callback)
|
2016-10-30 22:48:18 +00:00
|
|
|
callback (this, json === true, error);
|
2016-09-23 22:47:34 +00:00
|
|
|
}
|
2016-09-24 14:32:31 +00:00
|
|
|
|
|
|
|
,_closeClient: function ()
|
|
|
|
{
|
|
|
|
this._connected = false;
|
2016-10-20 08:37:08 +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
|
|
|
/**
|
|
|
|
* Suppants another user.
|
|
|
|
*
|
|
|
|
* @param {String} user The user name
|
|
|
|
* @param {Function} callback The callback function
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2016-10-04 15:27:49 +00:00
|
|
|
,supplantUser: function (user, callback)
|
|
|
|
{
|
|
|
|
var params = {'supplantUser': user};
|
|
|
|
this.send ('core/supplant', params,
|
|
|
|
this._onUserSupplant.bind (this, callback));
|
|
|
|
}
|
|
|
|
|
2017-04-08 11:42:27 +00:00
|
|
|
,_onUserSupplant: function (callback, json)
|
2016-10-04 15:27:49 +00:00
|
|
|
{
|
|
|
|
if (json)
|
2016-10-13 15:07:48 +00:00
|
|
|
this.token = json;
|
2016-10-04 15:27:49 +00:00
|
|
|
|
|
|
|
if (callback)
|
|
|
|
callback (json != null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ends the user supplanting and restores the last login.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2016-10-04 15:27:49 +00:00
|
|
|
,supplantEnd: function ()
|
|
|
|
{
|
2016-10-20 08:37:08 +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
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2016-09-23 22:47:34 +00:00
|
|
|
,send: function (restService, params, callback)
|
|
|
|
{
|
|
|
|
if (!params)
|
|
|
|
params = {};
|
|
|
|
|
|
|
|
params['srv'] = 'json:'+ restService;
|
|
|
|
|
2016-12-20 09:32:17 +00:00
|
|
|
this.sendWithUrl (params, callback, 'POST', '.');
|
2016-09-23 22:47:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
,sendForm: function (form, callback)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2016-12-20 09:32:17 +00:00
|
|
|
this.sendWithUrl (params, callback, 'POST', form.action);
|
2016-09-23 22:47:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
,sendFormMultipart: function (form, callback)
|
|
|
|
{
|
|
|
|
var formData = new FormData (form);
|
|
|
|
|
2016-10-13 15:07:48 +00:00
|
|
|
if (this.token)
|
|
|
|
formData.append ('token', this.token);
|
2016-09-23 22:47:34 +00:00
|
|
|
|
|
|
|
var request = new XMLHttpRequest ();
|
2016-12-20 09:32:17 +00:00
|
|
|
request.open ('POST', form.action, true);
|
2016-09-23 22:47:34 +00:00
|
|
|
request.onreadystatechange =
|
|
|
|
this._onStateChange.bind (this, request, callback);
|
|
|
|
request.send (formData);
|
|
|
|
|
|
|
|
this._addRequest ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called when REST response is received.
|
|
|
|
*/
|
|
|
|
,sendWithUrl: function (params, callback, method, url)
|
|
|
|
{
|
2016-10-13 15:07:48 +00:00
|
|
|
if (this.token)
|
|
|
|
params['token'] = this.token;
|
2016-09-23 22:47:34 +00:00
|
|
|
|
|
|
|
var request = new XMLHttpRequest ();
|
|
|
|
request.open (method, url, true);
|
|
|
|
request.setRequestHeader ('Content-Type',
|
|
|
|
'application/x-www-form-urlencoded');
|
|
|
|
request.onreadystatechange =
|
|
|
|
this._onStateChange.bind (this, request, callback);
|
|
|
|
request.send (Vn.Url.makeUri (params));
|
|
|
|
|
|
|
|
this._addRequest ();
|
|
|
|
}
|
|
|
|
|
|
|
|
,_addRequest: function ()
|
|
|
|
{
|
|
|
|
this._requestsCount++;
|
|
|
|
|
|
|
|
if (this._requestsCount === 1)
|
2017-04-19 06:16:37 +00:00
|
|
|
this.emit ('loading-changed', true);
|
2016-09-23 22:47:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
,_onStateChange: function (request, callback)
|
|
|
|
{
|
|
|
|
if (request.readyState !== 4)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._requestsCount--;
|
|
|
|
|
|
|
|
if (this._requestsCount === 0)
|
2017-04-19 06:16:37 +00:00
|
|
|
this.emit ('loading-changed', false);
|
2016-09-23 22:47:34 +00:00
|
|
|
|
|
|
|
var data = null;
|
|
|
|
var error = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (request.status == 0)
|
|
|
|
{
|
2016-09-26 09:28:47 +00:00
|
|
|
var ex = new JsonException ();
|
2016-10-20 08:37:08 +00:00
|
|
|
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
|
|
|
|
.getResponseHeader ('Content-Type')
|
|
|
|
.split (';')[0]
|
|
|
|
.trim ();
|
|
|
|
}
|
|
|
|
catch (e) {}
|
|
|
|
|
|
|
|
if (contentType != 'application/json')
|
|
|
|
{
|
2016-09-26 09:28:47 +00:00
|
|
|
var ex = new JsonException ();
|
2016-09-23 22:47:34 +00:00
|
|
|
ex.message = request.statusText;
|
|
|
|
ex.code = request.status;
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
|
|
|
|
var json = JSON.parse (request.responseText);
|
|
|
|
var jsData = json.data;
|
2016-10-30 22:48:18 +00:00
|
|
|
//var jsWarns = json.warnings;
|
2016-09-23 22:47:34 +00:00
|
|
|
|
|
|
|
if (request.status == 200)
|
|
|
|
{
|
|
|
|
data = jsData;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-20 08:37:08 +00:00
|
|
|
var exception = jsData.exception;
|
|
|
|
|
|
|
|
if (exception)
|
|
|
|
exception = exception
|
|
|
|
.replace (/\\/g, '.')
|
|
|
|
.replace (/Exception$/, '')
|
|
|
|
.replace (/^Vn\.Web\./, '');
|
|
|
|
|
2016-09-26 09:28:47 +00:00
|
|
|
var ex = new JsonException ();
|
2017-04-08 11:42:27 +00:00
|
|
|
Object.assign (ex, {
|
|
|
|
exception: exception,
|
|
|
|
message: jsData.message,
|
|
|
|
code: jsData.code,
|
|
|
|
file: jsData.file,
|
|
|
|
line: jsData.line,
|
|
|
|
trace: jsData.trace
|
|
|
|
});
|
2016-09-23 22:47:34 +00:00
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (e)
|
|
|
|
{
|
|
|
|
data = null;
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (callback)
|
2017-04-08 11:42:27 +00:00
|
|
|
// try {
|
2016-09-23 22:47:34 +00:00
|
|
|
callback (data, error);
|
2017-04-08 11:42:27 +00:00
|
|
|
/* error = null;
|
2016-10-11 14:45:10 +00:00
|
|
|
}
|
|
|
|
catch (e)
|
|
|
|
{
|
|
|
|
error = e;
|
|
|
|
}
|
2017-04-08 11:42:27 +00:00
|
|
|
*/
|
2016-10-11 14:45:10 +00:00
|
|
|
if (error)
|
2016-10-20 08:37:08 +00:00
|
|
|
{
|
|
|
|
if (error.exception == 'SessionExpired')
|
|
|
|
this.clearToken ();
|
|
|
|
|
2017-04-19 06:16:37 +00:00
|
|
|
this.emit ('error', error);
|
2016-10-20 08:37:08 +00:00
|
|
|
}
|
2016-09-23 22:47:34 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|