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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • MuleSoft MCP and A2A in Production: What 17 Recipes Reveal
  • MuleSoft IDP: Enhancing Efficiency and Accuracy in Data Extraction
  • From AI Chaos to Control: Building Enterprise-Grade LLM Gateways With MuleSoft Anypoint
  • Revolutionizing Scaled Agile Frameworks with AI, MuleSoft, and AWS: An Insider’s Perspective

Trending

  • Stop Choosing Sides: An Engineering Leader's Framework for Build, Buy, and Hybrid AI Agents in 2026
  • How to Parse Large XML Files in PHP Without Running Out of Memory
  • Build a GitHub Slack Bot With AWS Bedrock and MCP, Part 1
  • Compliance Automated Standard Solution (COMPASS), Part 11: Compliance as Code, the OSCAL MCP Server Way
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Implementing Custom Policies With MuleSoft

Implementing Custom Policies With MuleSoft

In this article, see how to implement custom policies with MuleSoft.

By 
Jitendra Bafna user avatar
Jitendra Bafna
·
Dec. 19, 20 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
17.4K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

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: 

XML
 




x
21


 
1
<?xml version="1.0" encoding="UTF-8"?>
2
<profiles>
3
   <profile>
4
      <id>archetype-repository</id>
5
      <repositories>
6
         <repository>
7
            <id>archetype</id>
8
            <name>Mule Repository</name>
9
            <url>https://repositorymaster.mulesoft.org/nexus/content/repositories/public</url>
10
            <releases>
11
               <enabled>true</enabled>
12
               <checksumPolicy>fail</checksumPolicy>
13
            </releases>
14
            <snapshots>
15
               <enabled>true</enabled>
16
               <checksumPolicy>warn</checksumPolicy>
17
            </snapshots>
18
         </repository>
19
      </repositories>
20
   </profile>
21
</profiles>



Setting up a Project With the Archetype

Once maven profile has been setup, you can execute the following command:

Plain Text
 




xxxxxxxxxx
1


 
1
mvn -Parchetype-repository archetype:generate \ -DarchetypeGroupId=org.mule.tools \ -DarchetypeArtifactId=apigateway-custom-policy-archetype \ -DarchetypeVersion=1.2.0 \ -DgroupId=${orgId} \ -DartifactId=${policyName} \ -
2
Dversion=1.0.0-SNAPSHOT \ -Dpackage=mule-policy



These are the four files needed for having a working policy.

File Description
pom.xml
  • groupId is defined as the organization ID used with the archetype. This value must remain as it is. 
  • mule-policy packaging, so packager plugin can successfully build the JAR.
  • distributionManagement section is defined pointing to user’s Exchange.
  • mule-maven-plugin responsible of packaging the policy into a deployable jar
  • maven-deploy-plugin configured to deploy both the resulting jar and the YAML when uploading the policy to Exchange
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.

XML
 




xxxxxxxxxx
1
12


 
1
<dependency>
2
      <groupId>com.mulesoft.anypoint</groupId>
3
      <artifactId>mule-http-policy-transform-extension</artifactId>
4
      <version>${httpPolicyTransformVersion}</version>
5
      <classifier>mule-plugin</classifier>
6
      <exclusions>
7
          <exclusion>
8
              <groupId>org.mule.connectors</groupId>
9
              <artifactId>mule-http-connector</artifactId>
10
          </exclusion>
11
      </exclusions>
12
  </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.

MuleSoft

Opinions expressed by DZone contributors are their own.

Related

  • MuleSoft MCP and A2A in Production: What 17 Recipes Reveal
  • MuleSoft IDP: Enhancing Efficiency and Accuracy in Data Extraction
  • From AI Chaos to Control: Building Enterprise-Grade LLM Gateways With MuleSoft Anypoint
  • Revolutionizing Scaled Agile Frameworks with AI, MuleSoft, and AWS: An Insider’s Perspective

Partner Resources

×

Comments

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

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook