Search code examples
muleesbmel

Setting values from DB table to output HTTP headers


I am new to mule ESB. I have a requirement to set HTTP headers where the values should be fetched from MySQLDB. I am able to fetch values from DB. DB returned multiple column values with one row.

I can able to set one column value in flow variable(flowVars) and that can be set in HTTP headers. But if i have to set multiple column variables in each HTTP headers will leads me to write multiple set variable command.

How can I avoid to write multiple set variable command ? (Is there any mule expresion to set multiple variable by single command ?) Is there any other simple way to achieve this ?

<flow name="mule_eeFlow">
    <http:listener config-ref="HTTP_Input_eba_Listener_Configuration" path="/XXX/additem" doc:name="HTTP"/>
    <db:select config-ref="MySQL_Configuration" doc:name="Database">
        <db:template-query-ref name="Template_Query"/>
    </db:select>
    <set-variable variableName="LEVEL" value="#[message.payload[0].'X-API-COMPATIBILITY-LEVEL']" doc:name="Variable"/>
    <set-variable variableName="DEVNAME" value="#[message.payload[0].'X-API-DEV-NAME']" doc:name="Variable"/>
    <set-variable variableName="APPNAME" value="#[message.payload[0].'X-API-APP-NAME']" doc:name="Variable"/>
    <set-variable variableName="CERTNAME" value="#[message.payload[0].'X-API-CERT-NAME']" doc:name="Variable"/>
    <set-variable variableName="SITEID" value="#[message.payload[0].'X-API-SITEID']" doc:name="Variable"/>
    <set-variable variableName="CALLNAME" value="#[message.payload[0].'X-API-CALL-NAME']" doc:name="Variable"/>

    <custom-transformer class="AddingHTTPHeader" doc:name="Java"/>
</flow>

My Java code look like

@Override
public Object transformMessage(MuleMessage message, String outputEncoding)
        throws TransformerException {
    // TODO Auto-generated method stub
    message.setOutboundProperty("X-API-COMPATIBILITY-LEVEL", message.getInvocationProperty("LEVEL"));
    message.setOutboundProperty("X-API-DEV-NAME", message.getInvocationProperty("DEVNAME"));
    message.setOutboundProperty("X-API-APP-NAME", message.getInvocationProperty("APPNAME"));
    message.setOutboundProperty("X-API-CERT-NAME", message.getInvocationProperty("CERTNAME"));
    message.setOutboundProperty("X-API-SITEID", message.getInvocationProperty("SITEID"));
    message.setOutboundProperty("X-API-CALL-NAME", message.getInvocationProperty("CALLNAME"));

    return null;
}

Solution

  • You can set outbound properties directly in Mule without the use of variables and Java code, there are multiple ways of doing this.

    With the 'Set Property' transformer

    https://docs.mulesoft.com/mule-fundamentals/v/3.7/message-state#setting-a-property-on-a-message

    https://docs.mulesoft.com/mule-user-guide/v/3.7/property-transformer-reference

    Or if you use EE with DataWeave, you can set all your property in DataWeave and create them with only one DataWeave transformer instead of adding separate property transformers.

    https://docs.mulesoft.com/mule-user-guide/v/3.7/using-dataweave-in-studio#handling-multiple-outputs