hi I want to create resourceresolver instance using jcr session please help me with sample code
Solved! Go to Solution.
Views
Replies
Total Likes
try with below
Map authInfo = new HashMap(); authInfo.put(org.apache.sling.jcr.resource.api.JcrResourceConstants.AUTHENTICATION_INFO_SESSION, session); ResourceResolver resourceResolver = resolverFactory.getResourceResolver(authInfo);
Hi @akshaybhujbale ,
Are you asking about resolving Session from ResourceResolver? In that case below is the code snippet.
Use system user to get session via ResourceResolverFactory
Map<String, Object> param = new HashMap<String, Object>(); param.put(ResourceResolverFactory.SUBSERVICE, "<sub-service-name>"); ResourceResolver resolver = null; resolver = resolverFactory.getServiceResourceResolver(param); session = resolver.adaptTo(Session.class);
Correct me if I miss understood your question.
Regards,
Santosh
I know I might need Resource Resolver for different use cases, so normally I create a utility OSGi service and this OSGi service will be having two methods getResourceResolver() [it will return ResourceResolver] and closeResourceResolver() as shown below -
/** * */ package com.aem.demo.core.services; import org.apache.sling.api.resource.ResourceResolver; /** * @author debal * * This service will be used as a utility and it will help us to get * resource resolver object , JCR session and close resource resolver * */ public interface JcrUtility { public ResourceResolver getResourceResolver(); public void closeResourceResolver(ResourceResolver resourceResolver); }
Implementation class -
/** * */ package com.aem.demo.core.services.impl; import java.util.HashMap; import java.util.Map; import java.util.Objects; import org.apache.sling.api.resource.LoginException; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.api.resource.ResourceResolverFactory; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.aem.demo.core.services.JcrUtility; /** * @author debal * */ @Component(service = JcrUtility.class, immediate = true) public class JcrUtilityImpl implements JcrUtility { private final Logger logger = LoggerFactory.getLogger(JcrUtilityImpl.class); @Reference ResourceResolverFactory resourceResolverFactory; @Override public ResourceResolver getResourceResolver() { Map<String, Object> map = new HashMap<String, Object>(); map.put(resourceResolverFactory.SUBSERVICE, "readWriteService"); ResourceResolver serviceResourceResolver = null; try { serviceResourceResolver = resourceResolverFactory.getServiceResourceResolver(map); } catch (LoginException e) { logger.error("Could not get service user [ {} ]", "demoSystemUser", e.getMessage()); } return serviceResourceResolver; } @Override public void closeResourceResolver(ResourceResolver resourceResolver) { if (Objects.nonNull(resourceResolver)) { resourceResolver.close(); } } }
Now If I need ResourceResolver in Servlet and any other service then I can use
JcrUtility jcrUtility
Then I can get ResourceResolver like ResourceResolver resourceResolver = jcrUtility.getResourceResolver();
In SlingModel , I can do
@OSGiService
JcrUtility jcrUtility; and ResourceResolver resourceResolver = jcrUtility.getResourceResolver();
Not sure about your use case but you can take a look into this https://cqwemblog.wordpress.com/2015/02/12/get-resource-resolver-from-session-cq5aem/
Hi @akshaybhujbale ,
Please refer to this article - https://unlocklearning.in/resource-resolver-in-aem/
Hope this will answer all your query related to Resource Resolver.
Regards,
Shiv
try with below
Map authInfo = new HashMap(); authInfo.put(org.apache.sling.jcr.resource.api.JcrResourceConstants.AUTHENTICATION_INFO_SESSION, session); ResourceResolver resourceResolver = resolverFactory.getResourceResolver(authInfo);
Views
Likes
Replies
Views
Likes
Replies