Forum Discussion

bostrick's avatar
bostrick
Contributor
7 years ago

Obtaining the sso_id as a string in a macro?

I'm new to the world of lithium programming... any help on the following would be appreciated.

I'm working on an api request which I want to parameterize with a user's sso_id.  

Not finding the sso_id on the Freemarker User context object, I'm resorting to the rest() context object to pull the info...

<#assign sso_id=rest("/authentication/sessions/current/user/sso_id").value />

This suffices for text context, where the following works...

         sso_id: ${sso_id}

But if I try to pass this into another function, bad things happen.  Any ideas on how to cast a extended node+sequence+hash+string (!!) as a string?

        <#assign req = req.parameter("username", sso_id) />

    FreeMarker template error (HTML_DEBUG mode; use RETHROW in production!)

No compatible overloaded variation was found; declared parameter types and argument value types mismatch.
The FTL type of the argument values were: string (wrapper: f.t.SimpleScalar), extended node+sequence+hash+string (org.apache.xerces.dom.ElementImpl wrapped into f.e.dom.ElementModel).
The Java type of the argument values were: String, org.apache.xerces.dom.ElementImpl.
The matching overload was searched among these members:
    lithium.coreapi.webui.template.models.HttpClientRequestTemplateModel.parameter(String, Iterable),
    lithium.coreapi.webui.template.models.HttpClientRequestTemplateModel.parameter(String, String)

----
FTL stack trace ("~" means nesting-related):
	- Failed at: #assign req = req.parameter("username...  [in template "ole.this_breaks.ftl" at line 14, column 1]

 

  • Found the solution...

    rest(...) is returning XML, which in the Freemarker template is represented by a custom node type.    while ${node_var} as a convenience will extract and render a single text node, you need to work on the node_var with some operators to actually extract the text as a string.  Here's my working solution....

    <#assign sso_id=rest("/authentication/sessions/current/user/sso_id").value.@@text/>
    <#assign req = req.parameter("username", sso_id) /> 

     

    Gory details here...

    https://freemarker.apache.org/docs/xgui.html

    --b