DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Designing a Java Connector for Software Integrations
  • Vibe Coding With GitHub Copilot: Optimizing API Performance in Fintech Microservices
  • Revolutionizing Financial Monitoring: Building a Team Dashboard With OpenObserve
  • Unlocking the Benefits of a Private API in AWS API Gateway

Trending

  • Vibe Coding With GitHub Copilot: Optimizing API Performance in Fintech Microservices
  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  • Event-Driven Architectures: Designing Scalable and Resilient Cloud Solutions
  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation
  1. DZone
  2. Data Engineering
  3. Databases
  4. Working With Custom Policy in Mule 4

Working With Custom Policy in Mule 4

Let's work with custom policy in Mule 4.

By 
Rakesh Kumar Jha user avatar
Rakesh Kumar Jha
·
Aug. 16, 19 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
30.1K Views

Join the DZone community and get the full member experience.

Join For Free

MuleSoft Custom Policies can be layered on top of the implementation of the APIs to provide the governance, security, and visibility required. While the out-of-the-box API management policies cover the majority of use cases, an organization may need to create a custom policy to meet specific business needs.

There is a lot of documentation that shows how to work with MuleSoft custom policies through API manager, however, developing the custom policy and debugging them locally has been always challenging. In this article, I will demonstrate how to develop and test Mule custom policy on your local standalone Mule runtime instances before uploading to the API manager or Exchange for live running APIs.

Developing Mule Custom Policy

The first step to develop a custom policy is setting up a project with required files using a Maven archetype. The Maven archetype is not currently available in a Maven Central Repository, so we need to configure our Maven settings to find the archetype in one of MuleSoft’s repositories.

One way to do so is by configuring Maven’s settings.xml with the following section:

<profiles>
   <profile>
     <id>archetype-repository</id>
     <repositories>
       <repository>
         <id>archetype</id>
         <name>Mule Repository</name>
         <url>https://repository-master.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>

Once Maven is configured, Create a directory where the custom policy project will reside and run the below command from there by specifying Mule OrgId, custom policy name, and later policy description to create a custom policy project with all the required files, as shown below.

mvn -Parchetype-repository archetype:generate -DarchetypeGroupId=org.mule.tools -DarchetypeArtifactId=api-gateway-custom-policy-archetype -DarchetypeVersion=1.1.0 -DgroupId=${orgId} -DartifactId=${policyName} -Dversion=1.0.0-SNAPSHOT -Dpackage=mule-policy

Image title

In the YAML file, we will define all the policy configuration parameters that will appear in the UI, and the template.xml is where the actual policy logic will go. The best way to develop the template is to create a corresponding Mule flow in the studio and later convert them into the corresponding Mule custom policy codes. 

Packaging a Custom Policy

Once the template.xml and YAML file for the policy is ready, we need to package the custom policy to create a JAR file using command "mvn clean install". In the target folder of the policy, we will have a ready JAR package for the custom policy, as shown below.

Image title

Applying and Testing Custom Policy to the API on Mule 4 Standalone Runtime

Follow the below steps to deploy the Mule API and custom policy created above on standalone Mule runtime for testing.

1. Deploy a Mule API with "autodiscovery" on the Mule standalone runtime by placing the API jar file in the "apps" folder of the runtime. 

2. In the runtime folder, "policies", create a folder "offline-policies" (%MULE_HOME%/policies/offline-policies) and create a JSON file that defines the policy configurations as shown below. The name of the JSON file should be exactly the same as the policyID.
Image title

In the JSON file, we will define the "autodiscovery", which has been given in the API and the rest of the custom policy configuration parameters as below.

{
 "template" : {
   "groupId" : "com.mulesoft.anypoint",
   "assetId" : "Logging-Policy_V1",
   "version" : "1.0.0-SNAPSHOT"
 },
 "api": [
   {
     "id": "123"
   }
 ],
 "order": 1,
 "configuration" : {
   "payload" : "payload",
   "logging_req": true   

 }
}

3. In the runtime folder, "policies", create another folder, "policy-templates" ("%MULE_HOME%/policies/policy-templates) and place the Mule custom policy JAR file created after packaging the custom policy. Please refer to the screenshot below for reference.

Image title

Now that everything is in place, let's start the Mule runtime using command "mule -M-Danypoint.platform.gatekeeper=disabled" from the server bin folder. In the server logs, we can see that the custom policy gets applied to the API. Now we can make a request to the API and test the logic, which we have written in our custom policy. For debugging purposes, we can place logger statements in the policy template.xml and see them in the server logs after making requests to the API to which this policy is applied.

Uploading Custom Policy to Exchange

Once the testing completes, we will now publish this policy to exchange using "mvn clean deploy". The policy will then be available in the API manager custom policy section and will be ready to use for the live running APIs.

Let’s share our knowledge to expand our MuleSoft community.

Thank you! 

API

Opinions expressed by DZone contributors are their own.

Related

  • Designing a Java Connector for Software Integrations
  • Vibe Coding With GitHub Copilot: Optimizing API Performance in Fintech Microservices
  • Revolutionizing Financial Monitoring: Building a Team Dashboard With OpenObserve
  • Unlocking the Benefits of a Private API in AWS API Gateway

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!