Custom Policy in Mule 3
Apart from automated policies in API manager, we can also apply our custom policies to the APIs.
For creating a custom policy we need to make a project in Anypoint studio.
Go to:
File - New - API Custom policy Project
In the above project structure we have two important files:
1. custom_policy.xml
2. custom_policy.yaml
These two files we need to import into Anypoint platform in the custom policy section.
1. custom_policy.yaml:
In defaultValue, you can give custom expressions to define custom policy.
Here i have given a simple MEL expression that if query parameter id==1 then output gives error.
2. custom_policy.xml:
In custom_policy.xml, we have two important tags:
<before> and <after>
1. <before> - Code written within the <before> tag executes on every incoming request BEFORE sending it to main API services.
2. <after> - The after tag gets executed after completion of main API services, i.e after completion of the request.
So leave everything as it is in generated custom_policy.xml.
Upload both files in Anypoint platform.
API Manager- Custom Policies - Add new policy.
Once both files are uploaded successfully in Anypoint platform
you can apply custom policy through the policy section of API.
Apart from other automated policies, your custom policy will also be available there.
Select the custom policy and apply.
That's it. Wait for a minute for the policy to get apply.
Output:
As per policy, getting an error if id=1