4

Is there a standard way in Adobe CQ5 to get the current page url in order to create automatic canonical links in the head of the JSP?

Basically we have a dialogue config box where you can override the canonical link, but by default if this is empty I want to construct the link based on the raw URL path for the current request.

The JSP for the head is shared across all requests.

Many thanks

Darius
  • 5,180
  • 5
  • 47
  • 62

1 Answers1

11

The ${currentPage.path} variable will give you the path of the parent page to the current resource, but you should use the Externalizer to make this into a nice user-facing URL, (i.e. applying any mapping rules that you have in place & using the correct domain name, rather than the server name e.g. prod-server-123)

i.e.

ResourceResolver resourceResolver = request.getResourceResolver();
Externalizer externalizer = resourceResolver.adaptTo(Externalizer.class);
String canonicalUrl = externalizer.publishLink(resourceResolver, "http", currentPage.getPath());

You can customise what the externalizer treats as the base URL via Felix, or via an osgi:Config node in your repository. and read more on the official docs on the Adobe site.

(Note that the Externalizer is CQ5.5+)

anotherdave
  • 6,656
  • 4
  • 34
  • 65
  • So, forgive my noobness with CQ, but currentPage and/or request variables are passed where? How do I get the request in my model? Its not injected in to the constructor. And in head.jsp I have a page object, but it doesnt seem to have the path property? – Darius Oct 25 '13 at 10:42
  • If you're working with JSPs, you use the `cq:defineObjects` tag, which should make `currentPage` available in the request. If you're working in a Java servlet, you can extend from `SlingAllMethodsServlet`, to get access to the SlingRequest from which you can get the current resource. I'm not sure what you mean that it's not injected into the constructor — do you have any sample code that you could show? If you expand your question, I can expand my answer :) Might be difficult to do through comments otherwise! – anotherdave Oct 25 '13 at 12:50
  • You were a great deal of help in finally solving it! The currentPage variable did exist, and I just had to namespace the Externalizer to get it to work correctly. All is well now! Many thanks again – Darius Oct 25 '13 at 14:26
  • Cool, no problem — glad you got it sorted :) – anotherdave Oct 25 '13 at 15:51