如何从资源解析器工厂获取会话对象?

时间:2019-01-17 10:33:38

标签: java aem sightly htl

@Component
public class AddNode {
    @Reference
    static ResourceResolverFactory resolverFactory;
    static Session session;

    public static void main(String[] args) throws Exception { 


        ResourceResolver resourceResolver = resolverFactory.getServiceResourceResolver(null);

        session = resourceResolver.adaptTo(Session.class);        
          Node root = session.getRootNode(); 

          Node adobe = root.addNode("ProgramNode"); 
          Node day = adobe.addNode("subnode"); 
          day.setProperty("jcr:title", "programNode"); 

          Node node = root.getNode("ProgramNode/subnode"); 
          System.out.println(node.getPath()); 
          System.out.println(node.getProperty("jcr:title").getString()); 
          session.save(); 
          session.logout();


        }

}

此处resolverFactory.getServiceResourceResolver(null);我需要在null处传递任何参数。请举一些示例以访问存储库。

2 个答案:

答案 0 :(得分:0)

通过Factory访问时,我们应使用System Users访问存储库。您需要

  1. 创建系统用户,提供适当的权限
  2. 将捆绑包符号名称映射到系统用户
  3. 使用系统用户通过ResourceResolverFactory

    获取会话
    Map<String, Object> param = new HashMap<String, Object>();
    param.put(ResourceResolverFactory.SUBSERVICE, "<sub-service-name>");
    ResourceResolver resolver = null;         
    resolver = resolverFactory.getServiceResourceResolver(param);
    session = resolver.adaptTo(Session.class);
    

您也可以参考this helpx链接文章,以获取有关使用服务用户创建会话的想法

答案 1 :(得分:0)