Custom Policy

 

MuleSoft provide a space where we can develop, add and apply our own custom policy. Sometime the OOTB policies doesn’t suffice the need and then we can leverage the use of custom-policy.

To start with we have to first update maven’s setting.xml

Adding profile and server information in setting.xml

<servers>
    <server>
      <id>exchange-server</id>
      <username>{anypoint.platform.username}</username>
      <password>{anypoint.platform.password}</password>
    </server>
</servers>
<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>

Run the below command to create custom_policy maven project

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

Set you organization id and artefact id

Organization id you will get from Access Management -&> Organization

Custom Policy

Updated Command

mvn -Parchetype-repository archetype:generate \
-DarchetypeGroupId=org.mule.tools \
-DarchetypeArtifactId=api-gateway-custom-policy-archetype \
-DarchetypeVersion=1.2.0 \
-DgroupId=2b7408cd-ed46-4998-ac9a-42017c0becd7 \
-DartifactId=my-custom-policy \
-Dversion=1.0.0 \
-Dpackage=mule-policy

Run the command in maven

mvn -Parchetype-repository archetype:generate -DarchetypeGroupId=org.mule.tools -DarchetypeArtifactId=api-gateway-custom-policy-archetype -DarchetypeVersion=1.2.0 -DgroupId=2b7408cd-ed46-4998-ac9a-42017c0becd7 -DartifactId=my-custom-policy -Dversion=1.0.0 -Dpackage=mule-policy

Command result

D:\Study>mvn -Parchetype-repository archetype:generate  -DarchetypeGroupId=org.mule.tools  -DarchetypeArtifactId=api-gateway-custom-policy-archetype  -DarchetypeVersion=1.2.0  -DgroupId=2b7408cd-ed46-4998-ac9a-42017c0becd7  -DartifactId=my-custom-policy  -Dversion=1.0.0  -Dpackage=mule-policy
[INFO] Scanning for projects...
Downloading from mule-public: https://repository.mulesoft.org/nexus/content/repositories/public/org/mule/tools/maven-metadata.xml
Downloaded from mule-public: https://repository.mulesoft.org/nexus/content/repositories/public/org/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml (1.1 kB at 743 B/s)
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.2.0:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.2.0:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.2.0:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[WARNING] Archetype not found in any catalog. Falling back to central repository.
[WARNING] Add a repository with id 'archetype' in your settings.xml if archetype's repository is elsewhere.
[INFO] Using property: groupId = 2b7408cd-ed46-4998-ac9a-42017c0becd7
[INFO] Using property: artifactId = my-custom-policy
[INFO] Using property: version = 1.0.0
[INFO] Using property: package = mule-policy
[INFO] Using property: encryptionSupported = false
[INFO] Using property: exchangeRepositoryUrl = https://maven.anypoint.mulesoft.com/api/v1/organizations
[INFO] Using property: minMuleVersion = 4.1.1
Define value for property 'policyDescription': My Custom Policy
Define value for property 'policyName': My Custom Policy
Confirm properties configuration:
groupId: 2b7408cd-ed46-4998-ac9a-42017c0becd7
artifactId: my-custom-policy
version: 1.0.0
package: mule-policy
encryptionSupported: false
exchangeRepositoryUrl: https://maven.anypoint.mulesoft.com/api/v1/organizations
minMuleVersion: 4.1.1
policyDescription: My Custom Policy
policyName: My Custom Policy
 Y: : Y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: api-gateway-custom-policy-archetype:1.2.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: 2b7408cd-ed46-4998-ac9a-42017c0becd7
[INFO] Parameter: artifactId, Value: my-custom-policy
[INFO] Parameter: version, Value: 1.0.0
[INFO] Parameter: package, Value: mule-policy
[INFO] Parameter: packageInPathFormat, Value: mule-policy
[INFO] Parameter: minMuleVersion, Value: 4.1.1
[INFO] Parameter: exchangeRepositoryUrl, Value: https://maven.anypoint.mulesoft.com/api/v1/organizations
[INFO] Parameter: encryptionSupported, Value: false
[INFO] Parameter: groupId, Value: 2b7408cd-ed46-4998-ac9a-42017c0becd7
[INFO] Parameter: version, Value: 1.0.0
[INFO] Parameter: policyDescription, Value: My Custom Policy
[INFO] Parameter: policyName, Value: My Custom Policy
[INFO] Parameter: package, Value: mule-policy
[INFO] Parameter: artifactId, Value: my-custom-policy
[INFO] Project created from Archetype in dir: D:\Study\my-custom-policy
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:24 min
[INFO] Finished at: 2020-10-19T18:16:04+05:30
[INFO] ------------------------------------------------------------------------

Now we can see the project

Custom Policy

 

Let’s import the project in Anypoint Studio

Custom Policy

 

Now we can see the template with default values

Default template – will add the body and header in response.

<http-transform:set-response statusCode="201">
    <http-transform:body>#[ 'Hello World!' ]</http-transform:body>
    <http-transform:headers>#[ {'New-Header': 'Hello World!'} ]</http-transform:headers>
</http-transform:set-response>

 

We can do different manipulation with incoming request and outgoing response. Adding, removing the request response headers and adding our own logic. Please go through below link to see what all different logic we can add.

https://docs.mulesoft.com/api-manager/2.x/http-policy-transform

Once we are done with the changes we can deploy the policy to Exchange. For doing this we have to first build the policy and then deploy the policy to exchange using maven command.

First build the application – mvn clean install

D:\Study\my-custom-policy>mvn clean install
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for 2b7408cd-ed46-4998-ac9a-42017c0becd7:my-custom-policy:mule-policy:1.0.0
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-deploy-plugin is missing. @ line 46, column 21
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] -------< 2b7408cd-ed46-4998-ac9a-42017c0becd7:my-custom-policy >--------
[INFO] Building My Custom Policy 1.0.0
[INFO] ----------------------------[ mule-policy ]-----------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean-1) @ my-custom-policy ---
[INFO] Deleting D:\Study\my-custom-policy\target
[INFO]
[INFO] --- mule-maven-plugin:3.3.6:clean (default-clean) @ my-custom-policy ---
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ my-custom-policy ---
[INFO]
[INFO] --- mule-maven-plugin:3.3.6:validate (default-validate) @ my-custom-policy ---
[INFO]
[INFO] --- mule-maven-plugin:3.3.6:initialize (default-initialize) @ my-custom-policy ---
[INFO]
[INFO] --- mule-maven-plugin:3.3.6:generate-sources (default-generate-sources) @ my-custom-policy ---
[INFO]
[INFO] --- mule-maven-plugin:3.3.6:process-sources (default-process-sources) @ my-custom-policy ---
[INFO] About to fetch required dependencies for artifact: 2b7408cd-ed46-4998-ac9a-42017c0becd7:my-custom-policy:pom:1.0.0. This may take a while...
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ my-custom-policy ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- mule-maven-plugin:3.3.6:process-resources (default-process-resources) @ my-custom-policy ---
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ my-custom-policy ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- mule-maven-plugin:3.3.6:compile (default-compile) @ my-custom-policy ---
[INFO]
[INFO] --- mule-maven-plugin:3.3.6:process-classes (default-process-classes) @ my-custom-policy ---
[INFO]
[INFO] --- mule-maven-plugin:3.3.6:generate-test-sources (default-generate-test-sources) @ my-custom-policy ---
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ my-custom-policy ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- mule-maven-plugin:3.3.6:generate-test-resources (default-generate-test-resources) @ my-custom-policy ---
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ my-custom-policy ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- mule-maven-plugin:3.3.6:test-compile (default-test-compile) @ my-custom-policy ---
[INFO]
[INFO] --- maven-surefire-plugin:2.19.1:test (default-test) @ my-custom-policy ---
[INFO]
[INFO] --- mule-maven-plugin:3.3.6:package (default-package) @ my-custom-policy ---
[INFO] Building zip: D:\Study\my-custom-policy\target\my-custom-policy-1.0.0-mule-policy.jar
[INFO]
[INFO] --- mule-maven-plugin:3.3.6:verify (default-verify) @ my-custom-policy ---
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ my-custom-policy ---
[INFO] No primary artifact to install, installing attached artifacts instead.
[INFO] Installing D:\Study\my-custom-policy\pom.xml to C:\Users\mulesy\.m2\repository\2b7408cd-ed46-4998-ac9a-42017c0becd7\my-custom-policy\1.0.0\my-custom-policy-1.0.0.pom
[INFO] Installing D:\Study\my-custom-policy\target\my-custom-policy-1.0.0-mule-policy.jar to C:\Users\mulesy\.m2\repository\2b7408cd-ed46-4998-ac9a-42017c0becd7\my-custom-policy\1.0.0\my-custom-policy-1.0.0-mule-policy.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.538 s
[INFO] Finished at: 2020-10-19T18:19:25+05:30
[INFO] ------------------------------------------------------------------------

Deploy to Exchange – mvn deploy

D:\Study\my-custom-policy>mvn deploy
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for 2b7408cd-ed46-4998-ac9a-42017c0becd7:my-custom-policy:mule-policy:1.0.0
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-deploy-plugin is missing. @ line 46, column 21
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] -------< 2b7408cd-ed46-4998-ac9a-42017c0becd7:my-custom-policy >--------
[INFO] Building My Custom Policy 1.0.0
[INFO] ----------------------------[ mule-policy ]-----------------------------
[INFO]
[INFO] --- mule-maven-plugin:3.3.6:validate (default-validate) @ my-custom-policy ---
[INFO]
[INFO] --- mule-maven-plugin:3.3.6:initialize (default-initialize) @ my-custom-policy ---
[INFO]
[INFO] --- mule-maven-plugin:3.3.6:generate-sources (default-generate-sources) @ my-custom-policy ---
[INFO]
[INFO] --- mule-maven-plugin:3.3.6:process-sources (default-process-sources) @ my-custom-policy ---
[INFO] About to fetch required dependencies for artifact: 2b7408cd-ed46-4998-ac9a-42017c0becd7:my-custom-policy:pom:1.0.0. This may take a while...
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ my-custom-policy ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- mule-maven-plugin:3.3.6:process-resources (default-process-resources) @ my-custom-policy ---
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ my-custom-policy ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- mule-maven-plugin:3.3.6:compile (default-compile) @ my-custom-policy ---
[INFO]
[INFO] --- mule-maven-plugin:3.3.6:process-classes (default-process-classes) @ my-custom-policy ---
[INFO]
[INFO] --- mule-maven-plugin:3.3.6:generate-test-sources (default-generate-test-sources) @ my-custom-policy ---
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ my-custom-policy ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- mule-maven-plugin:3.3.6:generate-test-resources (default-generate-test-resources) @ my-custom-policy ---
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ my-custom-policy ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- mule-maven-plugin:3.3.6:test-compile (default-test-compile) @ my-custom-policy ---
[INFO]
[INFO] --- maven-surefire-plugin:2.19.1:test (default-test) @ my-custom-policy ---
[INFO]
[INFO] --- mule-maven-plugin:3.3.6:package (default-package) @ my-custom-policy ---
[INFO] Building zip: D:\Study\my-custom-policy\target\my-custom-policy-1.0.0-mule-policy.jar
[INFO]
[INFO] --- mule-maven-plugin:3.3.6:verify (default-verify) @ my-custom-policy ---
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ my-custom-policy ---
[INFO] No primary artifact to install, installing attached artifacts instead.
[INFO] Installing D:\Study\my-custom-policy\pom.xml to C:\Users\mulesy\.m2\repository\2b7408cd-ed46-4998-ac9a-42017c0becd7\my-custom-policy\1.0.0\my-custom-policy-1.0.0.pom
[INFO] Installing D:\Study\my-custom-policy\target\my-custom-policy-1.0.0-mule-policy.jar to C:\Users\mulesy\.m2\repository\2b7408cd-ed46-4998-ac9a-42017c0becd7\my-custom-policy\1.0.0\my-custom-policy-1.0.0-mule-policy.jar
[INFO]
[INFO] --- maven-deploy-plugin:2.8.2:deploy (default-deploy) @ my-custom-policy ---
[INFO] No primary artifact to deploy, deploying attached artifacts instead.
Uploading to : https://maven.anypoint.mulesoft.com/api/v1/organizations/2b7408cd-ed46-4998-ac9a-42017c0becd7/maven/2b7408cd-ed46-4998-ac9a-42017c0becd7/my-custom-policy/1.0.0/my-custom-policy-1.0.0.pom
Uploaded to : https://maven.anypoint.mulesoft.com/api/v1/organizations/2b7408cd-ed46-4998-ac9a-42017c0becd7/maven/2b7408cd-ed46-4998-ac9a-42017c0becd7/my-custom-policy/1.0.0/my-custom-policy-1.0.0.pom (3.9 kB at 570 B/s)
Downloading from : https://maven.anypoint.mulesoft.com/api/v1/organizations/2b7408cd-ed46-4998-ac9a-42017c0becd7/maven/2b7408cd-ed46-4998-ac9a-42017c0becd7/my-custom-policy/maven-metadata.xml
Uploading to : https://maven.anypoint.mulesoft.com/api/v1/organizations/2b7408cd-ed46-4998-ac9a-42017c0becd7/maven/2b7408cd-ed46-4998-ac9a-42017c0becd7/my-custom-policy/maven-metadata.xml
Uploaded to : https://maven.anypoint.mulesoft.com/api/v1/organizations/2b7408cd-ed46-4998-ac9a-42017c0becd7/maven/2b7408cd-ed46-4998-ac9a-42017c0becd7/my-custom-policy/maven-metadata.xml (332 B at 100 B/s)
Uploading to : https://maven.anypoint.mulesoft.com/api/v1/organizations/2b7408cd-ed46-4998-ac9a-42017c0becd7/maven/2b7408cd-ed46-4998-ac9a-42017c0becd7/my-custom-policy/1.0.0/my-custom-policy-1.0.0-mule-policy.jar
Uploaded to : https://maven.anypoint.mulesoft.com/api/v1/organizations/2b7408cd-ed46-4998-ac9a-42017c0becd7/maven/2b7408cd-ed46-4998-ac9a-42017c0becd7/my-custom-policy/1.0.0/my-custom-policy-1.0.0-mule-policy.jar (39 kB at 7.1 kB/s)
[INFO]
[INFO] --- maven-deploy-plugin:2.8.2:deploy-file (upload-template) @ my-custom-policy ---
Uploading to : https://maven.anypoint.mulesoft.com/api/v1/organizations/2b7408cd-ed46-4998-ac9a-42017c0becd7/maven/2b7408cd-ed46-4998-ac9a-42017c0becd7/my-custom-policy/1.0.0/my-custom-policy-1.0.0-policy-definition.yaml
Uploaded to : https://maven.anypoint.mulesoft.com/api/v1/organizations/2b7408cd-ed46-4998-ac9a-42017c0becd7/maven/2b7408cd-ed46-4998-ac9a-42017c0becd7/my-custom-policy/1.0.0/my-custom-policy-1.0.0-policy-definition.yaml (262 B at 72 B/s)
Downloading from : https://maven.anypoint.mulesoft.com/api/v1/organizations/2b7408cd-ed46-4998-ac9a-42017c0becd7/maven/2b7408cd-ed46-4998-ac9a-42017c0becd7/my-custom-policy/maven-metadata.xml
Downloaded from : https://maven.anypoint.mulesoft.com/api/v1/organizations/2b7408cd-ed46-4998-ac9a-42017c0becd7/maven/2b7408cd-ed46-4998-ac9a-42017c0becd7/my-custom-policy/maven-metadata.xml (284 B at 358 B/s)
Uploading to : https://maven.anypoint.mulesoft.com/api/v1/organizations/2b7408cd-ed46-4998-ac9a-42017c0becd7/maven/2b7408cd-ed46-4998-ac9a-42017c0becd7/my-custom-policy/maven-metadata.xml
Uploaded to : https://maven.anypoint.mulesoft.com/api/v1/organizations/2b7408cd-ed46-4998-ac9a-42017c0becd7/maven/2b7408cd-ed46-4998-ac9a-42017c0becd7/my-custom-policy/maven-metadata.xml (359 B at 102 B/s)
Uploading to : https://maven.anypoint.mulesoft.com/api/v1/organizations/2b7408cd-ed46-4998-ac9a-42017c0becd7/maven/2b7408cd-ed46-4998-ac9a-42017c0becd7/my-custom-policy/1.0.0/my-custom-policy-1.0.0-mule-policy.jar
Uploaded to : https://maven.anypoint.mulesoft.com/api/v1/organizations/2b7408cd-ed46-4998-ac9a-42017c0becd7/maven/2b7408cd-ed46-4998-ac9a-42017c0becd7/my-custom-policy/1.0.0/my-custom-policy-1.0.0-mule-policy.jar (39 kB at 9.2 kB/s)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  32.208 s
[INFO] Finished at: 2020-10-19T18:26:04+05:30
[INFO] ------------------------------------------------------------------------

 

Once deployed we can see the policy in Exchange

 

To Start with we will create a mule application and connect it with API manager – please see how we can connect the mulesoft application to API Manager

Let’s go to API manager to attach the policy

 

Click on policies

 

Select the policy and apply

 

Select the option

Custom Policy

 

New policy is applied now

Custom Policy

 

We can see the policy is downloaded and applied in runtime

Custom Policy

 

Test the service using SOAP UI

Custom Policy

 

We can see the altered body and header in the response

Sample mule application – sample-rest-service-custom-policy

Sample Policy application – my-custom-policy

Sample SOAP UI project – sample-rest-service-custom-policy-soapui-project

  
Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
 
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.


Share this:
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments