MD5-hash / calling a Java varargs method in a template
Author: choff
Publication Date: 3/27/2019 14:26
Hello everyone,
In one of our templates, we create an MD5-hash of a string and print it inside the template. Among other classes, we used javax.xml.bind.annotation.adapters.HexBinaryAdapter for this.
After updating to OpenJDK 11, javax.* is not available anymore, and our existing solution breaks. Therefore we are looking for an alternative solution.
We found that, in plain Java, we can solve our problem with something like this:
String.format("%032x", new BigInteger(1, MessageDigest.getInstance("MD5").digest(myString.getBytes())));
How can we use String.format() in a FirstSpirit template? If we write $CMS_VALUE(class("java.lang.String").format("hallo"))$ in a template, empty output and the warning
27.03.2019 14:53:05.675 WARN ($CMS_VALUE(class("java.lang.String").format("hallo"))$ at 30, 7): Replacing null value with empty default! Undefined method 'java.lang.String#format(java.lang.String)'!
is created.
Thanks a lot,
Christian
Tags: java11, string, template
-
Author: felix_reinhold - 3/28/2019 15:00
Hi Christian,
have you tried providing a second parameter to the format - method? It requires a string and an object[].
Best regards
Felix
0 -
Author: choff - 3/28/2019 15:15
Hi Felix,
$CMS_VALUE(class("java.lang.String").format("hallo", []))$ still gives a warning and empty output:
28.03.2019 16:09:54.963 WARN ($CMS_VALUE(class("java.lang.String").format("hallo", []))$ at 15, 16): Replacing null value with empty default! Undefined method 'java.lang.String#format(java.lang.String, java.util.ArrayList)'!
Best,
Christian
0 -
Author: felix_reinhold - 3/28/2019 15:54
Hi Christian,
using [] creates an ArrayList in FS - not an array.
You have to create an Array manually. I think I never needed to create an array in FS, so I don't know, if there's a way to use the "[]" Syntax.
You could use
$CMS_VALUE(class("java.lang.String").format("hallo", class("org.apache.commons.lang3.ArrayUtils").EMPTY_STRING_ARRAY))$
But your code would look like that:
$CMS_SET(args, [])$
$CMS_SET(messageDigest, class("java.security.MessageDigest").getInstance("MD5"))$
$CMS_SET(myString, "TEST")$
$CMS_SET(void, args.add(class("java.math.BigInteger").new(1, messageDigest.digest(myString.getBytes()))))$
$CMS_VALUE(class("java.lang.String").format("%032x", args.toArray()))$
Works but I would put it in a module or script, to avod all those class-usages...
Bets regards
Felix
0 -
Author: choff - 3/29/2019 11:07
Thanks to Felix' work I can answer my own question now as follows:
To call a Java method with varargs in a FirstSpirit template, we have to create an array of objects that represents the varargs. Example:
$CMS_VALUE(class("java.lang.String").format("hallo", [].toArray()))$Our final solution to generate the MD5 hash looks like this:
$CMS_SET(set_hashedBytes, class("java.security.MessageDigest")
.getInstance("MD5")
.digest(inputString.getBytes()))$
$CMS_SET(set_hashedInputString, class("java.lang.String")
.format("%032x", [class("java.math.BigInteger").new(1, set_hashedBytes)].toArray()))$
0
Bitte melden Sie sich an, um einen Kommentar zu hinterlassen.
Kommentare
4 Kommentare