Sling Framework Part – 1

25/06/2021 / BY / IN Introduction to Sling / 1 Comments

Sling Architecture, REST Principles, Sling Resolving

Hello! Today I will introduce you to the Sling Framework.

After this session you should have an

  1. Understanding of Sling Request Processing
  2. Sling Resolving and Script Processing Mechanism

What is Apache Sling?

  • Sling started as an internal project of Day management and was included in the installation of AEM.
  • It has since been contributed to the Apache Software Foundation.
  • It is Content-centric framework. Sling application used either server side JavaScript or Java servlets.
  • Selected according to simple naming convention to process HTTP request in a RESTful way.
  • The embedded Apache Felix OSGI framework & console, provides a dynamic runtime environment where code & content bundles can loaded, un-loaded & re-configured at runtime.

Introduction to Apache Sling

  • Apache Sing is a Content-centric that means that when processing a request, it first finds the content then based on the content and request data determines how to render it.
  • Sling framework uses a JCR based repository as its data storage. In AEM all its data including the content, application binaries and configurations are all stored in the JCR repository.
  • When processing a web request Apache Sling resolves the request URL, analyze the content at the resolved paths and determines which script in the repository should be executed for rendering content view.
  • In addition, a part of that framework provides a very powerful functionality for component and dialogs inheritance.

Apache Sling: Script Resolution

Suppose we have the URL, GET /wiki/Sling.edit.html/richtext?simple=true

The path here is /wiki/Sling and it determines the content location.

Sling will send a request to the repository to find a node at a path, because a node has sling:resourceType property, Sling will use it to search further for suitable script.

If such property is missing Sling will use sling:resourceSuperType or Jcr:primaryType properties, so the value for sling:resourceType is wiki/page and searching for the resource is carried out in two paths

  1. /apps/wiki/page – In case the resource is not found there
  2. /libs/wiki/page

Let’s suppose that component resource is found under apps folder and there you have a few scripts to perform, Which one will render our page?

The script is selected based on the best match which depends on available extensions in URL, available selectors or simply HTTP request method.


In the above case the best matching script is edit.html.esp, as a result sling will execute this script which should turn us includes other script.

URL Decomposition

http://localhost:4502/content/we-retail/us/en/men.sample.a1.html/a/b?x=12

protocolhostportcontent pathselector(s)extensionsuffixparam(s)
http://localhost4502/content/we-retail/us/en/men.sample.a1.html/a/b?x=12

Let’s see How a Sling parses a URL and splits it into parts.

First, We have protocol, host and port then path to content- a node in repository, it starts right after the host or port and last till the first dot.

sample and a1 are selectors they start after path and last till the very last dot before the extension.

After selectors we typically have an extension html, json, xml etc.

Then suffix. a/b and get parameters at the end.

Suffix and selectors unrequired to use but a very important for the Sling Framework. In order to determine suffixes and selectors in the request you can use following API.

RequestPathInfo pathInfo = slingRequest.getRequestPathInfo(); 
String suffix = pathInfo.getSuffix();
String [] selectors = pathInfo.getSelectors();

Sling Resolution

Now let’s see How Sling Process a request to a we-retail page.

A content path again defines resource in the JCR repository and extension defines a rendition, it tells sling how to render this request.

Page /content/we-retail/us/en/men has property sling:resourceType with value ie. page. This page, Sling will try to find a resource by value of the property and remember that first searching is done under apps folder and then under libs folder in case it’s not found under apps.

So resourceType is found under apps folder and let’s take a look at properties of that page as below.

So, page has the property sling:resourceSuperType with this value which also found under apps ie. page. This page in turn inherited from the basic page, again, with a sling:resourceSuperType property and the basic page is located under libs folder.

Hope you enjoyed this article, I would like to suggest, Kindly watch below video for better understanding with practical session, Thank you for your attention! In the next part we will review Part 2 – Sling Post Servlet, Resource Resolver, Service Users. Click here to visit page!

1 comments on Sling Framework Part – 1
  1. Sathisj Says

    30/06/2021

    Hi Santosh,

    Could you please explain how men.html will execute?

    It already reached men page and it took slingResourceType and went to apps-> page from there it went to libs -> page.

    after that what will happen how the men page will render. Could you please elaborate it.

    Thanks,
    Satish

    Reply

Leave a Reply