Programatically find out if a page is configured with vanities AEM 6.5 | Community
Skip to main content
New Participant
August 11, 2023
Solved

Programatically find out if a page is configured with vanities AEM 6.5

  • August 11, 2023
  • 2 replies
  • 735 views

Hi

I would like to find out programatically if a page with Vanity was called. 

This is what I tried:

public boolean getVerifyResourceVanity(Resource resource) {
if (resource == null) {
return false;
}

final String[] vanities = resource.getValueMap().get(VANITY_PROPERTY, new String[0]);
if (vanities.length == 0) {
return false;
}

final Matcher matcher = PREFIX_PATTERN.matcher(resource.getPath());
if (!matcher.find()) {
return false;
}
return true;
}

This information would help me disable a disclaimer when a page with Vanity was called.

Thanks.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by arunpatidar

You can try

 

import com.day.cq.wcm.api.Page; import org.apache.sling.api.resource.ValueMap; // Assuming you have the Page object Page page = // Obtain the Page object here // Get the page's properties ValueMap properties = page.getProperties(); // Get the cq:vanityUrl property (which can be single or multivalued) String[] vanityUrls = properties.get("cq:vanityUrl", String[].class); if (vanityUrls != null && vanityUrls.length > 0) { for (String vanityUrl : vanityUrls) { System.out.println("Vanity URL: " + vanityUrl); } } else { System.out.println("No vanity URLs configured for this page."); }

2 replies

arunpatidar
arunpatidarAccepted solution
New Participant
August 14, 2023

You can try

 

import com.day.cq.wcm.api.Page; import org.apache.sling.api.resource.ValueMap; // Assuming you have the Page object Page page = // Obtain the Page object here // Get the page's properties ValueMap properties = page.getProperties(); // Get the cq:vanityUrl property (which can be single or multivalued) String[] vanityUrls = properties.get("cq:vanityUrl", String[].class); if (vanityUrls != null && vanityUrls.length > 0) { for (String vanityUrl : vanityUrls) { System.out.println("Vanity URL: " + vanityUrl); } } else { System.out.println("No vanity URLs configured for this page."); }
Arun Patidar
Shashi_Mulugu
New Participant
August 14, 2023

@anasustic AEM Pagemanager API already have method to fetch vanity url. Please check and also try to use highest abstraction level api where possible AEM > Sling > JCR

 

https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/day/cq/wcm/api/Page.html#getVanityUrl--

anasusticAuthor
New Participant
August 14, 2023

Thank you so much for your answer @shashi_mulugu .

The getVanityUrl() method in AEM Pagemanager API returns a String but as you can see in AEM -> Page Property -> Vanity one can configure an array of strings for Vanities. 

 

 

That is why I used 

final String[] vanities = resource.getValueMap().get(VANITY_PROPERTY, new String[0]);