Get ResourceResolver From ResourceResolverFactory, but the ResourceResolver is not able to get Resource by given path

Given siutation like this, author click publish (activate) a page Then I have following listener to handleEvent

public class ArticleContentActivationEventHandler implements EventHandler {


private static final Logger LOGGER = LoggerFactory.getLogger(ArticleContentActivationEventHandler.class);

private static final String ARTICLE_PAGE_TEMPLATE = "/conf/myproject/settings/wcm/templates/article-template";

@Reference
private ResourceResolverService resourceResolverService;

@Override
public void handleEvent(Event event) {

    ResourceResolver resourceResolver = null;
    try {
        resourceResolver = resourceResolverService.getServiceResourceResolver();
        String resourcePath = getResourcePath(event);
        Resource resource = resourceResolver.getResource(resourcePath);

        //the resource is null, the resource path is /content/myproject/us/en, 
        //resourceResolver is not able to resolve it somehow
        for (Iterator<Resource> it = resourceResolver.getResource("/content/myproject/us/en").getChildren().iterator(); it.hasNext(); ) {
            Resource r = it.next();
            String s = r.getPath();
            String t = r.getResourceType();
        }


        if (resource.isResourceType("cq:Page")) {
            Resource jcr_content = resource.getChild("jcr:content");
            ValueMap vm = jcr_content.getValueMap();
            String template = null;
            if (vm.containsKey("cq:template")) {
                template = PropertiesUtil.toString(vm.get("cq:template"), "");
            }

and below is the interface:

public interface ResourceResolverService {

ResourceResolver getServiceResourceResolver() throws LoginException;

void closeResourceResolver(ResourceResolver resourceResolver);
}

and the impl class:

@Component(service = ResourceResolverService.class, immediate = true)
public class ResourceResolverServiceImpl implements ResourceResolverService {

    @Reference
    private ResourceResolverFactory resourceResolverFactory;

    Logger logger = LoggerFactory.getLogger(ResourceResolverServiceImpl.class);

    @Activate
    protected void activate() {
        logger.info("*** Activating Service ResourceResolverServiceImpl");
    }

    @Override
    public ResourceResolver getServiceResourceResolver() throws LoginException {
        final Map<String, Object> param = Collections.singletonMap(ResourceResolverFactory.SUBSERVICE, (Object) "getResourceResolver");
        //the ResourceResolver returned here may got some issue?
        return resourceResolverFactory.getServiceResourceResolver(param);
    }

    @Override
    public void closeResourceResolver(ResourceResolver resourceResolver) {
        resourceResolver.close();
    }

}

I do have the osgi config setup by following tutorial http://www.aemcq5tutorials.com/tutorials/resourceresolver-from-resourceresolverfactory/

But in my actually even handler class, it never successfully resolved resource resourceResolver is not able to resolve /content/myproject/us/en , resourceResolver keep give me null value

Could anyone experienced this suggest me some code sample to resolve my issue? thanks

1 answer

  • answered 2021-12-12 18:41 user1846696

    Check the User mapping configuration, the bundle id used in the configuration should match to your project core artifact id. For example: in the configuration bundleId = com.day.cq.wcm.cq-msm-core

    Alternatively you can try below code instead

    Map<String, Object> param = new HashMap<String, Object>(); param.put(ResourceResolverFactory.SUBSERVICE, "service-user"); ResourceResolver resolver = resourceResolverFactory.getServiceResourceResolver(param);

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum