Generating URL based on AEM RUN mode using AEM Externalizer Service

AEM allows us to create content for various channels. In large enterprise ecosystem AEM is central system to deliver content to mobile, web, email, big screens and devices like echo show. Same content can be rendered in different ways and formats and that is a big advantage of AEM.

In this article, we are going to look at one specific challenge that becomes very visible when AEM delivers content via email, mobile etc. The challenge is making sure that links embedded in emails and content delivered via email are not broken. There are various ways in which you can fix this problem:
  1. Dynamically determine the host URL based on incoming request (sling request and resolver) but, this is not possible in all situations (especially when original request is not originated from a resource/page served by same AEM instance)
  2. Create your own OSGi configuration to store AEM instance host domain. This needs to be different for Author and Publisher
  3. Hardcoded URLs … a big NO for this

Haven’t AEM team thought about this? Yes, they did and if we want to solve this problem in AEM way then we should AEM Externalizer Service.

Externalizer is an OSGI service programmatically transform a resource path (e.g. /content/mysite/mypage) into an external and absolute URL (for example, http://www.site.com/content/mysite/mypage) by prefixing the path with a pre-configured DNS. Author and Publish mapping is done for you but, we can map other custom URLs based on run mode/host DNS and while building URLs we can use appropriate configuration based on run mode of server to generate external URLs.

In this article, we are going to create a simple OSGi service that can be used to generate a link for external content delivery.

Step 1: Configure AEM with URLs for all run modes
  1. Navigate to system console by entering URL http://:/system/console/configMgr
  2. Open “Day CQ Link Externalizer” configuration, this will look like

  1. Define a domain mapping for custom domains (if needed)
  2. Save changes

Step 2: Create sample OSGi service (interface and implementation)
Create a simple interface for sample OSGi service that we will implement


Implement ExternalLinkBuilderService interface


Step 3: Inject service created in Step# 2 in other classes
Now we can inject this service into other OSGi components (using @Reference) and call method buildExternalLink() with internal path and it’ll return an absolute link based on run mode

Small but a very useful concept. I hope you have enjoyed reading this. Feel free to post your comments.

References:
Externalizer Documentation & API usage: https://helpx.adobe.com/experience-manager/6-3/sites/developing/using/reference-materials/javadoc/com/day/cq/commons/Externalizer.html

Comments

Popular posts from this blog

AEM as a Cloud Service (AEMaaCS) – Architecture Overview

AEM 6.3 - Bundle Whitelisting - Deprecation of administrative authentication

AEM - Query list of components and templates

Custom synchronisation or rollout action for blueprint ?