Question

I would like to access the content programmatically, I tried the following:

private Session getSession(String user, String passwort) {
        Session session = null;
        try {
            Repository repository = JcrUtils.getRepository("http://localhost:4503/crx/server");
            JcrUtils.getRepository("http://localhost:4503/crx/server");
            session = repository.login(new SimpleCredentials(user, passwort.toCharArray()));
        } catch (RepositoryException e) {
            LOG.error(e.getMessage());
        }
        return session;
    }

I get the following exception when I call the getRepository method:

 javax.jcr.RepositoryException: 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.

I have the version cq5.4. Any idea?

P.S: I tried to Access publish and author instance and got the same exception.

Was it helpful?

Solution

You don't need and don't want to call JcrUtils.getRepository in a Sling/CQ app, you should rather access the SlingRepository OSGi service that CQ provides.

The simplest is to use an @Reference annotation in an OSGi declarative services @Component - there are multiple examples of this in the Apache Sling codebase, the http://svn.apache.org/repos/asf/sling/trunk/samples/slingbucks sample is a good starting point for that.

If your code executes as part of request processing, you don't need to create a Session yourself, you can get it by calling request.getResourceResolver().adaptTo(Session.class) where request is the SlingHttpServletRequest that you get in a Sling servlet.

OTHER TIPS

here's a barebones service to get an admin session:

package your.package;

import org.apache.felix.scr.annotations.*;

import org.apache.sling.jcr.api.SlingRepository;
import javax.jcr.Session;

@Component(immediate=true, enabled=true)
@Service
public class YourServiceForRepository {
    @Reference
    protected SlingRepository repository;

    protected Session session;

    @Activate
    protected void activate() throws Exception {
        session = repository.loginAdministrative(null);
    }

    @Deactivate
    protected void deactivate() throws Exception {
        session.logout();
        session = null;
    }
}

you can also modify this to return a session based on credentials. but it's unclear what context you're trying to get a session from, so i can't give any pointers on how to structure your service.

Holding onto a JCR reference such as a session or a node as part of bundle activation is a bad idea since the session or the node can become invalid over time - I've had to debug and fix this problem in the past. Instead, you should use the @Reference annotation and adaptTo method described above or loginAdministrative() when it's absolutely required for security reasons.

Using an OSGi bundle is fine if you want to interact with a JCR from an OSGi service. However - you can still interact with it from an external Java App. Purhaps you want to write a Java tool to get user within an AEM JCR and dump them to a spreadsheet.

In your above example - http://localhost:4503/crx/server may not have been the proper URL. You may be http://localhost:4502/crx/server. BUt this works - see: http://scottsdigitalcommunity.blogspot.ca/2012/03/programmatically-accessing-day-cq.html

And once done with the session- close it: session.save(); session.logout();

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top