1 Reply Latest reply: Feb 20, 2013 11:48 PM by ManPakHong,Dave RSS

    LdapContent

    ManPakHong,Dave
      Dear expert,

      I have a problem of searching LDAP. If i use the following code, i can get level 2 using the following code.
      but I want to get Level 4 object. Thanks for any kind help.

      the current base: ou=HQ2-BR
      filter: "(ou=*)";

      Regards,
      Man Pak Hong, Dave
      manpakhong@hotmail.com
      manpakhong@gmail.com

      ldap structure

      - o=com,dc=rabbitforever #(level 0)
      - ou=HQ2-BR // referal to other ad #(level 1)
      - ou=TSB // #(level 2)
      - ou=BM1 // #(level 2)
      - ou=IIC // #(level 2)
      - ou=People // #(level 3)
      -uid=IICCIO // #(level 4)
      -uid=IICSIO1 // #(level 4)
      // ----------------------------------------------------------------------------------------------------------------------------------
           public void loopLDAP() {
                String adminName = "uid=writer,ou=People,o=com,dc=rabbitforever";
                String adminPassword = "password";

                Properties env = new Properties();
                env.put(Context.INITIAL_CONTEXT_FACTORY,
                          "com.sun.jndi.ldap.LdapCtxFactory");
                // env.put(Context.PROVIDER_URL,
                // "ldap://192.168.1.127:389/dc=rabbitforever,dc=com");
                env.put(Context.PROVIDER_URL,
                          "ldap://10.10.176.156:389/o=com,dc=rabbitforever");
                // env.put(Context.SECURITY_AUTHENTICATION, "none");

                env.put(Context.SECURITY_PRINCIPAL, adminName);
                env.put(Context.SECURITY_CREDENTIALS, adminPassword);
                env.put(Context.SECURITY_AUTHENTICATION, "simple");
                env.put(Context.REFERRAL, "follow");


                try {
                     LdapContext ctx = new InitialLdapContext(env, null);
                     ctx.setRequestControls(null);

                     String filter = "(ou=*)";

                     NamingEnumeration<?> namingEnum = ctx.search("ou=HQ2-BR", filter,
                               getSimpleSearchControls());
                     while (namingEnum.hasMore()) {
                          SearchResult result = (SearchResult) namingEnum.next();
                          Attributes attrs = result.getAttributes();

                          String cn = "";
                          String sn = "";
                          String description = "";
                          String uid = "";
                          if (null != attrs.get(cn)) {
                               cn = attrs.get("cn").toString();
                          }
                          if (null != attrs.get("sn")) {
                               sn = attrs.get("sn").toString();
                          }
                          if (null != attrs.get("description")) {
                               description = attrs.get("description").toString();
                          }
                          if (null != attrs.get("uid")) {
                               uid = attrs.get("uid").toString();
                          }
                          System.out.println(cn + " | " + sn + " | " + description
                                    + " | " + uid);

                     }
                } catch (Exception ex) {
                     ex.printStackTrace();
                }
           }// end loopLDAP()
        • 1. Re: LdapContent
          ManPakHong,Dave
          I eventually find the answer myself.

          ldap structure

          - o=com,dc=rabbitforever #(level 0)
          - ou=HQ2-BR // referal to other ad #(level 1)
          - ou=TSB // #(level 2)
          - ou=BM1 // #(level 2)
          - ou=IIC // #(level 2)
          - ou=People // #(level 3)
          -uid=IICCIO // #(level 4)
          -uid=IICSIO1 // #(level 4)
          // public void loopLDAP() {
          String adminName = "uid=writer,ou=People,o=com,dc=rabbitforever";
          String adminPassword = "password";

          Properties env = new Properties();
          env.put(Context.INITIAL_CONTEXT_FACTORY,
          "com.sun.jndi.ldap.LdapCtxFactory");

          env.put(Context.PROVIDER_URL,
          *"ldap://10.10.176.156:389/");*
          // env.put(Context.SECURITY_AUTHENTICATION, "none");

          env.put(Context.SECURITY_PRINCIPAL, adminName);
          env.put(Context.SECURITY_CREDENTIALS, adminPassword);
          env.put(Context.SECURITY_AUTHENTICATION, "simple");
          env.put(Context.REFERRAL, "follow");

          try {
          LdapContext ctx = new InitialLdapContext(env, null);
          ctx.setRequestControls(null);

          String filter = "(ou=*)";

          NamingEnumeration<?> namingEnum = ctx.search("_ou=People,ou=IIC,ou=HQ2-BR,dc=rabbitforever,dc=com_", filter,
          getSimpleSearchControls());
          while (namingEnum.hasMore()) {
          SearchResult result = (SearchResult) namingEnum.next();
          Attributes attrs = result.getAttributes();

          String cn = "";
          String sn = "";
          String description = "";
          String uid = "";
          if (null != attrs.get(cn)) {
          cn = attrs.get("cn").toString();
          }
          if (null != attrs.get("sn")) {
          sn = attrs.get("sn").toString();
          }
          if (null != attrs.get("description")) {
          description = attrs.get("description").toString();
          }
          if (null != attrs.get("uid")) {
          uid = attrs.get("uid").toString();
          }
          System.out.println(cn + " | " + sn + " | " + description
          + " | " + uid);

          }
          } catch (Exception ex) {
          ex.printStackTrace();
          }
          }// end loopLDAP()