aspose file tools*
The moose likes Distributed Java and the fly likes LDAP Connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Distributed Java
Bookmark "LDAP Connection" Watch "LDAP Connection" New topic
Author

LDAP Connection

Jamie Smithy
Greenhorn

Joined: Aug 15, 2005
Posts: 1
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

Joined: Dec 10, 2003
Posts: 17
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

Joined: Dec 16, 2003
Posts: 74
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());


Measure twice cut once carpenter theory <br /> <br />Baiju <br />SCJCP, SCBCD, IBM-486, IBM-484, SCEA Part I
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: LDAP Connection