Expand my Community achievements bar.

SOLVED

Creating session not working

Avatar

Level 6

I'm following this code here. First I tried to create session with the following code from createuser.json.jsp which is running on author instance and its working fine -


<%@include file="/libs/foundation/global.jsp"%>
<%@ page import="org.apache.sling.jcr.api.SlingRepository" %>
<%@ page import="com.day.cq.security.UserManager" %>
<%@ page import="com.day.cq.security.UserManagerFactory" %>
<%@ page import="com.day.cq.security.User" %>
<%@ page import="com.day.cq.security.Authorizable" %>
<%@ page import="com.day.cq.security.profile.Profile" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="javax.jcr.Repository" %>
<%@ page import="javax.jcr.Session" %>
<%@ page import="javax.jcr.SimpleCredentials" %>
<%@ page import="org.apache.jackrabbit.commons.JcrUtils" %>
<%@ page import="org.apache.sling.commons.json.io.*" %><%

final SlingRepository repos = sling.getService(SlingRepository.class);

final UserManagerFactory umFactory = sling.getService(UserManagerFactory.class);
 
Session session = null;

try{
session = repos.loginAdministrative(null); // I'm able to create session
  
catch (Exception e)
{

   System.out.println("Exception Occured: " + e.getMessage());
}
finally
{
    session.logout(); 
    session = null;
}%>

but above code for creating session does not work on publish instance. So, I modified the code as below and tried to run on author instance but in this case createuser.json.jsp is not called. I do not see Sysout statement that I added -

<%@include file="/libs/foundation/global.jsp"%>
<%@ page import="org.apache.sling.jcr.api.SlingRepository" %>
<%@ page import="com.day.cq.security.UserManager" %>
<%@ page import="com.day.cq.security.UserManagerFactory" %>
<%@ page import="com.day.cq.security.User" %>
<%@ page import="com.day.cq.security.Authorizable" %>
<%@ page import="com.day.cq.security.profile.Profile" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="javax.jcr.Repository" %>
<%@ page import="javax.jcr.Session" %>
<%@ page import="javax.jcr.SimpleCredentials" %>
<%@ page import="org.apache.jackrabbit.commons.JcrUtils" %>
<%@ page import="org.apache.sling.commons.json.io.*" %><%
final SlingRepository repos = sling.getService(SlingRepository.class);
final UserManagerFactory umFactory = sling.getService(UserManagerFactory.class);

try
{ System.out.println("Hello session creating");
Repository repository = JcrUtils.getRepository("http://host:4502/crx");
            Session session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()),"crx.default"); 
       System.out.println("session: " + session);
  catch (Exception e)

{

   System.out.println("Exception Occured: " + e.getMessage());
}
finally
{
    session.logout(); 
    session = null;
}%>

Please let me know whats wrong I'm doing here. Appreciate your help.

Regards,

Sam

1 Accepted Solution

Avatar

Correct answer by
Level 10

See this community article:

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

In this article - the proper way to create a session is using the ResourceResolver's adaptTo method:

/Invoke the adaptTo method to create a Session used to create a QueryManager
ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
session = resourceResolver.adaptTo(Session.class);
 

 

You may want to use this way to create a Session.

View solution in original post

5 Replies

Avatar

Level 10

You are able to run this on author but not on Publish? Is your code being replicated on publish? 

http://dev.day.com/docs/en/cq/current/deploying/replication.html

Avatar

Level 6

Suddenly it started working. Do not know what was wrong when doing it from publish. Thank you for your help!

Avatar

Level 6

Thank you Scott for your help!

The first piece of code for creating session is working in author  but not in publish. To replicate the code to publish instance, I opened http://localhost:4502/etc/replication/treeactivation.html and then I selected /apps/<my project> and then Activate. Is this correct way to do it?

I found this url and it says This method MUST not be used to handle client requests of whatever kinds. To handle client requests a regular authenticated session retrieved through Repository.login(javax.jcr.Credentials, String) or Session.impersonate(javax.jcr.Credentials) must be used.

Then I thought loginAdministrative() is not applicable from publish instance. Kindly correct me if my understanding is wrong.

Thank you very much for your help! 

Avatar

Correct answer by
Level 10

See this community article:

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

In this article - the proper way to create a session is using the ResourceResolver's adaptTo method:

/Invoke the adaptTo method to create a Session used to create a QueryManager
ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
session = resourceResolver.adaptTo(Session.class);
 

 

You may want to use this way to create a Session.

Avatar

Level 10

that is great-- I am gald that you got it working. I am going to write a CQ UserManager article that shows use of that API via Java and an OSGi bundle too.