Dataweave Modules
What are they?
DataWeave modules provide reusable functions that aren't necessarily core to the runtime, like isAlpha
, takeWhile
, countBy
, etc. DataWeave provides a large number of built in libraries to help with common tasks which we can import; for example, if you wanted to create a signed JWT for authentication, you would leverage the Binaries and Crypto modules:
So DataWeave provides a lot of out-of-the-box modules we can leverage for common tasks.
Like the code above, we frequently run into situtations where a reusable function would be extremely useful.. but there isn't one! So what can we do? Well, just like the vast majority of languages, we can create and publish our own reusable modules; once published, we can include it as a maven depedency just like anything else. Reusable modules provide benefits beyond just standardization and reuse - we can actually put these modules into their own CI/CD pipelines, which allows us to introduce new, reusable features that are properly tested and documented!
Let's Build a JWT Module!
A large number of APIs require that we generate a signed JWT in order to authenticate. MuleSoft does not currently have a built in function/connector for this process, so we'll leverage functionality that already exists within DataWeave to build our own reusable library.
Development Environment Setup
DataWeave modules are still Maven projects, so we can develop them in Anypoint Studio; however, using IntelliJ provides an extended tool set which makes for an improved developer experience.
- Install IntelliJ: If you don't already have IntelliJ installed, visit https://www.jetbrains.com/idea/ and download the Community edition.
- Add the DataWeave plugin, which provides tooling around DataWeave project types, line-by-line debugging, and more.
Create the DataWeave Project
- In IntelliJ, click Create Project
- Select the type “Weave Project”
- Set groupId to
com.mulesoft.codelabs
- Set artifactId to
jwt-dw-module
- Leave the version as
1.0.0-SNAPSHOT
- On the next screen, leave all of the defaults
- Set the project name to
jwt-dw-module
and choose a location to save the files
- Create the project
Examining the Project Structure
Once the project has been created, you'll notice that a number of folders are present by default:
This is where we will setup our code, as well as any unit testing we wish to include. The project structure is dictated by the configuration in the POM file:
This entry indicates that, when building the project, we should look for DataWeave files in src/main/dw
. Your folder structure here dictates what the import looks like; eg: creating a subfolder src/main/dw/jwt
with a file called RSA.dwl will make our import: import * from dw::jwt::RSA
.
When adding DataWeave files in a Mule Application, we typically place them in the default src/main/resources
directory; however, for the purposes of a DataWeave module best practice suggests using src/main/dw
.
These entries dictate where the build plugin should check for unit tests that need to be run.
Populate the Code
Now that we have the project scaffolded, let's add our code.
Common.dwl: Functions shared by both the RSA and HMAC functions
- In
src/main/dw
, create a folder called jwt. - In
src/main/dw/jwt
, create a Dataweave Component (module) called Common.dwl
- Paste the following code:
HMAC.dwl: Functions used for creating HMAC-SHA signed JSON Web Tokens
- In
src/main/dw/jwt
, create a Dataweave Component (module) called HMAC.dwl
- Paste the following code:
RSAHelper.java: Provides functionality not available in DataWeave via Java necessary for RSA signing
When creating DataWeave modules, we can even include and leverage custom Java code, or java dependencies! DataWeave does not currently have built in support for RSA signing, so we will create a simple helper java file to handle this for us.
- In
src/main/java
, create Java class RSAHelper.java
- Paste the following code:
RSA.dwl: Functions used for creating RSA signed JSON Web Tokens
- In
src/main/dw/jwt
, create a Dataweave Component (module) RSA.dwl
- Paste the following code:
Seeing an error about the import java!RSAHelper
? This is normal; unfortunately the DataWeave plugin isn't aware of changes made to Java code. While you can test and develop Java code like normal, you will need to reload the project after making changes before the DataWeave "linter" becomes aware. You may also have to build and install (mvn install) the project first.
Setup a simple mapping test:
Next, in order to have an entry point for testing our code, lets setup a simple mapping.
- In
src/test/dwtest
, create a DataWeave Component (mapping) called HMAC_Testing.dwl
- At the bottom of IntelliJ, click Weave Preview to open the mapping preview. This shows us a live preview of the results of our dataweave!
- In order to simulate some incoming data, we'll add an input. This allows us to set a payload object with some JSON. Click the + button in the Inputs panel
- Set a sample payload which we will include in our token:
- And finally in the mapping DataWeave code, paste the following:
And there we go! We should now see a signed JWT being output in the preview pane. That tokens should look like:
Assuming that you used the same input payload that I did. We can take this token and key, and use
https://jwt.io in order to verify a valid token was created!
Now that we have our module wrapped up, lets go ahead and add a basic Unit Test and run the test lifecycle phase to see what how it looks.
Setup a Unit Test
- In
src/test/dwmit
, create a new folder called HMAC_Payload_Only
. This folder will contain a unit test against which we are testing calling HMAC::JWT
with only a payload and key. - In
src/test/dwmit/HMAC_Payload_Only
, create transform.dwl
and paste the following code:
- In
src/test/dwmit/HMAC_Payload_Only
, create out.json
and paste the following:
- Open terminal, and run
mvn test
; you should see your test show up as passing!
Now that you see the basic functionality, take some time to play around with the test in order to see failures, or create additional tests!
Publish Module to Maven Repository
For this example, we will be using your Anypoint Exchange as a maven repository target.
Note: This is not currently officially supported, as DataWeave modules are not recognized by the Exchange Maven Facade. You are free to deploy with this method, but until the Facade recognizes DataWeave modules it will not be officially supported.
- Find your organization ID:
- Log into https://anypoint.mulesoft.com
- Make sure you are in the master business group.
- Navigate to Access Management
- Click on your master business group and copy your Organization Id
- In your DataWeave project, remove the line
dw-library
in POM.xml
- In your DataWeave project, update your
groupId
key to your Organization Id in POM.xml
- In your DataWeave project, update your
POM.xml
by adding a distributionManagement key
Note: In order to deploy, you will need a server entry with credentials for the repository id
exchange-repository
. For an example
settings.xml
file you can include in your maven install, check this file:
https://github.com/mikeacjones/dw-jwt-module/blob/master/.maven/settings.xml Strictly speaking, the only piece required in this file is the server entry for
exchange-repository
.
Now that we have a maven target setup, run the following commands in terminal:
mvn install
(this install into our local repo; if you can't get your credentials setup for the Exchange, this will allow us to continue with the next section)mvn deploy
(this will deploy to our exchange maven repo, assuming your credentials are setup)
Use our Module in a Mule Application
There you go! We can now easily include this module in our Mule application simply by adding a dependency to our project's POM.xml
file!
And now you can include it in your transform:
And now we have a reusable library separate from our Mule applications, with its own Unit Testing and versioning!