Skip to main content

Getting Service Resource Resolver in AEM 6 [Copied from Code lit]



From AEM 6 usage of admin session and admin resource resolver have been deprecated. Now we should use service resource resolver API to retrieve the session. Below are the steps
Creating System users
2.      Click on user administration
3.      Click on create system user
4.      Enter user id.
 
You can create any number of system users based on your requirement. A typical use case is to have two separate system users, one for read operation and another for write. You may want to further sub categorize and create role oriented users. Once system users are created, assign them permission according to their roles or categories.
Assign user mapping
1.      Go to felix bundle console
2.      Find org.apache.sling.serviceusermapping.impl.ServiceUserMapperImpl configuration
3.      Add a new entry in below format –
4.      bundleId:subserviceName = systemUser
5.      Write a service which uses above configuration to return session.
 
Below is the service API code which uses "readSubService" and "writeSubService" and provides separate methods for retrieving ResourceResolver and JcrSession

SessionService.java
 
package com.test.aem.session.service.api;
import javax.jcr.Session;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
 
/**
 * Wrapper API for ResourceResolverFactory/ResourceResolver for retrieving JCR session
 * @author gdagur
 *
 */
public interface SessionService {
               /**
                * Method retrieves Service Resource resolver with write access
                * @return ResourceResolver
                * @throws LoginException
                */
               public ResourceResolver getWriteServiceResourceResolver() throws LoginException;
               /**
                *  Method retrieves Service Resource resolver with read access
                * @return
                * @throws LoginException
                */
               public ResourceResolver getReadServiceResourceResolver() throws LoginException;
               /**
                *  Method retrieves JCR Session with write access
                * @return
                * @throws LoginException
                */
               public Session getReadJcrSession() throws LoginException;
               /**
                *  Method retrieves JCR session with write access
                * @return
                * @throws LoginException
                */
               public Session getWriteJcrSession() throws LoginException;
              
}
 
 
SessionServiceImpl.java
 
package com.test.aem.session.service.impl;
 
import java.util.HashMap;
import java.util.Map;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import com.test.aem.session.service.api.*;
@Component(immediate=true, metatype = true, label = "test Session Service", name = "SessionServiceImpl" )
@Service(value = SessionService.class)
public class SessionServiceImpl implements SessionService{
               /**
                * Resource resolver factory object to retrieve session
                */
               @Reference
               ResourceResolverFactory rrFactory;
               /**
                * Logger object
                */
               private static final Logger LOGGER = LoggerFactory.getLogger(SessionServiceImpl.class);
               /**
                * Read Service System user
                */
               private static final String READ_SUB_SERVICE = "readSubService";
               /**
                * Write Service System user
                */
               private static final String WRITE_SUB_SERVICE = "writeSubService";
 
               /*
                * (non-Javadoc)
                * @see com.test.aem.session.service.api.SessionService#getReadServiceResourceResolver()
                */
               public ResourceResolver getReadServiceResourceResolver() throws LoginException{
                              Map<String, Object> param = new HashMap<String, Object>();
                              param.put(ResourceResolverFactory.SUBSERVICE, READ_SUB_SERVICE);
                              LOGGER.debug("Getting Read access serviceResourceResolver for param {}", param);
                              return rrFactory.getServiceResourceResolver(param);
               }
               /*
                * (non-Javadoc)
                * @see com.test.aem.session.service.api.SessionService#getWriteServiceResourceResolver()
                */
               public ResourceResolver getWriteServiceResourceResolver() throws LoginException{
                              Map<String, Object> param = new HashMap<String, Object>();
                              param.put(ResourceResolverFactory.SUBSERVICE, WRITE_SUB_SERVICE);
                              LOGGER.debug("Getting Write access serviceResourceResolver for param {}", param);
                              return rrFactory.getServiceResourceResolver(param);
               }
               /*
                * (non-Javadoc)
                * @see com.test.aem.session.service.api.SessionService#getReadJcrSession()
                */
               public Session getReadJcrSession() throws LoginException{
                              ResourceResolver resolver = getReadServiceResourceResolver();
                              Session session = null;
                              if(null != resolver){
                                             session =  resolver.adaptTo(Session.class);
                              }
                              return session;
               }
               /*
                * (non-Javadoc)
                * @see com.test.aem.session.service.api.SessionService#getWriteJcrSession()
                */
               public Session getWriteJcrSession() throws LoginException{
                              ResourceResolver resolver = getWriteServiceResourceResolver();
                              Session session = null;
                              if(null != resolver){
                                             session =  resolver.adaptTo(Session.class);
                              }
                              return session;
               }
 
}

Comments

Popular posts from this blog

AEM 6.3 - How to extend Granite UI

Here in this article, I am going to explain how to extent Granite UI that is basically used to create AEM 6.x consoles. There can be various use cases where in you may need that. In our case, author wants to take all the page creation requests through AEM instead of email communication. Authors are basically looking for a form in AEM authoring console so that requestor can login and submit page creation/Asset upload request. Overall requirement is - Need a link in Navigation so that author can click on it and go to a Intake form. Author must be able to fill and save the intake requests. Author must be able to see the history of submitted intake requests Able to see the details of submitted intake requests. Solution - 1. Create a following structure - /apps/cq/core/content/nav/form and add the details given in below screenshot - You basically need to add - href, title and icon. Href is the path of page where user will land on clicking it.  Af...

CQ Page Properties from Javascript

To get CQ page properties inside javascript you can use core CQ JS API. It can be convenient if you need to get this information inside your custom JS widgets.              var pageData = CQ.HTTP.get(CQ.HTTP.externalize(CQ.utils.WCM.getPagePath() + "/jcr:content.json")); After that you can retrieve any property you need (assuming it's present in JCR):              var resourceType = pageData ? CQ.Util.formatData(CQ.HTTP.eval(pageData))['sling:resourceType'] : null; Please do not overuse it because it invokes additional ajax call to server. It's OK to use it in edit mode on author instance. Copied from -  http://adobecms.blogspot.com/2014/04/cq-page-properties-in-javascript.html

How Catalan Numbers Help in Solving DSA Problems?

Catalan numbers are a sequence of natural numbers that appear in various combinatorial problems, often related to recursively defined structures. These numbers are widely used in counting problems, particularly in dynamic programming and combinatorics. The sequence of Catalan numbers starts as follows: C 0 = 1 , C 1 = 1 , C 2 = 2 , C 3 = 5 , C 4 = 14 , C 5 = 42 , … C_0 = 1, C_1 = 1, C_2 = 2, C_3 = 5, C_4 = 14, C_5 = 42, \dots Formula for Catalan Numbers The n-th Catalan number can be computed using the following formula: C n = ∑ i = 0 n − 1 C i ⋅ C n − i − 1 C_n = \sum_{i=0}^{n-1} C_i \cdot C_{n-i-1} Alternatively, it can be expressed using the binomial coefficient: Catalan numbers are a sequence of natural numbers that appear in various combinatorial problems, often related to recursively defined structures. These numbers are widely used in counting problems, particularly in dynamic programming and combinatorics. The sequence of Catalan numbers starts as follows: Formula for Catalan ...