Solved

Access Active Directory with JNDI

Posted on 2004-08-06
1
314 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

636 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