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);
}
}
Views
Replies
Total Likes
So this code works sometimes and fails other times?
Views
Replies
Total Likes
Yes, it is working perfectly for adding the properties (sling:vanityPath) in JCR Node. But not sure where it is getting internally failed for adding the vanity paths in JCR Resolver Mapping. Actually all the properties are getting added in the JCR Node but some are not getting added in the JCR Resolver Mapping for which the Vanity URLs are getting failed. Please suggest.
Note : I'm suspecting there could be any problem in session.save() where it is saving in the JCR node but not in JCR Resolver. Not sure.
Views
Replies
Total Likes
Forgot to mention, if I edit the Vanity URL which is added through the code and then Save it through CRX then it is getting added in the JCR Resolver.
Views
Replies
Total Likes
Are you sure the session has all the required permissions. To rule out a permission issue, try using admin call and white list the bundle.
Views
Replies
Total Likes
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies