Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.

Vanity Paths not getting added to the JCR Resolver.

Avatar

Level 2

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);

     }

}

5 Replies

Avatar

Level 10

So this code works sometimes and fails other times?

Avatar

Level 2

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.

Avatar

Level 2

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.

Avatar

Level 10

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.

Avatar

Level 2
  • Yes, Session has all the permissions. Actually, after having further more debugging on the issue we noticed that mostly it is failing for the pages which are having the "jcr:primaryType"  as "nt:unstructured".
  • It is a success for the pages which are having "jcr:primaryType" as "cq:PageContent" in the bulk update(Updating more than 1000 pages).
  • But if I try to update pages less than 10, it is even updating the pages with "sling:vanityPath" property of type "nt:unstructured" and also adding the mapping to the page and Vanity URL in JCR Resolver.
  • It is only getting failed when we do a bulk update (pages around 1000)