Can I somehow define sizelimit for my ldap searches?
Can I somehow define sizelimit for my ldap searches?
You can use one of the search methods that take SearchControls as an argument and set that parameter manually.
However if the sizelimit is actually exceeded you will get a SearchLimitExceededException thrown from LdapTemplate, which will cause the collected objects thus far to be lost, which may or may not be what you want.
If you want to keep the results you'll need to call the search(Name, String, SearchControls, SearchResultCallbackHandler) method and create the SearchResultCallbackHandler (e.g. a LdapTemplate.ContextMapperCallbackHandler) manually.
Hello...
Here is my test code:
and I've got a:Code:private static Logger log = Logger.getLogger(TestSpringLDAP.class); @Test public void getAllUsers() { LdapContextSource source = new LdapContextSource(); source.setUrl(TestConfig.LDAP_CONNECTION_URL); source.setBase(TestConfig.LDAP_BASE); source.setUserName(TestConfig.LDAP_USER_NAME); source.setPassword(TestConfig.LDAP_PASSWORD); source.setDirObjectFactory(DefaultDirObjectFactory.class); source.setAnonymousReadOnly(true); source.setContextFactory(com.sun.jndi.ldap.LdapCtxFactory.class); source.setCacheEnvironmentProperties(false); source.setPooled(false); LdapTemplate template = new LdapTemplate(source); template.setIgnorePartialResultException(true); SearchControls searchControls = new SearchControls(); searchControls.setCountLimit(5); searchControls.setReturningObjFlag(true); searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); List result = null; result = template.search("", "(objectClass=person)", searchControls, new PersonContextMapper()); } private static class PersonContextMapper implements ContextMapper { public Object mapFromContext(Object ctx) { DirContextAdapter context = (DirContextAdapter) ctx; String result = context.getStringAttribute("uid"); Person person = new Person(); System.out.println("ctx " + ctx); return result; } }
LDAP: error code 4 - Sizelimit Exceeded]; remaining name ''
What's wrong?
Thanx for reply.
But method search(Name, String, SearchControls, SearchResultCallbackHandler) is in the LdapTemplate package from http://sourceforge.net/projects/ldaptemplate/ and there is not defined LdapTemplate(LdapContextSource) constructor.
Hello,
As stated in this thread I now pass an AttributesMapperCallbackHandler to the search method and still get SearchLimitExceededException. I even tried setting count limit in SearchControls to 0.
Here's the code:
CollectingNameClassPairCallbackHandler handler = ldapTemplate.new AttributesMapperCallbackHandler(new PersonAttributesMapper());
ldapTemplate.search("", orFilter.encode(), searchControls, handler);
<bean id="searchControls" class="javax.naming.directory.SearchControls">
<property name="countLimit" value="0" />
<property name="searchScope" value="2" />
</bean>
<bean id="ldapSearchBean" class="de.mun.services.cd.LdapSearchServiceImpl">
<property name="ldapTemplate"><ref bean="ldapTemplate"/></property>
<property name="searchControls"><ref bean="searchControls"/></property>
</bean>
Please help :-(
It's possible to specify search limit in server configuration as well, so there's always the possibility of getting that exception even though you haven't specified any count limit yourself.
Also, using an AttributesMapperCallbackHandler will not prevent any exceptions from being thrown. It does however give you access to the results that have been processed up to when the exception is thrown, using the getList() method of CollectingNameClassPairCallbackHandler.
Hello,
Thanks for the reply.
If I implement the same search functionality in the traditional way using InitialDirContext, I get all the results returned without any exception being thrown.
ctx = new InitialDirContext(env);
SearchControls ctls = new SearchControls();
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
ctls.setReturningAttributes (ldapattrs );
NamingEnumeration answer = ctx.search(CONTEXT, filter, ctls);
This implies there's no search limit specified on the server. So what are the other possibilities of getting the exception? Is there no way of getting all the results?
Did you actually try to loop through the NamingEnumeration you get back in the plain JNDI code? The LimitExceededException may be thrown when calling NamingEnumeration.hasMore().
Once again, if the search limit is actually exceeded there's really no simple way of avoiding the Exception being thrown. But you can get hold of the entries collected up to when the exception was thrown using e.g. an AttributesMapperCallbackHandler instantiated by you before you call LdapTemplate. That instance holds a list that will be filled with all entries found so far.