• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

connection to LDAP

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
age spets
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not easy to find an download the classes from netsape. Been searching for a while..
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
{
}
}
}
 
Quick! Before anybody notices! Cover it up with this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic