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:38: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 (2)</h4>
                                 
    
<div id="page-diffs">
            <table class="diff" cellpadding="0" cellspacing="0">
            <tr><td class="diff-added-lines" style="background-color: #dfd;">h1.
Getting Resources and Properties in Sling <br> <br></td></tr>
            <tr><td class="diff-unchanged" >The Resource is one of the central
parts of Sling. Extending from JCR&#39;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. <br> <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" >For a list of all the objects
available through this tag, please refer to the <span class="diff-changed-words">[<span
class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">http://dev.day.com/docs/en/cq/current/howto/taglib.html#%3Csling:defineObjects%3E|</span>JSP</span>
Tag <span class="diff-changed-words">Libraries<span class="diff-added-chars"style="background-color:
#dfd;"> | http://dev.day.com/docs/en/cq/current/howto/taglib.html#%3Csling:defineObjects%3E</span>]</span>
page. <br></td></tr>
            <tr><td class="diff-unchanged" > <br> <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 ResourceResolver and then getting the
Resource itself.</p>

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

<p>You can access a resource through the JcrResourceResolverFactory 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 SlingHttpServletRequest:</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 ResourceResolver:</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 &lt;sling:defineObjects&gt; tag in a JSP file, you have
access to a few handy objects, one of them is 'resource', the resource that is resolved from
the URL. Another one is 'resourceResolver', the ResourceResolver defined through the SlingHttpServletRequest.
</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>For a list of all the objects available through this tag, please refer to the <a
href="http://dev.day.com/docs/en/cq/current/howto/taglib.html#%3Csling:defineObjects%3E" class="external-link"
rel="nofollow">JSP Tag Libraries </a> page.</p>


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

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

<p>The ValueMap is an easy way to access properties of a resource. With most resources
you can use Adaptable.adaptTo(Class) 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 ResourceUtil 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 'propName':</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="/confluence/pages/createpage.action?spaceKey=SLINGxSITE&amp;title=Sling+documentation+about+Resources&amp;linkCreation=true&amp;fromPageId=24189099"
class="createlink">http://sling.apache.org/site/resources.html</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=2&originalVersion=1">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