Zum Hauptinhalt gehen

MD5-hash / calling a Java varargs method in a template

Kommentare

4 Kommentare

  • Zendesk API User
    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
  • Zendesk API User
    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
  • Zendesk API User
    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
  • Zendesk API User
    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.