2

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

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;
    }

调用该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.

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

PS:我尝试访问发布和作者实例并得到相同的异常。

4

4 回答 4

5

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

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

如果您的代码作为请求处理的一部分执行,则您不需要自己创建 Session,您可以通过调用request.getResourceResolver().adaptTo(Session.class)where requestis the SlingHttpServletRequestSling servlet 来获取它。

于 2013-09-05T06:31:34.767 回答
1

这是获取管理会话的准系统服务:

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;
    }
}

您还可以修改它以根据凭据返回会话。但目前尚不清楚您要从什么上下文中获取会话,因此我无法就如何构建您的服务提供任何指示。

于 2013-09-09T05:50:12.187 回答
0

保留 JCR 引用(例如会话或节点)作为捆绑激活的一部分是一个坏主意,因为会话或节点可能会随着时间的推移变得无效 - 我过去不得不调试和修复这个问题。相反,当出于安全原因绝对需要时,您应该使用上述 @Reference 注释和 adaptTo 方法或 loginAdministrative()。

于 2013-12-30T20:48:05.143 回答
0

如果您想与来自 OSGi 服务的 JCR 进行交互,那么使用 OSGi 包就很好。但是 - 您仍然可以从外部 Java 应用程序与其交互。或许您想编写一个 Java 工具来获取 AEM JCR 中的用户并将其转储到电子表格中。

在您上面的示例中 - http://localhost:4503/crx/server可能不是正确的 URL。你可能是http://localhost:4502/crx/server但这有效 - 请参阅: http ://scottsdigitalcommunity.blogspot.ca/2012/03/programmatically-accessing-day-cq.html

一旦完成会话-关闭它: session.save(); session.logout();

于 2015-10-23T16:52:21.837 回答