How to enable Logging of AEM query

As Adobe AEM mentioned :

To enable logging, you need to enable DEBUG level logs for the categories pertaining to Oak indexing and queries. These categories are:

  • org.apache.jackrabbit.oak.plugins.index
  • org.apache.jackrabbit.oak.query
  • com.day.cq.search

The com.day.cq.search category is only applicable if you are using the AEM provided QueryBuilder utility.

You can enable logging by following pointing your browser to http://localhost:4502/system/console/slinglog

query-log

Tips of using Sightly in AEM6

Here are some tips of using Sightly in AEM6

Passing arguments

  • To avoid comlex expressions inside emplates, Sightly does not allow passing arguments to functional call. Only zero argument calls are allowed from templates
  • Java Use-API doesn’t support passing parameters to the getter method You may pass parameters once, during the Use class initialization. Take a look on this example inspired by the Sightly documentation

<!– info.html –>
< div data-sly-use.info=”${‘Info’ @ text=’Some text’}” >
< p>${info.reversed}< /p>
< /div>

// Info.java
public class Info extends WCMUse {

private String reversed;

@Override
public void activate() throws Exception {
String text = get(“text”, String.class);
reversed = new StringBuilder(text).reverse().toString();
}

public String getReversed() {
return reversed;
}
}

Make page to be authorable

Put < div data-sly-include=”/libs/wcm/core/components/init/init.jsp”>< /div> in header

Loading Client Libraries

The client libraries helper template library (/libs/granite/sightly/templates/clientlib.html) can be loaded through data-sly-use and stored in a clientLib block element variable. Loading the library’s CSS style sheets and JavaScript is done through data-sly-call. The clientLib template library exposes three templates:

  • css – loads only the CSS files of the referenced client libraries
  • js – loads only the JavaScript files of the referenced client libraries
  • all – loads all the files of the referenced client libraries

Each helper template defines a categories option that accepts either an array of string values or a string containing a comma separated values list for referencing the desired client libraries. Example:

Referencing client libraries components in different sections of a page
< head data-sly-use.clientLib=”${‘/libs/granite/sightly/templates/clientlib.html’}” >
<  css data-sly-call=”${clientLib.css @ categories=[‘category1’, ‘category2’]}” data-sly-unwrap/ >
< /head>
< body>
< !– content — >
< js data-sly-call=”${clientLib.js @ categories=[‘category1’, ‘category2’]}” data-sly-unwrap/ >
< /body>

Referencing client libraries in the <head> tag of a page
< head data-sly-use.clientLib=”${‘/libs/granite/sightly/templates/clientlib.html’}”>
< clientlib data-sly-call=”${clientLib.all @ categories=[‘category1’, ‘category2’]}” data-sly-unwrap / >
< /head>

Pass params between htmls via data-sly-template and  data-sly-call

renderer.html: this page has html template blocks
< div data-sly-use.lib=”renderer.html” data-sly-unwrap data-sly-call=”${lib.simpleTextRenderer @ foods=[‘Coffee’,’Tea’,’Milk’,’Rice’,’Other’]}”>< /div>

sightly-cmponent.html
< div data-sly-template.simpleTextRenderer=”${@ foods}”>
< ul data-sly-list.food=”${foods}” class=”simple-list”>
< li>${food}< /li>
< /ul>
< /div>

5. Great resrouces

Tips of using Sling model in AEM6

Here are some tips of using Sling model in AEM6

1. Config pom in my app bundle

<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>

<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Sling-Model-Packages>
au.com.sensis.yp.selcitra.slingmodels
</Sling-Model-Packages>
</instructions>
</configuration>
</plugin>

2. Config debug log
Go to http://localhost:4502/system/console/configMgr, add org.apache.sling.commons.log.LogManager item with org.apache.sling.models
slingmodel-log

3. AEM6 uses slign model 1.0.0, so some features are not avaliable:
1) Collecton/List injection are introduced from 1.0.6, I use code below
2) Can’t inject field like @named(“jcr:name”) etc, So I use AEM Objects Sling Models Injector to achieve this

@Inject
private Resource resource;

public String getName() {
resouce.getName();
}

4. Which injectors are available
http://localhost:4502/system/console/status-slingmodels

5. Great resrouces
https://sling.apache.org/documentation/bundles/models.html
https://docs.adobe.com/docs/en/aem/6-0/develop/platform/sling-adapters.html
http://felix.apache.org/documentation/subprojects/apache-felix-maven-bundle-plugin-bnd.html

6. Node types resources
https://docs.adobe.com/docs/en/aem/6-0/develop/platform/custom-nodetypes.html
http://jackrabbit.apache.org/jcr/node-type-visualization.html
https://docs.adobe.com/docs/en/aem/6-0/develop/ref/javadoc/com/day/cq/commons/jcr/JcrConstants.html

Specify resourceType of component in dialog.xml

I am facing a html5smartimage node lack of foundation/components/image resourceType issue.

Using  name=”./image/sling:resourceType” in dialog.xml is the key

<smallImage
jcr:primaryType=”cq:Widget”
cropParameter=”./image/imageCrop”
ddGroups=”media”
fieldLabel=”Small Image”
fileNameParameter=”./image/fileName”
fileReferenceParameter=”./image/fileReference”
height=”100″
mapParameter=”./image/imageMap”
name=”./image/file”
renditionSuffix=”/_jcr_content/renditions/original”
requestSuffix=”.img.png”
rotateParameter=”./image/imageRotate”
width=”100″
xtype=”html5smartimage”>
<items jcr:primaryType=”cq:WidgetCollection”>
            <resType
                    jcr:primaryType=”cq:Widget”
                    ignoreData=”{Boolean}true”
                    name=”./image/sling:resourceType”
                    value=”foundation/components/image”
                    xtype=”hidden”/>
        </items>
</smallImage>

Or can simply use

<theAttr
jcr:primaryType=”cq:Widget”
ignoreData=”{Boolean}true”
name=”./image/sling:resourceType”
value=”foundation/components/image”
xtype=”hidden”/>

Feature Flags

Recently, I try to implement a feature toggle in my project. I find Sling already have such feature: Feature Flags

https://sling.apache.org/documentation/the-sling-engine/featureflags.html

The API document in AEM6 is

http://docs.adobe.com/content/docs/en/aem/6-0/develop/ref/javadoc/org/apache/sling/featureflags/package-summary.html

My implementation details list below

1. bundle configuration

The config file should be /config/org.apache.sling.featureflags.Feature.xml.
This file name can be gotten from  Factory Persistent Identifier (Factory PID) which is located in /system/console/configMgr

The configure details are

<?xml version=”1.0″ encoding=”UTF-8″?>
<jcr:root xmlns:sling=”http://sling.apache.org/jcr/sling/1.0&#8243; xmlns:jcr=”http://www.jcp.org/jcr/1.0&#8243;
jcr:primaryType=”sling:OsgiConfig”
name=”feature1″
description=”feature1-des”
enabled=”{Boolean}true”/>

Note: You can get all parameters by manually create a config then go to /system/console/status-Configurations and search “featureflags”

2. Multiple feature flags which are done by adding extra string to the name of the xml file.

If you like to use mutiple feature flags, you can create xml file by appending “-string” after the name, and all
e.g. org.apache.sling.featureflags.Feature-feature1.xml org.apache.sling.featureflags.Feature-feature1.xml

The results like below (Click image to get lager image)

featureflags

feature_flag_details

3. The Featureflag service can be access via Felix @Reference annotation or code below

import org.apache.sling.featureflags.Features;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;
public class FeatureSwitcher {
   public static boolean isFeatureOneOn() {
       BundleContext bundleContext = FrameworkUtil.getBundle(Features.class).getBundleContext();
       ServiceReference factoryRef = bundleContext.getServiceReference(Features.class.getName());
       Features features = (Features) bundleContext.getService(factoryRef);
       return features.isEnabled("feature1");
   }
}

}

4. Use it in Sightly

data-sly-use.featureFlag="com.aemtreasury.FeatureFlag"

${featureFlag.featureOneOn} 
package com.aemtreasury;

import com.adobe.cq.sightly.WCMUse;
import org.apache.sling.featureflags.Features;

public class FeatureFlag extends WCMUse {

    private Features features;

    @Override
    public void activate() throws Exception {
       features = getSlingScriptHelper().getService(Features.class);
    }

    public Boolean isFeatureOneOn() {
       return features.isEnabled("feature1");
    }
}

What are the sling changes within AEM 6.1

The list below are quoted from  a great blog https://cqdump.wordpress.com/2015/06/05/what-is-new-in-sling-with-aem-6-1.

SYMBOLIC NAME OF THE BUNDLE

AEM 5.6.1

AEM 6.0

AEM 6.1

org.apache.sling.adapter

2.1.0

2.1.0

2.1.4

org.apache.sling.api

2.4.3.R1488084

2.7.0

2.9.0

org.apache.sling.atom.taglib

0.9.0.R988585

0.9.0.R988585

0.9.0.R988585

org.apache.sling.auth.core

1.1.2

1.1.7.R1584705

1.3.6

org.apache.sling.bgservlets

0.0.1.Rev1231138

0.0.1.R1582230

0.0.1.R1582230

org.apache.sling.bundleresource.impl

2.1.2

2.2.0

2.2.0

org.apache.sling.commons.classloader

1.3.0

1.3.2

1.3.2

org.apache.sling.commons.compiler

2.1.0

2.1.0

2.2.0

org.apache.sling.commons.fsclassloader

1.0.0

org.apache.sling.commons.html

1.0.0

1.0.0

1.0.0

org.apache.sling.commons.json

2.0.6

2.0.6

2.0.10

org.apache.sling.commons.log

3.0.0

4.0.0

4.0.2

org.apache.sling.commons.logservice

1.0.2

1.0.2

1.0.4

org.apache.sling.commons.mime

2.1.4

2.1.4

2.1.8

org.apache.sling.commons.osgi

2.2.0

2.2.0

2.2.2

org.apache.sling.commons.scheduler

2.3.4

2.4.2

2.4.6

org.apache.sling.commons.threads

3.1.0

3.2.0

3.2.0

org.apache.sling.datasource

1.0.0

org.apache.sling.discovery.api

0.1.0.R1484784

1.0.0

1.0.2

org.apache.sling.discovery.impl

0.1.0.R1486590

1.0.8

1.1.0

org.apache.sling.discovery.support

0.1.0.R1484784

1.0.0

1.0.0

org.apache.sling.distribution.api

0.1.0

org.apache.sling.distribution.core

0.1.1.r1678168

org.apache.sling.engine

2.2.8

2.3.3.R1588174

2.4.2

org.apache.sling.event

3.1.5.R1485539

3.3.10

3.5.5.R1667281

org.apache.sling.event.dea

1.0.0

org.apache.sling.extensions.threaddump

0.2.2

0.2.2

0.2.2

org.apache.sling.extensions.webconsolesecurityprovider

1.0.0

1.0.0

1.1.4

org.apache.sling.featureflags

1.0.0

1.0.0

org.apache.sling.fragment.ws

1.0.2

1.0.2

1.0.2

org.apache.sling.fragment.xml

1.0.2

1.0.2

1.0.2

org.apache.sling.hc.core

1.1.0

1.2.0

org.apache.sling.hc.webconsole

1.1.0

1.1.2

org.apache.sling.i18n

2.2.4

2.2.8

2.4.0

org.apache.sling.installer.api

1.0.0

org.apache.sling.installer.console

1.0.0

1.0.0

1.0.0

org.apache.sling.installer.core

3.4.6

3.5.0

3.6.4

org.apache.sling.installer.factory.configuration

1.0.10

1.0.12

1.1.2

org.apache.sling.installer.factory.subsystems

1.0.0

org.apache.sling.installer.provider.file

1.0.2

1.0.2

1.1.0

org.apache.sling.installer.provider.jcr

3.1.6

3.1.6

3.1.16

org.apache.sling.javax.activation

0.1.0

0.1.0

0.1.0

org.apache.sling.jcr.api

2.1.0

2.2.0

2.2.0

org.apache.sling.jcr.base

2.1.2

2.2.2

2.2.2

org.apache.sling.jcr.classloader

3.1.12

3.2.0

org.apache.sling.jcr.compiler

2.1.0

2.1.0

2.1.0

org.apache.sling.jcr.contentloader

2.1.6

2.1.6

2.1.10

org.apache.sling.jcr.davex

1.2.0

1.2.0

1.2.2

org.apache.sling.jcr.jcr-wrapper

2.0.0

2.0.0

2.0.0

org.apache.sling.jcr.registration

0.0.1.R1345943

1.0.0

1.0.2

org.apache.sling.jcr.resource

2.2.9.R1483758

2.3.7.R1591843

2.5.0

org.apache.sling.jcr.resourcesecurity

0.0.1.R1562502

1.0.2

org.apache.sling.jcr.webdav

2.2.0

2.2.2

2.2.2

org.apache.sling.jmx.provider

1.0.2

1.0.2

org.apache.sling.launchpad.installer

1.2.0

1.2.0

1.2.0

org.apache.sling.models.api

1.0.0

1.1.0

org.apache.sling.models.impl

1.0.2

1.1.0

org.apache.sling.resource.inventory

1.0.2

1.0.4

org.apache.sling.resourceaccesssecurity

0.0.1.R1579485

1.0.0

org.apache.sling.resourcecollection

0.0.1.R1479861

1.0.0

1.0.0

org.apache.sling.resourcemerger

1.1.2

1.2.9.R1675563-B002

org.apache.sling.resourceresolver

1.0.6

1.1.0

1.2.4

org.apache.sling.rewriter

1.0.4

1.0.4

1.0.4

org.apache.sling.scripting.api

2.1.4

2.1.6

2.1.6

org.apache.sling.scripting.core

2.0.24

2.0.26

2.0.28

org.apache.sling.scripting.java

2.0.6

2.0.6

2.0.12

org.apache.sling.scripting.javascript

2.0.12

2.0.13.R1566989

2.0.16

org.apache.sling.scripting.jsp

2.0.28

2.0.28

2.1.6

org.apache.sling.scripting.jsp.taglib

2.1.8

2.2.0

2.2.4

org.apache.sling.scripting.jst

2.0.6

2.0.6

2.0.6

org.apache.sling.scripting.sightly

1.0.2

org.apache.sling.scripting.sightly.js.provider

1.0.4

org.apache.sling.security

1.0.4

1.0.6

1.0.10

org.apache.sling.serviceusermapper

1.0.0

1.2.0

org.apache.sling.servlets.compat

1.0.0.Revision1200172

1.0.0.Revision1200172

1.0.0.Revision1200172

org.apache.sling.servlets.get

2.1.4

2.1.8

2.1.10

org.apache.sling.servlets.post

2.3.1.R1485589

2.3.4

2.3.6

org.apache.sling.servlets.resolver

2.2.4

2.3.2

2.3.6

org.apache.sling.settings

1.2.2

1.3.0

1.3.6

org.apache.sling.startupfilter

0.0.1.Rev1387008

0.0.1.Rev1526908

0.0.1.Rev1526908

org.apache.sling.startupfilter.disabler

0.0.1.Rev1387008

0.0.1.Rev1387008

0.0.1.Rev1387008

org.apache.sling.tenant

1.0.0

1.0.0

1.0.2

COMPONENT HIERARCHY AND INHERITANCE

Component hierarchy is mentioned in ADOBE official component doucments

Components within AEM are subject to 3 different hierarchies:

1.Resource Type Hierarchy:

This is used to extend components using the property sling:resourceSuperType. This enables the component to inherit; for example a text component will inherit various attributes from the standard component.

  • scripts (resolved by Sling)
  • dialogs
  • descriptions (including thumbnail images, icons, etc)
  • Note: a local copy/instance of a component element will take precedence over an inherited element.

2. Container Hierarchy :

  • This is used to populate configuration settings to the child component and is most commonly used in a parsys scenario.For example, configuration settings for the edit bar buttons, control set layout (editbars, rollover), dialog layout (inline, floating) can be defined on the parent component and propagated to the child components.
  • Configuration settings (related to edit functionality) in cq:editConfig and cq:childEditConfig are propagated.

3. Include Hierarchy:

  • This is imposed at runtime by the sequence of includes.
  • This hierarchy is used by the Designer, which in turn acts as the base for various design aspects of the rendering; including layout information, css information, the available components in a parsys among others.

UnderstandingAEM mapping request to resource

Locate content resource

Resource path is used to locate content resource

  1. Sling checks weather a node exists at the location specified in the request(e.g. /content/articles/category-name/article-name.print.a4.html)
  2. If no node is found ,the the extension is dropped and search repeated (e.g. /content/articles/category-name/article-name.print.a4)
  3. If no node is found then return 404 http code

Locate scripts once content resource is located

  • The sling:resourceType is used to locate script to be used for rendering the content.
  • Scripts are located in /apps or /libs.
  • If certain rest method(GET, POST) is required, it should be specified in upercase within script name(e,g. a4.POST.jsp).
  • If multiple scripts apply for a given request

The script with the best match is selected. The more specific a match is ,the better it is; in other word, the more selector matched the better, regardless of any request extension or method name match.

Rules:

  1. Folder(e.g. nodes of tyope nt:folder) takes precedence over jsp file names when resolving using selectors, at least the first selector.
  2. Only one selector in a file name has effect, any file name contains two selectors don’t ever get selected, but name of folder can be used to match the selector in the request.
  3. Scripts with HTTP method names(e.g. GET.jsp) is selected as a last resort even after the default script(examples.jsp)
  4. The precedence of same name files are html > jsp > esp. E.g. if examples.html and examples .jsp both exist,then the examples.html will be shown.

For example: http://www.aemtreasury.com/content/resolution.print.a4.html/a/b?name=Dale of type sling:resourceType=”dale/example”

Assuming scripts structure as diagram below:

jcr_resolution

The order of preference should be as shown:

  1. /apps/dale/example/print/a4.html.jsp
  2. /apps/dale/example/print/a4/html.jsp
  3. /apps/dale/example/print/a4.jsp
  4. /apps/dale/example/print.html.jsp
  5. /apps/dale/example/print.jsp
  6. /apps/dale/example/html.jsp
  7. /apps/dale/example/example.jsp
  8. /apps/dale/example/GET.jsp

I prefer to make the script name is the same with fold name which is easy to remember, in this case is /dale/example/example.jsp

Note