I want to write a function to format Vaadin messages. These messages have the format
108|0ff1255e-e2be-4e7b-ac5c-1ff2709ce886[["0_11_12_13_login_username","v","v",["text",["s","Agent00232"]]]]
The first number is the length then there some kind of session id (later called vaadin_security_key) followed by the payload. (In this example I set the value "Agent00232" to the textfield with the connector id "0_11_12_13_login_username") I wrote a function like this:
def sendMessage(name: String, vaadinCommand: String) = {
def createMessage(session: Session) = {
val message = session("vaadin_security_key").as[String] + "\u001d" + vaadinCommand
val message2 = ELCompiler.compile(message)(classTag[String])(session).toString
val message3 = message2.substring(8, message2.length - 2)
val len = message3.length
val completeMessage = len.toString() + "|" + message3
completeMessage
}
exec(
ws(name)
.sendText(createMessage)
.check(wsAwait
.within(Vaadin.defaultTimeout)
.until(1)
.regex("""(.+)""")
.saveAs("lastResult")))
.exec { session =>
println(session("lastResult").as[String])
session
}
}
I'd like to use this method like this and use EL expressions in the string:
exec(Vaadin.sendMessage("setting Name", "[[\"0_11_12_13_login_username\",\"v\",\"v\",[\"text\",[\"s\",\"${username}\"]]]]"))
Therefore I have to evaluate the String before calculating the length. The method ELCompiler.compile()... returns a Expression[String]. The problem is that I need this string and concat it with the calculated length. When I do message2.toString it returns
Success(0ff1255e-e2be-4e7b-ac5c-1ff2709ce886[["0_11_12_13_login_username","v","v",["text",["s","Agent00232"]]]])
and therefor I have to use substring(8, message2.length - 2) to get the evaluated payload (with the security key) to calculate the length of it.
Is there a better (more elegant) way to extract the String out of the expression then the use of substring(...)
?