Solved

Access Active Directory with JNDI

Posted on 2004-08-06
1
309 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

Suggested Solutions

Title # Comments Views Activity
Java Restore security prompts not working 10 270
Html split(text) 2 50
Java Inheritance super keyword use 8 74
What does this method description mean? 1 17
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

752 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