Implementing Custom Policies With MuleSoft
In this article, see how to implement custom policies with MuleSoft.
Join the DZone community and get the full member experience.Join For Free
MuleSoft provides out-of-box policies that can be applied to APIs for enhancing the functionalities of the APIs. These policies include various categories like security, compliance, troubleshooting, transformation, QoS, custom, etc.
MuleSoft also provides the capabilities to implement custom policies and Custom Policies are policies that anyone can develop and apply to their APIs, with the intention of extending existing functionality or defining new ones.
We need to perform a few steps for implementing the Custom policies.
- Develop the policy
- Package the policy
- Upload the resulting policy assets to Exchange
- Apply the policy to any API through API Manager
Setting Up a Project With the Archetype
The first step to develop a custom policy consists of setting up a project with the required files. The easiest way to gather all your required files is by using the maven archetype. One way to do so is by configuring Maven’s settings.xml with the following section:
Setting up a Project With the Archetype
Once maven profile has been setup, you can execute the following command:
These are the four files needed for having a working policy.
|mule-artifact.json||Exists for the mule-maven-plugin. This is the same file you need for Mule applications.|
|my-custom-policy.yaml||Renders the policy configuration UI. If this file is not provided, the policy won’t be able to be applied through API Platform’s UI.|
|template.xml||The actual logic of the policy and Mule configuration that defines the policy behavior.|
Here is the list of the videos explaining how to create custom policies and applied to API
Anypoint Custom Policies - Part I | MuleSoft
Anypoint Custom Policies - Part II | MuleSoft
Anypoint Custom Policies - Part III | MuleSoft
HTTP Policy Transform Extension
HTTP Policy Transform Extension is maven plugin and very useful when it comes to modifying the request and response that go through different policies.
Various list of the operation perform by HTTP Policy Transform Extension.
- Add and Remove request header or list of request headers.
- Add and Remove response header or list of response headers.
- Modify the request.
- Modify the response.
One of the use case where we can use this extension when we want encrypt or decrypt any field in request or response.
Note: To use this extension, you need to have MuleSoft enterprise nexus repository credentials.
Now you know how to implement Custom Policies with MuleSoft.
Opinions expressed by DZone contributors are their own.