Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events

Resource Resolver is returning no resource sometimes

Avatar

Level 2

we have resource resolver and that is getting resource from JCR using service user mapper(have created one service user with read  access to resource).

Problem is sometimes the resource resolver gives null results for fetching resource from jcr even if we have resource at the location and even access also exit...

this is happening in local and all other environments...  this happens frequently...sometimes it picks the resource.

here is a code.

try (final ResourceResolver resolver = ContentFragmentUtils.getResourceResolver(this.resourceResolverFactory,
TotalRewardsConstants.USER_FOR_SERVICE)) {
final Resource resourceMasterCf = resolver.getResource(path);  --> here path is referring to the CF path in jcr.

 

resolver.getResource(path);  --> here im having null even if we have resolver object.

5 Replies

Avatar

Community Advisor

@Sudarshan1992  I think it's better to create resource resolver object using ResourceResolverFactory than using ContentFragmentUtils. 

 

 

 

@Reference
    private ResourceResolverFactory resourceResolverFactory;

 

 

final Map<String, Object> authInfo = Collections.singletonMap(ResourceResolverFactory.SUBSERVICE,

                "wknd-examples-statistics");

 

        // Get the auto-closing Service resource resolver

        // Remember, any ResourceResolver you get, you must ensure is closed!

        try (ResourceResolver serviceResolver = resourceResolverFactory.getServiceResourceResolver(authInfo)) {

              //Write all your logic here

            Resource resourceMasterCf = resolver.getResource(path); 

        } catch (LoginException e) {

            log.error("Login Exception when obtaining a User for the Bundle Service: {} ", e);

        }

Avatar

Level 2

ContentFragmentUtils  is just a utils class for generic methods.
even we are using resolverFactory to get resource resolver.

Avatar

Community Advisor

Hi @Sudarshan1992 Ideally this should not happen. Tried multiple times, but always getting the resource.

 

Try adding more logs in your code and then track the path whenever the resource is null.

logger.error("Resource is null for the path {}", path);

 

IMO, this could mainly happen, when the asset is also getting created using the same service. If that is the case, you might need to change the logic to read the asset (Content Fragment) via some scheduled sling jobs or workflow or any other mode.

 

Hope this helps!

Thanks

Avatar

Level 2

we are not creating asset using same service.
Logs also we dont see any issue.

Avatar

Level 6

@Sudarshan1992

This can happen when the service user (in this case service user associated with sub service TotalRewardsConstants.USER_FOR_SERVICE) used for getting resource resolver does not have access to the resource being accessed. So I would suggest you to check the access of the service user for problematic CFs.