Welcome Serena Central users! CLICK HERE
The migration of the Serena Central community is currently underway. Be sure to read THIS MESSAGE to get your new login set up to access your account.
Marcus Tornberg Super Contributor.
Super Contributor.
292 views

ECMA script error


Hi all.

I have a customer that is running an HR-system using Service Bus for
Windows Server. I need to retrieve updates from this HR-system using
REST.

To authenticate I need to send a SAS token as an Authorization header.

To generate the SAS token I have created a ECMA-script seen below. I
call the function using
getToken('https://fake.url.org','secretkey','username');

When I call the function using Designer it works well. However when I
try to run it on the engine I get an error message. What could be the
cause for this issue on the engine side? Does anyone have any idea what
could be done to fix it?

The response should be similar to:

Code:
--------------------
SharedAccessSignature sr=https%3a%2f%2ffake.url.org&sig=lCXLvuP%2fbek0vRpPYiCWXXe%2brcGt8h4nGvmYaJtGgqc%3d&se=1441794669&skn=username
--------------------


Error message on the engine:

Code:
--------------------
DirXML Log Event -------------------
Driver: \DEV-TREE\idm\system\Driver Set\ECMA
Channel: Subscriber
Status: Error
Message: Code(-9083) Error submitting event to subscriber: Code(-9010) An exception occurred: java.lang.NoSuchMethodError: com.novell.soa.script.mozilla.javascript.optimizer.OptRuntime.thisGet(Lcom/novell/soa/script/mozilla/javascript/Scriptable;Ljava/lang/String;Lcom/novell/soa/script/mozilla/javascript/Scriptable;)Ljava/lang/Object;
at com.novell.soa.script.mozilla.javascript.gen.c9._c13(vnd.nds.stream://DEV-TREE/idm/system/Driver+Set/ECMA/SAS-token2#DirXML-Data:152)
at com.novell.soa.script.mozilla.javascript.gen.c9.call(vnd.nds.stream://DEV-TREE/idm/system/Driver+Set/ECMA/SAS-token2#DirXML-Data)
at com.novell.soa.script.mozilla.javascript.optimizer.OptRuntime.call1(Unknown Source)
at com.novell.soa.script.mozilla.javascript.gen.c9._c24(vnd.nds.stream://DEV-TREE/idm/system/Driver+Set/ECMA/SAS-token2#DirXML-Data:221)
at com.novell.soa.script.mozilla.javascript.gen.c9.call(vnd.nds.stream://DEV-TREE/idm/system/Driver+Set/ECMA/SAS-token2#DirXML-Data)
at com.novell.soa.script.mozilla.javascript.optimizer.OptRuntime.call1(Unknown Source)
at com.novell.soa.script.mozilla.javascript.gen.c9._c29(vnd.nds.stream://DEV-TREE/idm/system/Driver+Set/ECMA/SAS-token2#DirXML-Data:257)
at com.novell.soa.script.mozilla.javascript.gen.c9.call(vnd.nds.stream://DEV-TREE/idm/system/Driver+Set/ECMA/SAS-token2#DirXML-Data)
at com.novell.soa.script.mozilla.javascript.optimizer.OptRuntime.call1(Unknown Source)
at com.novell.soa.script.mozilla.javascript.gen.c9._c45(vnd.nds.stream://DEV-TREE/idm/system/Driver+Set/ECMA/SAS-token2#DirXML-Data:375)
at com.novell.soa.script.mozilla.javascript.gen.c9.call(vnd.nds.stream://DEV-TREE/idm/system/Driver+Set/ECMA/SAS-token2#DirXML-Data)
at com.novell.soa.script.mozilla.javascript.optimizer.OptRuntime.callProp0(Unknown Source)
at com.novell.soa.script.mozilla.javascript.gen.c9._c44(vnd.nds.stream://DEV-TREE/idm/system/Driver+Set/ECMA/SAS-token2#DirXML-Data:370)
at com.novell.soa.script.mozilla.javascript.gen.c9.call(vnd.nds.stream://DEV-TREE/idm/system/Driver+Set/ECMA/SAS-token2#DirXML-Data)
at com.novell.soa.script.mozilla.javascript.ScriptRuntime.applyOrCall(Unknown Source)
at com.novell.soa.script.mozilla.javascript.BaseFunction.execIdCall(Unknown Source)
at com.novell.soa.script.mozilla.javascript.IdFunctionObject.call(Unknown Source)
at com.novell.soa.script.mozilla.javascript.optimizer.OptRuntime.call2(Unknown Source)
at com.novell.soa.script.mozilla.javascript.gen.c9._c7(vnd.nds.stream://DEV-TREE/idm/system/Driver+Set/ECMA/SAS-token2#DirXML-Data:114)
at com.novell.soa.script.mozilla.javascript.gen.c9.call(vnd.nds.stream://DEV-TREE/idm/system/Driver+Set/ECMA/SAS-token2#DirXML-Data)
at com.novell.soa.script.mozilla.javascript.optimizer.OptRuntime.call2(Unknown Source)
at com.novell.soa.script.mozilla.javascript.gen.c9._c35(vnd.nds.stream://DEV-TREE/idm/system/Driver+Set/ECMA/SAS-token2#DirXML-Data:279)
at com.novell.soa.script.mozilla.javascript.gen.c9.call(vnd.nds.stream://DEV-TREE/idm/system/Driver+Set/ECMA/SAS-token2#DirXML-Data)
at com.novell.soa.script.mozilla.javascript.optimizer.OptRuntime.call2(Unknown Source)
at com.novell.soa.script.mozilla.javascript.gen.c9._c1(vnd.nds.stream://DEV-TREE/idm/system/Driver+Set/ECMA/SAS-token2#DirXML-Data:12)
at com.novell.soa.script.mozilla.javascript.gen.c9.call(vnd.nds.stream://DEV-TREE/idm/system/Driver+Set/ECMA/SAS-token2#DirXML-Data)
at com.novell.soa.script.mozilla.javascript.ScriptRuntime.doTopCall(Unknown Source)
at com.novell.soa.script.mozilla.javascript.gen.c9.call(vnd.nds.stream://DEV-TREE/idm/system/Driver+Set/ECMA/SAS-token2#DirXML-Data)
at com.novell.xsl.extensions.ECMAScriptFunctionImpl.invoke(ECMAScriptFunctionImpl.java:108)
at com.novell.xml.xpath.FunctionCall.evaluate(FunctionCall.java:90)
at com.novell.xml.dom.DOMEvaluator.evaluate(DOMEvaluator.java:89)
at com.novell.nds.dirxml.engine.rules.RuleDynamicContext.evaluateXPath(RuleDynamicContext.java:165)
at com.novell.nds.dirxml.engine.rules.TokenXPath.expand(TokenXPath.java:72)
at com.novell.nds.dirxml.engine.rules.Arg.evaluate(Arg.java:460)
at com.novell.nds.dirxml.engine.rules.DoSetLocalVariable.apply(DoSetLocalVariable.java:100)
at com.novell.nds.dirxml.engine.rules.ActionSet.apply(ActionSet.java:180)
at com.novell.nds.dirxml.engine.rules.DirXMLScriptProcessor.applyRules(DirXMLScriptProcessor.java:307)
at com.novell.nds.dirxml.engine.Subscriber.processEvents(Subscriber.java:902)
at com.novell.nds.dirxml.engine.Driver.submitTransaction(Driver.java:628)
at com.novell.nds.dirxml.engine.DriverEntry.submitTransaction(DriverEntry.java:1065)
at com.novell.nds.dirxml.engine.DriverEntry.processCachedTransaction(DriverEntry.java:949)
at com.novell.nds.dirxml.engine.DriverEntry.eventLoop(DriverEntry.java:771)
at com.novell.nds.dirxml.engine.DriverEntry.run(DriverEntry.java:561)
at java.lang.Thread.run(Unknown Source)

--------------------


I have uploaded the ECMA-script to pastebin:
http://pastebin.com/V7TG3t44

Any help is greatly appriciated!

Best Regards
Marcus


--
marcus_jonsson
------------------------------------------------------------------------
marcus_jonsson's Profile: https://forums.netiq.com/member.php?userid=1157
View this thread: https://forums.netiq.com/showthread.php?t=54258

Labels (1)
0 Likes
5 Replies
Knowledge Partner
Knowledge Partner

Re: ECMA script error

marcus jonsson wrote:

> vnd.nds.stream://DEV-TREE/idm/system/Driver+Set/ECMA/SAS-token2#DirXML-Data:152


The error occurs at this line in your script.

As for the actual reason, not so clear.


What is the patch version of the engine vs Designer?
Alex McHugh - Knowledge Partner - Stavanger, Norway
Who are the Knowledge Partners
If you appreciate my comments, please click the Like button.
If I have resolved your issue, please click the Accept as Solution button.
0 Likes
Knowledge Partner
Knowledge Partner

Re: ECMA script error

On 9/9/2015 8:39 AM, Alex McHugh wrote:
> marcus jonsson wrote:
>
>> vnd.nds.stream://DEV-TREE/idm/system/Driver+Set/ECMA/SAS-token2#DirXML-Data:152

>
> The error occurs at this line in your script.
>
> As for the actual reason, not so clear.
>
>
> What is the patch version of the engine vs Designer?


Some JAR that Designer has and engine does not, is relied on, you have a
Class Not Found error.

0 Likes
Knowledge Partner
Knowledge Partner

Re: ECMA script error

Geoffrey Carman <geoffreycarmanNOSPAM@NOSPAMgmail.com> wrote:

> Some JAR that Designer has and engine does not, is relied on, you have a
> Class Not Found error.


I think it is that there needs to be a bit more explicit type conversion.
The optimizer is choking due to this.

--
If you find this post helpful and are logged into the web interface, show
your appreciation and click on the star below...
Alex McHugh - Knowledge Partner - Stavanger, Norway
Who are the Knowledge Partners
If you appreciate my comments, please click the Like button.
If I have resolved your issue, please click the Accept as Solution button.
0 Likes
Marcus Tornberg Super Contributor.
Super Contributor.

Re: ECMA script error


Hi.

Thanks for your replies.

I'm sure you are on the right track. However I got help from a colleague
in rewriting the script, and now I have a version that works with the
engine. I won’t pursue cause of the original problem I posted.

I will share the code used now in case anyone else needs to generate a
SAS token for Service Bus for Windows (I think the same applies to Azure
as well).


Code:
--------------------
importPackage(java.io);
importPackage(java.net);
importPackage(java.util);
importPackage(java.math);
importPackage(Packages.javax.crypto.spec);
importPackage(Packages.javax.crypto);
importPackage(Packages.javax.xml.bind);
importPackage(Packages.org.w3c.dom);
importClass(Packages.com.novell.xml.util.Base64Codec);
importClass(Packages.com.novell.nds.dirxml.driver.XmlDocument);
importClass(Packages.com.novell.xml.dom.DOMWriter);
importClass(java.util.GregorianCalendar);
importClass(java.text.DateFormat);
importClass(java.text.SimpleDateFormat);
importClass(java.lang.System);
importClass(java.lang.Integer);

function getToken2(resourceUri, keyName, key) {

var token = "";

try {

var targetUri = URLEncoder.encode(resourceUri.toLowerCase(), "UTF-8").toLowerCase();
var currentTime = new Date();
var expiration = Math.round((currentTime.getTime() + 300000) / 1000);
var expAsString = new java.lang.String(expiration);
var signature = targetUri + "\n" + expiration;
var keyJ = new java.lang.String(key);
var signingKey = new SecretKeySpec(keyJ.getBytes(), "HmacSHA256");
var mac = Mac.getInstance("HmacSHA256");

mac.init(signingKey);

var signatureJ = new java.lang.String(signature);
var rawHmac = mac.doFinal(signatureJ.getBytes());
var hmac = URLEncoder.encode(DatatypeConverter.printBase64Binary(rawHmac), "UTF-8");
var array = java.lang.reflect.Array.newInstance(java.lang.Object, 4);

array[0] = targetUri;
array[1] = hmac;
array[2] = expAsString;
array[3] = keyName;

var sasToken = java.lang.String.format("SharedAccessSignature sr=%s&sig=%s&se=%s&skn=%s", array);

token = sasToken;

} catch (e) {

token = "Token generation failed: " + e + "\n";
if (e.javaException != null) {
var stacktrace = e.javaException.getStackTrace();
for (var i = 0; i < stacktrace.length; i++) {
token = token + stacktrace + "\n";

}
}

}

return token;

}
--------------------


--
marcus_jonsson
------------------------------------------------------------------------
marcus_jonsson's Profile: https://forums.netiq.com/member.php?userid=1157
View this thread: https://forums.netiq.com/showthread.php?t=54258

0 Likes
Knowledge Partner
Knowledge Partner

Re: ECMA script error

marcus jonsson wrote:

>
> Hi.
>
> Thanks for your replies.
>
> I'm sure you are on the right track. However I got help from a colleague
> in rewriting the script, and now I have a version that works with the
> engine. I won�t pursue cause of the original problem I posted.
>
> I will share the code used now in case anyone else needs to generate a
> SAS token for Service Bus for Windows (I think the same applies to Azure
> as well).



Thanks for sharing. This version seems far shorter and simpler than the other one.
Alex McHugh - Knowledge Partner - Stavanger, Norway
Who are the Knowledge Partners
If you appreciate my comments, please click the Like button.
If I have resolved your issue, please click the Accept as Solution button.
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.