Created

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.

COMMENTS

  • By Dogs patience - 10:09 AM on Mar 20, 2013   Reply
    Thanks for the awesome tutorial! I didn't know about .absoluteLink() and .relativeLink().
    until now. Thank you!
  • By nj photo booth rental - 4:49 PM on Apr 27, 2013   Reply
    Thanks for keeping us informed
  • By best wedding photographer nj - 5:34 PM on Apr 27, 2013   Reply
    Its great to see you taking the time to share this information
  • By green coffee bean extract - 3:09 PM on May 04, 2013   Reply
    Appreciate for highlighting that. I'm not very familiar with it, but my buddy is the one who is doing coding most of the time. Certainly, this can provide a good reference for him. This is really useful for someone who knows well about the system.
  • By CONTINUOUS INK SUPPLY - 4:08 PM on May 13, 2013   Reply
    You actually make it appear so easy with your presentation but I find this matter to be actually something which I think I would never understand. It seems too recondite and extremely broad for me. I'm looking forward for your next post, I’ll try to get the hang of it!
  • By wireless DMX - 11: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.