Accessing JCR from remote application (i.e. Hybris) | Community
Skip to main content
Level 2
October 16, 2015
Solved

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

  • October 16, 2015
  • 3 replies
  • 1062 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Sham_HC
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,

3 replies

Sham_HC
Sham_HCAccepted solution
Level 10
October 16, 2015
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,

Level 2
October 16, 2015

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

smacdonald2008
Level 10
October 16, 2015

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