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 > Getting Resources and Properties in Sling
Date Tue, 07 Dec 2010 08:47:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1810/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/Getting+Resources+and+Properties+in+Sling">Getting
Resources and Properties in Sling</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~jck">Jean-Christophe
KAUTZMANN</a>
    </h4>
        <br/>
                         <h4>Changes (9)</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" >In this article we will explore a
few ways to programmatically map a resource path (String) to a resource object (Resource)
and its properties in Sling, from within an OSGI service, a servlet and a JSP. <br>
<br></td></tr>
            <tr><td class="diff-changed-lines" >The whole game consists in first
getting a <span class="diff-changed-words"><span class="diff-added-chars"style="background-color:
#dfd;">{{</span>ResourceResolver<span class="diff-added-chars"style="background-color:
#dfd;">}}</span></span> and then getting the <span class="diff-changed-words"><span
class="diff-added-chars"style="background-color: #dfd;">{{</span>Resource<span
class="diff-added-chars"style="background-color: #dfd;">}}</span></span> itself.
<br></td></tr>
            <tr><td class="diff-unchanged" > <br>h2. Within an OSGI Service/Compoment
 <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >You can access a resource through
the <span class="diff-changed-words"><span class="diff-added-chars"style="background-color:
#dfd;">{{</span>JcrResourceResolverFactory<span class="diff-added-chars"style="background-color:
#dfd;">}}</span></span> service: <br></td></tr>
            <tr><td class="diff-unchanged" > <br>{code} <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >h2. Within a Servlet  <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >You can access the resource defined
by the request URL through the <span class="diff-changed-words"><span class="diff-added-chars"style="background-color:
#dfd;">{{</span>SlingHttpServletRequest<span class="diff-added-chars"style="background-color:
#dfd;">}}</span>:</span> <br></td></tr>
            <tr><td class="diff-unchanged" > <br>{code} <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{code} <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >You can access any resource by
first accessing the <span class="diff-changed-words"><span class="diff-added-chars"style="background-color:
#dfd;">{{</span>ResourceResolver<span class="diff-added-chars"style="background-color:
#dfd;">}}</span>:</span> <br></td></tr>
            <tr><td class="diff-unchanged" > <br>{code} <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >h2. Within a JSP file  <br>
<br></td></tr>
            <tr><td class="diff-changed-lines" >When you use the <span class="diff-changed-words"><span
class="diff-added-chars"style="background-color: #dfd;">{{</span>&lt;sling:defineObjects&gt;<span
class="diff-added-chars"style="background-color: #dfd;">}}</span></span> tag
in a JSP file, you have access to a few handy objects, one of them is <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">&#39;resource&#39;,</span>
<span class="diff-added-words"style="background-color: #dfd;">{{resource}},</span>
the resource that is resolved from the URL. Another one is <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">&#39;resourceResolver&#39;,</span>
<span class="diff-added-words"style="background-color: #dfd;">{{resourceResolver}},</span>
the <span class="diff-changed-words"><span class="diff-added-chars"style="background-color:
#dfd;">{{</span>ResourceResolver<span class="diff-added-chars"style="background-color:
#dfd;">}}</span></span> defined through the <span class="diff-changed-words"><span
class="diff-added-chars"style="background-color: #dfd;">{{</span>SlingHttpServletRequest<span
class="diff-added-chars"style="background-color: #dfd;">}}</span>.</span> <br></td></tr>
            <tr><td class="diff-unchanged" > <br>To access a resource: <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{code} <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >Note: <span class="diff-changed-words"><span
class="diff-added-chars"style="background-color: #dfd;">{{</span>resource.adaptTo(Node.class)<span
class="diff-added-chars"style="background-color: #dfd;">}}</span></span> may
return null if the resource is not backed by a JCR node. This is particularly the case for
<span class="diff-changed-words"><span class="diff-added-chars"style="background-color:
#dfd;">{{</span>NonExistingResource<span class="diff-added-chars"style="background-color:
#dfd;">}}</span></span> resources or resource provided by a non-JCR resource
provider. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>h2. Accessing a Property
 <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >The <span class="diff-changed-words"><span
class="diff-added-chars"style="background-color: #dfd;">{{</span>ValueMap<span
class="diff-added-chars"style="background-color: #dfd;">}}</span></span> is
an easy way to access properties of a resource. With most resources you can use <span class="diff-changed-words"><span
class="diff-added-chars"style="background-color: #dfd;">{{</span>Adaptable.adaptTo(Class)<span
class="diff-added-chars"style="background-color: #dfd;">}}</span></span> to
adapt the resource to a value map: <br></td></tr>
            <tr><td class="diff-unchanged" > <br>{code} <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{code} <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >You can also access the properties
through the <span class="diff-changed-words"><span class="diff-added-chars"style="background-color:
#dfd;">{{</span>ResourceUtil<span class="diff-added-chars"style="background-color:
#dfd;">}}</span></span> utility class: <br></td></tr>
            <tr><td class="diff-unchanged" > <br>{code} <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{code} <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >Then, to access a specific String
property called <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">&#39;propName&#39;:</span>
<span class="diff-added-words"style="background-color: #dfd;">{{propName}}:</span>
<br></td></tr>
            <tr><td class="diff-unchanged" > <br>{code} <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
        </table>
</div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h1><a name="GettingResourcesandPropertiesinSling-GettingResourcesandPropertiesinSling"></a>Getting
Resources and Properties in Sling</h1>

<p>The Resource is one of the central parts of Sling. Extending from JCR's Everything
is Content, Sling assumes Everthing is a Resource. Thus Sling is maintaining a virtual tree
of resources, which is a merger of the actual contents in the JCR Repository and resources
provided by so called resource providers. By doing this Sling fits very well in the paradigm
of the REST architecture.</p>

<p>In this article we will explore a few ways to programmatically map a resource path
(String) to a resource object (Resource) and its properties in Sling, from within an OSGI
service, a servlet and a JSP.</p>

<p>The whole game consists in first getting a <tt>ResourceResolver</tt>
and then getting the <tt>Resource</tt> itself.</p>

<h2><a name="GettingResourcesandPropertiesinSling-WithinanOSGIService%2FCompoment"></a>Within
an OSGI Service/Compoment </h2>

<p>You can access a resource through the <tt>JcrResourceResolverFactory</tt>
service:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
    /** @scr.reference */
    <span class="code-keyword">private</span> SlingRepository repository;
    
    /** @scr.reference */
    <span class="code-keyword">private</span> JcrResourceResolverFactory resolverFactory;
    
    <span class="code-keyword">public</span> void myMethod() {
        Session adminSession = <span class="code-keyword">null</span>;
	<span class="code-keyword">try</span> {
	    <span class="code-object">String</span> resourcePath = <span class="code-quote">"path/to/resource"</span>;
            adminSession = repository.loginAdministrative(<span class="code-keyword">null</span>);
	    ResourceResolver resourceResolver = resolverFactory.getResourceResolver(adminSession);
	    Resource res = resourceResolver.getResource(resourcePath);
	} <span class="code-keyword">catch</span> (RepositoryException e) {
	     log.error(<span class="code-quote">"RepositoryException: "</span> + e);
        } <span class="code-keyword">finally</span> {
             <span class="code-keyword">if</span> (adminSession != <span class="code-keyword">null</span>
&amp;&amp; adminSession.isLive()) {
              	 adminSession.logout();
            	 adminSession = <span class="code-keyword">null</span>;
             }
         }
     }
</pre>
</div></div>


<h2><a name="GettingResourcesandPropertiesinSling-WithinaServlet"></a>Within
a Servlet </h2>

<p>You can access the resource defined by the request URL through the <tt>SlingHttpServletRequest</tt>:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
Resource res = req.getResource();
<span class="code-comment">// req is the SlingHttpServletRequest</span>
</pre>
</div></div>

<p>You can access any resource by first accessing the <tt>ResourceResolver</tt>:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-object">String</span> resourcePath = <span class="code-quote">"path/to/resource"</span>;
ResourceResolver resourceResolver = req.getResourceResolver();
<span class="code-comment">// req is the SlingHttpServletRequest
</span>Resource res = resourceResolver.getResource(resourcePath);
</pre>
</div></div>

<h2><a name="GettingResourcesandPropertiesinSling-WithinaJSPfile"></a>Within
a JSP file </h2>

<p>When you use the <tt>&lt;sling:defineObjects&gt;</tt> tag in
a JSP file, you have access to a few handy objects, one of them is <tt>resource</tt>,
the resource that is resolved from the URL. Another one is <tt>resourceResolver</tt>,
the <tt>ResourceResolver</tt> defined through the <tt>SlingHttpServletRequest</tt>.
</p>

<p>To access a resource:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&lt;sling:defineObjects&gt;
&lt;%
<span class="code-object">String</span> resourcePath = <span class="code-quote">"path/to/resource"</span>;
Resource res = resourceResolver.getResource(resourcePath);
%&gt;
</pre>
</div></div>

<p>If needed you can adapt a Sling Resource to a JCR Node:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
Node node = resource.adaptTo(Node.class);
</pre>
</div></div>

<p>Note: <tt>resource.adaptTo(Node.class)</tt> may return null if the resource
is not backed by a JCR node. This is particularly the case for <tt>NonExistingResource</tt>
resources or resource provided by a non-JCR resource provider.</p>

<h2><a name="GettingResourcesandPropertiesinSling-AccessingaProperty"></a>Accessing
a Property </h2>

<p>The <tt>ValueMap</tt> is an easy way to access properties of a resource.
With most resources you can use <tt>Adaptable.adaptTo(Class)</tt> to adapt the
resource to a value map:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
ValueMap properties = res.adaptTo(ValueMap.class);
<span class="code-comment">// res is the Resource</span>
</pre>
</div></div>

<p>You can also access the properties through the <tt>ResourceUtil</tt>
utility class:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
ValueMap properties = ResourceUtil.getValueMap(res);
<span class="code-comment">// res is the Resource</span>
</pre>
</div></div>

<p>Then, to access a specific String property called <tt>propName</tt>:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-object">String</span> rule = properties.get(propName, (<span
class="code-object">String</span>) <span class="code-keyword">null</span>);
</pre>
</div></div>

<p>For more details about resources and how to access them in Sling, you can refer to
the <a href="http://sling.apache.org/site/resources.html" class="external-link" rel="nofollow">Sling
documentation about Resources</a>.</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/Getting+Resources+and+Properties+in+Sling">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=24189099&revisedVersion=5&originalVersion=4">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/SLINGxSITE/Getting+Resources+and+Properties+in+Sling?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message