Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

Resource resolver returning null...

christopherv662
Level 1
Level 1

Hello all,

 

I have two classes. Class A, is where I am calling a method to Class B (Which contains code that's trying to get the resource resolver).

Code for class B where I am getting the null error:

 

@component

@ServiceMode

public class NotifyAdminImpl implements NotifyAdminService
{

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

@reference
ResourceResolverFactory resolverFactory;

public void notifyAdmin(String location,String exception)
{
LOGGER.debug("We are inside the notifyAdmin method.");

try
{
LOGGER.debug("Begin.");

String timeStamp = DateTimeFormatter.ofPattern("MM_dd_yyyy_HH_mm_ss").format(LocalDateTime.now());

HashMap<String, Object> authInfo = new HashMap<String, Object>();

authInfo.put("user.name","serviceadmin");
authInfo.put("user.password",new java.lang.String("randomPassword!LOL").toCharArray());

ResourceResolver resourceResolver = resolverFactory.getResourceResolver(authInfo); // line where it nulls out.

Session session = resourceResolver.adaptTo(Session.class);

Resource res = resourceResolver.getResource("/apps/errorHandle/error");

Node node = res.adaptTo(Node.class);

Calendar c = Calendar.getInstance();

c.setTimeInMillis(c.getTimeInMillis());

String nodeName = location + timeStamp;

String path = node.getPath();

LOGGER.debug("PATH OF INCOMING NODE IS: " + path);

Node errorNode = node.addNode(nodeName,"nt:unstructured");

errorNode.setProperty("jcr:created", c);

errorNode.setProperty("exception", exception);

errorNode.setProperty("isEmailSent", false);

session.save();

session.logout();

LOGGER.debug("End.");

}
catch (Exception e)
{
e.printStackTrace();
LOGGER.info("An exception in notifyAdmin method occurred due to: " + e.getMessage(),e);
}

}

}

 

Class A code snippet of calling this method from Class B that's within my OSGi bundle:

NotifyAdminImpl notify = new NotifyAdminImpl();

notify.notifyAdmin("this_is_an_error",printExceptionString);

 

 

 

 

6 Replies
Ritesh_M
Community Advisor
Community Advisor

Hi @christopherv662 ,

 

You can get resource resolver through sub service. All you have to do is -

 

1. Create a System user and give proper permission.

2. Configure "Apache Sling Service User Mapper Service" for System user and Service mapping.

3. Use below code to get the Resource resolver-

 

public ResourceResolver getResourceResolver() {
ResourceResolver resourceResolver = null;
try {
Map<String, Object> param = new HashMap<>();
param.put(ResourceResolverFactory.SUBSERVICE, "writeService");
resourceResolver = resolverFactory.getServiceResourceResolver(param);
} catch (Exception e) {
}
return resourceResolver;
}

 

I have explained the step by step process in below video-

https://www.youtube.com/watch?v=CatRHs0rt4o

christopherv662
Level 1
Level 1
Hi, I tried this and I am still getting the null. I created the service user and mapped it as you did.
ChitraMadan
Community Advisor
Community Advisor

Hi @christopherv662,

 

First of all, try to see, why the way suggested by @Ritesh_M is not working for you. Here's another article for your reference - 

https://one-inside.com/aem6-resourceresolver-access-in-services/

 

Secondly, try to use 

ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver(null) - If the param is null, the resource resolver returned will generally not be authenticated and only provide minimal privileges
 
Thirdly, What is class A? Do you have access to Resource/Request in that class, so you can pass resource resolver as a parameter, while calling class B
 
Thanks,
Chitra
Bimmi_Soi
Community Advisor
Community Advisor

Hi @christopherv662 

 

Can you try "Apache Sling Service User Mapper Service Amendment" rather than "Apache Sling Service User Mapper Service".

shelly-goel
Community Advisor
Community Advisor

@christopherv662  If you're using OSGi R6 annotations, error is probably  beacuse  @Reference annotation would not wrok as below. Try changing it to @OSGIService

@reference
ResourceResolverFactory resolverFactory;