• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

LDAP sample program

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone provide a simple program demonstrating
the use of LDAP?
 
Author
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sun provide quite a good starter tutorial at Sun's JNDI tutorials
Andee
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
}
 
Ranch Hand
Posts: 1066
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic