我想以编程方式访问内容,我尝试了以下操作:
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/