/* User paged results to control page size. Some systems can have limited query size (for example Active Directory, which limits
* results count to 1000. So we must use Range property to perform multiple search
*/
PagedResultsControl pagedControls = new PagedResultsControl(500, Control.CRITICAL);
context.setRequestControls(new Control[] {pagedControls});
/* Cookie is used to inform server to send another page */
byte[] cookie = null;
int total = 0;
/* Sum of all users imported from LDAP */
int ldapUserCount = 0;
do
{
/* Create search controls and apply range limit */
SearchControls searchControls = createSearchControls(screenName, attributesToFetch);
NamingEnumeration<SearchResult> enu = searchLdapUsers(context, companyId, ldapServerId, screenName, searchControls);
/* There should be no exception here becouse we use paged searching */
while (enu.hasMore())
{
ldapUserCount++;
SearchResult result = enu.nextElement();
Attributes attributes = result.getAttributes();
String login = getAttributeValue(attributes, userMappingsScreenName, null);
if (login != null)
{
logger.info("Teta user login: "+login);
Properties userAttributes = propertiesMap.get(login);
if (userAttributes != null) {
for (String propertyName : customAttributes.stringPropertyNames()) {
String value = getAttributeValue(attributes, customAttributes.getProperty(propertyName), null);
logger.info("Teta user property="+propertyName+", value="+value);
if (value != null) {
userAttributes.setProperty(propertyName, value);
}
}
}
else
{
logger.info("Teta user userAttributes are empty for login="+login);
}
}
}
/* Search response controls form paged results control */
Control[] controls = context.getResponseControls();
if (controls != null)
{
for (int i = 0; i < controls.length; i++)
{
if (controls[i] instanceof PagedResultsResponseControl)
{
PagedResultsResponseControl prrc = (PagedResultsResponseControl)controls[i];
total = prrc.getResultSize();
cookie = prrc.getCookie();
/* Update ldap context. In this moment, we inform server that it should
* send another page of results
*
* If cookie == null, there is no more results
*/
pagedControls = new PagedResultsControl(500, cookie, Control.CRITICAL);
context.setRequestControls(new Control[] {pagedControls});
}
}
}