With the introduction of Mule 4 beta, Mule has also introduced the next version of DataWeave which is 2.0, an enhancement from previous version 1.0.
In Mule 4, the Dataweave expression language will be the default expression language. With DataWeave script version 2.0, there will be few changes in the syntax as well as few enhanced feature that was not available in earlier versions.
Here we will discuss two main features available in Dataweave version 2.0, that will bring a new dimension in Mule application.
Let’s see on such feature, which is to call a Java method in DataWeave component. In DataWeave 1.0, we need to define a global function in Mule XML with Java logic that can be called within DataWeave.
But with DataWeave 2.0, we can directly call a Java method defined in a Java class.
To call a java method from a Dataweave, let’s first create a Java class under a package called test under src/main/java as follows:
Here in this Java class, we can see, we can encrypt a string with the different algorithm of our choice like MD5, SHA-1, SHA-256.
So, we will be calling this Java method in our DataWeave, will pass 2 parameters, one is the String to be encrypted another will be the algorithm of the encryption as follows:
We can call the Java method in DataWeave code, by first importing the class and then referring to the method, as shown above. We will be passing both the parameters as HTTP query params.
Our Mule flow will be as follows:
Testing the code:
Once our application is deployed, we will call the application by hitting HTTP url where we will pass both the parameters of Java methods as the query params something as below:
So, we can see above, the String testString is encrypted with hashing algorithm MD5 we have passed into the url as query params.
If we change the algorithm as SHA-1 and pass it in the query param as follows:
Similarly, changing the value to SHA-256 will encrypt the input String with SHA-256 algorithm with different encrypted value:
So, as we can see above, we can directly call any Java method in Dataweave 2.0 easily within it’s code.
DataWeave Encryption function:
But before ending the topic, we will discuss another important feature of Dataweave 2.0, which is DataWeave inbuild Encryption function.
Yes, DataWeave has it’s own inbuilt encryption functions, which means all the above encryption features can be done by using DataWeave inbuilt functions.
The inbuilt encryption functions that will be described now are packaged in the Crypto module. The module is included with the Mule runtime, and in our DataWeave code, we need to import it to our DataWeave code by adding the line import dw::Crypto to our header.
So, now if we want to encrypt our input String, now we don’t need to call any complex Java method, instead, we can simply call the inbuild encrypt function in our DataWeave code as below:
So, now we can use this inbuild function above and encrypt our String with the MD5 algorithm:
Since this Crypto module contains encrypting functions, we will get the same encrypted value we got above with our MD5 algorithm defined in our Java class.
Similarly, we can use other algorithms like SHA-256, SHA-1 directly as an inbuild crypto function in of Dataweave code to encrypt our value.
Hope you like this article. You can explore more on Dataweave 2.0 features that are already available with Mule beta.