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

标签 java aem jcr sling

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

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。任何想法?

附注:我尝试访问发布和作者实例,但遇到了同样的异常。

最佳答案

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

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

如果代码是作为请求处理的一部分执行的,则无需自己创建Session,可以通过调用request.getResourceResolver().adaptTo(Session.class)来获得它,其中request是您在Sling servlet中获得的SlingHttpServletRequest

关于java - 使用JCR以编程方式访问CQ5内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18610681/

相关文章:

java - 在 Jackrabbit Repository 中存档文档的最佳方式是什么?

java - 在 SQL 中运行的准备好的语句中存在语法错误

aem - 需要帮助解决和理解 com.adobe.granite.csrf.impl.CSRFFilter doFilter : the provided CSRF token is invalid in AEM 6. 1

aem - CQ 又名 AEM |蓝图与实时副本

selenium - 当目标和目标元素在不同的框架中时,如何使用 selenium-webdriver 执行拖放?

java - JCR在运行时查找强制属性

aem - 从 CQ5 中的 jcr 节点获取 html 输出

java - 将 UML 图映射到 Java 代码转换

java - 如何将文本区域/字段与怪物动画以及代码同步?

java - 如何检查 HashMap 中具有特定值的键数