//2012-06-06 johnpfeiffer
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InvalidNameException;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.PartialResultException;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.LdapName;
public class LdapSearch
{
DirContext ctx = null;
private String host;
private int port = 389;
private LdapName bindUserDN;
private String bindUserPassword;
private String baseDN;
private String userAttribute = "sAMAccountName"; //ldap uses uid
private String searchBase = "";
LdapSearch( String host , int port , String baseDN , String bindUserDN , String bindUserPassword , String userAttribute , String searchBase ) throws InvalidNameException , IllegalStateException
{
// System.out.println( "DEBUG: " + host + " , " + port + " , " + baseDN + " , " + bindUserDN + " , " + bindUserPassword + " , " + userAttribute + " , " + searchBase );
if( host == null )
{ throw new IllegalArgumentException( "ERROR: host cannot be null" );
}
if( host.isEmpty() )
{ throw new IllegalArgumentException( "ERROR: host cannot be empty" );
}
// if( StringUtils.containsWhiteSpace( host ) ) { throw new IllegalArgumentException( "ERROR: host cannot contain whitespace" ); }
if( port < 0 )
{ throw new IllegalArgumentException( "ERROR: " + port + " is an invalid port number, try 389 or 636" );
}
if( baseDN == null )
{ throw new IllegalArgumentException( "ERROR: baseDN cannot be null" );
}
if( bindUserDN == null )
{ throw new IllegalArgumentException( "ERROR: bindUserDN cannot be null" );
}
if( bindUserPassword == null )
{ throw new IllegalArgumentException( "ERROR: bindUserDNPassword cannot be null" );
}
if( userAttribute == null )
{ throw new IllegalArgumentException( "ERROR: userAttribute cannot be null" );
}
if( searchBase == null )
{ throw new IllegalArgumentException( "ERROR: searchBase cannot be null" );
}
new LdapName( baseDN ); //test if throws InvalidNameException
new LdapName( bindUserDN ); //test if throws InvalidNameException
this.host = host;
this.port = port;
this.bindUserDN = new LdapName( bindUserDN );
this.bindUserPassword = bindUserPassword;
this.baseDN = baseDN;
this.searchBase = searchBase;
this.userAttribute = userAttribute;
}
//System.out.println( "DEBUG: " + this.bindUserDN.size() + " RDNs: ");
//System.out.println( "DEBUG: " + userName );
private String getFirstComponent()
{
String userName = "";
for( Enumeration<String> names = this.bindUserDN.getAll(); names.hasMoreElements(); )
{ userName = names.nextElement();
}
return userName;
}
private Hashtable <String , String> buildEnvironment()
{
Hashtable <String , String> env = new Hashtable <String , String> ();
env.put( Context.INITIAL_CONTEXT_FACTORY , "com.sun.jndi.ldap.LdapCtxFactory" );
env.put( Context.REFERRAL , "follow" );
env.put( Context.SECURITY_AUTHENTICATION , "simple" );
env.put( Context.SECURITY_PRINCIPAL , bindUserDN.toString() );
env.put(Context.SECURITY_CREDENTIALS, bindUserPassword );
env.put( Context.PROVIDER_URL, "ldap://" + host + ":" + port + "/" + baseDN );
return env;
}
private SearchControls buildSearchControls()
{
SearchControls searchcontrols = new SearchControls(); // tree limit, count limit, time limit, attribs to return
searchcontrols.setSearchScope( SearchControls.SUBTREE_SCOPE );
String[] attributeFilter = { "distinguishedname" , "logoncount" };
searchcontrols.setReturningAttributes( attributeFilter );
return searchcontrols;
}
protected ArrayList <String> query()
{
Hashtable <String , String> env = buildEnvironment();
ArrayList <String> results = null;
try{
ctx = new InitialDirContext( env );
results = searchWithFilter( ctx );
// Attributes domainName = ctx.getAttributes( "" );
// System.out.println( "DEBUG: Domain Name:"+ domainName.get( "name" ).get() );
}catch( Exception e )
{ System.err.println( e.getMessage() );
}
if( ctx != null )
{ try { ctx.close(); } catch (Exception e) { }
}
return results;
}
private ArrayList <String> searchWithFilter( DirContext ctx ) throws NamingException
{
NamingEnumeration <SearchResult> results = null;
ArrayList <String> resultList = new ArrayList <String> ();
SearchControls searchcontrols = buildSearchControls();
String userName = getFirstComponent();
String searchFilter = "(" + userName + ")";
try{
results = ctx.search( searchBase , searchFilter , searchcontrols );
while( results.hasMore() )
{
SearchResult sr = results.next();
Attributes attrs = sr.getAttributes();
resultList.add( attrs.toString() );
// resultList.add( sr.getNameInNamespace() );
// System.out.println( attrs.getIDs() );
// Attribute attr = attrs.getIDs()
// resultList.add( )
// Attribute attr = attrs.get( userAttribute );
// System.out.println( "RETRIEVED: " + attr.get() );
}
}catch( PartialResultException e )
{ // System.out.println( "ignoring partial result exception" );
}
if( results != null )
{ try { results.close(); } catch( Exception e ) { }
}
return resultList;
}
} //end class