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 FreeIntroduction
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:
x
<profiles>
<profile>
<id>archetype-repository</id>
<repositories>
<repository>
<id>archetype</id>
<name>Mule Repository</name>
<url>https://repositorymaster.mulesoft.org/nexus/content/repositories/public</url>
<releases>
<enabled>true</enabled>
<checksumPolicy>fail</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
Setting up a Project With the Archetype
Once maven profile has been setup, you can execute the following command:
xxxxxxxxxx
mvn -Parchetype-repository archetype:generate \ -DarchetypeGroupId=org.mule.tools \ -DarchetypeArtifactId=apigateway-custom-policy-archetype \ -DarchetypeVersion=1.2.0 \ -DgroupId=${orgId} \ -DartifactId=${policyName} \ -
Dversion=1.0.0-SNAPSHOT \ -Dpackage=mule-policy
These are the four files needed for having a working policy.
File | Description |
pom.xml |
|
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.
xxxxxxxxxx
<dependency>
<groupId>com.mulesoft.anypoint</groupId>
<artifactId>mule-http-policy-transform-extension</artifactId>
<version>${httpPolicyTransformVersion}</version>
<classifier>mule-plugin</classifier>
<exclusions>
<exclusion>
<groupId>org.mule.connectors</groupId>
<artifactId>mule-http-connector</artifactId>
</exclusion>
</exclusions>
</dependency>
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.
Code: https://github.com/Jitendra85/MuleSoft-Custom-Policies.git
Now you know how to implement Custom Policies with MuleSoft.
Opinions expressed by DZone contributors are their own.
Comments