0% found this document useful (0 votes)
5K views

JS

This document contains code for interacting with the Archer API. It defines functions for making HTTP requests to Archer, parsing responses as XML, and formatting data to send back to Archer. The code takes Archer credentials and authentication tokens as parameters, logs in to generate a session token, and provides a callback function to return data and context to Archer.

Uploaded by

Mostafa Ehab
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5K views

JS

This document contains code for interacting with the Archer API. It defines functions for making HTTP requests to Archer, parsing responses as XML, and formatting data to send back to Archer. The code takes Archer credentials and authentication tokens as parameters, logs in to generate a session token, and provides a callback function to return data and context to Archer.

Uploaded by

Mostafa Ehab
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

//Modules used within Archer

var httpRequest = require('request');


var xPath = require('xpath');
var xmlDOM = require('xmldom');
var parser = require('xml2js');

//Archer Stuff
/*********************************************************** */

//DataFeed Parameters
var params = (typeof context !== "undefined") ? context.CustomParameters : {
BaseURI: "https://archerdev.intranet.commerzbank.com/",
Instance: "Entwicklung",
User: "A2A_Delete_SCC_Records",
Password: "***"
};

//DataFeed Token
var tokens = (typeof context !== "undefined") ? context.Tokens : {
LastRunDate: '2018-06-19T05:52:08Z',
PreviousRunContext: "XYZ"
}

function callback_archer(myJSON) {
//FINALIZE THE DATA SETS
//--As a String
myDataString = jsonToXml(myJSON).toString();
//--As a Byte Array
myDataByteArray = new Buffer(myDataString);

myDataSet = myDataByteArray || myDataString || "";

//Callback to Archer only in Archer environment


if (typeof callback === "function") {
callback(null, {
//Return the data to RSA Archer
output: myDataSet,
previousRunContext: script_start_timestamp.toISOString()
});
}
else {
console.log(myDataString);
}
}
/*********************************************************** */

//XML Functions
/*********************************************************** */
function jsonToXml(jsonData) {
var bldrOpts = {
headless: true,
rootName: "ROOT",
renderOpts: {
'pretty': true,
'indent': ' ',
'newline': '\r\n',
'cdata': true
}
}
var responseBuilder = new parser.Builder(bldrOpts);
myResponse = responseBuilder.buildObject(jsonData).toString();

//Fix XML Tags


myResponse = myResponse.replace(new RegExp("<.*?>", 'g'), removeSpaces);
myResponse = myResponse.replace(new RegExp("<.*?>", 'g'), removeDashes);
myResponse = myResponse.replace(new RegExp("<.*?>", 'g'), fixLeadingDigits);

//Fix Timezone
myResponse = myResponse.replace(new RegExp("<state_time>.*?<\/state_time>",
'g'), fixTimeZone);
return myResponse;
}

function removeSpaces(source) {
return source.replace(new RegExp(" ", 'g'), "_");
}
function removeDashes(source) {
return source.replace(new RegExp("-", 'g'), "_");
}
function fixLeadingDigits(source) {
//End Tag
if(source.charAt(1) == '/' && ['0', '1', '2', '3', '4', '5', '6', '7', '8',
'9'].indexOf(source.charAt(2)) != -1) {
return '</_' + source.substring(2);
}
//Start Tag
if(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'].indexOf(source.charAt(1))
!= -1) {
return '<_' + source.substring(1);
}
return source;
}
function fixTimeZone(source) {
//create a Date object from source data and return an ISO String
myDateString = source.substring(source.indexOf(">")+1, source.indexOf("</"));
isoString = new Date(myDateString).toISOString();
tagName = source.substring(source.indexOf("<") + 1, source.indexOf(">"));
return "<" + tagName + ">" + isoString + "</" + tagName + ">";
}
function escapeXml(unsafe) {
return unsafe.replace(/[<>&'"]/g, function (c) {
switch (c) {
case '<': return '&lt;';
case '>': return '&gt;';
case '&': return '&amp;';
case '\'': return '&apos;';
case '"': return '&quot;';
}
});
}
/*********************************************************** */

//Global Variables
var script_start_timestamp = new Date();
var archerSessionToken = "";

//Requests
var request_1_login = {
method: "POST",
url: params.BaseURI + "/ws/general.asmx",
rejectUnauthorized: false,
headers: {
SOAPAction: "http://archer-
tech.com/webservices/CreateUserSessionFromInstance",
'Content-Type': "text/xml; charset=utf-8"
},
body: '<?xml version="1.0" encoding="utf-8"?>' +
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
'<soap:Body>' +
'<CreateUserSessionFromInstance xmlns="http://archer-
tech.com/webservices/">' +
'<userName>' + escapeXml(params.User) + '</userName>' +
'<instanceName>' + escapeXml(params.Instance) +
'</instanceName>' +
'<password>' + escapeXml(params.Password) + '</password>' +
'</CreateUserSessionFromInstance>' +
'</soap:Body>' +
'</soap:Envelope>'
}

//Let's start our work in callback hell ;-)


//==========================================

//Step 1: xyz
httpRequest(request_1_login, function handleResponse(error1, response1, body1) {
if (typeof response1 !== "undefined" && response1.statusCode == 200) {
myParser1 = new parser.Parser({explicitArray : false});
myParser1.parseString(body1, function(err, result) {
archerSessionToken = result['soap:Envelope']
['soap:Body'].CreateUserSessionFromInstanceResponse.CreateUserSessionFromInstanceRe
sult;
//console.log("archerSessionToken: "+archerSessionToken);
callback_archer({ergebnis:'Login erfolgt', token: archerSessionToken});

});
}
else {
throw new Error("The request did not return a 200 (OK) status.\r\nThe
returned error was:\r\n" + error1 + response1 + body1);
}
});

You might also like