Pages

Friday, 20 December 2013

A starter on Apache Sling and its use in Adobe CQ

What is Sling?

In a few words, Apache Sling can be described as a RESTful web framework implementation, primarily a content-driven framework that sits on top of the JCR (Java Content Repository). It is an Apache Open Source project, originally started as an internal project by Day Software. One of the primary usages of Sling is for storing and managing content on the JCR.

Benefits
  • Supports multiple languages (JSP, ESP, scala, etc..)
  • Easy to interact with, deploy a whole application in one go (when used in CQ, just a HTTP POST call to /apps/install/, etc.)
  • Stateless and RESTful (based on HTTP)
  • Flexible enough so as to power small and simple applications as well as large enterprise level systems
  • Provides an easy-to-extend API library which enables easy extension of the core functionality
  • Built as a series of OSGi bundles and makes use of a number of OSGi core and services. The OSGi framework is a specification to build modular and extensible applications, providing significant benefits like easy deployment; as OSGi bundles which are essentially JAR files with metadata, reusability, dependency management, etc.

CQ5 with Sling

Sling is the backbone for building web applications in Adobe CQ/AEM; it is content-centric. This means that processing of a request is focused on the content, as each (HTTP) request is mapped onto content in the form of a JCR resource (a repository node). As in JCR, everything is content, in Sling, everything is a resource. Thus, Sling manages a virtual tree of resources, which is a merger of actual contents in the JCR repository and resources provided by the so-called resource provider.

Let us analyze the following URI and how it maps to a particular resource/script in CQ–
URI : http://myhost/tools/spy.printable.a4.html/a/b?x=12
protocol
host
content path
selector(s)
extension
suffix
param(s)
http://
myhost
tools/spy
.printable.a4.
html
a/b
x=12

The mapping of the protocol and the host is fairly straightforward. But the magic begins starting from the content path mapping, i.e., from the above example, tools/spy is mapped as the resource itself (which is holding the content). Then, the script is retrieved from the resource properties in the combination with certain parts of the request (i.e., selectors and/or extensions) In the above example, the selector(s), are options provided for how to render the content (in this case, a printable format with A4 as the page size and the .html extension specifies the script to be used for rendering.) The suffix can be used to specify additional information and the param(s) are specific to the script which are generally used to render content dynamically.
Thus from the above example, you can see that a URI mapping in sling with CQ happens a bit differently. It goes without saying that you cannot call a script in sling directly as this would break the strict concept of a RESTful server; you would mix resources and representations. This means, that if you call the script (which is the representation) directly, you would be hiding the resource inside your script, so the sling framework no longer knows about it. This has some serious implications like losing automatic handling of methods other than GET (POST, DELETE, PUT).

Conclusion
Apache Sling is really a great tool to checkout if someone wants to get started in RESTful Web Applications, doing it the right way. It is very flexible and dynamic. Also, there are many tools available today to connect Sling with the popular Java technologies like Apache Maven plugin, Apache felix SCR plugins, etc.

No comments:

Post a Comment

Converting InputStream to String

    private String convertToString(InputStream inputStreamObj)             throws IOException {         if (inputStreamObj != null) {     ...