Vanity Paths not getting added to the JCR Resolver. | Community
Skip to main content
sambashiva0808
Level 2
March 8, 2018

Vanity Paths not getting added to the JCR Resolver.

  • March 8, 2018
  • 1 reply
  • 3001 views

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

     }

}

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

1 reply

smacdonald2008
Level 10
March 8, 2018

So this code works sometimes and fails other times?

sambashiva0808
Level 2
March 9, 2018

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.

sambashiva0808
Level 2
March 9, 2018

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.