This Confluence has been LDAP enabled, if you are an ASF Committer, please use your LDAP Credentials to login. Any problems file an INFRA jira ticket please.

Child pages
  • GSoC 2010 mini-CMS project
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 15 Next »

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.

See http://tinyurl.com/asfgsoc for the full list of GSoC 2010 projects at the ASF, and http://community.apache.org/gsoc for general GSoC information.

Introduction

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.

OSGi

JCR

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:

 

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:

curl -F"sling:resourceType=foo/bar" -F"title=Hello world" http://admin:admin@localhost:8080/content/myfirstnode

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: 

  • ESP
  • JSP
  • Java Servlet
  • Scala
  • Python
  • Groovy
  • Ruby

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:

Content loading

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.

In David there are two different nodes created when you deploy your application: /content/david and /content/tags .

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

"sling:resourceType": "david"

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.

These JSON files are loaded using the Maven Bundle Plugin , as you can see in the David core/pom.xml file 

<!-- initial content to be loaded on bundle installation -->
<Sling-Initial-Content>
	initial-content;overwrite:=true;uninstall:=true
</Sling-Initial-Content>                 

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.

...
...
<title>David Mini CMS</title>
<%
load("header.esp");
%>

</head>
<body onload="checkAuth()">
	<div id="lCenter">
	<div id="desktop">
	<%
	load("menu.esp");
	%>
	<div id="contentPanel" class="centralPanel">
...
...

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:

......
...
//Function called when there is a click of the
//submit button in new.esp page
$('#button').click(function() {

    //Retrieve the input texts from the HTML
    var title = $("#title").val();
    var text = $("#editor1").val();
    var editor_data = CKEDITOR.instances.editor1.getData();
    text=editor_data;
    var tagValues = $("#tags").val();

    //Now create a list with tags
    tagValues=tagValues.split(",");


    for(var i=0; i<tagValues.length; i++) {
	tagValues[i] = tagValues[i].replace(/ /g,'');
    }

    var token = new Array();
    for(var i=0; i<tagValues.length; i++)
		if(tagValues[i] != "")
			token.push(tagValues[i]);

    //Every information is stored in the "data" variable
    var data="title="+title+"&text="+text+"&sling:resourceType=david&jcr:mixinTypes=mix:referenceable";

    for (var j = 0 ; j < token.length ; j++){
		data=data.concat("&tag="+token[j]);
    }

    //The current date will be used to create the folders
    //in the Sling repository where we will put the content
    var currentDate = new Date();
    var year = currentDate.getYear()+1900;
    var month = currentDate.getMonth()+1;
    var day = currentDate.getDate()+1;

    if (month<10)
	month = "0" + month;
    if (day<10)
	day = "0" + day;


    //The url is under the David root node /content/david
    //created during the initial content loading
    var url="/content/david/"+year+"/"+month+"/"+day+"/";

    //Simple AJAX call to create a HTTP POST request
    $.ajax({
	type: "POST",
	url: url,
	data: data,
	success: function(msg){
		alert("Entry saved");
		window.location = "/content/david.html";
	},
	error: function(msg){
		alert("Error during save");
	}
    });
});
...
...
...

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 understand also the edit page will be equal to this one :P ).
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.
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.

Authentication

  • No labels