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:
@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);
Views
Replies
Total Likes
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
Hi @christopherv662,
First of all, try to see, why the way suggested by @Ritesh_Mittal 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
Can you try "Apache Sling Service User Mapper Service Amendment" rather than "Apache Sling Service User Mapper Service".
I tried this. Still returns null.
@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;
Views
Likes
Replies