Understanding Apache Sling rendering of resources 1

I’m making a heavy use on the already existing tutorials on the Apache Sling’s website. Not because i want to be lazy about it, but because i think the Sling works quite different to what i have already seen. So i hope to extend the already existing documentation a bit and perhaps help to get a grasp on how Sling works. So, as prerequisite, i expect the Sling launchpad application and a Webdav client up and running.

This post centres around the way Apache Sling works in terms of rendering its content. This is the first part of two, which looks at content, resources and selectors in first place. Content is, by default, stored in Java Content Repository (JCR) conform data store. By default Apache Jackrabbit is used.

The interesting approach is the strict view on resources. Scripts, as Sling refers to views displaying and perhaps transforming data, can not be used without content. So, in order to understand the rendering of resources, one has to understand the way resources are addressed in first hand.

To start with the basics, we need content on the repository. To do so, we’ll use cURL to post data and create a new resource.

curl -u admin:admin -F"sling:resourceType=foo" -F"title=My content node" http://localhost:8080/content/bar

will do the job. This created a new node in the repository. When opening your browser of choice and access http://localhost:8080/content/bar.html, you’ll see a the default script executed and applied to the resource. I suggest getting the code to track back the usage of the HtmlRendererServlet to see that, depending on the suffix, other output formats are accessible as well, like XML or JSON.

Now, when trying to find the appropriate scripts, Sling will use the resource type to resolve a script under /apps/my/resource/type or /libs/my/resource/type. To verify the method, let’s create to new resources:

curl -u admin:admin -F"sling:resourceType=inapps" -F"title=Node displayed via script in apps." http://localhost:8080/content/in/apps

and

curl -u admin:admin -F"sling:resourceType=inlibs" -F"title=Node displayed via script in libs." http://localhost:8080/content/in/libs

When placing the following scripts in /apps/inapps

<!-- Place in /apps/inapps/GET.esp -->
<html>
  <body>
    <h1><%= currentNode.title %></h1>
    <p>This script is located in the <code>/apps/inapps</code>.
    	It's path is resolved simply by using <code>GET</code>.</p>
  </body>
</html>
<!-- Place in /apps/inapps/index.esp -->
<html>
  <body>
    <h1><%= currentNode.title %></h1>
    <p>This script is located in the <code>/apps/inapps</code>.
    	It is adressed with the <code>index</code> selector.</p>
  </body>
</html>

and these in /libs/inlibs

<!-- Place in /libs/inlibs/GET.esp -->
<html>
  <body>
    <h1><%= currentNode.title %></h1>
    <p>This script is located in the <code>/apps/libs</code>.
    	It's path is resolved simply by using <code>GET</code>.</p>
  </body>
</html>
<!-- Place in /libs/inlibs/index.esp -->
<html>
  <body>
    <h1><%= currentNode.title %></h1>
    <p>This script is located in the <code>/apps/inlibs</code>.
    	It is adressed with the <code>index</code> selector.</p>
  </body>
</html>

It gets obvious which scripts are resolved for each resource when calling the different urls:

The reason behind placing the scripts in these specific locations is because the engine will look by default into these paths.

Posted in Uncategorized | Leave a comment

June 1st

Ok, this will be a tough month. I’m planning on heavily investing into the future by hacking lots of WebGL and Clojure. Why those technologies? Clojure is a small language, compared to Scala – as well as i recognized it in the past. The more i read an learn about functional programming, the sexier it gets. The second one is WebGL. Well, WebGL is some sort of love/hate thing. There has been a lot of noise regarding this standard and api because it introduces a lot of security vulnerabilities. But … and here is the thing: it works, it is basically OpenGL ES 2 and the turn around cycles are short. Just refresh the browser. So, a month of fun is about to come. I plan on regularly update this blog with posts about my progress, so … have a nice time as read you guys soon.

Posted in Uncategorized | Leave a comment

My talk at the Flex User group

I visited the Flex user group in Hamburg yesterday. It was a nice meeting as usual and i had a really good time. The feedback was super duper and i’m already having other ideas to talk about in the pipeline. If anyone wants to get in touch with me, just drop me a line and i’m happy to answer questions or just chitchat a bit.

http://www.flexughh.de/2012/05/06/flexughh-meeting-22-05-2012-florian-salihovic-what-about-some-coffee-and-a-piece-of-cake/

 

See you guys there next time!

Posted in CoffeeScript, flexughh | Leave a comment

Installing Ruby on Mac OS X

The prefered way to install Ruby on Mac OS X is using RVM. Although this is an established way, i’m not really feeling comfortable with it. I like to compile Ruby from source. I’ve been following the short introduction at Linux user group. But in order to save some people some headaches, i’d like give some more pointers. Next to readline, libyaml might be necessary. Also make sure you get the latest stable versions.

 curl -O ftp://ftp.cwru.edu/pub/bash/readline-6.2.tar.gz
tar -xvzf readline-6.2.tar.gz
cd readline-6.0
./configure --prefix=/usr/local
make
sudo make install
 curl -O http://pyyaml.org/download/libyaml/yaml-0.1.4.tar.gz
tar -xvzf yaml-0.1.4.tar.gz
cd yaml-0.1.4
./configure
make
sudo make install
 curl -O http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p194.tar.gz
tar -xvzf ruby-1.9.3-p194.tar.gz
cd ruby-1.9.3-p194
./configure --prefix=/usr/local \
--enable-pthread \
--with-readline-dir=/usr/local \
--enable-shared
make
sudo make install
sudo make install-doc

Now, after having the whole package installed, update the PATH (the executable are located there).

export PATH=”/usr/local/bin:$PATH"

Running

$ ruby -v

should print

ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin10.8.0]

For those not being familiar with the command line, i recommend Peepcode’s Meet the command line for a quick jump into the cold blue. The last step would be, using the GEM_HOME environment variable to reference a user directory to store downloaded gems. I like to do this because i can avoid “sudoing” gem install. Referencing the ~/.gem directory might be a good choice, since it is created anyway. Also, add $GEM_HOME/bin to the path to execute the gems/commands from any directory in the file system.

export GEM_HOME=~/.gem
PATH=$GEM_HOME/bin:$PATH

For the RubyMine user’s: just open the application from the shell to make the new paths available to the IDE.

Posted in Rails, Ruby | Leave a comment

Swedish Greys - a WordPress theme from Nordic Themepark.