private void handleLdapSearch(final ILdapClientConnection connection, List<ILdapSearchEntry> searchEntries, LDAPIdentitySource config,
            LdapConfigBean ldapConfigBean, ILdapComponent ldapComponent, IPolicyContext context, String username,
            String password, final IAsyncResultHandler<Boolean> handler) {

        if (searchEntries.size() > 1) {
            NamingException ex = new NamingException("Found multiple entries for the same username: " + username); //$NON-NLS-1$
            handler.handle(AsyncResultImpl.<Boolean>create(ex));
        } else if (searchEntries.isEmpty()) {
            handler.handle(AsyncResultImpl.create(Boolean.FALSE));
        } else { // Just one result
            String userDn = searchEntries.get(0).getDn(); // First entry
            if (userDn != null) {
                ldapConfigBean.setBindDn(userDn);
                ldapConfigBean.setBindPassword(password);
                bind(config, ldapConfigBean, ldapComponent, context, new IAsyncResultHandler<ILdapResult>() {

                    @Override
                    public void handle(IAsyncResult<ILdapResult> result) {
                        if (result.isError()) {
                            if (result.getError() instanceof LdapException) {
                                LdapException ex = (LdapException) result.getError();
                                if (ex.getResultCode().isAuthFailure()) {
                                    handler.handle(AsyncResultImpl.create(Boolean.FALSE));
                                } else {
                                    handler.handle(AsyncResultImpl.<Boolean>create(ex));
                                }
                                connection.close(ex);
                            } else {
                                handler.handle(AsyncResultImpl.<Boolean>create(result.getError()));
                                connection.close();
                            }
                        } else {
                            LdapResultCode resultCode = result.getResult().getResultCode();
                            if (LdapResultCode.isSuccess(resultCode)) {
                                handler.handle(AsyncResultImpl.create(Boolean.TRUE));
                            } else {
                                handler.handle(AsyncResultImpl.create(Boolean.FALSE));// TODO handle errors better?
                            }
                            connection.close();
                        }
                    }
                });
            } else {
                handler.handle(AsyncResultImpl.create(Boolean.FALSE));
            }
        }
    }
@see io.apiman.gateway.engine.policies.auth.IIdentityValidator#validate(java.lang.String, java.lang.String, io.apiman.gateway.engine.beans.ApiRequest, io.apiman.gateway.engine.policy.IPolicyContext, java.lang.Object, io.apiman.gateway.engine.async.IAsyncResultHandler)
gateway/engine/policies/src/main/java/io/apiman/gateway/engine/policies/auth/LDAPIdentityValidator.java