使用JCR以编程方式访问CQ5内容

Kri*_*ter 2 java jcr sling aem

我想以编程方式访问内容,我尝试了以下内容:

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;
    }
Run Code Online (Sandbox Code Playgroud)

当我调用该getRepository方法时,我得到以下异常:

 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.
Run Code Online (Sandbox Code Playgroud)

我有版本cq5.4.任何的想法?

PS:我试图访问发布和作者实例并获得相同的异常.

Ber*_*taz 5

您不需要也不想JcrUtils.getRepository在Sling/CQ应用程序中调用,您应该访问CQ提供的SlingRepository OSGi服务.

最简单的是@Reference在OSGi声明性服务中使用注释@Component- 在Apache Sling代码库中有多个这样的示例,http://svn.apache.org/repos/asf/sling/trunk/samples/slingbucks示例是一个这是一个很好的起点.

如果你的代码执行的请求处理的一部分,你不需要自己创建一个会话,您可以通过调用得到它request.getResourceResolver().adaptTo(Session.class)那里requestSlingHttpServletRequest您在吊带的servlet获得.