Using Freemarker for dynamic header in camunda connect

Hello,

I am trying to connect to an application that accepts basic authentication. I am able to authenticate and retrieve data without any issue when I provide

Authorization: Basic XXXXXXXXX

in the header as a mapped entity.

The problem is, the passwords are subject to change and we maintain them in an ini file. I have a script task that get the base64 encoded value from an ini and save it in an execution variable.

How can I use this variable in the header ?

I have tried freemarker. But that dosent seems to map

" org.camunda.bpm.engine.rest.exception.ExceptionHandler.toResponse java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map"

<bpmn:serviceTask id=“clone_vm” name=“Clone VM”>
bpmn:extensionElements
camunda:connector
camunda:inputOutput
<camunda:inputParameter name=“url”>https://192.168.1.201/post/cloneVm</camunda:inputParameter>
<camunda:inputParameter name=“method”>POST</camunda:inputParameter>
<camunda:inputParameter name=“headers”>
<camunda:script scriptFormat=“freemarker” resource=“header.ftl” />
</camunda:inputParameter>

The template file(header.ftl) looks like

Content-Type: application/json
accept: application/json
Authorization: Basic ${da_user}

The ${da_user} is an execution variable set using the below script

static void main(String[] args) {
    def sout = new StringBuilder(), serr = new StringBuilder()
   
    def proc = 'bash /etc/gasf/drupal-admin.sh'.execute()
    proc.consumeProcessOutput(sout, serr)
    proc.waitForOrKill(1000)
    execution.setVariable("da_user", sout)
   }

Any assistance would be much appreciated.

I have found the best way is to import your ini file/password as a input-variable/input-mapping on the specific task, create the Basic f9w8fw9e8fuwe98fuwe98... string, and then create a local variable with that string. Then in your Auth header you do ${myBasicString}

same concept was applied here: Send SMS through Service Task (http-connector) and Twilio (Script Based)

The reason for the input mapping is so you dont pollute the global scope of variables with your specific var. Each time you need the password it would be retrieved by the input mapping of the specific task using the password.

Edit: @Rajiv_Cj if you need more flexibility on your http-connector usage, see: Replacing Http-Connector with Jsoup usage as a alternative.

2 Likes

Thank you @StephenOTT for showing me alternatives. I will certainly think about using Jsoup in the future.

In the above scenario, there was a “/n” character in the output of the script that read the ini file. So I called replaceAll function to remove the extra /n and it worked :slight_smile:

The issues in encoding the script was with a new line character in the variable.

static void main(String args) {
def sout = new StringBuilder(), serr = new StringBuilder()

def proc = 'bash /etc/gasf/drupal-admin.sh'.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(1000)
sout = sout.replaceAll("[\r\n]+","");
execution.setVariable("da_user", sout)

}