Access nodes from osgi bundle | Community
Skip to main content
Level 3
October 16, 2015
Solved

Access nodes from osgi bundle

  • October 16, 2015
  • 2 replies
  • 1473 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by smacdonald2008

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! 

2 replies

JakolczAuthor
Level 3
October 16, 2015

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()); }
smacdonald2008
smacdonald2008Accepted solution
Level 10
October 16, 2015

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!