
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

/*
 * Copyright (c) 2014 Paul Scherrer Institute. All rights reserved.
 */


public class TestLdap {
    private static void getUserInfo(String userName) throws NamingException{
           Hashtable env = new Hashtable();
            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            env.put(Context.PROVIDER_URL, "ldap://d.psi.ch");                   

            DirContext ctx = new InitialDirContext(env);
            Attributes attrs = ctx.getAttributes("cn="+userName+",ou=users,ou=psi,dc=d,dc=psi,dc=ch");
            if(attrs == null){
                System.out.println("nothing found for " + userName);
            }else{
                System.out.println( attrs.get("sn").get().toString());
                System.out.println( attrs.get("mail").get().toString());
            }        
    }
    
    public static void main(String[] args) throws Exception {
        getUserInfo("gobbo_a");
        
    }
}
