Thursday, March 30, 2017

How to customize the page properties Dialog to include dynamic DropDownList in AEM/CQ5

This post will explain the details to customize page properties Dialog in both Touch and Classic UI's, the version used for implementing this is AEM 6.1.

The basic page properties dialog will be displayed if the sling:resourceSuperType of the page rendering component is specified as foundation page component(wcm/foundation/components/page for Sightly and foundation/components/page for JSP)

Steps to customize the basic page properties and including dynamic DropDownList to the dialog.

/apps/training/components/page-content/ will be referred as the page rendering component path in the post.

Classic UI:

Copy /libs/foundation/components/page/dialog to /apps/training/components/page-content/
Rename the new node to required name e.g. custom

Copy /libs/foundation/components/page/tab_basic to /apps/training/components/page-content/
Rename /apps/training/components/page-content/tab_basic to required name e.g tab_custom

Remove all the nodes under  /apps/training/components/page-content/tab_custom/items

Change path value in the node /apps/training/components/page-content/dialog/items/tabs/items/custom to /apps/training/components/page-content/tab_custom.infinity.json

Change title property of the node /apps/training/components/page-content/tab_custom to required value e.g Custom

Save All configurations

Open the page from site Admin(e.g. http://localhost:4502/cf#/content/training-site/en.html), now the new tab(Custom) will be added to the page properties with empty panel

Adding a textfield to the custom panel:

Create a new node of type cq:Widget under /apps/training/components/page-content/tab_custom/items

Name Type Value
xtype String textfield
name String ./fieldName e.g. ./customtext
fieldLabel String Enter the field lable

Save All configurations - open the page(e.g. http://localhost:4502/cf#/content/training-site/en.html), the new textfield will be displayed under Custom tab.

Adding a dynamic DropDownList:

Create a servlet that returns the JSON data.

import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONObject;
import org.apache.felix.scr.annotations.Properties;

@Service(value = Servlet.class)
@Component(immediate = true, metatype = true)
@Properties({
@Property(name = "sling.servlet.paths", value = "/services/getCountries"),
@Property(name = "service.description", value = "returns list of countries"),
@Property(name = "label", value = "GetCountryList") })
public class GetCountryList extends SlingSafeMethodsServlet{

private static final long serialVersionUID = 1180258251365536303L;

@Override
protected void doGet(SlingHttpServletRequest request,SlingHttpServletResponse response) throws ServletException,IOException {
try{
response.setContentType("application/json; charset=utf-8");
response.setCharacterEncoding("UTF-8");

JSONArray contryJsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject();
jsonObject.put("text", "US").put("value", "United States");
contryJsonArray.put(jsonObject);
jsonObject = new JSONObject();
jsonObject.put("text", "UK").put("value", "United Kingdom");
contryJsonArray.put(jsonObject);
response.getWriter().write(contryJsonArray.toString());
}catch(Exception e){
}
}
}

Create a new node of type cq:Widget under /apps/training/components/page-content/tab_custom/items

Add the following properties to the new node

Name Type Value
xtype String selection
name String ./fieldName e.g. ./customtext
fieldLabel String Enter the field label
type String select
options String Servlet path that returns JSON data (e.g. /services/getCountries)

Save All Configurations - open the page(e.g. http://localhost:4502/cf#/content/training-site/en.html), the new DropDownList will be displayed under Custom tab.


Touch UI:

Copy /libs/foundation/components/page/cq:dialog to /apps/training/components/page-content

Copy /apps/training/components/page-content/cq:dialog/content/items/tabs/items/basic and paste to /apps/training/components/page-content/cq:dialog/content/items/tabs/items

Rename the node name to required value e.g Custom

Change the jcr:title property of the node e.g Custom

Remove nodes that are not required and create/rename nodes as required(better remove all the nodes under /apps/training/components/page-content/cq:dialog/content/items/tabs/items/custom/items/column/items and create the required nodes).

Adding a Dynamic Dropdownlist:

Create a servlet that will return the DataSource

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceMetadata;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import com.adobe.granite.ui.components.ds.DataSource;
import com.adobe.granite.ui.components.ds.SimpleDataSource;
import com.adobe.granite.ui.components.ds.ValueMapResource;
@SlingServlet(resourceTypes = "/services/getCountryList")
public class GetCountryListServlet extends SlingSafeMethodsServlet{
private static final long serialVersionUID = 1180258251365536303L;
@Override
protected void doGet(SlingHttpServletRequest request,SlingHttpServletResponse response) throws ServletException,IOException {
try{
ResourceResolver resolver = request.getResourceResolver();
List<Resource> countryList = new ArrayList<Resource>();
ValueMap valueMap = new ValueMapDecorator(new HashMap<String, Object>()); 
valueMap.put("value","United States");
valueMap.put("text","US");
countryList.add(new ValueMapResource(resolver, new ResourceMetadata(), "nt:unstructured", valueMap));
 
valueMap = new ValueMapDecorator(new HashMap<String, Object>()); 
valueMap.put("value","United Kingdom");
valueMap.put("text","UK");
countryList.add(new ValueMapResource(resolver, new ResourceMetadata(), "nt:unstructured", valueMap));
     
DataSource dataSource = new SimpleDataSource(countryList.iterator());
    request.setAttribute(DataSource.class.getName(), dataSource);
}catch(Exception e){
e.printStackTrace();
}
}
}

Create a node(e.g. customlist) with type nt:unstructured under /apps/training/components/page-content/cq:dialog/content/items/tabs/items/custom/items/column/items

Add the following properties to the new node

Name Type Value
emptyText String selection
name String ./fieldName e.g. ./customlist
fieldLabel String Enter the field label
sling:resourceType String granite/ui/components/foundation/form/select

Create a node with name datasource and type  nt:unstructured under the node created in the previous step

Enter the below property

Name Type Value
sling:resourceType String Servlet Path(e.g. /services/getCountryList)

 Save All Configurations - open the page(e.g. http://localhost:4502/editor.html/content/training-site/en.html), the new fileds/DropDownList will be displayed under Custom tab.





Saturday, March 25, 2017

Integration of AEM with Salesforce - Part1


AEM Salesforce cloud connector can be used to integrate to Salesforce via connected App configured in the Salesforce

The AEM Salesfore integration supports the following functionalities:
  • Lead Search
  • Contact Search
  • Export AEM user as Salesforce Lead
  • Associate an AEM user with a Salesforce "Contact" or "Lead"
The connector can be extended to support the additional functionalities.


Defining the connected App in Salesforce:

A connected app integrates an application with Salesforce using APIs
Connected apps use standard SAML and OAuth protocols to authenticate and provide tokens for use with Salesforce APIs. The required security policies can be set to control the connected Apps access.

Login to Salesforce - login.salesforce.com
Click on the user name- Setup- Click on Create then Apps
Create New connected App
Enter the name and Email address


Click on Enable OAuth settings
Add the below two OAuth scopes - Add the additional scopes if required
  • Access and manage your data (api)
  • Perform requests on your behalf at any time (refresh_token, offline_access)
Specifiy the call back URL - https://<<AEM Host name>>:<<AEM Port name>>//etc/cloudservices/salesforce/<<AEM Salesforce Connector Name>>

e.g. https://localhost:5403/etc/cloudservices/salesforce/SalesforceConnect.html

The call back URL should be https
Click Save


Copy the Consumer Key and Consumer Secret


Click on Manage - Edit Policies
Select the following options
  • Relax IP restrictions
  • Refresh token is valid until revoked
Click on Save


AEM Configuration:

Open https;//<<AEM Server host>>:<<AEM Server port>>/etc/cloudservices/salesforce.html
e.g - https://localhost:5403/etc/cloudservices/salesforce.html
AEM server should be configured with https to enable the Salesforce integration.

Add new Configuration


Enter the Title - Specify the Tile Sames as what ever provided in the callback URL.
Click on Create

Enter the Customer Key and Customer Secret Values


Click on Connect to Salesforce - Wait for 10 mins after creating the Connected App in Salesforce

If the connection is success the browser will redirect to Salesforce login page
Page will be displayed with permission to required access to connected App - Click on Allow


The popup will be displayed with successful message after success connection. Click ok on the popup and the setting window


The redirect_mismatch error will be displayed if the calback URL configured in the Salesforce and the redirect_uri send by the Connector is not maching


The popup with error "Error Getting Access Token" will be displayed if the server is not able to connect to Salesfore login URL- login.salesforce.com


Testing the integration: Use the "Salesforce.com Export workflow " to export the AEM user to Salesforce as Lead.
Edit the "Salesforce.com Export" workflow and select the Salesforce configuration


Enter all the mandatory field to the user profile

Run the workflow by selecting the required profile


The Lead will be created in Salesforce


This post is written based on AEM 6,1.






Friday, March 3, 2017

Time zone difference in Author/Publishers - Adobe CQ5/AEM

The timezone configured in the OS(Linux) level is CST but some time the log files displays the timezone in GMT

To fix the issue force the server to use the required timezone in startup file(start.sh)
e.g.
CQ_JVM_OPTS='-server -Xmx1024m -XX:MaxPermSize=256M -Duser.timezone=US/Central -Djava.awt.headless=true'




Thursday, March 2, 2017

How to protect the content from anonymous access through SAML based SSO - Adobe CQ5/AEM

How to enable SAML based SSO for publisher - Adobe CQ5/AEM
How to enable SAML based SSO in publisher to protect the content while accessing via dispatcher - Adobe CQ5/AEM

This post will explain the steps required to protect the published content from anonymous access through SAML based SSO while accessing via dispatcher/publisher - Adobe CQ5/AEM

Out of scope for this post - Configurations of IDP provider. Make sure the return URL configured in SAML provider is /saml_login

Enable Authentication for required content path:
Go to http://localhost:4503/system/console/configMgr(publisher)
Search for Apache Sling Authentication Service
Add the path that required the authentication to Authentication requirements in the following format +<<Content Path>> e.g. +/content/test


Configure the IDP certificate in AEM:
Go to http://localhost:4503/system/console/configMgr(publisher)
Under /etc/key in the repository, create a node called "saml"(type nt:folder).
Inside this node, add a new binary property called  "idp_cert" for the public certificate of the IdP.
Upload the certificate file by double clicking on idp_cert property
Save All


Go to: http://localhost:4503/libs/granite/security/content/useradmin.html(publisher)
Select any user because TrustStore is global to AEM
Create trust store by supplying the password & then manage trust store
Upload the IdP certificate & make note of the certificate Alias

Go to: http://localhost:4503/libs/granite/security/content/useradmin.html(publisher)
Select authentication-service
Create KeyStore by supplying the password
If encrypting SAML assertions then go to manage KeyStore for uploading the private & public key

Configure the user group for restricting the access to required content:
Go to http://localhost:4503/useradmin(publisher)
Create a new group(content-access-group) with read access to content(provide the read assess only to the specific folder under content -/content/test and the required dam folders) and etc folder


Configure SAML 2.0 Authentication Handler:

Go to http://localhost:4503/system/console/configMgr(publisher)
Search for Adobe Granite SAML 2.0 Authentication Handler
Provide the required details


IDP URL - URL of the IDP where the SAML Authentication Request should be sent to
Provide all the required values
Service Provider Entity ID - ID which uniquely identifies this service provider with the identity provider
UserID Attribute - The name of the attribute containing the user ID from IDP
IDP Certificate Alias - Provide the certificate alais created in the above step
Select Auto create CRX Users
Select Add to Groups
Specify content-access-group in Default Groups(group created in previous step)
Password of Key Store - Specify the key store password specified in the above step.

Configure Referrer Filter:
Go to http://localhost:4503/system/console/configMgr(publisher)
Serach for Apache Sling Referrer Filter
Configure IDP host at Allow Hosts


Access the content URL now - http://test.dispatcher.com/content/test/en.html
The user will be redirected to SAML provider and will be redirected to target page upon successful authentication.

All the user configured in SAML provider will be able to access the target page and the users will be auto created in AEM after successful authentication.
If we want to give the access only to predefined users in AEM then remove Auto create CRX Users and Add to Groups from Adobe Granite SAML 2.0 Authentication Handler configuration.

The authentication will not work if multiple publisher serve the request, as a solution we have to enable sticky session to make sure the browser sessions are directed to same dispatcher/publisher 



Wednesday, March 1, 2017

How to generate sitemap for multi site environments? - Adobe CQ5/AEM

How to generate sitemap for multi site environments? - Adobe CQ5/AEM

This post will explain how to generate the sitemap for different sites(home pages) in multi site environment

Factory servlet to generate the sitemap.xml:

import java.io.IOException;
import java.util.*;

import javax.servlet.ServletException;
import javax.xml.stream.*;

import org.apache.commons.lang3.time.FastDateFormat;
import org.apache.felix.scr.annotations.*;
import org.apache.sling.api.*;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.day.cq.commons.Externalizer;
import com.day.cq.wcm.api.*;

@Component(metatype = true, label = "Site Map", description = "Site Map", configurationFactory = true)
@Service
@SuppressWarnings("serial")
@Properties({
@Property(name = "sling.servlet.resourceTypes", unbounded = PropertyUnbounded.ARRAY,
label = "Homepage Resource Type", description = "Sling Resource Type for Home Page component"),
@Property(name = "sling.servlet.selectors", value = "sitemap", propertyPrivate = true),
@Property(name = "sling.servlet.extensions", value = "xml", propertyPrivate = true),
@Property(name = "sling.servlet.methods", value = "GET", propertyPrivate = true),
@Property(name = "webconsole.configurationFactory.nameHint",
value = "Site Map on resource types: [{sling.servlet.resourceTypes}]") })
public final class SiteMapGeneratorServlet extends SlingSafeMethodsServlet {

private static final Logger LOG = LoggerFactory.getLogger(SiteMapGeneratorServlet.class);
private static final FastDateFormat DATE_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd");
private static final boolean INCLUDE_LAST_MODIFIED_DEFAULT_VALUE = false;

@Property(boolValue = INCLUDE_LAST_MODIFIED_DEFAULT_VALUE, label = "Include Last Modified Date",
description = "If checked, last modified value will be shown in sitemap.")
private static final String INCLUDE_LAST_MODIFIED_PROPERTY = "include.lastmod";

private static final String SITEMAP_NAMESPACE = "http://www.sitemaps.org/schemas/sitemap/0.9";

@Reference
private Externalizer externalizer;

private boolean incLastModified;

@Activate
protected void activate(Map<String, Object> properties) {
this.incLastModified = PropertiesUtil.toBoolean(properties.get(INCLUDE_LAST_MODIFIED_PROPERTY),
INCLUDE_LAST_MODIFIED_DEFAULT_VALUE);
}

@Override
protected void doGet(SlingHttpServletRequest slingRequest, SlingHttpServletResponse slingResponse)
throws ServletException, IOException {

slingResponse.setContentType(slingRequest.getResponseContentType());
ResourceResolver resourceResolver = slingRequest.getResourceResolver();
PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
Page pageObj = pageManager.getContainingPage(slingRequest.getResource());

XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
try {
XMLStreamWriter stream = outputFactory.createXMLStreamWriter(slingResponse.getWriter());

stream.writeStartDocument("1.0");
stream.writeStartElement("", "urlset", SITEMAP_NAMESPACE);
stream.writeNamespace("", SITEMAP_NAMESPACE);

// Current page
writeXML(pageObj, stream, slingRequest);

for (Iterator<Page> children = pageObj.listChildren(new PageFilter(), true); children.hasNext();) {
Page childPage = (Page) children.next();
// If condition added to make sure the pages hidden in search in page properties do not show up in sitemap
if (null != childPage) {
if (!childPage.getProperties().containsKey("hideInSearch")
|| (childPage.getProperties().containsKey("hideInSearch")
&& childPage.getProperties().get("hideInSearch").equals("false"))
|| (childPage.getProperties().containsKey("hideInSearch")
&& childPage.getProperties().get("hideInSearch").equals("")))
writeXML(childPage, stream, slingRequest);
}
}

stream.writeEndElement();
stream.writeEndDocument();

} catch (XMLStreamException e) {
throw new IOException(e);
}
}

private void writeXML(Page pageObj, XMLStreamWriter xmlStream, SlingHttpServletRequest slingRequest)
throws XMLStreamException {
xmlStream.writeStartElement(SITEMAP_NAMESPACE, "url");

String protocolPort = "http";
if (slingRequest.isSecure())
protocolPort = "https";

String locPath = this.externalizer.absoluteLink(slingRequest, protocolPort,
String.format("%s.html", pageObj.getPath()));

writeXMLElement(xmlStream, "loc", locPath);

if (this.incLastModified) {
Calendar calendarObj = pageObj.getLastModified();
if (null != calendarObj) {
writeXMLElement(xmlStream, "lastmod", DATE_FORMAT.format(calendarObj));
}
}
xmlStream.writeEndElement();
}

private void writeXMLElement(final XMLStreamWriter xmlStream, final String elementName, final String xmlText)
throws XMLStreamException {
xmlStream.writeStartElement(SITEMAP_NAMESPACE, elementName);
xmlStream.writeCharacters(xmlText);
xmlStream.writeEndElement();
}

}

Create new servlet configuration from the factory through OSGI console by providing the following details

Home Page Resouce Type - add the Home page resource types that should be considered for generating sitemap.xml

Include Last Modified Date - If selected the last modified date of the page will be included as part of the sitemap.xml



Enable hideInSearch checkbox in all page properties


Select hideInSearch for those child pages should be excluded from siemap.xml

Access the sitemap.xml for the site with the following URL -  http://<<site host>>/<<parent node with configured resource type>>.sitemap.xml

e.g.
http://example1.com/en.sitemap.xml
http://example2.com/en.sitemap.xml

<?xml version="1.0"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://example1.com/en.html</loc>
<lastmod>2017-02-28</lastmod>
</url>
<url>
<loc>http://example1.com/en/test.html</loc>
<lastmod>2017-02-28</lastmod>
</url>
</urlset>



Contact Form

Name

Email *

Message *