Expand my Community achievements bar.

SOLVED

Access nodes from osgi bundle

Avatar

Level 4

Hi,
I'm trying to access some nodes from the OSGi bundle following this article but I'm getting this error

03.10.2013 07:29:50.290 *ERROR* [10.224.133.98 [1380778190289] POST /services/gallery/getimage HTTP/1.1] my.package.gallery.GetImages Error working with nodes! Unable to access a repository with the following settings: org.apache.jackrabbit.repository.uri: http://localhost:4503/crx/server The following RepositoryFactory classes were consulted: Perhaps the repository you are trying to access is not available at the moment.

and here is part of my code

@Override protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { private Repository repository = null; private Session session = null; try{ repository = JcrUtils.getRepository("http://localhost:4503/crx/server"); session = repository.login(); javax.jcr.Node node = session.getNode("/content/TEST/302104/jcr:content"); log.info("property: "+node.getProperty("jcr:title").getString()); }catch(Exception e){ log.error("Error working with nodes! "+e.getMessage()); } }

I've also tried http://localhost:4502/crx/server as the URI but the result was exactly the same. The 

Thanks for any help

1 Accepted Solution

Avatar

Correct answer by
Level 10

A Note in that article pointed out that the article was not meant for OSGi bundles. It was for non-OSGi Java applications. It referenced the articles to read to use the JCR API in OSGi bundles. 

If you are interested in using the JCR API from an OSGi bundle, read these community articles:

Persisting CQ data in the Java Content Repository

http://helpx.adobe.com/adobe-cq/using/querying-experience-manager-data-using.html

These articles talk about how to use Declarative Services and Maven to inject a ResourceResolverFactory instance into an OSGi bundle. You can then use the JCR API within an OSGi bundle to access nodes. 

The way you specified in the other comment is using SlingHttpServletRequests not the JCR API. 

Hope this Helps! 

View solution in original post

2 Replies

Avatar

Level 4

I solved it. Nodes can be accessed via SlingHttpServletRequest

try{ Resource resource = request.getResourceResolver().getResource("/path/to/node"); log.info("Resource loaded with path: "+resource.getPath()); log.info("and type: "+resource.getResourceType()); javax.jcr.Node node = resource.adaptTo(javax.jcr.Node.class); log.info("Node loaded with path: "+node.getPath()); javax.jcr.NodeIterator iterator = node.getNodes(); log.info("Subnodes: "+iterator.getSize()); }catch(Exception e){ log.error("Error working with nodes! "+e.getMessage()); }

Avatar

Correct answer by
Level 10

A Note in that article pointed out that the article was not meant for OSGi bundles. It was for non-OSGi Java applications. It referenced the articles to read to use the JCR API in OSGi bundles. 

If you are interested in using the JCR API from an OSGi bundle, read these community articles:

Persisting CQ data in the Java Content Repository

http://helpx.adobe.com/adobe-cq/using/querying-experience-manager-data-using.html

These articles talk about how to use Declarative Services and Maven to inject a ResourceResolverFactory instance into an OSGi bundle. You can then use the JCR API within an OSGi bundle to access nodes. 

The way you specified in the other comment is using SlingHttpServletRequests not the JCR API. 

Hope this Helps!