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 Wed, 15 Dec 2010 09:27: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 (13)</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" >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-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">Jcr</span>ResourceResolverFactory}}</span>
service: <br></td></tr>
            <tr><td class="diff-unchanged" > <br>{code} <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">
   private SlingRepository repository; <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">private
ResourceResolverFactory resolverFactory; <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">
   /** @scr.reference */ <br>    private JcrResourceResolverFactory resolverFactory;
<br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">public
void myMethod() { <br>    try { <br>	String resourcePath = &quot;path/to/resource&quot;;
<br>    	ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
<br>	Resource res = resourceResolver.getResource(resourcePath); <br>	// do something
with the resource <br>	// when done, close the ResourceResolver <br></td></tr>
            <tr><td class="diff-unchanged" >/** @scr.reference */
 <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">
</span> <span class="diff-added-words"style="background-color: #dfd;">resourceResolver.close();</span>
<br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">
   public void myMethod() { <br>        Session adminSession = null; <br>	try
{ <br>	    String resourcePath = &quot;path/to/resource&quot;; <br>  
         adminSession = repository.loginAdministrative(null); <br>	    ResourceResolver
resourceResolver = resolverFactory.getResourceResolver(adminSession); <br>	    Resource
res = resourceResolver.getResource(resourcePath); <br></td></tr>
            <tr><td class="diff-changed-lines" >} catch <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">(RepositoryException</span>
<span class="diff-added-words"style="background-color: #dfd;">(LoginException</span>
e) { <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">
     log.error(&quot;RepositoryException: &quot; + e); <br>        } finally
{ <br>             if (adminSession != null &amp;&amp; adminSession.isLive())
{ <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">
   	// log the error <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">adminSession.logout();</span>
<span class="diff-added-words"style="background-color: #dfd;">}</span> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">
           	 adminSession = null; <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;">
        } <br>     } <br></td></tr>
            <tr><td class="diff-unchanged" >{code} <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 <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>ResourceResolverFactory</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> ResourceResolverFactory resolverFactory;

<span class="code-keyword">public</span> void myMethod() {
    <span class="code-keyword">try</span> {
	<span class="code-object">String</span> resourcePath = <span class="code-quote">"path/to/resource"</span>;
    	ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(<span
class="code-keyword">null</span>);
	Resource res = resourceResolver.getResource(resourcePath);
	<span class="code-comment">// <span class="code-keyword">do</span> something
with the resource
</span>	<span class="code-comment">// when done, close the ResourceResolver
</span>	resourceResolver.close();
    } <span class="code-keyword">catch</span> (LoginException e) {
    	<span class="code-comment">// log the error
</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=6&originalVersion=5">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