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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • MuleSoft OAuth 2.0 Provider: Password Grant Type
  • Solving Parallel Writing Issues in MuleSoft With Distributed Locking
  • Optimizing MuleSoft Performance With HikariCP: A Complete Guide
  • Generic and Dynamic API: MuleSoft

Trending

  • Memory Leak Due to Time-Taking finalize() Method
  • The Ultimate Guide to Code Formatting: Prettier vs ESLint vs Biome
  • Navigating Double and Triple Extortion Tactics
  • Simplifying Multi-LLM Integration With KubeMQ
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Creating Custom Policies in MuleSoft

Creating Custom Policies in MuleSoft

In this tutorial, learn how to create custom policies in Mule, allowing you to define the behavior of your API for each incoming request.

By 
Kapil Arora user avatar
Kapil Arora
·
Oct. 06, 17 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
14.5K Views

Join the DZone community and get the full member experience.

Join For Free

MuleSoft provides certain built-in policies which can be referred to address general situations to filter unwanted traffic coming to your API. However, MuleSoft provides the capability to create custom policies, which are designed primarily to address complex scenarios like SQL injection. Custom policies are configuration files that define the behavior of your API for each incoming request.

To create custom policies, mainly two configuration files are required 

  • Mulesoft Definition file (YAML) - File in which you define characteristics of the custom policy. Characteristics are defined using:

    • :name - used to describe policy in API platform

    • :description - detailed description of policy

    • :category - text used in API platform to list policies

    • :standalone - true if policy can work independently, false if it can only be applied as a                                             sub-part of another.

    • :configuration - defines custom configuration parameters for your policy.

  • MuleSoft Configuration file(XML) - File in which you define the actual logic/implementation of your defined custom policy. There are two main tags:

    • <before> - Code written within the <before> tag executes on every incoming request BEFORE sending it to main API services.

    • <after> - The after tag gets executed after completion of main API services, i.e after completion of the request.  

Generally, it happens that we don't have anything to do once the API request is completed. The <after> tag will not contain anything, but we can't have <before> and <after> tags left empty. In such scenarios, simply remove the declaration of the tag to avoid errors.

Similarly, for MuleSoft 3.8+, you can create an API custom policy project in Anypoint Studio. It will automatically have two files with extensions .xml and .yaml.

To create, navigate to File > New > API custom policy Project(Beta)

Image title

A project can have multiple xml and yaml combinations, but you can apply only one pair at a time, which you can set in mule-policy.xml.

Image title

Let's create a simple custom policy that can be used for logging incoming requests. We will start by creating a YAML configuration file.

Image title

Once the configuration is done, make the necessary changes in XML as well. Within the before tag, we will print the incoming payload.

Image title

Now we can apply the custom policy in the API manager using these two files.

MuleSoft

Opinions expressed by DZone contributors are their own.

Related

  • MuleSoft OAuth 2.0 Provider: Password Grant Type
  • Solving Parallel Writing Issues in MuleSoft With Distributed Locking
  • Optimizing MuleSoft Performance With HikariCP: A Complete Guide
  • Generic and Dynamic API: MuleSoft

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!