Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Access Active Directory with JNDI

Posted on 2004-08-06
1
Medium Priority
?
322 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 300 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

885 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