This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes connection to LDAP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "connection to LDAP" Watch "connection to LDAP" New topic
Author

connection to LDAP

age spets
Ranch Hand

Joined: Aug 07, 2002
Posts: 68
Hi!
I know very little about LDAP, but want to write a program in java that connects to a LDAP server and then make a query with one input parameter 'alias' that will give me one outpur parameter 'emailadress'.
Should not be difficult this. Anybody that can help?
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
Do a google search. I saw an article about six months ago giving step by step description on how to set up a connection to a Novell LDAP server.


The nice thing about Standards is that there are so many to choose from!
age spets
Ranch Hand

Joined: Aug 07, 2002
Posts: 68
Thats what I have been doing... Before I posted it here:-) Is doing that for the moment, while waiting for someone that might now the answer.
Have two possibilities:
1. test out this code:

import javax.naming.directory.*;
import com.ibm.ldap.ldapv3.*;
import com.ibm.ldap.*;
import javax.naming.*;
import java.util.*;
import java.io.*;

public static void main(String[] args) {
Hashtable env = new Hashtable(11);
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "LDAP://server:389/DC=company,DC=com,DC=au");
env.put(Context.SECURITY_AUTHENTICATION,"simple");
// specify the username
env.put(Context.SECURITY_PRINCIPAL,"CN=UserID,CN=Users,DC=company,DC=com,DC=au");
// specify the password
env.put(Context.SECURITY_CREDENTIALS,"password");
try {
// Create the initial context
System.out.println("1. Made it here!");
DirContext ctx = new InitialDirContext(env);
System.out.println("Dont make it here!");
String[] attrIDs = {"sn", "telephoneNumber", "mail"};
Attributes matchAttrs = new BasicAttributes(true); // ignore case
matchAttrs.put(new BasicAttribute("sn", "smith"));
matchAttrs.put(new BasicAttribute("mail"));
NamingEnumeration answer = ctx.search( "CN=Users",matchAttrs, attrIDs);
// Print the response
printSearchEnumeration(answer);
// Close the context
ctx.close();
} catch (Exception e) {
e.printStackTrace();
}
}
2. netscape have some good classes.
cs.sun.com/source/816-5618-10/netscape/ldap/beans/LDAPGetProperty.html#getProperty(java.lang.String,%20int,%20java.lang.String,%20java.lang.String,%20java.lang.String)
and LDAPSimpleAuth is another class to watch.
If I find something I let you all now.. If someone tested one of these or have better suggestions please let me know.
age spets
Ranch Hand

Joined: Aug 07, 2002
Posts: 68
not easy to find an download the classes from netsape. Been searching for a while..
Arnold Reuser
Ranch Hand

Joined: Nov 20, 2003
Posts: 194
Maybe you should start at :
http://developer.netscape.com/docs/manuals/dirsdk/jsdk40/contents.htm
There are a lot of examples over there that I've used myself.
have fun!
age spets
Ranch Hand

Joined: Aug 07, 2002
Posts: 68
Here is the solution:

import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
public class Ldap
{
public static void main(String[] args)
{
Hashtable env = new Hashtable(11);
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://'servername'.amazon.com:389/DC=amazon,DC=com");
env.put(Context.SECURITY_AUTHENTICATION,"simple");
//env.put(Context.SECURITY_PRINCIPAL,"CN='username',DC=OBOS,DC=NO");
env.put(Context.SECURITY_PRINCIPAL,"'username'@amazon.com");
env.put(Context.SECURITY_CREDENTIALS,"'password'");
try
{
DirContext ctx = new InitialDirContext(env);
SearchControls s=new SearchControls();
String[] attrIDs = {"sn", "telephoneNumber", "mail", "mailNickname"};
s.setReturningAttributes(attrIDs);
s.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration answer = ctx.search("","mailNickname=speaag", s);
print(answer);
answer.close();
ctx.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}

public static void print(NamingEnumeration e)
{
try
{
while(e.hasMoreElements())
{
SearchResult s=(SearchResult)e.next();
Attributes a=s.getAttributes();
System.out.println("mail="+a.get("mail").get());
}
e.close();
}
catch (NamingException el)
{
}
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: connection to LDAP
 
Similar Threads
LDAP sample program
LDAP Basics
Lightweight LDAP provider
ibm-slapdPageSizeLmt for ibmslapd.conf
problem in WIN NT