Custom Policy in Mule 4

Michael T

Last Update 4 года назад

Custom Policy is a Policy which we can add at runtime to extend the functionality of Mule application. We can apply these custom policies to mule application at a Runtime level in cloudhub.

The current workflow to get a working policy for Mule 4 that can be applied in Anypoint Platform consists of:

- Develop the policy.

- Package the policy.

- Upload the resulting policy assets to Exchange.

- Apply the policy to any API through API Manager.

Step 1: Develop the policy.

The first step to develop a custom policy consists in setting up a project with the required files.

The easiest way to gather all your required files is by using the maven archetype. This archetype creates all the necessary files for you. Then, use Maven to package your custom policy into a deployable JAR.

Update below details in Settings.xml to generate the required archetype for custom policy.

Once Maven is configured:

  1. Create a new directory where the custom policy project will live.
  2. Go to that directory in the command line.
  3. Execute the following command:

Replace:

  • ${orgId} with the Anypoint Platform Organization Id where the policy will be uploaded.
  • Get your organization ID from Access Management > Organization:
  • Click the name of your organization.
  • Copy the UUID from the browser address. For example, copy 2a4b93c3-7899-4ea7-9374-f787744d8784 from the URL.
  • ${policyName} with the desired name for the custom policy.


  1. Before finishing, Maven asks you to set up:
  2. policyDescription: A brief description of your policy.
  3. policyName: The identifier name of your policy.


my-custom-policy/

├── my-custom-policy.yaml

├── mule-artifact.json

├── pom.xml

└── src

 └── main

  └── mule

   └── template.xml

Import the code in Anypoint Studio workspace.

Click on template.xml file -> configuration XML.

This file will contain a Hello World application by default:

Change this XML to implement out use case.

Note: We will not be able to use Message Flow section for drag and drop. For Custom policies, We will have to edit configuration xml manually.

This Policy will call “http://get-group-info.us-e2.cloudhub.io/api/department” to check if the provided header has “Department = IT” in the request. If the Department is “IT”, The request will be sent to implementation URL.

Step 2. Packaging a Custom Policy.

From the command line in your project’s folder, run the install phase:

> mvn clean install

The packager then packages your application and creates the deployable JAR file into the target directory within your project’s folder.

It will also verify that all the necessary files were provided for the packaging, and that the information provided in the mule-artifact and in the policy yaml are valid.

Step 3: Deploying a Policy Created Using the Maven Archetype.

Run >mvn deploy to publish the policy to Exchange.

The custom policy is now available for you to apply to APIs that belong to the specified organization.

Step 4. Using Custom Policy in exchange

Create Proxy and add Custom Policy in the policy section as shown below:

Step 5: Testing the custom policy.

Add the custom policy in your proxy and test it using postman with department header.

This should redirect to implementation URL.

As a negative test, give any value other than IT in department or exclude the department from header section.

This should generate an error saying “Invaild Department”.

" I hope this was helpful !! " 

Was this article helpful?

2 out of 2 liked this article