How to use Freemarker with Filtered Resources ?

How to use Freemarker with Filtered Resources ?

AuthorFullName__c
JFrog Support
articleNumber
000003989
ft:sourceType
Salesforce
FirstPublishedDate
2017-10-10T16:13:24Z
lastModifiedDate
2023-01-22T11:05:54Z
VersionNumber
4

Freemarker is a Java based template engine, a generic tool to generate text output (anything from HTML to autogenerated source code) based on templates. It's a Java package, a class library for Java programmers.

The Filtered Resources Add-on (introduced in Artifactory version 2.3.3) allows treating any textual file as a filtered resource by processing it as a FreeMarker template.

With FreeMarker and filtered resource on Artifactory you can create  “Dynamic XML File”, the file that will be downloaded from Artifactory will change according to different parameters. An example use case would be a different readme file for different groups in the organization.

The first example is creating a dynamic file that will show the group name.

In this case we can use getCurrentUserGroupNames() function.

Create a new xml file and insert the following code :

<servers>
	<#list security.getCurrentUserGroupNames() as groupName>hello ${groupName} </#list>
</servers>

Deploy this XML file to Artifactory and do not forget to check the filtered field:

User-added image
 

Afterwards try to download and open it. Artifactory will set the value of the groups parameter according to the logged in user.

User-added image 

Now if we try to login from another user in a different group and download the file, the content will be different:
 User-added image

Another example is the boolean function isAdmin().  Which we can use a part of an if statement to dynamically set the content of the filtered resource, as demonstrated in the below example:
 

<servers>
<#if !security.isAdmin()>False, this user is not Admin user <#else>True, this user is Admin user </#if>
</servers>

When logging in with a non-admin user and downloading the file we will get the following content:

 User-added image

Alternatively, when logging in with an admin user, we get the following content:

User-added image