Vanity Paths not getting added to the JCR Resolver.
Hi Everyone,
I am trying to add the "sling:vanityPath" to the existing pages using a Script, where it is adding the property to all the pages correctly but that Vanity Path is not getting assigned or not getting registered in the JCR Resolver (http://localhost:4502/system/console/jcrresolver ) for which the Vanity URL is getting failed. Please suggest what could be the reason, why it is not getting added to the JCR Resolver Mapping but getting added on the Page.
Note: This is not happening to all the pages that I'm running on, it happening only for few pages which are having the same structure as other pages.
This is the code I'm using :
// Calling this method in Servlet and passing the Resource Resolver through that Servlet.
public void updateAllPaths (String absoluteURL,String basePath, ResourceResolver resourceResolver, String pageName){
String propertyName = "sling:vanityPath";
String [] finalArray = null;
try {
//Adapting the resourceResolver to Session
Session session = resourceResolver.adaptTo(Session.class);
Resource resource = resourceResolver.getResource(absoluteURL + "/" + JCR);//absoluteURL is the page which has to be updated and JCR is "jcr:content"
ModifiableValueMap modifiableValueMap = resource.adaptTo(ModifiableValueMap.class);
//Checking whether the page is already having the "sling:vanityPath" property.
if(modifiableValueMap.containsKey(propertyName)){
//If the page has "sling:vanityPath" property but of String type it removes and adds the same property with String Array
if(modifiableValueMap.get(propertyName).getClass().getCanonicalName().equals("java.lang.String")){
String tempValue = modifiableValueMap.get(propertyName, String.class);
logger.info("Deleting the invalid String type Property:" + tempValue);
modifiableValueMap.remove(propertyName);
session.save();
finalArray = new String[1];
finalArray[0] = basePath+"/"+pageName;
}
//If there's already an existing value then this block reads that value and appends the latest value
else{
String tempArray[] = modifiableValueMap.get(propertyName, String[].class);
finalArray = new String[tempArray.length+1];
for (int i = 0; i < tempArray.length; i++) {
finalArray[i] = tempArray[i];
}
finalArray[finalArray.length-1] = basePath+"/"+pageName;
}
}
//If nothing's there it directly adds the value of type String Array
else{
finalArray = new String[1];
finalArray[0] = basePath+"/"+pageName;
}
modifiableValueMap.put(propertyName, finalArray);
modifiableValueMap.put("sling:redirect", true);
session.save();
//Saving the Session
} catch (Exception e) {
logger.error("Exception in updating all pages", e);
}
}