Posts tagged "cq vanity url"

August 27, 2012

How to prevent users from entering duplicate vanity URL’s

This is something which should come very handy at every CQ implementation. The requirement for vanity URLs is that you cant have two pages in CQ with the same vanity URL. In this blog I will try to go over an implementation which will prevent your content authors from entering duplicate vanity URL in the first place.

1. First thing you will need to do is override the OOTB page component dialog and the tab_basic node. To do this, please copy the following nodes

    • /libs/foundation/components/page/dialog
    • /libs/foundation/components/page/tab_basic

    2. Paste these nodes as the child of your projects page component whose “sling:resourceSuperType” is “foundation/components/page”


    3. For the dialog node, change the path to the basic tab to match the path of the “tab_basic” you pasted in your projects page component.
    4. For the “tab_basic” node update the “tab_basic/items/vanity/items/vanityPath/fieldConfig” node to add the following two properties
    • vtype
    • vtypeText
    5. Override  the following in your apps folder
    • /libs/cq/ui/widgets/js.txt
    • /libs/cq/ui/widgets/source/widgets
    6. To your overridden widgets directory at “/apps/cq/ui/widgets/source/widgets”, add a file called duplicateVanityCheck.js
    CQ.Ext.apply(CQ.Ext.form.VTypes, {
    duplicateVanityCheck: function(v, f) {var dialog = f.findParentByType("dialog");
    var dialogPath = dialog.path;
    var cqresponse = CQ.HTTP.get("/apps/duplicateVanityCheck?vanityPath="+v+"&pagePath="+dialogPath);
    var json = eval(cqresponse);
    var vanitypathsjson = json.responseText;
    var JSONObj = JSON.parse(vanitypathsjson);
    var jsonVanityPath = JSONObj.vanitypaths;
    if (jsonVanityPath.length == 0) {
    return true;
    } else {
    // check whether the path of the page where the vanity path is defined matches the dialog's path
    // which means that the vanity path is legal
    return false;
    //alert( "Checking Duplicate" );


    7. In your overridden js.txt file, add this line

    • widgets/duplicateVanityCheck.js


    8. You also need to make sure that your foundation client lib has a dependency on “cq.widgets”.

    9. Now, we have to create an OSGi bundle that will query the JCR for an entered vanity and return the JSON that will be used by duplicateVanityCheck.js. The code for this class will look something like below

     * @scr.component immediate="true" metatype="false"
     * @scr.service interface="javax.servlet.Servlet"
     * name="sling.servlet.methods" values.0="GET"
     * name="sling.servlet.paths" values.1="/apps/duplicateVanityCheck"
    public class VanityDuplicateCheck extends SlingAllMethodsServlet{
        private static final Logger logger = LoggerFactory.getLogger(VanityDuplicateCheck.class);
        protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
                Session session = request.getResourceResolver().adaptTo(Session.class);
                final String vanityPath = request.getParameter("vanityPath");
                final String pagePath = request.getParameter("pagePath");
      "vanity path parameter passed is {}", vanityPath);
      "page path parameter passed is {}", pagePath);
                try {
                    QueryManager qm = session.getWorkspace().getQueryManager();
                    String xpath = "//element(*)[sling:vanityPath='"+ vanityPath + "']";
          "xpath is {}", xpath);
                    Query query = qm.createQuery(xpath, Query.XPATH);
          "Xpath Query Statement is {}", query.getStatement());
                    QueryResult result = query.execute();
                    NodeIterator nodes = result.getNodes();
          "result is ", result.getNodes().toString());
                    TidyJSONWriter tidyJSONWriter = new TidyJSONWriter(response.getWriter());
                    while (nodes.hasNext()) {
                        Node node = nodes.nextNode();
              "Node path is {}", node.getPath());
              "Page path is {}", pagePath);
                        if(node != null && node.getPath().contains("/content"))
                            // check whether the path of the page where the vanity path is defined matches the dialog's path
                            // which means that the vanity path is legal.
                                //do not add that to the list
                      "Node path is {}", node.getPath());
                      "Page path is {}", pagePath);
                            } else {
                catch(RepositoryException re){
                    logger.error( "Error in doGet", re );
            } catch (JSONException e) {
                logger.error( "Error in doGet", e );

    8. At this point you should have everything you need to prevent content authors from entering duplicate vanity URL’s. If they do enter a URL that is already in use, it will fail the validation and they will see an error message similar to what you entered in the vtypeText property.


    Enjoy..and as always, please leave comments/questions and I will try to answer them as soon as possible.

    5:49 PM Permalink
    • Authors

    • Archives

    • Developer Resources