directory-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Emmanuel Lecharny (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (DIRAPI-202) Can't get LdapConnectionTemplate working
Date Fri, 22 Aug 2014 03:31:11 GMT

    [ https://issues.apache.org/jira/browse/DIRAPI-202?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14106409#comment-14106409
] 

Emmanuel Lecharny commented on DIRAPI-202:
------------------------------------------

Here, the pb seems to be that the entry is a referral to another entry. If you look at the
stack trace you get, you see this line sat the end :

Caused by: org.apache.directory.api.ldap.model.cursor.CursorLdapReferralException
at org.apache.directory.ldap.client.api.EntryCursorImpl.get(EntryCursorImpl.java:164)
at org.apache.directory.ldap.client.api.EntryCursorImpl.get(EntryCursorImpl.java:50)
at org.apache.directory.api.ldap.model.cursor.CursorIterator.next(CursorIterator.java:80)

At this point, there is a special treatment of the entry that needs to be done in order to
fetch the correct entry (you just got a LDAPURL pointing to the entry you want to fetch).

The same code should also produce the same result on something different than AD assuming
the entry you get back is a referral.

> Can't get LdapConnectionTemplate working
> ----------------------------------------
>
>                 Key: DIRAPI-202
>                 URL: https://issues.apache.org/jira/browse/DIRAPI-202
>             Project: Directory Client API
>          Issue Type: Bug
>    Affects Versions: 1.0.0-M24
>         Environment: WAS 8.0 JDK, RAD 9.0.1, Windows 7 Enterprise Edition
>            Reporter: Chris Harris
>            Priority: Blocker
>              Labels: EntryMapper, LdapConnectionTemplate, documentation, newbie
>
> Hi,
> I’ve been following the example code from Section 2.10 using v1.0.0-M24.  My goal is
to use an LdapConnectionTemplate to bind to AD, return a response/cursor, and use an EntryMapper
to map each cursor iteration’s value to a Person object.
> I’m getting the following error message:
> ERR_02002_FAILURE_ON_UNDERLYING_CURSOR Failure on underlying Cursor
> I’m not sure what’s wrong at this point.  I have a basic query example working that
doesn’t use a template.
> I’m including my code, which contains the method with the basic query that does work
and the method with the query using the template that doesn’t work.
> Can you help me figure out why searchLdapForCeoUsingTemplate() is not working?
> Here’s LdapClient.java:
> /**
> * 
>  */
> import java.io.IOException;
> import java.util.List;
> import java.util.concurrent.TimeUnit;
> import java.util.logging.Level;
> import java.util.logging.Logger;
> import org.apache.commons.pool.impl.GenericObjectPool;
> import org.apache.directory.api.ldap.model.cursor.CursorException;
> import org.apache.directory.api.ldap.model.cursor.SearchCursor;
> import org.apache.directory.api.ldap.model.entry.Entry;
> import org.apache.directory.api.ldap.model.exception.LdapException;
> import org.apache.directory.api.ldap.model.message.Response;
> import org.apache.directory.api.ldap.model.message.SearchRequest;
> import org.apache.directory.api.ldap.model.message.SearchRequestImpl;
> import org.apache.directory.api.ldap.model.message.SearchResultDone;
> import org.apache.directory.api.ldap.model.message.SearchResultEntry;
> import org.apache.directory.api.ldap.model.message.SearchScope;
> import org.apache.directory.api.ldap.model.name.Dn;
> import org.apache.directory.ldap.client.api.DefaultLdapConnectionFactory;
> import org.apache.directory.ldap.client.api.LdapConnection;
> import org.apache.directory.ldap.client.api.LdapConnectionConfig;
> import org.apache.directory.ldap.client.api.LdapConnectionPool;
> import org.apache.directory.ldap.client.api.LdapNetworkConnection;
> import org.apache.directory.ldap.client.api.PoolableLdapConnectionFactory;
> import org.apache.directory.ldap.client.api.SearchCursorImpl;
> import org.apache.directory.ldap.client.template.EntryMapper;
> import org.apache.directory.ldap.client.template.LdapConnectionTemplate;
> /**
> * @author Chris Harris
> *
> */
> public class LdapClient {
>                
>                public LdapClient() {
>                               
>                }
>                
>                private static final EntryMapper personEntryMapper = 
>                               new EntryMapper<Person>() {
>                                              @Override
>                                              public Person map( Entry entry ) throws
LdapException {
>                                                             return new Person.Builder()
>                                                                            .setFirstName(entry.get(
"givenName" ).getString())
>                                                                            .setLastName(entry.get(
"sn" ).getString())
>                                                                            .build();
>                                              }
>                               };
>                               
>                public Person searchLdapForCeoUsingTemplate() {
>                               LdapConnectionConfig config = new LdapConnectionConfig();
>                               config.setLdapHost( <my host> );
>                               config.setLdapPort( <my port> );
>                               config.setName( <my DN> );
>                               config.setCredentials( <my Password> );
>                               DefaultLdapConnectionFactory factory = new DefaultLdapConnectionFactory(
config );
>                               factory.setTimeOut( 30000 );
>                               // optional, values below are defaults
>                               GenericObjectPool.Config poolConfig = new GenericObjectPool.Config();
>                               poolConfig.lifo = true;
>                               poolConfig.maxActive = 8;
>                               poolConfig.maxIdle = 8;
>                               poolConfig.maxWait = -1L;
>                               poolConfig.minEvictableIdleTimeMillis = 1000L * 60L * 30L;
>                               poolConfig.minIdle = 0;
>                               poolConfig.numTestsPerEvictionRun = 3;
>                               poolConfig.softMinEvictableIdleTimeMillis = -1L;
>                               poolConfig.testOnBorrow = false;
>                               poolConfig.testOnReturn = false;
>                               poolConfig.testWhileIdle = false;
>                               poolConfig.timeBetweenEvictionRunsMillis = -1L;
>                               poolConfig.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK;
>                               LdapConnectionTemplate ldapConnectionTemplate = 
>                                   new LdapConnectionTemplate( new LdapConnectionPool(
>                                       new PoolableLdapConnectionFactory( factory ), poolConfig
) );
>                               
>                               List<Person> allThePeople = ldapConnectionTemplate.search(

>                                                  <my search base>, 
>                                                  <my query string>, 
>                                                  SearchScope.SUBTREE,
>                                                  personEntryMapper );
>                               
>                               for (Person p : allThePeople) {
>                                              System.out.println(p.getFirstName());
>                                              System.out.println(p.getLastName());
>                               }
>                               
>                               return allThePeople.get(0);
>                }
>                
>                public Entry searchLdapForCeo() {
>                               SearchCursor cursor = new SearchCursorImpl(null, 30000,
TimeUnit.SECONDS);
>         LdapConnection connection = new LdapNetworkConnection(<my host>, <my
port>);
>         Entry entry = null;
>         try {
>             connection.bind(<my DN>, <my password>);
>             
>             SearchRequest sr = new SearchRequestImpl();
>            sr.setBase(new Dn(<my search base>));
>             StringBuilder sb = new StringBuilder(<my query string>);
>             sr.setFilter(sb.toString());
>             sr.setScope( SearchScope.SUBTREE );
>             cursor = connection.search(sr);
>             Response response;
>             while (cursor.next() && cursor.isEntry()) {
>                 response = cursor.get();
>                 System.out.println(((SearchResultEntry)response).getEntry());
>                 entry = cursor.getEntry();
>             }
>             SearchResultDone done = cursor.getSearchResultDone();
>         } catch (LdapException ex) {
>             Logger.getLogger(LdapClient.class.getName()).log(Level.SEVERE, null, ex);
>         } catch (CursorException ex) {
>             Logger.getLogger(LdapClient.class.getName()).log(Level.SEVERE, null, ex);
>         } finally {
>             cursor.close();
>             try {
>                 connection.close();
>             } catch (IOException ex) {
>                 Logger.getLogger(LdapClient.class.getName()).log(Level.SEVERE, null,
ex);
>             }
>         }
>         return entry;
>                }
> }
> Here’s Person.java:
> /**
> * 
>  */
> public class Person {
>        
>        protected Person() {
>              
>        }
>        public static class Builder {
>              private Person person;
>              
>              public Builder() {
>                     this.person = new Person();
>              }
>              
>              public Builder setFirstName(String firstName) {
>                     this.person.firstName = firstName;
>                     return this;
>              }
>              
>              public Builder setLastName(String lastName) {
>                     this.person.lastName = lastName;
>                     return this;
>              }
>              public Person build() {
>                     return this.person;
>              }
>        }
>        
>        private String firstName;
>        private String lastName;
>        
>        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;
>        }
> }
> Here’s my JUnit test:
> /**
> * 
>  */
> import static org.junit.Assert.*;
> import org.junit.After;
> import org.junit.AfterClass;
> import org.junit.Before;
> import org.junit.BeforeClass;
> import org.junit.Test;
> public class LdapClientTest {
>        private static LdapClient ldapClient;
>        
>        @BeforeClass
>        public static void setUpBeforeClass() throws Exception {
>              ldapClient = new LdapClient();
>        }
>        @Before
>        public void setUp() throws Exception {
>        }
>        @Test
>        public void testSearchLdapForCeo() {
>              assertNotEquals(null, ldapClient.searchLdapForCeo());
>        }
>        
>        @Test
>        public void testSearchLdapForCeoUsingLdapSearchTemplate() {
>              assertNotEquals(null, ldapClient.searchLdapForCeoUsingTemplate());
>        }
>        
>        @After
>        public void tearDown() throws Exception {
>        }
>        
>        @AfterClass
>              public static void tearDownAfterClass() throws Exception {
>        }
> }



--
This message was sent by Atlassian JIRA
(v6.2#6252)

Mime
View raw message