Overblog Suivre ce blog
Editer l'article Administration Créer mon blog
24 avril 2008 4 24 /04 /avril /2008 01:51

Il arrive que le serveur de test de www.openldap.com ne soit pas disponible. C'est bien dommage car c'est celui ci que j'ai utilisé dans le tutorial que j'ai publié sur Srping Ldap sur ce blog :Tutorial Spring Ldap et sur developpez.com Tuto Spring Ldap

Dans ce cas quand on execute TestLdap.java on ce genre de message dans la console :

23 avr. 2008 20:00:18 org.springframework.ldap.core.support.AbstractContextSource afterPropertiesSet
INFO: Property 'userDn' not set - anonymous context will be used for read-write operations
Exception in thread "main"
org.springframework.ldap.CommunicationException: www.openldap.com:389; nested exception is javax.naming.CommunicationException: www.openldap.com:389 [Root exception is java.net.ConnectException: Connection refused: connect]Caused by: javax.naming.CommunicationException: www.openldap.com:389 [Root exception is java.net.ConnectException: Connection refused: connect]
at com.sun.jndi.ldap.Connection.<init>(
Connection.java:207
)
at com.sun.jndi.ldap.LdapClient.<init>(
LdapClient.java:118
)
at com.sun.jndi.ldap.LdapClientFactory.createPooledConnection(
LdapClientFactory.java:46
)
at com.sun.jndi.ldap.pool.Connections.<init>(
Connections.java:97
)
at com.sun.jndi.ldap.pool.Pool.getPooledConnection(
Pool.java:114
)
at com.sun.jndi.ldap.LdapPoolManager.getLdapClient(
LdapPoolManager.java:310
)
at com.sun.jndi.ldap.LdapClient.getInstance(
LdapClient.java:1572
)
at com.sun.jndi.ldap.LdapCtx.connect(
LdapCtx.java:2616
)
at com.sun.jndi.ldap.LdapCtx.<init>(
LdapCtx.java:287
)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(
LdapCtxFactory.java:175
)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(
LdapCtxFactory.java:193
)
at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(
LdapCtxFactory.java:136
)
at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(
LdapCtxFactory.java:66
)
at javax.naming.spi.NamingManager.getInitialContext(
NamingManager.java:667
)
at javax.naming.InitialContext.getDefaultInitCtx(
InitialContext.java:288
)
at javax.naming.InitialContext.init(
InitialContext.java:223
)
at javax.naming.ldap.InitialLdapContext.<init>(
InitialLdapContext.java:134
)
at org.springframework.ldap.core.support.LdapContextSource.getDirContextInstance(
LdapContextSource.java:43
)
at org.springframework.ldap.core.support.AbstractContextSource.createContext(
AbstractContextSource.java:223
)
at org.springframework.ldap.core.support.AbstractContextSource.getReadOnlyContext(
AbstractContextSource.java:107
)
at org.springframework.ldap.core.LdapTemplate.executeReadOnly(
LdapTemplate.java:770
)
at org.springframework.ldap.core.LdapTemplate.lookup(
LdapTemplate.java:821
)
at com.neoneto.demo.springLdap1_2.PersonDao.findByPrimaryKey(
PersonDao.java:31
)
at com.neoneto.demo.springLdap1_2.TestLdap.main(
TestLdap.java:28
)Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(
Native Method
)
at java.net.PlainSocketImpl.doConnect(
PlainSocketImpl.java:333
)
at java.net.PlainSocketImpl.connectToAddress(
PlainSocketImpl.java:195
)
at java.net.PlainSocketImpl.connect(
PlainSocketImpl.java:182
)
at java.net.SocksSocketImpl.connect(
SocksSocketImpl.java:366
)
at java.net.Socket.connect(
Socket.java:519
)
at java.net.Socket.connect(
Socket.java:469
)
at java.net.Socket.<init>(
Socket.java:366
)
at java.net.Socket.<init>(
Socket.java:180
)
at com.sun.jndi.ldap.Connection.createSocket(
Connection.java:349
)
at com.sun.jndi.ldap.Connection.<init>(
Connection.java:184
)

- La solution numero 1 à ce type de probleme conciste à trouver un serveur ldap public disponible et à modifier les sources pour ca fonctionne. Pour trouver un serveur public qui fonctionne il suffit d'utiliser Ldap Browser et la liste des serveurs publiques fournit dans les liens du tutorial.

Faire tous ca, ca prend du temps et c'est pas toujours facile quand on debute avec Ldap. 
C'est pourquoi il y a la solution numero 2.

- Solution numero 2 : Utiliser ces sources java qui permettent de se connecter sur un autre serveur ldap public, celui de Baylor University.

 

package com.neoneto.demo.springLdap1_2.rescue;
public class Person2 {

 private String uid;
 private String firstName;
 private String lastName;
 private String email;
 
 public String getEmail() {
  return email;
 }
 public void setEmail(String email) {
  this.email = email;
 }
 public String getFirstName() {
  return firstName;
 }
 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }
 public String getLastName() {
  return lastName;
 }
 public void setLastName(String lastName) {
  this.lastName = lastName;
 }
 public String getUid() {
  return uid;
 }
 public void setUid(String uid) {
  this.uid = uid;
 }
 public String toString(){
  return uid+" - "+firstName+" "+lastName;
 }
}

 


package com.neoneto.demo.springLdap1_2.rescue;
import javax.naming.Name;
import javax.naming.directory.Attributes;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.LdapTemplate;

public class PersonDao2 {

 private LdapTemplate ldapTemplate;

 private static class PersonAttributMapper implements AttributesMapper {

  public Person2 mapFromAttributes(Attributes attrs)
    throws javax.naming.NamingException {
   Person2 p = new Person2();
   p.setFirstName(attrs.get("givenName").get().toString());
   p.setLastName(attrs.get("sn").get().toString());
   p.setUid(attrs.get("cn").get().toString());
   p.setEmail(attrs.get("mail").get().toString());
   return p;
  }
 }

 public Person2 findByPrimaryKey(String cn) {
  Name dn = buildDn(cn);
  return (Person2) ldapTemplate.lookup(dn, new PersonAttributMapper());
 }

 private Name buildDn(String cn) {
  DistinguishedName dn = new DistinguishedName();
  dn.add("ou", "Faculty and Staff");
  dn.add("ou", "Computer Science - Eng/Comp Sci");
  dn.add("cn", cn);
  return dn;
 }

 public void setLdapTemplate(LdapTemplate ldapTemplate) {
  this.ldapTemplate = ldapTemplate;
 }

}



package com.neoneto.demo.springLdap1_2.rescue;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.support.LdapContextSource;

public class LdapContextSourceFactory2 {

 public static ContextSource getLdapContextSource() throws Exception {
  LdapContextSource ldapContextSource = new LdapContextSource();
  ldapContextSource.setUrl("ldap://ldap.baylor.edu:389");
  ldapContextSource.setBase("ou=People,o=Baylor University,"+
"c=US");
  ldapContextSource.afterPropertiesSet();
  return ldapContextSource;
 }
}



package com.neoneto.demo.springLdap1_2.rescue;
import java.util.List;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.LdapTemplate;

public class TestLdap2 {

 /** Retrieve a Person from ldap server and display it in Standard Out - Use Baylor University server*/
 public static void main(String[] args) {

  // 1 Retrieve a LdapContextSource
  ContextSource ldapContextSource = null;
  try {
   ldapContextSource = LdapContextSourceFactory2.getLdapContextSource();
  } catch (Exception e) {
   System.out.println("Impossible to get a"
+" LdapContextSource.");
   e.printStackTrace();
  }

  // 2 Instanciate a LdapTemplate
  LdapTemplate ldapTemplate = new LdapTemplate();
  ldapTemplate.setContextSource(ldapContextSource);

  // 3 instanciate a PersonDao
  PersonDao2 dao = new PersonDao2();
  dao.setLdapTemplate(ldapTemplate);

  // 4 retrieve a Person and display it
  Person2 person = dao.findByPrimaryKey("Patrick Hynan");
  System.out.println("Uid: " + person.getUid());
  System.out.println("FirstName: " + person.getFirstName());
  System.out.println("LastName: " + person.getLastName());
  System.out.println("Email: " + person.getEmail() + "n");

 }
}

 

 

 

Partager cet article

Repost 0
Published by David Gimelle - dans Spring Ldap
commenter cet article

commentaires

a 15/01/2016 10:30

Il y a quelques erreurs au niveau du buildn ( dn.add("ouaculty and Staff") -> dn.add("ou","aculty and Staff") ); mais j'ai la meme erreur qu'au debut (CommunicationException)

ccc 05/06/2012 17:23


cane marche pas aussi

Summary

  • : GetJ2ee -Java development
  • : Articles about Java and EE
  • Contact

Profil

  • David Gimelle
  • Java Developer for more 10 years. I worked in France, Canada and Switzerland. I am contractor in London since 2010.
  • Java Developer for more 10 years. I worked in France, Canada and Switzerland. I am contractor in London since 2010.

Contact

You can contact me here

or by Twitter

Search

Archives