Example of the protocol adaptor

Here is an example of a protocol adapter developed in the Nexacro Module Developer. This code is written as an example, and the suitability of the code has not been verified.

JSON data processing

This is an example of receiving the JSON data and converting it to SSV format.

1

Create a new object with the Protocol Adapter. Set the object id to "JSONRecvAdp".

2

Edit the code as follows.

Set a constant value in the initialize function, and convert the JSON data into SSV format in decrypt.

nexacro.JSONAdp.prototype.initialize = function()		
{
	this._rs_ = String.fromCharCode(30); //Record Separator
	this._us_ = String.fromCharCode(31); //Unit Separator
};

nexacro.JSONAdp.prototype.decrypt = function(strUrl, strData)		
{	
	var oJsonData = JSON.parse(strData);
	// Stream Header
	var ssvData = "SSV:" + oJsonData.codepage + this._rs_;

	// Variables
	var arrObj = oJsonData.parameters;
	var arrKey = Object.keys(arrObj);
	if(arrKey)
	{
		for(var i = 0; i < arrKey.length; i++)
		{
			if(i>0)
				ssvData +=this._rs_;
			ssvData += arrKey[i] + "=" + arrObj[arrKey[i]];
		}
		ssvData +=this._rs_;
	}

	// Datasets
	for(var i = 0; i < oJsonData.datasets.length; i++)
	{
		var dataset = oJsonData.datasets[i];
		// Dataset Header
		ssvData += "Dataset:" + dataset.ds_id + this._rs_;

		// Const Column Infos
		arrObj = dataset.ds_colinfo.constcolumn;
		arrKey = Object.keys(arrObj);
		if(arrKey)
		{
			ssvData += "_Const_"+this._us_;
			for(var j = 0; j < arrKey.length; j++)
			{
				if(j>0)
					ssvData +=this._us_;
				ssvData += arrKey[j] + "=" + arrObj[arrKey[j]];
			}
			ssvData += this._rs_;
		}

		// Column Infos
		var column = dataset.ds_colinfo.column;
		for(var j = 0; j < column.length; j++)
		{
			var cols = column[j];
			if(j == 0)
			{
				if(cols.id == "_RowType_")
				{
					ssvData += cols.id;
				}
				else
				{
					ssvData += "_RowType_";
					ssvData += this._us_ + cols.id+ ":" + cols.type + "(" + cols.size +")";
				}
			}
			else
			{
				ssvData += this._us_ + cols.id+ ":" + cols.type + "(" + cols.size +")";
			}
		}
		ssvData += this._rs_;

		// Records
		var row = dataset.ds_rows.row;
		for(var j = 0; j < row.length; j++)
		{
			var recode = row[j];
			arrKey = Object.keys(recode);

			if(arrKey[0] == "_RowType_")
			{
				ssvData += "N";
				var nCol = 1;
			}
			else
			{
				ssvData += "N";
				var nCol = 0;
			}
			for(; nCol < arrKey.length; nCol++)
			{
				if(recode[arrKey[nCol]] == null)
				{
					ssvData += this._us_ + "";
				}
				else
				{
					ssvData += this._us_ + recode[arrKey[nCol]];
				}
			}
			ssvData += this._rs_;
		}
		// Null Record
		ssvData += this._rs_;
	}
	trace(ssvData);
	return ssvData;
};

3

Select the menu [Deploy > Deploy Package] to run the Deploy Wizard.

4

Click the [Deploy] button to deploy the module installation file.

5

Install modules in Nexacro Studio and add protocol adapters.

6

Add a Dataset object and set it as follows.

7

Add the Grid and Button components to the screen and bind the Dataset object to the Grid component.

8

Write the onclick event handler function of the Button component as follows.

Set the nDataType parameter to 2 (SSV) because the JSON data will be converted to SSV format for processing.

this.Button00_onclick = function(obj:nexacro.Button,e:nexacro.ClickEventInfo)
{
    this.transaction("srv00", "JSONRecvAdp://127.0.0.1:4098/test.json", "","Dataset00=dsJson","","fnCallback", true, 2);
};

this.fnCallback = function(id, code, message)
{
    trace("Error["+code+"]:"+message);
}

9

Create a JSON file (test.json) in the Output folder as follows.

{
	"datatype": "JSON",
	"codepage": "utf-8",
	"parameters": {
		"ErrorCode": "0",
		"ErrorMsg": "OK"
	},
	"datasets": [
		{
			"ds_id": "dsJson",
			"ds_colinfo": {
				"constcolumn": {
					"ConstColumn0": "nexacro"
				},
				"column": [
					{
						"id": "Column0",
						"type": "STRING",
						"size": "256"
					},
					{
						"id": "Column1",
						"type": "DATE",
						"size": "256"
					}
				]
			},
			"ds_rows": {
				"row": [
					{
						"Column0": "3",
						"Column1": "20201124"
					},
					{
						"Column0": "4",
						"Column1": "20200122"
					}
				]
			}
		}
	]
}

10

After running it in a web browser with QuickView (Ctrl + F6), check if the data set in the JSON file is reflected when the button is clicked.

You can see the string converted to SSV data and a success message in the web browser console window.

Symmetric key encryption and decryption processing (crypto-js)

This is an example using an open source crypto-js. Encryption and decryption are applied when a protocol adapter is applied.

Refer to the following URL for the crypto-js.

https://code.google.com/archive/p/crypto-js/

The CryptoJS v3.1.2 was used for the sample below.

A license notice must be included when distributing modules including crypto-js.

https://code.google.com/archive/p/crypto-js/wikis/License.wiki

1

Create a new object with the Protocol Adapter. Set the object id to "cryptojsAdp".

2

Edit the code as follows.

Set the symmetric key ("nexacro platform") and the initialization vector values in the initialize function. Encryption and decryption are processed in the encrypt and decrpt, and the original data and converted data are displayed using the trace method.

nexacro.cryptojsAdp.prototype.initialize = function()		
{	
	this._key = CryptoJS.enc.Utf8.parse("nexacro platform");
	this._iv = CryptoJS.enc.Utf8.parse(1234567812345678);
};	

nexacro.cryptojsAdp.prototype.encrypt = function(strUrl, strData)		
{	
	var encrypted = CryptoJS.AES.encrypt(strData, this._key, { 
		iv: this._iv,
		mode:CryptoJS.mode.CBC,
		padding:CryptoJS.pad.ZeroPadding
	});
	trace("strData: "+strData);
	trace("encrypted.toString(): "+encrypted.toString());
	return encrypted.toString();
};	
		
nexacro.cryptojsAdp.prototype.decrypt = function(strUrl, strData)		
{	
	var decrypted = CryptoJS.AES.decrypt(strData, this._key, {
		iv:this._iv,
		padding:CryptoJS.pad.ZeroPadding
	}); 
	trace("strData: "+strData);
	trace("decrypted: "+decrypted.toString(CryptoJS.enc.Utf8));		
	return decrypted.toString(CryptoJS.enc.Utf8);
};

3

Add User Folder.

4

Change the name of User Folder to “CryptoJS v3.1.2”.

5

Select User Folder and select [Import Files] from the context menu.

6

Select the two files below in Explorer and add them to User Folder.

CryptoJS v3.1.2
+ rollups > aes.js
+ components > pad-zeropadding-min.js

7

Select the menu [Deploy > Deploy Package] to run the Deploy Wizard.

8

Click [Next] and move to the JSON editing screen.

9

Add the js file added to the User Folder to the scripts item.

{
	"name": "cryptojsAdp",
	"moduletype": "protocoladaptor",
	"objInfo": [
	    "cryptojsAdp/_metainfo_/cryptojsAdp.info"
	],
	"scripts": [
    	"cryptojsAdp/CryptoJS v3.1.2/aes.js",
		"cryptojsAdp/CryptoJS v3.1.2/pad-zeropadding-min.js",
	    "cryptojsAdp/cryptojsAdp.js"
	],
	"userfolder": [
	    "cryptojsAdp/CryptoJS v3.1.2"
	]
}

The edited JSON scripts are not saved. They need to be manually edited when distributing.

10

Click the [Deploy] button to deploy the module installation file.

11

Install modules in Nexacro Studio and add protocol adapters.

12

Enter the transaction script in the screen as follows.

this.Button00_onclick = function(obj:nexacro.Button,e:nexacro.ClickEventInfo)
{
    this.transaction("srv00", "cryptojsAdp://127.0.0.1:4098/test.xml", "","dsOut=Dataset00","value=a","fnCallback");
};

this.fnCallback = function(id, code, message)
{
    trace("Error["+code+"]:"+message);
}

13

After running it in a web browser with QuickView (Ctrl + F6), check if the data is encrypted and decrypted when the button is clicked.

The test.xml file will return a string that encrypts the request value for the functional test. In this example, the request value is decrypted and returned.

strData: <?xml version="1.0" encoding="UTF-8"?>
<Root xmlns="http://www.nexacroplatform.com/platform/dataset">
    <Parameters>
        <Parameter id="value">a</Parameter>
    </Parameters>
</Root>

encrypted.toString(): 3TlyqNLXxHG8CN1MMIb3vAO/KGkU1OSAFl1wuMvAtEfq6jS6Vp95xvFLhCH/KKrSr5svsoRf1zi8lYmuQ0QMecYIPS+gz9KjeVCdwZ1F9/n/BQIrhN2ahuVDlQevU0+1fwirvXakwR6whvDPPY4ecgDgv7c3kCRVpxjYQk3AKJnZcKTwscCmfolfx9JzHmhq8R6SQKS9C8YJ/ju8j+VI2ZstQCoLU9JaJ0ATZbwGCmE=

strData: 3TlyqNLXxHG8CN1MMIb3vAO/KGkU1OSAFl1wuMvAtEfq6jS6Vp95xvFLhCH/KKrSr5svsoRf1zi8lYmuQ0QMecYIPS+gz9KjeVCdwZ1F9/n/BQIrhN2ahuVDlQevU0+1fwirvXakwR6whvDPPY4ecgDgv7c3kCRVpxjYQk3AKJnZcKTwscCmfolfx9JzHmhq8R6SQKS9C8YJ/ju8j+VI2ZstQCoLU9JaJ0ATZbwGCmE=


decrypted.toString(CryptoJS.enc.Utf8): <?xml version="1.0" encoding="UTF-8"?>
<Root xmlns="http://www.nexacroplatform.com/platform/dataset">
    <Parameters>
        <Parameter id="value">a</Parameter>
    </Parameters>
</Root>