Overview (from SLING-1438)
This is a Google Summer of Code 2010, Federico Paparoni has been accepted as a student to work on it, mentored by Bertrand Delacretaz
The goal is to create a mini-CMS with Sling, that demonstrates Sling best practices.
Apache Sling is an opensource project with a lot of technologies and features. The goal of this project is to create a mini-CMS, that developers can use to understand how to develop a simple application with Sling.
So it is necessary to know a little about two main topics: OSGi and JCR. The following links are useful resources to read something about these technologies.
The repository for this project can be found at http://code.google.com/p/davidgsoc2010/.
Some words about David Mini CMS
This project shows some features of Apache Sling and can be used for educational purpose to move your first steps with this framework.
David uses the following opensource library/technologies:
- jQuery 1.4.2
- jQueryUI 1.8
- LavaLamp - A menu plugin for jQuery with cool hover effects
- WP-Cumulus SWF - used to display tag cloud
The available features are
- CRUD (Create, Read, Update and Delete) for the content
- Full HTML creation of article
- Creation of PDF rappresentation of a single post or the entire list of contents available
- Tagging system
- Search by title,text,tag
- Send email to someone with the link of article
- Background service that checks number of articles/tags in the repository
These aren't space age features, but are useful to understand how to create an application using Apache Sling
First steps into Sling
Firstly you must setup the environment for Sling, so you can follow the guide at http://sling.apache.org/site/getting-and-building-sling.html.
Now that you created your environment, you can setup some other tools that can be useful during the development:
- cURL : Command line tool to send HTTP request
- JCR Explorer : Extension that create a useful explorer for the JCR repository you are working in
- BitKinex : FTP/SFTP/HTTP/WebDAV Client that can be used to manage the files you uploaded in Sling
It's time to make our "Hello world" in Apache Sling.
Open a console and simply launch the next command:
This is a simple HTTP request, where you pass some parameters and values. Using it you have created a first resource under Sling.
The resource is a JCR node, as every resource in Sling, put under folder /content. This node has two parameters, title and sling:resourceType.
Node creation is a simple task, but you must understand how you can render the information stored in the nodes using Sling.
The first document you can read is the next one: http://dev.day.com/content/ddc/blog/2008/07/cheatsheet/_jcr_content/par/download/file.res/cheatsheet.pdf
It simply describes how content resolution works in Sling.
Another important information to better understand Sling, is that a resource is rendered by a script.
Script files are stored under the folders /apps or /libs and there is a wide choice of possible scripting engine:
- Java Servlet
To select a script, Sling uses the node's sling:resourceType property. So if we say that sling:resourceType is foo/bar, Sling will search under the /apps/foo/bar/ folder.
The following links describes how the scripts work and what type of variables we have during the script execution:
You can setup some initial contents that can be used in your application. It is a useful thing, because with a simple configuration you have some nodes already created when your application starts.
These nodes are defined in the application folders, using a JSON format. Every information stored in these JSON will be a property of the created nodes.
The most important property is the next one
This property defines "david" as resource type, so Sling knows that it will search under the folder /apps/david to find the scripts that will be called on this node.
JSON isn't the only way to load initial content. Further informations about content loading can be found in the Content Loading Bundle Documentation.
Create new entry
There is a script that provide this basic function, /apps/david/new.esp. As you can see in David I choose the ESP scripting language, but as we already said, you can choose among a lot of scripting engines with Sling. This script loads two other script files, used in every script of David: /apps/david/header.esp and /apps/david/menu.esp. These scripts, as the name suggests, contain header informations and the menu for David.
In the header there are jQuery functions and CSS definitions, useful for the whole CMS. In the menu script we can find the definition of a classic menu.
Turning back to the new.esp script, we can see in the following code as header and menu are loaded, using an ESP function.
So we loaded these two scripts in new.esp. In addition to this, in this script we defined a simple form, with some input text and a CKEditor panel.
Once the user fills the input, the page is like in the following image
The submit button of this page is bounded to a jQuery function defined in the header.esp file. To create a new entry we have only to create a HTTP POST request, including the informations the user put in the input texts. The function that create the new entry is the following:
If we click on the button and everything goes well, we will see a popup with the text "Entry saved" and our article will be in the repository at the path /content/david/YEAR/MONTH/DAY/somethingliketitle.
As you can see, there isn't a definition for the name of the entry but anyway we will have this entry saved...how is it possible?
The request URL we created is where we would like to insert our entry. If this URL already exist, we will only update properties of this node (and so you can already understand that the edit page will be equal to this one).
If the resource doesn't exist, a new item is created. If the resource name ends with /* or /, the name of the item will be created using an algorith that also uses the name of new node.