Highlighted

How to get the New way of ResourceResolver object from Sling Mapper service

Avatar

Avatar

sunil_kumarn484

Avatar

sunil_kumarn484

sunil_kumarn484

06-09-2018

Hi ,

I need some help on where I am making the mistake in getting the ResourceResolver Object using the Apache Sling Service User Mapper Service.

I am trying to get the ResourceResolver Object from the Java service using the mapper service.

I am getting NULL Pointer Exception .

I had pasted the code snippet of the Service class below.

I had pasted the code snippet of the WCMUsePojo class below.

Created the System User with full permission for /content folder.

And mapper the System user with the service name and bundleID in the below format.

1566145_pastedImage_1.png

ServiceMapping= telstrahealth.core:datawrite=datawrite-service

BundleID or SymbolicName of the bundle folder= telstrahealth.core

Subservice Name=datawrite

System User created/mapped=datawrite-service

Please help to identify which step i am missing, as I am getting NULL Pointet Exception.

Any pointers in this regards is much appreciated.

thanks.

Sunil

Code snippet  of my WCMUsePojo class is below:

@Component(service=WCMUsePojo.class)

public class SolutionsComponent extends WCMUsePojo

{

   private final Logger logger = LoggerFactory.getLogger(getClass());

  private final String QUERY_PROP = "_property";
  private final String QUERY_PROP_VAL = "_property.value";

   List<TagCountBean> tagList = null;
   List<ProductTagCountBean> tagList2 = null;
  public void activate()

  {

  TagService tagSrvc= getSlingScriptHelper().getService(TagService.class);
   logger.info("tagSrvctagSrvctagSrvctagSrvctagSrvc"+tagSrvc);
   ResourceResolver resolver=tagSrvc.getResolverObject();

   logger.info("Inside Activate method---->>>");

}

}

I wrote a TagServiceImpl java service class  in the project.

Below is the code snippet:

import com.au.telstrahealth.core.service.TagService;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.service.component.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.osgi.service.component.annotations.*;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.ComponentContext;
import java.util.*;
import javax.jcr.Session;



//This is a component so it can provide or consume services
@Component(service=TagService.class,immediate = true)

public class TagServiceImpl implements TagService {

  @Reference

  ResourceResolverFactory resFactory;
  private Session adminSession;
  private final Logger logger = LoggerFactory.getLogger(getClass());

   //Define a class member named key
   private int key = 0 ;


   ResourceResolver resolver;


  public ResourceResolver getResolverObject(){

   try{

  Map<String, Object> param = new HashMap<String, Object>();
   param.put(ResourceResolverFactory.SUBSERVICE, "datawrite");

   logger.info("ResourceResolver inside TagServiceImpl Just before getting in to try block--");
   resolver = resFactory.getServiceResourceResolver(param);

   logger.info("ResourceResolver in TagServiceImpl class associated System User--"+resolver.getUserID());


   }catch(Exception e){

  logger.info("ERROR" + e + e.getStackTrace().toString());

   e.printStackTrace();

   }

   return resolver;

   }

}

Replies

Highlighted

Avatar

Avatar

jetate

Avatar

jetate

jetate

18-03-2019

Did you ever figure this out?

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

(val/1000)?string[".0"]}K

Likes

958

Correct Answer

820

Avatar

Arun_Patidar

MVP

Total Posts

(val/1000)?string[".0"]}K

Likes

958

Correct Answer

820
Arun_Patidar
MVP

18-03-2019

Hey Guys,

Could you please make sure you followed all 3 steps to get service user session/resource

Step 1: Use in-built system user in OSGI configuration. In OSGI select Apache Sling Service User Mapper Service

e.g. AEM63App.core:readService=training-user

Step 2: Create system user(training-user) and assign that system user the permissions to access the desire folder/paths.

Step 3: In code, do below, as example:

Session session = null;
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put(ResourceResolverFactory.SUBSERVICE, "readService");
try {
resourceResolver = resourceFactory.getServiceResourceResolver(paramMap);
// logger.debug("User id inside {}", resourceResolver.getUserID());
session = resourceResolver.adaptTo(Session.class);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.debug("getSubserviceSession : Unable to Login : " + e);
}

Adobe Experience Manager Help | Querying Adobe Experience Manager 6 data using the Sling getServiceR...

Highlighted

Avatar

Avatar

smacdonald2008

Total Posts

(val/1000)?string[".0"]}K

Likes

(val/1000)?string[".0"]}K

Correct Answer

(val/1000)?string[".0"]}K

Avatar

smacdonald2008

Total Posts

(val/1000)?string[".0"]}K

Likes

(val/1000)?string[".0"]}K

Correct Answer

(val/1000)?string[".0"]}K
smacdonald2008

18-03-2019

This is not new - been around since 2014.

We have plenty of docs on this - see this:

Adobe Experience Manager Help | Querying Adobe Experience Manager 6.4 JCR data using the QueryBuilde...