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 - Check if page is published or not

If you want to know if the page is published or not you can use the below utility method to know if the page is published or not. Steps - Take Resource Object. Adapt it to Page Adapt page to ReplicationStatus, you will get the status Here is the code - public static Boolean isPublished(Resource resource) { Boolean activated; ReplicationStatus status = null; activated = false; if (resource != null) { try { Page page = resource.adaptTo( Page.class ); status = page.adaptTo( ReplicationStatus.class ); } catch (Exception e) { LOG.debug(e.getMessage (), e); } if (status != null) { activated = status.isActivated(); } } return activated; }

Search engine optimization in CQ

SEO Optimization Search engine optimization ( SEO ) is the process of affecting the visibility of a website or a web page in a search engine 's "natural" or un-paid (" organic ") search results . Source : Wiki Whenever you enter a query in a search engine and hit 'enter' you get a list of web results that contain that query term. Users normally tend to visit websites that are at the top of this list as they perceive those to be more relevant to the query. If you have ever wondered why some of these websites rank better than the others then you must know that it is because of a powerful web marketing technique called Search Engine Optimization (SEO) . SEO is a technique which helps search engines find and rank your site higher than the millions of other sites in response to a search query. SEO thus helps you get traffic from search engines.  HTML Tags and SEO Here is the list of important html tags which help search engine t

Create Your Own Private Ethereum Blockchain

Prerequisites You need to have Geth installed. The easiest way to do this is through homebrew. Open Terminal and  install homebrew ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 2. Now  install geth brew tap ethereum/ethereum brew install ethereum Create Genesis File The Genesis  block  is the first block in the chain, the Genesis  file  is a JSON file that defines the characteristics of that initial block and subsequently the rest of the blockchain. Create a directory to hold your network files mkdir my-eth-chain cd my-eth-chain 2. Create your genesis file touch myGenesis.json 3. Open your genesis file and paste the following { "config": { "chainId": 1994, "homesteadBlock": 0, "eip155Block": 0, "eip158Block": 0, "byzantiumBlock": 0 }, "difficulty": "400", "gasLimit": "2000000", "alloc"