• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

LDAP sample program

 
Kevin Knowles
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone provide a simple program demonstrating
the use of LDAP?
 
Clayton Donley
Author
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example using JNDI (included with Java 1.3 and above and available for download with older versions of Java). I've thrown in some comments that walk through what it does a bit.
This program does a simple LDAP search for the root entry in the directory. It doesn't print anything fancy, but shows the general process of searching and retrieving results.

(edited by Cindy to format code)
[ March 21, 2003: Message edited by: Cindy Glass ]
 
Andee Weir
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sun provide quite a good starter tutorial at Sun's JNDI tutorials
Andee
 
Ramnath krishnamurthi
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package security;
import java.util.Hashtable;
import java.util.Enumeration;
import javax.naming.*;
import javax.naming.directory.*;
public class JDSAuthentication {
public static String INITCTX = "com.sun.jndi.ldap.LdapCtxFactory";
public static String MY_HOST = "ldap://yoda:391";
public static String MY_SEARCHBASE = "dc=isdintegration,dc=com";
public static String MY_FILTER = "jabberID=test1@yoda";
public static String MGR_DN = "cn=directory manager";
public static String MGR_PW = "passwordadmin";
public static void main(String[] args) {
try{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,INITCTX);
env.put(Context.PROVIDER_URL,MY_HOST);
env.put(Context.SECURITY_AUTHENTICATION,"simple");
env.put(Context.SECURITY_PRINCIPAL,MGR_DN);
env.put(Context.SECURITY_CREDENTIALS,MGR_PW);
DirContext ctx = new InitialDirContext(env);
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
//performs the actual search
//We give it a searchbase, a filter and the contraints containing the scope
//of the search
NamingEnumeration results = ctx.search(MY_SEARCHBASE,MY_FILTER,constraints);
//now stop through the search results
while(results != null && results.hasMore()){
SearchResult sr = (SearchResult)results.next();
String dn = sr.getName();
System.out.println("Distinguished name is "+dn);
Attributes attrs = sr.getAttributes();
for(NamingEnumeration ne = attrs.getAll();ne.hasMoreElements() {
Attribute attr = (Attribute) ne.next();
String attrID = attr.getID();
System.out.println(attrID+" :");
for(Enumeration vals = attr.getAll();vals.hasMoreElements() {
System.out.println("\t"+vals.nextElement());
}
}
System.out.println("\n");
}
}catch(Exception ex){
ex.printStackTrace();
System.exit(1);
}
}
}
 
Vishwa Kumba
Ranch Hand
Posts: 1066
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With which LDAP Servers do the above samples work?

DirContext.INITIAL_CONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory".

Will this property setting in the client program accessing the LDAP database diff from One LDAP Server to another?

thanks,
Vishwa
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will this property setting in the client program accessing the LDAP database diff from One LDAP Server to another


Yes.
 
Vishwa Kumba
Ranch Hand
Posts: 1066
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using Weblogic 8.1, trying to query/update the LDAP attributes for a user stored in the Weblogic's Embedded LDAP database.


I am able to query the LDAP database, if the used INITIAL_CONTEXT_FACTORY is com.sun.jndi.ldap.LdapCtxFactory(part of JDK1.4.1, I believe).

When I use Weblogic's classes,weblogic.jndi.WLInitialContextFactory
or weblogic.jndi.T3InitialContextFactory , I am getting the following exception.

javax.naming.NotContextException: Not an instance of DirContext

Hence my question, was, can com.sun.jndi.ldap.LdapCtxFactory JNDI driver be used for accesing most of the LDAP databases?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic