Repoinit not able to set ACL properly in AEM SDK | Not Able to correctly retrieve a Session in AEM
Hey Guys,
As I've said before I'm trying to create a system user to retrieve a valid session in order to create nodes in AEM using a JCR Session.
Down bellown I have my repoinit config file
org.apache.sling.jcr.repoinit.RepositoryInitializer~aem-showcase.cfg.json
{
"scripts": [
"create path (sling:OrderedFolder) /content/dam/aem-showcase",
"create path (nt:unstructured) /content/dam/aem-showcase/jcr:content",
"set properties on /content/dam/aem-showcase/jcr:content\n set cq:conf{String} to /conf/aem-showcase\n set jcr:title{String} to \"AEM Showcase\"\nend",
"create path (nt:Folder) /content/dam/aem-showcase/comments",
"create path (nt:unstructured) /content/dam/aem-showcase/comments/jcr:content",
"create service user aem-showcase-jcr-service-user with forced path system/cq:services/aem-showcase-jcr-service-user\n set principal ACL for aem-showcase-jcr-service-user\n allow jcr:all on :repository,/content/dam/aem-showcase/comments\nend"
]
}
Also I've made sure to have a sling.serviceusermapping config in order to make sure I have the user mapped to a subservice you can see the content of that in the script down bellow as well
org.apache.sling.serviceusermapping.impl.ServiceUserMapperImpl.amended~aem-showcase.cfg.json
{
"user.mapping": [
"aem-showcase.core:AEMShowCaseUserJCR=[aem-showcase-jcr-service-user]"
]
}
And finally I've created a OSGI service to create my nodes programatically, but of course when I retrieve the session and I try to grab the comments node It says it does not exist even though I checked on the crx and it does exist,
package com.aem.showcase.core.services.impl;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.Session;
import javax.jcr.nodetype.NodeType;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.serviceusermapping.ServiceUserMapped;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import com.aem.showcase.core.pojos.CommentPojo;
import com.aem.showcase.core.services.CommentsService;
@Component(
service = CommentsService.class,
reference = {
@3214626(
name = CommentServiceImpl.SERVICE_ID,
service = ServiceUserMapped.class,
target = "(subServiceName=AEMShowCaseUserJCR)"
)
},
immediate = true)
public class CommentServiceImpl implements CommentsService{
@3214626
ResourceResolverFactory resourceResolverFactory;
protected static final String SERVICE_ID = "AEMShowCaseUserJCR";
@9944223
public boolean createComment(CommentPojo commentPojo) {
try (ResourceResolver resolver = resourceResolverFactory.getResourceResolver(getAdminResourceResolverMap())){
Session session = resolver.adaptTo(Session.class);
Node commentsNode = null;
try {
commentsNode = session.getNode("/content/dam/aem-showcase/comments");
} catch (PathNotFoundException e) {
commentsNode = session.getNode("/content/dam/aem-showcase").addNode("comments", NodeType.NT_FOLDER);
}
Node comment = commentsNode.addNode(commentPojo.getId(), NodeType.NT_UNSTRUCTURED);
comment.setProperty("id", commentPojo.getId());
comment.setProperty("content", commentPojo.getContent());
comment.setProperty("created", commentPojo.getCreated());
comment.setProperty("fullName", commentPojo.getFullname());
comment.setProperty("upvote", commentPojo.getUpvote_count());
session.save();
session.logout();
return true;
} catch (Exception e) {
String test = "Just to see the exception";
// TODO: handle exception
}
return false;
}
@9944223
public boolean deleteComment(CommentPojo commentPojo) {
// TODO Auto-generated method stub
return false;
}
@9944223
public List<CommentPojo> findAll(String fullname) {
// TODO Auto-generated method stub
return null;
}
@9944223
public CommentPojo getComment(long id) {
// TODO Auto-generated method stub
return null;
}
Map<String, Object> getAdminResourceResolverMap() {
Map<String, Object> authInfo = Collections.singletonMap(
ResourceResolverFactory.SUBSERVICE, SERVICE_ID);
return authInfo;
}
}
I'm starting to think that maybe because I'm trying to access a node there's inside of /content/dam/* my permissions are not valid to set or being replaced by some other admin acl or something like that.
Please If anybody know how to properly handle that case let me know ASAP.