In OSGI Service/Compoment
You can access a resource through the JcrResourceResolverFactory service:
@Referenceprivate SlingRepository repository;
@Referenceprivate 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);
%>