• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

LDAP Connection

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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());
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic