a RESTful webserver should think in resources, these are the basic elements of information accessible

a resource can also have different representations (html, pdf, atom, ...)

and the URL is an important identifier, giving bookmarkability of resources/representations, and modeling many things in a standardized way: hierarchy of resources, request parameters, etc.

now sling:
the URL / resource hierarchy is implemented by using JCR, a tree structured store

sling only maps from the URL to JCR (aka it finds the resource first) and then finds out which representation is needed (also part of the URL or maybe the HTTP headers) (to be exact, representation is for GET requests, for PUT/POST we need a "handler" or resourceType)

the handlers are valid for a specific resource type, ie. resources in jcr have the attribute "sling:resourceType", which points back to the handlers, ie. the scripts that can handle the resource in question

each handler is typically a directory in JCR itself with scripts for the various representations and the PUT, POST methods

those representations or handlers are implemented by scripts (quick and cool) or plain old Java servlets (IDE support + managed code)

(scripts can be in many popular languages (javascript, ruby, python, etc.) - sling as it is written in Java uses the Java Scripting framework (JSR-223))

handle.selector.extension...

to repeat: what happens when a HTTP request comes in:
1) sling finds the resource in the JCR (or some other place)
2) depending on HTTP method

for a GET:
GET.1) sling determines the representation wanted and finds the handler (script/servlet)
GET.2) this is done by looking at sling:resourceType (an attachement to the JCR node) and calling "resourceType/<extension>.js" (.js could be .py, .rb, whatever scripting language is available)
GET.2) handler runs with full access to its resource (JCR node)

for a PUT:
PUT.1) sling looks for a PUT.js in the resourceType script folder
PUT.2) or sling does the upload automatically

for a POST:
POST.1) sling looks for a POST.js in the resourceType script folder
POST.2) or sling creates JCR nodes/properties based on a simple request parameter mapping

3) response is sent back, done!

  • No labels

3 Comments

  1. (Considering this is work in progress I just comment for now)

    • This is not entirely correct as the resource type is also used to resolve the script (or servlet) to be called in the case of a GET request. So GET.2 should rather be "resourceType/<extension>.js"
    • In addition the resource type is taken to be the primary node type name if not sling:resourceType property is set.
    • The extension .js is not required. The important part is the base name of the script without the extension, provided the extension maps to any scripting language available. Soe this may be PUT.jsp and POST.js or html.vm or something.py.
  2. Thanks, Felix, I updated the text. But feel free to modify it directly, I don't have any more input at the moment, so the experts are invited to fine-tune it. I will try to overlook the goal of a short-introduction style without too much detail information for the beginner.