Resource resolver returning null...

Avatar

Avatar
Boost 1
Level 1
christopherv662
Level 1

Likes

2 likes

Total Posts

3 posts

Correct reply

0 solutions
Top badges earned
Boost 1
Give Back
Ignite 1
View profile

Avatar
Boost 1
Level 1
christopherv662
Level 1

Likes

2 likes

Total Posts

3 posts

Correct reply

0 solutions
Top badges earned
Boost 1
Give Back
Ignite 1
View profile
christopherv662
Level 1

23-06-2021

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);

 

 

 

 

Replies

Avatar

Avatar
Boost 5
Level 7
Ritesh_M
Level 7

Likes

179 likes

Total Posts

142 posts

Correct reply

45 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
Applaud 5
Affirm 1
View profile

Avatar
Boost 5
Level 7
Ritesh_M
Level 7

Likes

179 likes

Total Posts

142 posts

Correct reply

45 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
Applaud 5
Affirm 1
View profile
Ritesh_M
Level 7

23-06-2021

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

Avatar

Avatar
Boost 1
Level 1
christopherv662
Level 1

Likes

2 likes

Total Posts

3 posts

Correct reply

0 solutions
Top badges earned
Boost 1
Give Back
Ignite 1
View profile

Avatar
Boost 1
Level 1
christopherv662
Level 1

Likes

2 likes

Total Posts

3 posts

Correct reply

0 solutions
Top badges earned
Boost 1
Give Back
Ignite 1
View profile
christopherv662
Level 1

23-06-2021

Hi, I tried this and I am still getting the null. I created the service user and mapped it as you did.

Avatar

Avatar
Boost 250
MVP
ChitraMadan
MVP

Likes

273 likes

Total Posts

165 posts

Correct reply

68 solutions
Top badges earned
Boost 250
Establish
Ignite 1
Give Back 5
Give Back 3
View profile

Avatar
Boost 250
MVP
ChitraMadan
MVP

Likes

273 likes

Total Posts

165 posts

Correct reply

68 solutions
Top badges earned
Boost 250
Establish
Ignite 1
Give Back 5
Give Back 3
View profile
ChitraMadan
MVP

23-06-2021

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

Avatar

Avatar
Affirm 25
Level 5
Bimmi_Soi
Level 5

Likes

79 likes

Total Posts

83 posts

Correct reply

28 solutions
Top badges earned
Affirm 25
Contributor
Applaud 5
Boost 50
Ignite 1
View profile

Avatar
Affirm 25
Level 5
Bimmi_Soi
Level 5

Likes

79 likes

Total Posts

83 posts

Correct reply

28 solutions
Top badges earned
Affirm 25
Contributor
Applaud 5
Boost 50
Ignite 1
View profile
Bimmi_Soi
Level 5

23-06-2021

Hi @christopherv662 

 

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

Avatar

Avatar
Boost 1
Level 1
christopherv662
Level 1

Likes

2 likes

Total Posts

3 posts

Correct reply

0 solutions
Top badges earned
Boost 1
Give Back
Ignite 1
View profile

Avatar
Boost 1
Level 1
christopherv662
Level 1

Likes

2 likes

Total Posts

3 posts

Correct reply

0 solutions
Top badges earned
Boost 1
Give Back
Ignite 1
View profile
christopherv662
Level 1

24-06-2021

I tried this. Still returns null.

Avatar

Avatar
Boost 250
MVP
shelly-goel
MVP

Likes

250 likes

Total Posts

410 posts

Correct reply

107 solutions
Top badges earned
Boost 250
Affirm 100
Give Back 25
Ignite 3
Give Back 10
View profile

Avatar
Boost 250
MVP
shelly-goel
MVP

Likes

250 likes

Total Posts

410 posts

Correct reply

107 solutions
Top badges earned
Boost 250
Affirm 100
Give Back 25
Ignite 3
Give Back 10
View profile
shelly-goel
MVP

26-06-2021

@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;