3 Replies Latest reply: Sep 12, 2011 6:56 AM by Ashish Vashisht RSS

    Access Users in CRX

    JPatSoco Community Member

      How can I get the list of users in /home/users in CRX? Is this possible from within the Flex application?

        • 1. Re: Access Users in CRX
          MattFromLC Community Member

          Unfortunately, there aren't any Flex APIs available to get at that info. But it's not hard to create yourself an OSGI Service that does get at it, and expose it using Data Services.


          Here's some code I happened to have lying around that has the bulk of the necessary bits. I used declarative services with the following manifest and XML config files. I keep meaning to convert this to use the Maven SCR plugin, but haven't gotten around to it yet.



          Manifest-Version: 1.0
          Bundle-ManifestVersion: 2
          Bundle-Name: User Manager Service
          Bundle-SymbolicName: matt.usermanagerservice.impl
          Bundle-Version: 1.0.0
          Import-Package: org.osgi.framework;version="1.3.0",
          Export-Package: matt.usermanagerservice.impl;version="0.0.1"
          Bundle-ClassPath: .
          Service-Component: OSGI-INF/usermanagerservice.xml



          <?xml version="1.0" encoding="UTF-8"?>
            <scr:component enabled="true" immediate="true"
              <scr:implementation class="matt.usermanagerservice.impl.UserManagerService"/>
              <scr:service servicefactory="false">
                <scr:provide interface="matt.usermanagerservice.UserManagerService"/>
              <scr:property name="service.description" value="User Manager Service"/>
              <scr:property name="service.vendor"      value="Matt Butler"/>
              <scr:property name="service.pid"         value="matt.usermanagerservice.impl"/>
              <scr:property name="connectors.remoting.id" value="UserManagerService"/>
              <scr:reference name="repository"
                cardinality="1..1" policy="static"



          package matt.usermanagerservice.impl;


          import java.util.ArrayList;
          import java.util.HashMap;
          import java.util.Iterator;
          import java.util.List;
          import java.util.Map;


          import javax.jcr.AccessDeniedException;
          import javax.jcr.PropertyType;
          import javax.jcr.RepositoryException;
          import javax.jcr.Session;
          import javax.jcr.UnsupportedRepositoryOperationException;
          import javax.jcr.Value;
          import javax.jcr.ValueFormatException;


          import org.apache.jackrabbit.api.JackrabbitSession;
          import org.apache.jackrabbit.api.security.user.Authorizable;
          import org.apache.jackrabbit.api.security.user.Group;
          import org.apache.jackrabbit.api.security.user.UserManager;
          import org.apache.sling.jcr.api.SlingRepository;


          import matt.usermanagerservice.User;  // A Java bean used to hold User data.. Set it up however you want.


          public class UserManagerService  implements matt.usermanagerservice.UserManagerService
              private SlingRepository repository = null;


              public UserManagerService()




              protected void setRepository(SlingRepository repository) {
                  this.repository = repository;
              protected void unsetRepository(SlingRepository repository) {
                  this.repository = null;
              // TODO: Look into user org.apache.jackrabbit.api.security.principal.PrincipalManager since it has better searching APIs and an explicit getEveryone() method. Can be got via JRSession.getPrincipleManager()
              public List<User> findUsers(String propertyNameToFind, String propertyValueToFind)  {
                  List<User> users = new ArrayList<User>();
                  JackrabbitSession session = null;
                  try {
                      session = getSession();
                      Iterator<Authorizable> userIter = session.getUserManager().findAuthorizables(propertyNameToFind, propertyValueToFind, UserManager.SEARCH_TYPE_USER);
                      while (userIter.hasNext()) {
                          org.apache.jackrabbit.api.security.user.User jackRabbitUser = (org.apache.jackrabbit.api.security.user.User) userIter.next();
                          matt.usermanagerservice.impl.User user = new matt.usermanagerservice.impl.User();
                          Map<String, String> properties = new HashMap<String, String>();
                          properties.put("principalName", jackRabbitUser.getPrincipal().getName());
                          populateProperties(jackRabbitUser, ".", properties);
                          populateProperties(jackRabbitUser, "profile", properties);


                          Iterator<Group> memberOfIter = jackRabbitUser.memberOf();
                          List<String> groups = new ArrayList<String>();
                          while (memberOfIter.hasNext()) {
                              Group group = memberOfIter.next();


                      return users;
                  } catch (RepositoryException e) {
                      // TODO Auto-generated catch block
                      return null;
                  } catch (CantFindSession e) {
                      // TODO Auto-generated catch block
                      return null;
                  } finally {
                      if (session != null)




              private void populateProperties(
                      org.apache.jackrabbit.api.security.user.User jackRabbitUser,
                      String relativePath,
                      String> properties)
                              throws RepositoryException {
                  Iterator<String> propertyNameIter = jackRabbitUser.getPropertyNames(relativePath);
                  while (propertyNameIter.hasNext()) {
                      String propertyName = propertyNameIter.next();
                      Value propertyValue = jackRabbitUser.getProperty(relativePath+"/"+propertyName)[0];
                      if (propertyValue.getType() != PropertyType.BINARY) { // tends to screw things up.. check it out later.
                          try {
                              properties.put(propertyName, propertyValue.getString());
                          } catch (ValueFormatException vfe) {
                              properties.put(propertyName, "Cannot convert to String");
              private JackrabbitSession getSession() throws CantFindSession {
                  String whatAmIDoing = "";
                     if (repository == null) {
                      throw new CantFindSession("No SlingRepository bound");
                  try {
                      Session jcrSession = repository.loginAdministrative(repository.getDefaultWorkspace());
                      if ( jcrSession instanceof JackrabbitSession)  {
                          org.apache.jackrabbit.api.JackrabbitSession jackrabbitSession = (JackrabbitSession) jcrSession;
                          return jackrabbitSession;
                      } else {
                          throw new CantFindSession("Didn't get a JackrabbitSession from repository.loginAdministrative");
                  } catch (RepositoryException e) {
                      throw new CantFindSession("Failed calling repository.loginAdministrative", e);

          • 2. Re: Access Users in CRX
            JPatSoco Community Member

            Yea....I'm completely new to the OSGI realm and maven as well. Is there a tutorial that would help me understand what you're solution is asking me to do?


            Also, for the time being, we will still be using the Tomcat Data Services until the NIO channels (i.e. RTMP) are available with the ADEP version so would the below java code work in the old data services container?

            • 3. Re: Access Users in CRX
              Ashish Vashisht Community Member



              Inside of the Experience Server, you can create Data Services destinations by deploying your code as OSGi bundles. These bundles can export Java Packages - these will then become available for use by the Data Services Message Broker.


              For more information on this, see this sample for a step by step overview on creating and deploying bundles to CRX which setup Data Services destinations. Once you have done that, you can try out this sample which shows how to retrieve data from the Sample DB via a POJO. (The first sample is a pre-requisite, since it shows the overall approach to getting your code into the Experience Server as bundles)


              You can see the "Creating Data Services applications for the Experience Server" on this page for a full set of samples. Also, when you start the Experience Server, you should see a Getting Started link which provides more information about developing for ADEP.


              I don't think Matt's sample code would work remotely outside of the CRX container, but can you provide some more information on how you are planning on deploying this?


              http://help.adobe.com/en_US/enterpriseplatform/10.0/AEPDeveloperGuide/WSe2a127949a92fa1061 747727130427d726e-8000.html

              http://help.adobe.com/en_US/enterpriseplatform/10.0/AEPDeveloperGuide/WSe2a127949a92fa10-3 22b2f813003fea16b-8000.html






              Ashish Vashisht