Announcement Announcement Module
Collapse
No announcement yet.
Simple Test Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Simple Test

    I'm trying to create a simple test to get my feet wet with LdapTemplate but keep running into issues. I'm sure it is something simple. Any help would be great.


    Here is my simple test

    public void testLookup() throws Exception {
    //set-up Template in Java code

    LdapContextSource source = new LdapContextSource();
    source.setUrl("ldap://an.ip.address);
    source.setBase("dc=testlocal,dc=com");
    source.setUserName("cn-manager");
    source.setPassword("somepassword");
    source.setDirObjectFactory(DefaultDirObjectFactory .class);
    source.setContextFactory(com.sun.jndi.ldap.LdapCtx Factory.class);
    //source.setCacheEnvironmentProperties(false);
    source.setPooled(false);

    LdapTemplate template = new LdapTemplate(source);

    //do look-up
    PersonContextMapper mapper = new PersonContextMapper();
    Object result = _template.lookup("uid=admin,ou=Users", mapper);
    assertNotNull(result);
    System.out.println(result);

    }


    private static class PersonContextMapper
    implements ContextMapper {

    public Object mapFromContext(Object ctx) {
    DirContextAdapter context = (DirContextAdapter) ctx;
    String result = context
    .getStringAttribute("cn");

    System.out.println("result = " + result + ", ctx " + ctx);

    return result;
    }
    }



    With the above code I'm getting a "need to specifly javax.namingfactory.initial" error. If I uncomment out the setCacheEnviroment I get an emptry result back

    result = null, ctx net.sf.ldaptemplate.support.DirContextAdapter: dn=uid=admin, ou=Users {}

    and if I do a System.setProperty(Context.INITIAL_CONTEXT_FACTORY ,"com.sun.jndi.ldap.LdapCtxFactory" ); first I get a Connection refused exception. Note it the stack trace lists localhost:389 but I set the ip to something else "source.setUrl(ldap://ip)"

    org.springframework.dao.DataRetrievalFailureExcept ion: Unable to communicate with LDAP server; nested exception is javax.naming.CommunicationException: localhost:389 [Root exception is java.net.ConnectException: Connection refused: connect]
    javax.naming.CommunicationException: localhost:389 [Root exception is java.net.ConnectException: Connection refused: connect]
    at com.sun.jndi.ldap.Connection.<init>(Unknown Source)
    at com.sun.jndi.ldap.LdapClient.<init>(Unknown Source)
    at com.sun.jndi.ldap.LdapClient.getInstance(Unknown Source)
    at com.sun.jndi.ldap.LdapCtx.connect(Unknown Source)
    at com.sun.jndi.ldap.LdapCtx.<init>(Unknown Source)
    at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext (Unknown Source)
    at javax.naming.spi.NamingManager.getInitialContext(U nknown Source)
    at javax.naming.InitialContext.getDefaultInitCtx(Unkn own Source)
    at javax.naming.InitialContext.init(Unknown Source)
    at javax.naming.ldap.InitialLdapContext.<init>(Unknow n Source)
    at net.sf.ldaptemplate.support.LdapContextSource.getD irContextInstance(LdapContextSource.java:45)
    at net.sf.ldaptemplate.support.AbstractContextSource. createContext(AbstractContextSource.java:194)
    at net.sf.ldaptemplate.support.AbstractContextSource. getReadOnlyContext(AbstractContextSource.java:107)
    at net.sf.ldaptemplate.LdapTemplate.executeReadOnly(L dapTemplate.java:394)
    at net.sf.ldaptemplate.LdapTemplate.lookup(LdapTempla te.java:491)
    at com.discovery.oneplace.events.TestLDAPRoleMappingA ction.testLookup(TestLDAPRoleMappingAction.java:86 )
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at junit.framework.TestCase.runTest(TestCase.java:154 )
    at junit.framework.TestCase.runBare(TestCase.java:127 )
    at junit.framework.TestResult$1.protect(TestResult.ja va:106)
    at junit.framework.TestResult.runProtected(TestResult .java:124)
    at junit.framework.TestResult.run(TestResult.java:109 )
    at junit.framework.TestCase.run(TestCase.java:118)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:478)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:344)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:196)
    Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(Unknown Source)
    at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at com.sun.jndi.ldap.Connection.createSocket(Unknown Source)
    ... 29 more





    This looks like a great tool, I just need to get over this stumbling block.

  • #2
    Got it.

    nevermind I figured it out based on http://forum.springframework.org/showthread.php?t=27609

    Comment


    • #3
      Hello mavmh,

      I think problem is that you don't specify port to connect with LDAP server.

      I hope that my reply can help you.

      Comment

      Working...
      X