Wednesday, July 31, 2013

How to get Resource?

In OSGI Service/Compoment
You can access a resource through the JcrResourceResolverFactory service:

@Reference
private SlingRepository repository;

@Reference
private JcrResourceResolverFactory resolverFactory;

public void myMethod() {
    Session adminSession = null;   
    try {       
         String resourcePath = "path/to/resource";       
         adminSession = repository.loginAdministrative(null);       
         ResourceResolver resourceResolver = resolverFactory.getResourceResolver(adminSession);       
         Resource res = resourceResolver.getResource(resourcePath);    
    } catch (RepositoryException e) {
        log.error("RepositoryException: " + e);   
    } finally {       
        if (adminSession != null && adminSession.isLive()) {
           adminSession.logout();          
                adminSession = null;       
         }    
     }
}
In JSP

<sling:defineObjects>
<%
String resourcePath = "path/to/resource";
Resource res = resourceResolver.getResource(resourcePath);
%>
 

No comments:

Post a Comment