Solved

Access Active Directory with JNDI

Posted on 2004-08-06
1
271 Views
Last Modified: 2013-11-24
Hi,

I need to get a list of user emails from an NT Domain. is this possible using LDAP and JNDI. If so, could someone plz post code to do that.

thanks.
0
Comment
Question by:thefallguy
1 Comment
 
LVL 7

Accepted Solution

by:
bvanderveen earned 75 total points
ID: 11739952
Here is some sample code for doing this from a Novell Groupwise server.  Please note, this didn't require any authentication.  Your mileage may vary.  The LDAP tutorials on java.sun.com are pretty good.  Best to first try to view all of the elements.  In this case, I have to parse, because I get back one big string.

===============================
/*
 * Created on Apr 27, 2004
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */

/**
 * @author Bill Vanderveen
 *
 *GroupWise is  
 *
 */
import javax.naming.*;
import javax.naming.directory.*;
import java.util.Hashtable;
import java.util.regex.*;

public class GroupWise {
      
      public static void main(String[] args){
            String s;
            if (args.length > 0)
                  s = getGroupWiseMail(args[0]);
            else
                  s = getGroupWiseMail("NAboulhosn");
            System.out.println("Final answer is: " + s);
                  
      }

    public static String getGroupWiseMail(String s) {
          Hashtable env = new Hashtable();          
          env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
          env.put(Context.PROVIDER_URL,"ldap://168.201.10.9:389");
          DirContext ctx;
          String mail = new String();
        try {
            ctx = new InitialDirContext(env);
         
                //String[] attrIDs = {"mail"};
                  String[] attrIDs = {"uid","sn","givenName","cn","mail","mail","o","ou","title","manager"};
                SearchControls sc = new SearchControls();
                sc.setSearchScope(SearchControls.SUBTREE_SCOPE);  //search subtree
                sc.setReturningAttributes(attrIDs);
                String searchBase = "o=rbc";
                String searchMask = "cn=" + s; //search for uid passed in
                //String searchMask = "sn=Jamison";
          
          
                NamingEnumeration answer = ctx.search(searchBase, searchMask, sc);
                if(answer.hasMore()){
                      SearchResult entry= (SearchResult)answer.next();
                      System.out.println(entry);      
                      mail = getMail(entry.toString());            
                }                            
          }catch(NamingException e){
                e.printStackTrace();
          }  
          return mail;       
    }
   
    private static String getMail(String s){
            Pattern p = Pattern.compile("[,{}]"); //will parse out by comma, curly braces
            String split[] = p.split(s);
            String ret = new String();
            for(int i=0; i < split.length; i++){
                  System.out.println("split[" + i + "]: " + split[i]);
                  if(getElement(split[i]).equalsIgnoreCase("mail")){
                        ret = split[i].substring(split[i].indexOf(':') +1).trim();
                        System.out.println("mail is: " + ret);                        
                  }
                  
            }
            return ret;
                
    }
   
      private static String getElement(String s){
            int t = s.indexOf("=");
            if(t == -1)
                  return null;

            String ret = s.substring(0,t);
            return ret;
            
      }
   
}
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
factorial example challenge 10 62
endX challenge 2 50
pairs challenge 5 44
wordmultiple challenge 12 90
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now