incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Sling Website > Discover Sling in 15 minutes
Date Thu, 02 Jun 2011 16:23:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=SLINGxSITE&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/SLINGxSITE/Discover+Sling+in+15+minutes">Discover
Sling in 15 minutes</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~fmeschbe">Felix
Meschberger</a>
    </h4>
        <br/>
                         <h4>Changes (37)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >This page will help you get started
with the Launchpad. Fifteen minutes should be enough to get an overview of what Sling does.
<br> <br></td></tr>
            <tr><td class="diff-changed-lines" >While simple to run and understand,
the Launchpad is a <span class="diff-changed-words">full<span class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">y</span>-featured</span>
instance of Sling, an example configuration that we have created with the most common modules
and configurations. The full functionality of Sling is available by loading additional Sling
(or custom) OSGi <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">modules</span>
<span class="diff-added-words"style="background-color: #dfd;">bundles</span> as
needed, using the Launchpad&#39;s web-based OSGi management console. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>h2. See Also <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >h2. Prerequisites <br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">If
using the self-runnable jar from the Sling distribution, you only need a Java 5 JDK. <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">We&#39;ll
start with the self-runnable jar from the Sling distribution, you only need a Java 5 JDK.
Download the latest release from the Sling [Downloads] page or by clicking this link: [org.apache.sling.launchpad-6-standalone.jar|http://www.apache.org/dyn/closer.cgi/sling/org.apache.sling.launchpad-6-standalone.jar].
Alternatively you can deploy the [Sling Web application|http://www.apache.org/dyn/closer.cgi/sling/org.apache.sling.launchpad-6.war]
into any decent Servlet Container such as Jetty or Tomcat or you can [build the current source
yourself|Getting and Building Sling]. <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">If
using the war file from the Sling distribution, you need a suitable servlet container (all
recent versions of Jetty or Tomcat should work), running under a Java 5 JDK. <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">To
show the simplicity of the REST-style approach taken by Sling the examples below will be using
[cURL|http://curl.haxx.se/]. Any HTTP client would do, but cURL is the easiest to document
in a reproducible way. <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">If
building Sling yourself (which is the best way to get the latest and greatest), you&#39;ll
need: <br>* A Subversion client - to get the Sling code. <br>* A Java 5 JDK. <br>*
[Maven|http://maven.apache.org/] for the build, we currently recommend V 2.0.7 (and see our
[MavenTipsAndTricks]). <br> <br>And in all cases you&#39;ll need [cURL|http://curl.haxx.se/]
to run the examples below. Any HTTP client would do, but cURL is the easiest to document in
a reproducible way. <br> <br></td></tr>
            <tr><td class="diff-unchanged" >A WebDAV client makes editing server-side
scripts much more convenient, but to make our examples easy to reproduce, we&#39;re using
cURL below to create and update files in the JCR repository, via the Sling WebDAV server.
<br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">h2.
Get the Launchpad <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">See
[Getting and Building Sling] \- you can either use a released version (if it is current enough),
or build it yourself. <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h2. Start the Launchpad <br>
<br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">If
using the self-runnable jar from the Sling distribution, start it by double-clicking or with
_java \-jar ..._. <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">After
downloading the Sling Launchpad self-runnable jar just start it as follows: <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">If
using the war file from the Sling distribution, install it in your servlet container and start
that. <br> <br>If you built Sling yourself, change to the _launchpad/builder_
directory under the top-level _sling_ directory, and run <br></td></tr>
            <tr><td class="diff-unchanged" >{noformat} <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">mvn
jetty:run <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">$
java -jar org.apache.sling.launchpad-6-standalone.jar <br></td></tr>
            <tr><td class="diff-unchanged" >{noformat} <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">To
start the launchpad. <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">The
examples below assume that Sling is running on port 8888, which is the default for the _launchpad/builder_
module. If your setup is different you&#39;ll need to adjust the port number accordingly.
<br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">This
starts the Sling embedded Web Server on port 8080 and writes application files into the {{sling}}
folder found in the current working directory. <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-changed-lines" >Once started, look at <span
class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">[http://localhost:8888/system/console/bundles|http://localhost:8888/system/console/bundles]</span>
<span class="diff-added-words"style="background-color: #dfd;">[http://localhost:8080/system/console/bundles|http://localhost:8080/system/console/bundles]</span>
with your browser. Use _admin_ with password _admin_ if Sling asks you for a login. Sling
then displays the _Felix Web Management Console_ page. <br></td></tr>
            <tr><td class="diff-unchanged" > <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br>{tip:title=Log files} <br></td></tr>
            <tr><td class="diff-changed-lines" >If things go wrong, have a look
at the <span class="diff-changed-words">{{<span class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">target/</span>sling/logs/error.log}}</span>
log file <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">under
{{launchpad/builder}}</span> \- that&#39;s where Sling writes any error messages.
<br></td></tr>
            <tr><td class="diff-unchanged" >{tip} <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br>{noformat} <br></td></tr>
            <tr><td class="diff-changed-lines" >curl <span class="diff-added-words"style="background-color:
#dfd;">-u admin:admin</span> -F&quot;sling:resourceType=foo/bar&quot; -F&quot;title=some
title&quot; <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">http://admin:admin@localhost:8888/content/mynode</span>
<span class="diff-added-words"style="background-color: #dfd;">http://localhost:8080/content/mynode</span>
<br></td></tr>
            <tr><td class="diff-unchanged" >{noformat} <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">The
resulting node can be seen at [http://localhost:8888/content/mynode.html|http://localhost:8888/content/mynode.html],
or as json format under [http://localhost:8888/content/mynode.json|http://localhost:8888/content/mynode.json].
<br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">The
resulting node can be seen at [http://localhost:8080/content/mynode.html|http://localhost:8080/content/mynode.html],
or as json format under [http://localhost:8080/content/mynode.json|http://localhost:8080/content/mynode.json].
Lets try with cURL: <br> <br>{noformat} <br>$ curl http://localhost:8080/content/mynode.json
<br>{&quot;title&quot;:&quot;some title&quot;,&quot;sling:resourceType&quot;:&quot;foo/bar&quot;,&quot;jcr:primaryType&quot;:&quot;nt:unstructured&quot;}
<br>{noformat} <br> <br>This returns the properties of the {{/content/mynode}}
in JSON format as we have created it above. The additional property {{jcr:primaryType}} is
a special JCR property indicating the JCR primary node type. <br> <br></td></tr>
            <tr><td class="diff-unchanged" >{tip:title=Monitoring requests} <br>Sling
provides a simple tool (an OSGi console plugin) to monitor HTTP requests, which helps understand
how things work internally. See the [Monitoring Requests] page for details. <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >Sling uses scripts or servlets to
render and process content. <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >Several scripting languages are
available as additional Sling modules (packaged as OSGi _bundles_ that can be installed via
the Sling management console), but the launchpad currently <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">only</span>
includes the ESP (server-side <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">ECMAscript)
and</span> <span class="diff-added-words"style="background-color: #dfd;">ECMAscript),</span>
JSP (Java Server <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">Pages)
languages</span> <span class="diff-added-words"style="background-color: #dfd;">Pages),
and Groovy language</span> modules by default. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>To select a script, Sling
uses the node&#39;s _sling:resourceType_ property, if it is set. <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >* and appends _html.esp_, as the extension
of our URL is _html_ and the language of our script is _esp_. <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >Store this script under _apps/foo/bar/html.esp_,
either using a WebDAV client (connected to <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">[http://admin:admin@localhost:8888/]),</span>
<span class="diff-added-words"style="background-color: #dfd;">[http://admin:admin@localhost:8080/]),</span>
or using cURL as shown here, after creating the _html.esp_ script in the current directory
on your system: <br></td></tr>
            <tr><td class="diff-unchanged" >{noformat} <br></td></tr>
            <tr><td class="diff-changed-lines" >curl -X MKCOL <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">http://admin:admin@localhost:8888/apps/foo</span>
<span class="diff-added-words"style="background-color: #dfd;">http://admin:admin@localhost:8080/apps/foo</span>
<br>curl -X MKCOL <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">http://admin:admin@localhost:8888/apps/foo/bar</span>
<span class="diff-added-words"style="background-color: #dfd;">http://admin:admin@localhost:8080/apps/foo/bar</span>
<br></td></tr>
            <tr><td class="diff-unchanged" >{noformat} <br>create a local
file _html.esp_ and copy above content. <br>{noformat} <br></td></tr>
            <tr><td class="diff-changed-lines" >curl -T html.esp <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">http://admin:admin@localhost:8888/apps/foo/bar/html.esp</span>
<span class="diff-added-words"style="background-color: #dfd;">http://admin:admin@localhost:8080/apps/foo/bar/html.esp</span>
<br></td></tr>
            <tr><td class="diff-unchanged" >{noformat} <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >The HTML rendering of your node,
at <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">[http://localhost:8888/content/mynode.html],</span>
<span class="diff-added-words"style="background-color: #dfd;">[http://localhost:8080/content/mynode.html],</span>
is now created by this ESP script. You should see the node&#39;s title alone as an &lt;h1&gt;
element in that page. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>A script named _POST.esp_
instead of _html.esp_ would be called for a POST request, _DELETE.esp_ for DELETE, _xml.esp_
for a GET request with a _.xml_ extension, etc. See [URL to Script Resolution|http://cwiki.apache.org/SLING/url-to-script-resolution.html]
on the Sling wiki for more info. <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >Start by creating a new _/blog_ folder:
<br>{noformat} <br></td></tr>
            <tr><td class="diff-changed-lines" >curl -X POST <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">&quot;http://admin:admin@localhost:8888/content/blog&quot;</span>
<span class="diff-added-words"style="background-color: #dfd;">&quot;http://admin:admin@localhost:8080/content/blog&quot;</span>
<br></td></tr>
            <tr><td class="diff-unchanged" >{noformat} <br>And create a
node with a Sling-generated name under it: <br>{noformat} <br></td></tr>
            <tr><td class="diff-changed-lines" >curl -D - -F&quot;title=Adventures
with Sling&quot; <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">&quot;http://admin:admin@localhost:8888/content/blog/*&quot;</span>
<span class="diff-added-words"style="background-color: #dfd;">&quot;http://admin:admin@localhost:8080/content/blog/*&quot;</span>
<br></td></tr>
            <tr><td class="diff-unchanged" >{noformat} <br>Using cURL&#39;s
_\-D_ option shows the full HTTP response, which includes a _Location_ header to indicate
where the new node was created: <br>{noformat} <br></td></tr>
            <tr><td class="diff-changed-lines" >Location: <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">http://localhost:8888/content/blog/adventures_with_slin_0</span>
<span class="diff-added-words"style="background-color: #dfd;">http://localhost:8080/content/blog/adventures_with_slin_0</span>
<br></td></tr>
            <tr><td class="diff-unchanged" >{noformat} <br>The actual node
name might not be _adventures_with_slin_0_ \- depending on existing content in your repository,
Sling will find a unique name for this new node, based on several well-know property values
like title, description, etc. which are used for this if provided. <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >So, in our case, our new node
can be displayed in HTML via the <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">[http://localhost:8888/content/blog/adventures_with_slin_0.html]</span>
<span class="diff-added-words"style="background-color: #dfd;">[http://localhost:8080/content/blog/adventures_with_slin_0.html]</span>
URL. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>Note that we didn&#39;t
set a _sling:resourceType_ property on our node, so if you want to render that node with a
script, you&#39;ll have to store the script under _/apps/nt/unstructured/html.esp_. <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >In this example, we create a node
at _/content/header_, rendered with a logo using an _html.esp_ script, then use that header
at the top of the _html.esp_ script that we created previously for the _foo/bar_ resource
type. <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >Start by checking that <span
class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">[http://localhost:8888/content/mynode.html]</span>
<span class="diff-added-words"style="background-color: #dfd;">[http://localhost:8080/content/mynode.html]</span>
is rendered using the _html.esp_ script created above. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>Create this script and
name it _header.esp_: <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >Upload it so that it is used to render
resources having _sling:resourceType=foo/header_: <br>{noformat} <br></td></tr>
            <tr><td class="diff-changed-lines" >curl -X MKCOL  <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">http://admin:admin@localhost:8888/apps/foo/header/</span>
<span class="diff-added-words"style="background-color: #dfd;">http://admin:admin@localhost:8080/apps/foo/header/</span>
<br>curl -T header.esp <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">http://admin:admin@localhost:8888/apps/foo/header/html.esp</span>
<span class="diff-added-words"style="background-color: #dfd;">http://admin:admin@localhost:8080/apps/foo/header/html.esp</span>
<br></td></tr>
            <tr><td class="diff-unchanged" >{noformat} <br>Create the header
node: <br>{noformat} <br></td></tr>
            <tr><td class="diff-changed-lines" >curl -F&quot;sling:resourceType=foo/header&quot;
-F&quot;headline=Hello, Sling world&quot; <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">http://admin:admin@localhost:8888/content/header</span>
<span class="diff-added-words"style="background-color: #dfd;">http://admin:admin@localhost:8080/content/header</span>
<br></td></tr>
            <tr><td class="diff-unchanged" >{noformat} <br>Upload the logo
that the script uses (using sling.jpg or another logo in the current directory): <br>{noformat}
<br></td></tr>
            <tr><td class="diff-changed-lines" >curl -X MKCOL  <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">http://admin:admin@localhost:8888/images/</span>
<span class="diff-added-words"style="background-color: #dfd;">http://admin:admin@localhost:8080/images/</span>
<br>curl -T sling.jpg  <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">http://admin:admin@localhost:8888/images/sling.jpg</span>
<span class="diff-added-words"style="background-color: #dfd;">http://admin:admin@localhost:8080/images/sling.jpg</span>
<br></td></tr>
            <tr><td class="diff-unchanged" >{noformat} <br></td></tr>
            <tr><td class="diff-changed-lines" >And check that the header is rendered
with the logo at <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">[http://localhost:8888/content/header.html].</span>
<span class="diff-added-words"style="background-color: #dfd;">[http://localhost:8080/content/header.html].</span>
<br></td></tr>
            <tr><td class="diff-unchanged" > <br>Now, update the html.esp
script that we created for our first example above, to include the header: <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >And upload it again to replace the
previous version: <br>{noformat} <br></td></tr>
            <tr><td class="diff-changed-lines" >curl -T html.esp <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">http://admin:admin@localhost:8888/apps/foo/bar/html.esp</span>
<span class="diff-added-words"style="background-color: #dfd;">http://admin:admin@localhost:8080/apps/foo/bar/html.esp</span>
<br></td></tr>
            <tr><td class="diff-unchanged" >{noformat} <br></td></tr>
            <tr><td class="diff-changed-lines" >The <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">[http://localhost:8888/content/mynode.html],</span>
<span class="diff-added-words"style="background-color: #dfd;">[http://localhost:8080/content/mynode.html],</span>
once refreshed, now shows the blue headline and logo, and this layout also applies to any
node created with _sling:resourceType=foo/bar_. <br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h1><a name="DiscoverSlingin15minutes-DiscoverSlingin15minutestheSlingLaunchpad"></a>Discover
Sling in 15 minutes - the Sling Launchpad</h1>

<p>The Sling Launchpad is a ready-to-run Sling configuration, providing an embedded
JCR content repository and web server, a selection of Sling components, documentation and
examples. The Launchpad makes it easy to get started with Sling and to develop script-based
applications.</p>

<p>This page will help you get started with the Launchpad. Fifteen minutes should be
enough to get an overview of what Sling does.</p>

<p>While simple to run and understand, the Launchpad is a full-featured instance of
Sling, an example configuration that we have created with the most common modules and configurations.
The full functionality of Sling is available by loading additional Sling (or custom) OSGi
bundles as needed, using the Launchpad's web-based OSGi management console.</p>

<h2><a name="DiscoverSlingin15minutes-SeeAlso"></a>See Also</h2>


<p>Example applications and mini-applications for Sling can be found under <a href="http://svn.apache.org/repos/asf/sling/trunk/samples/"
class="external-link" rel="nofollow">http://svn.apache.org/repos/asf/sling/trunk/samples/</a>
(each application has a <tt>README.txt</tt> file, see these for more details).</p>

<p>Once you grok the basic examples of this page, we recommend studying the <em>espblog</em>
and <em>webloader</em> samples for more complete examples. The <em>javashell</em>
sample is useful to play with JCR java code (or any java code, for that matter) interactively.</p>

<h2><a name="DiscoverSlingin15minutes-Prerequisites"></a>Prerequisites</h2>

<p>We'll start with the self-runnable jar from the Sling distribution, you only need
a Java 5 JDK. Download the latest release from the Sling <a href="/confluence/display/SLINGxSITE/Downloads"
title="Downloads">Downloads</a> page or by clicking this link: <a href="http://www.apache.org/dyn/closer.cgi/sling/org.apache.sling.launchpad-6-standalone.jar"
class="external-link" rel="nofollow">org.apache.sling.launchpad-6-standalone.jar</a>.
Alternatively you can deploy the <a href="http://www.apache.org/dyn/closer.cgi/sling/org.apache.sling.launchpad-6.war"
class="external-link" rel="nofollow">Sling Web application</a> into any decent Servlet
Container such as Jetty or Tomcat or you can <a href="/confluence/display/SLINGxSITE/Getting+and+Building+Sling"
title="Getting and Building Sling">build the current source yourself</a>.</p>

<p>To show the simplicity of the REST-style approach taken by Sling the examples below
will be using <a href="http://curl.haxx.se/" class="external-link" rel="nofollow">cURL</a>.
Any HTTP client would do, but cURL is the easiest to document in a reproducible way.</p>

<p>A WebDAV client makes editing server-side scripts much more convenient, but to make
our examples easy to reproduce, we're using cURL below to create and update files in the JCR
repository, via the Sling WebDAV server.</p>


<h2><a name="DiscoverSlingin15minutes-StarttheLaunchpad"></a>Start the Launchpad</h2>

<p>After downloading the Sling Launchpad self-runnable jar just start it as follows:</p>

<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>$ java -jar org.apache.sling.launchpad-6-standalone.jar
</pre>
</div></div>

<p>This starts the Sling embedded Web Server on port 8080 and writes application files
into the <tt>sling</tt> folder found in the current working directory.</p>

<p>Once started, look at <a href="http://localhost:8080/system/console/bundles" class="external-link"
rel="nofollow">http://localhost:8080/system/console/bundles</a> with your browser.
Use <em>admin</em> with password <em>admin</em> if Sling asks you
for a login. Sling then displays the <em>Felix Web Management Console</em> page.</p>


<p>On the bundles page, all bundles should be marked <em>Active</em>. They're
all <a href="http://www.osgi.org/" class="external-link" rel="nofollow">OSGi</a>
bundles powered by <a href="http://felix.apache.org" class="external-link" rel="nofollow">Apache
Felix</a>, but that doesn't really matter to us right now.</p>

<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><b>Log files</b><br
/>If things go wrong, have a look at the <tt>sling/logs/error.log</tt> log
file &#45; that's where Sling writes any error messages.</td></tr></table></div>

<h2><a name="DiscoverSlingin15minutes-Createsomecontent"></a>Create some
content</h2>

<p>Until we have ready-to-test forms, you can create content with cURL, or you can create
an HTML form that posts to the specified URL.</p>

<p>To create a content node (nodes are a <a href="http://jackrabbit.apache.org/"
class="external-link" rel="nofollow">JCR</a> concept, a unit of storage) with cURL,
use:</p>

<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>curl -u admin:admin -F"sling:resourceType=foo/bar" -F"title=some title" http://localhost:8080/content/mynode
</pre>
</div></div>

<p>The resulting node can be seen at <a href="http://localhost:8080/content/mynode.html"
class="external-link" rel="nofollow">http://localhost:8080/content/mynode.html</a>,
or as json format under <a href="http://localhost:8080/content/mynode.json" class="external-link"
rel="nofollow">http://localhost:8080/content/mynode.json</a>. Lets try with cURL:</p>

<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>$ curl http://localhost:8080/content/mynode.json
{"title":"some title","sling:resourceType":"foo/bar","jcr:primaryType":"nt:unstructured"}
</pre>
</div></div>

<p>This returns the properties of the <tt>/content/mynode</tt> in JSON format
as we have created it above. The additional property <tt>jcr:primaryType</tt>
is a special JCR property indicating the JCR primary node type.</p>

<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><b>Monitoring requests</b><br
/>Sling provides a simple tool (an OSGi console plugin) to monitor HTTP requests, which
helps understand how things work internally. See the <a href="/confluence/display/SLINGxSITE/Monitoring+Requests"
title="Monitoring Requests">Monitoring Requests</a> page for details.</td></tr></table></div>

<h2><a name="DiscoverSlingin15minutes-Renderyourcontentusingserversidejavascript%28ESP%29"></a>Render
your content using server-side javascript (ESP)</h2>

<p>Sling uses scripts or servlets to render and process content.</p>

<p>Several scripting languages are available as additional Sling modules (packaged as
OSGi <em>bundles</em> that can be installed via the Sling management console),
but the launchpad currently includes the ESP (server-side ECMAscript), JSP (Java Server Pages),
and Groovy language modules by default.</p>

<p>To select a script, Sling uses the node's <em>sling:resourceType</em>
property, if it is set.</p>

<p>That is the case in our example, so the following script will be used by Sling to
render the node in HTML, if the script is found at <em>/apps/foo/bar/html.esp</em>
in the repository.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>html.esp</b></div><div class="codeContent
panelContent">
<pre class="code-java">
&lt;html&gt;   &lt;body&gt;     &lt;h1&gt;&lt;%= currentNode.title
%&gt;&lt;/h1&gt;   &lt;/body&gt; &lt;/html&gt;
</pre>
</div></div>
<p>To select the script, Sling:</p>
<ul>
	<li>looks under <em>/apps</em></li>
	<li>and appends the <em>sling:resourceType</em> value of our node ( which
is <em>foo/bar</em> )</li>
	<li>and appends <em>html.esp</em>, as the extension of our URL is <em>html</em>
and the language of our script is <em>esp</em>.</li>
</ul>


<p>Store this script under <em>apps/foo/bar/html.esp</em>, either using
a WebDAV client (connected to <a href="http://admin:admin@localhost:8080/" class="external-link"
rel="nofollow">http://admin:admin@localhost:8080/</a>), or using cURL as shown here,
after creating the <em>html.esp</em> script in the current directory on your system:</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>curl -X MKCOL http://admin:admin@localhost:8080/apps/foo
curl -X MKCOL http://admin:admin@localhost:8080/apps/foo/bar
</pre>
</div></div>
<p>create a local file <em>html.esp</em> and copy above content.</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>curl -T html.esp http://admin:admin@localhost:8080/apps/foo/bar/html.esp
</pre>
</div></div>

<p>The HTML rendering of your node, at <a href="http://localhost:8080/content/mynode.html"
class="external-link" rel="nofollow">http://localhost:8080/content/mynode.html</a>,
is now created by this ESP script. You should see the node's title alone as an &lt;h1&gt;
element in that page.</p>

<p>A script named <em>POST.esp</em> instead of <em>html.esp</em>
would be called for a POST request, <em>DELETE.esp</em> for DELETE, <em>xml.esp</em>
for a GET request with a <em>.xml</em> extension, etc. See <a href="http://cwiki.apache.org/SLING/url-to-script-resolution.html"
class="external-link" rel="nofollow">URL to Script Resolution</a> on the Sling wiki
for more info.</p>

<p>Servlets can also be easily "wired" to handle specific resource types, extensions,
etc., in the simplest case by using SCR annotations in the servlet source code. Servlets and
scripts are interchangeable when it comes to processing Sling requests.</p>

<h2><a name="DiscoverSlingin15minutes-Whatnext%3F"></a>What next?</h2>

<p>These simple examples show how Sling uses scripts to work with JCR data, based on
<em>sling:resourceType</em> or node types.</p>

<p>There's much more to Sling of course - you'll find some additional simple examples
below, and above in the <em>see also</em> section.</p>

<p>We are working on debugging features to help trace the way Sling processes requests.
Have a look at <a href="https://issues.apache.org/jira/browse/SLING-3" class="external-link"
rel="nofollow">SLING-3</a> to see what's possible already.</p>

<h1><a name="DiscoverSlingin15minutes-Additionalexamples"></a>Additional
examples</h1>

<h2><a name="DiscoverSlingin15minutes-LetSlinggeneratethepathofanewlycreatednode."></a>Let
Sling generate the path of a newly created node.</h2>

<p>To create a node with a unique path at a given location, end the URL of the POST
request with <em>/&#42;</em>.</p>

<p>In this case, the Sling response redirects to the URL of the created node.</p>

<p>Start by creating a new <em>/blog</em> folder:</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>curl -X POST "http://admin:admin@localhost:8080/content/blog"
</pre>
</div></div>
<p>And create a node with a Sling-generated name under it:</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>curl -D - -F"title=Adventures with Sling" "http://admin:admin@localhost:8080/content/blog/*"
</pre>
</div></div>
<p>Using cURL's <em>&#45;D</em> option shows the full HTTP response,
which includes a <em>Location</em> header to indicate where the new node was created:</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>Location: http://localhost:8080/content/blog/adventures_with_slin_0
</pre>
</div></div>
<p>The actual node name might not be <em>adventures_with_slin_0</em> &#45;
depending on existing content in your repository, Sling will find a unique name for this new
node, based on several well-know property values like title, description, etc. which are used
for this if provided.</p>

<p>So, in our case, our new node can be displayed in HTML via the <a href="http://localhost:8080/content/blog/adventures_with_slin_0.html"
class="external-link" rel="nofollow">http://localhost:8080/content/blog/adventures_with_slin_0.html</a>
URL.</p>

<p>Note that we didn't set a <em>sling:resourceType</em> property on our
node, so if you want to render that node with a script, you'll have to store the script under
<em>/apps/nt/unstructured/html.esp</em>.</p>

<h2><a name="DiscoverSlingin15minutes-Addapageheaderwithsling.include"></a>Add
a page header with sling.include</h2>

<p>The <em>sling.include</em> function can be called from scripts to include
the rendered result of another node.</p>

<p>In this example, we create a node at <em>/content/header</em>, rendered
with a logo using an <em>html.esp</em> script, then use that header at the top
of the <em>html.esp</em> script that we created previously for the <em>foo/bar</em>
resource type.</p>

<p>Start by checking that <a href="http://localhost:8080/content/mynode.html" class="external-link"
rel="nofollow">http://localhost:8080/content/mynode.html</a> is rendered using the
<em>html.esp</em> script created above.</p>

<p>Create this script and name it <em>header.esp</em>:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>header.esp</b></div><div class="codeContent
panelContent">
<pre class="code-java">
&lt;div&gt;   &lt;p style=<span class="code-quote">"color:blue;"</span>&gt;
    &lt;img src=<span class="code-quote">"/images/sling.jpg"</span> align=<span
class="code-quote">"right"</span>/&gt;     &lt;%= currentNode.headline %&gt;
  &lt;/p&gt; &lt;/div&gt;
</pre>
</div></div>
<p>Upload it so that it is used to render resources having <em>sling:resourceType=foo/header</em>:</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>curl -X MKCOL  http://admin:admin@localhost:8080/apps/foo/header/
curl -T header.esp http://admin:admin@localhost:8080/apps/foo/header/html.esp
</pre>
</div></div>
<p>Create the header node:</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>curl -F"sling:resourceType=foo/header" -F"headline=Hello, Sling world" http://admin:admin@localhost:8080/content/header
</pre>
</div></div>
<p>Upload the logo that the script uses (using sling.jpg or another logo in the current
directory):</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>curl -X MKCOL  http://admin:admin@localhost:8080/images/
curl -T sling.jpg  http://admin:admin@localhost:8080/images/sling.jpg
</pre>
</div></div>
<p>And check that the header is rendered with the logo at <a href="http://localhost:8080/content/header.html"
class="external-link" rel="nofollow">http://localhost:8080/content/header.html</a>.</p>

<p>Now, update the html.esp script that we created for our first example above, to include
the header:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>html.esp</b></div><div class="codeContent
panelContent">
<pre class="code-java">
&lt;html&gt;   &lt;body&gt;     &lt;div id=<span class="code-quote">"header"</span>&gt;
      &lt;% sling.include(<span class="code-quote">"/content/header"</span>);
%&gt;     &lt;/div&gt;     &lt;h1&gt;&lt;%= currentNode.title %&gt;&lt;/h1&gt;
  &lt;/body&gt; &lt;/html&gt;
</pre>
</div></div>
<p>And upload it again to replace the previous version:</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>curl -T html.esp http://admin:admin@localhost:8080/apps/foo/bar/html.esp
</pre>
</div></div>
<p>The <a href="http://localhost:8080/content/mynode.html" class="external-link"
rel="nofollow">http://localhost:8080/content/mynode.html</a>, once refreshed, now
shows the blue headline and logo, and this layout also applies to any node created with <em>sling:resourceType=foo/bar</em>.</p>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/SLINGxSITE/Discover+Sling+in+15+minutes">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=76179&revisedVersion=32&originalVersion=31">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/SLINGxSITE/Discover+Sling+in+15+minutes?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message