Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Accessing JCR from remote application (i.e. Hybris)

Avatar

Level 2

Hi,

I have written sling servlet to fetch DAM Asset from JCR Repository. Then retrieved InputStream from Rendition of an Asset, so that I can read an asset and write it to servlet response. I have attached servlet code. Code is working fine. When I hit URL http://localhost:4502/bin/testServlet?category=city browser redirects me to login page of CQ. After I enter credentials for login, browser displays an image which is returned from servlet response.

Can anyone please guide me how I can provide authentication in servlet itself, so that browser will not ask for credentials and will show image immediately after hitting URL?

I have also tried below code snippet but it gives me RepositoryException (Unable to access a repository with the following settings:
    org.apache.jackrabbit.repository.uri: http://localhost:4502/crx/server). 

String remoteURLLocation = "http://localhost:4502/crx/server";
repository = JcrUtils.getRepository(remoteURLLocation);
SimpleCredentials credential = new SimpleCredentials("admin","admin".toCharArray());
session = repository.login(credential);  

Please suggest best approach to achieve the same.

Thanks,

Manju

1 Accepted Solution

Avatar

Correct answer by
Level 10
Exclude Authentication for certain urls configure at 

http://host:port/system/console/configMgr/org.apache.sling.engine.impl.auth.SlingAuthenticator by adding entry in "Authentication Requirements".  If need to use current session or authorization follow scott recomendation,

View solution in original post

3 Replies

Avatar

Level 10

To access the JCR from an OSGi bundle - use the following code:

//Inject a Sling ResourceResolverFactory

@Reference
private ResourceResolverFactory resolverFactory;
 public String getCustomerData(String filter) {
 
Customer cust = null;
 
List<Customer> custList = new ArrayList<Customer>();
try {
           
    //Invoke the adaptTo method to create a Session used to create a QueryManager
    ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
    session = resourceResolver.adaptTo(Session.class);

 

DO not use:

SimpleCredentials credential = new SimpleCredentials("admin","admin".toCharArray());
session = repository.login(credential);  

See this article for more information:

http://helpx.adobe.com/experience-manager/using/querying-experience-manager-data-using1.html

Avatar

Level 2

Hi,

I have tried below code

ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
session = resourceResolver.adaptTo(Session.class);

But still when I access servlet with new browser (not the same browser where CQ session is available) it redirects me to CQ login page. 

How can I provide authentication details in servlet itself so that it will not redirect me to login page?

Thanks,

Manju

Avatar

Correct answer by
Level 10
Exclude Authentication for certain urls configure at 

http://host:port/system/console/configMgr/org.apache.sling.engine.impl.auth.SlingAuthenticator by adding entry in "Authentication Requirements".  If need to use current session or authorization follow scott recomendation,