Solved

Access Active Directory with JNDI

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Running JavaFX on JDeveloper 12C 1 76
How to convert from xls to xlsx using java 7 84
hashmap order 17 40
netbeans can not run self signed app 8 7
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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

679 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