Sharing ideas on property and environment vars in Mule

In this post we share an approach for accessing your property values and environment variables when performing dataweave transformations in Mule. In the example below, we create two transforms. In the first transform we’ll extract some properties and environment variables and save them in a HashMap for later use in the second transform.

In the second transform we’ll access some HashMap values and for fun, render them as a urlencoded string.

In the dataweave transform below, we’re redirecting the output to a variable called AUTH_MAP as a Java HashMap. The fields are just some typical contrived authentication parameters derived from a properties file.

The syntax for the assignments of grant_type and username makes use of Ant style property placeholders, you may notice some errors in the dataweave editor, but this actually works. Save, click out of the editor and then back in to see if errors resolve. Although this seems to work, there’s a better approach for working with properties in dataweave. The format for deriving properties in dataweave as shown using the example for grantProp.

%dw 2.0
import dw::System
output application/java
---
{
	grant_type: '${myprop.ws.grant_type}',
	username: '${myprop.ws.username}',
	grantProp: p('myprop.ws.grant_type'),
	grantEnv: dw::System::envVar('SHELL'),
	allVars: dw::System::envVars()
}

In the last two examples we show how you would access a single environment variable using the grantEnv example and a dictionary containing all environment variables in the allVars example.

%dw 2.0
output application/x-www-form-urlencoded 
---
{
    grant_type: vars.AUTH_MAP.grant_type, 
    username: vars.AUTH_MAP.username
}

In our second transform we generate a urlencoded string that typically would be sent using an HTTPRequestor as part of an authentication.

Example of URL encoding:

grant_type=1234&username=coyote%40acme.com

I hope you enjoyed this brief but hopefully succinct article on accessing properties and environment variables in dataweave. If you’re curious about how the property file gets read in see the brief note which follows.

src/main/resources/dev-properties.yaml

myprop:
  ws: 
    grant_type: "1234"
    username: "coyote@acme.com"

Reference your dev-properties.yaml file in your properties configuration using Ant style property placeholder like so:

In Anypoint Studio you would define your property as a runtime parameter like so:

-D env=dev

This approach allows for different property setting files for your different environments.

Mitch enjoys tinkering with tech across a wide range of disciplines. He loves learning new things and sharing his interests. His work interests run the gamut of: application integration, scalable secure clusters, embedded systems, and user interfaces. After hours you might find him dabbling in the hobby space with Raspberry Pi's, drones, photography, home wine making and other ferments.

Published by Mitch Dresdner

Mitch enjoys tinkering with tech across a wide range of disciplines. He loves learning new things and sharing his interests. His work interests run the gamut of: application integration, scalable secure clusters, embedded systems, and user interfaces. After hours you might find him dabbling in the hobby space with Raspberry Pi's, drones, photography, home wine making and other ferments.

Leave a comment

You can comment using your social media account

%d bloggers like this: