Feb 01, 2012

Getting to Know the Externalizer

Posted by Feike Visser

In this post I want to highlight the beauty of the Externalizer that is still unknown to a lot of developers.

Sometimes in your CQ5 application you want to get hold of the external url of a particular page, for example if you want to specify the url in your opengraph meta tag.

<meta property="og:url" content="http://mywebsite/myniceurl/mynicepage.html"/>

What you see quite often is this pattern:

String externalUrl = myPage.getPath() + ".html";
<meta property="og:url" content="<%= externalUrl %>"/>

This will result in something like :

<meta property="og:url" content="/content/myinternalpath/myinternalpage.html"/>

This method doesn’t respect settings like, vanity-urls, resource mappings, aliases, external host addresses.

What you should use is the Externalizer, that has methods like .absoluteLink() and .relativeLink().

Externalizer externalizer = bindings.getSling().getService(Externalizer.class);

String externalUrl = externalizer.absoluteLink((SlingHttpServletRequest)request,
request.getScheme(), currentPage.getPath());

This now results in the nice external url you want to share with your customers:

<meta property="og:url" content="http://mywebsite/myniceurl/mynicepage.html"/>

A very easy way to test this while being in development is to set an alias (Advanced-tab on Page properties) on your page. You will see now that the alias is used in the external url, while your path remains the same.


  • By wireless DMX - 9:39 AM on May 20, 2013   Reply
    I am very happy to discover your post as it will become on top in my collection of favorite blogs to visit.