Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

LDAP Connection

 
Jamie Smithy
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to get a swing application to connect to an ldap server using a url that is typed into a text field. i can't work out the correct syntax and as a result am getting a variety of errors. The code for the part of the program that tries to connect to the LDAP server is shown below. If anyone could possibly help it would be greatly appreciated.

private void connectAndDisplayJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connectAndDisplayJButtonActionPerformed

this.displayJTextArea.setText("Test Text");
//TODO: Add code here



urlString = ldapUrlJTextField.getText();

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, urlString);

try {

System.out.println("Creating Initial Context...");
DirContext ctx = new InitialDirContext(env);

System.out.println("Building Search Criteria...");

Attributes matchAttrs = new BasicAttributes(true);
matchAttrs.put(new BasicAttribute("on"));

System.out.println("Searchingdirectory ...");
NamingEnumeration answer = ctx.search("", matchAttrs);

//SearchControls searchControls = new SearchControls();
//String filter = "cn=*";
//System.out.println("Searching Directory ...");
//NamingEnumeration answer = ctx.search("",filter,searchControls);

printSearchEnumeration(answer);

System.out.println("\nClosing the context...\n");
ctx.close();
}catch (NamingException e) {
System.err.println("Problem getting attribute: "+ e);
}
}



public void printSearchEnumeration(NamingEnumeration answer){

while (answer.hasMoreElements()){

SearchResult sr=(SearchResult)answer.next();
System.out.println("\n>>>"+sr.getName());
this.displayJTextArea.setText(displayJTextArea.getText() + "\n"+sr.getName());

if(sr.getAttributes() == null){

System.out.println("No attributes");
this.displayJTextArea.setText(displayJTextArea.getText() + "\n" + "No attributes");
}else{

try{
NamingEnumeration ae = sr.getAttributes().getAll();
while (ae.hasMore()){
Attribute attr = (Attribute)ae.next();
System.out.println("Attributes: " + attr.getID());
this.displayJTextArea.setText(displayJTextArea.getText() + "\n" + "Attribute: " + attr.getID());

NamingEnumeration e = attr.getAll();
while (e.hasMore()){
System.out.println("value: " + e.next());
this.displayJTextArea.setText(displayJTextArea.getText() + "\n" + "value: " + e.next());
}

}
}catch (NamingException e){
e.printStackTrace();
}
}
}
}
}






}//GEN-LAST:event_connectAndDisplayJButtonActionPerformed
 
Harish Madhavan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the stacktrace what you are getting ? Suggest to try LDAP code snippet in a seperate java program.Once unit tested, you can plug it in your Swing client.

Harish
 
Baiju Varugese
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont see any problem with the code u posted, try the following

1. trim the text which u got from the test field
2. some ldap may not allow the ananomous user to do search operations, so use security principal, credentials, to connect the ldap

env.put(Context.SECURITY_PRINCIPAL,properties.getDirSecurityPrincipal());
env.put(Context.SECURITY_CREDENTIALS,properties.getDirSecurityCredentials());
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic