Search code examples
javaspringldapspring-ldapldapconnection

Implementation of timeout in LDAP


I have been handling an application in which we are using LDAP to fetch user details. Sometimes it will take more time to fetch user details. I want to implement time out on methods that fetch details so that we can avoid hanging transactions in server in worst case.

Here we are using LdapUtil class in which we have configured LdapTemplate class to fetch the required details.

How can we implement timeout on LDAP methods? (In this case ldapTemplate.search(...) methods)

public class LdapUtil {

    @Autowired(required = true)
    @Qualifier(value = "ldapTemplateApp")
    LdapTemplate ldapTemplate;

    public Set < ProductGroup > findProducts(String UserId) {
        final Set < ProductGroup > products = newHashSet();
        // Lookup the user         
        String usrFilter = String.format(USERID_FILTER, globalUserId);

        ldapTemplate.search("ou=Members", usrFilter, // note this line
        new NameClassPairCallbackHandler() {

            public void handleNameClassPair(NameClassPair nameClassPair) {
                SearchResult result = (SearchResult) nameClassPair;
                String user = result.getNameInNamespace();

                String GrpFilter = String.format(GROUP_FILTER, user);

                List < String > zonePrefixes = ldapTemplate.search("Zones", GrpFilter, // note this line
                new AttributesMapper() {
                    public Object mapFromAttributes(Attributes attributes) throws NamingException {
                        return substringBeforeLast((String) attributes.get("cn").get(), "-") + "-";
                    }
                });

            }
        });

        products.remove(null);
        return newHashSet(products);
    }
}   

We have one LDAP.xml in which ldapTemplete is configured

<beans xmlns="------">
<!-- LDAP -->
<bean id="contextSourceApp" class="org.springframework.ldap.pool.factory.PoolingContextSource">
<property name="contextSource" ref="contextSourceTargetApp" />
<property name="dirContextValidator">
<bean id="dirContextValidator"   
class="org.springframework.ldap.pool.validation.DefaultDirContextValidator"/>
</property>         
<property name="testOnBorrow" value="true" />
</bean>
<bean id="contextSourceTargetApp" class="org.springframework.ldap.core.support.LdapContextSource">
    <property name="url" value="${ldap.url}" />
    <property name="base" value="${ldap.base.}" />
    <property name="userDn" value="${ldap.user}" />
    <property name="password" value="${ldap.password}" />
    <property name="pooled" value="false" />
</bean>

<bean id="ldapTemplateApp" class="org.springframework.ldap.core.LdapTemplate">
    <constructor-arg ref="contextSourceApp" />
</bean>

I have few queries:

  1. How can we implement the TIMEOUT for LDAP methods and how to configure it?(In which class of LDAP framework timeout settings will be there)

  2. Is there any way to configure them in xml file i.e. LDAP.xml(in this case)?


Solution

  • I found a solution. I have added the following property in my ldap.xml file. So far it worked for me.

    <bean id="contextSourceTargetApp" 
          class="org.springframework.ldap.core.support.LdapContextSource">
        <property name="baseEnvironmentProperties">
            <map>
                <entry key="com.sun.jndi.ldap.connect.timeout" value="5000" />          
            </map>  
        </property>
    </bean>
    

    Please post any other solution if you have any idea about LDAP timeout implementation.