1

我正在尝试使用 Com4j 查询 ldap 以检索所有内部员工。下面的代码有效,但总是返回 960 个结果,而应该有数千个结果。有办法指定最大结果大小还是我应该做一些不同的事情?

public class SearchInternalPersons {

private static final Logger LOGGER = LoggerFactory.getLogger(SearchInternalPersons.class);

private static final String DEFAULT_FIELDS = "sAMAccountName,givenName,sn,employeeType";

public static void main(final String[] args) throws Exception {
    final Map<String, String> AD2attribute = Maps.newHashMap();
    final StringTokenizer tokenizer = new StringTokenizer(DEFAULT_FIELDS, ",");
    while (tokenizer.hasMoreTokens()) {
        final String token = tokenizer.nextToken();
        AD2attribute.put(token, token);
    }
    final _Connection con = ClassFactory.createConnection();
    con.provider("ADsDSOObject");
    con.open("Active Directory Provider", StringUtils.EMPTY, StringUtils.EMPTY, -1);
    final _Command cmd = ClassFactory.createCommand();
    cmd.activeConnection(con);
    String command = createCommand();
    LOGGER.debug("Command=" + command);
    cmd.commandText(command);
    _Recordset rs = cmd.execute(null, Variant.getMissing(), -1);
    if (rs.eof()) {
        LOGGER.error("no users not found.");
    } else {
        System.out.println(rs.pageCount()); // prints 96
        System.out.println(rs.pageSize()); // prints 10
        System.out.println(rs.recordCount()); // prints 960
        for (int i=0;i<63;i++) {
            System.out.println(rs.properties(i).name() + ":" + rs.properties(i).value());
        }
        for (int i = 0; i < rs.recordCount(); i++) {
            final Fields userData = rs.fields();
            final Map<String, String> userDataAttributes = new HashMap<String, String>();
            for (int j = 0; j < userData.count(); j++) {
                final Field field = userData.item(j);
                final String attribute = AD2attribute.get(field.name());
                if (attribute != null && !attribute.isEmpty()) {
                    final Object value = field.value();
                    final String textValue = (value == null) ? StringUtils.EMPTY : value.toString();
                    LOGGER.debug(field.name() + "=" + textValue);
                    userDataAttributes.put(attribute, textValue);
                }
            }
            rs.moveNext();
        }
    }
    rs.close();
    con.close();
}

private static String createCommand() {
    final StringBuilder commandBuilder = new StringBuilder("<LDAP://");
    commandBuilder.append((String) COM4J.getObject(IADs.class, "LDAP://RootDSE", null).get("defaultNamingContext"));
    commandBuilder.append(">;(employeeType=employee);");
    commandBuilder.append(DEFAULT_FIELDS);
    commandBuilder.append(";subTree");
    return commandBuilder.toString();
}

}

ResultSet 属性如下:

IAccessor:true
IColumnsInfo:true
IColumnsInfo2:true
IConvertType:true
IGetSession:true
IRow:false
IGetRow:true
IRowset:true
IRowsetIdentity:true
IRowsetInfo:true
IRowsetLocate:true
IRowsetScroll:true
Preserve on Abort:false
Blocking Storage Objects:true
Use Bookmarks:true
Skip Deleted Bookmarks:false
Bookmark Type:1
Fetch Backwards:true
Hold Rows:true
Scroll Backwards:true
Column Privileges:true
Preserve on Commit:false
Immobile Rows:true
Literal Bookmarks:false
Literal Row Identity:true
Maximum Open Rows:0
Maximum Pending Rows:0
Maximum Rows:0
Notification Phases:0
Column Set Notification:0
Row Delete Notification:0
Row First Change Notification:0
Row Insert Notification:0
Row Resynchronization Notification:0
Rowset Release Notification:0
Rowset Fetch Position Change Notification:0
Row Undo Change Notification:0
Row Undo Delete Notification:0
Row Undo Insert Notification:0
Row Update Notification:0
Bookmarks Ordered:true
Own Inserts Visible:false
Own Changes Visible:false
Quick Restart:true
Reentrant Events:true
Remove Deleted Rows:false
Report Multiple Changes:false
Row Privileges:false
Row Threading Model:1
Strong Row Identity:false
Asynchronous:false
Deref Aliases:0
Size Limit:0
Server Time Limit:0
Column Names only:false
SearchScope:2
Timeout:0
Page size:0
Time limit:0
Chase referrals:0
Sort On:null
Cache Results:true
Bookmarkable:true
4

2 回答 2

2

我可以通过更改属性“页面大小”来解决这个问题,如下所示:

cmd.properties("Page Size").value(50);

当然,除 50 之外的其他值也是有效的。

于 2013-03-19T13:02:06.600 回答
1

即使您的参数包括时间限制和大小限制等资源限制,这些值(称为客户端请求的资源限制)也不能覆盖服务器的限制。这是因为服务器保留了覆盖客户端请求资源限制的能力(由 LDAP 标准指定)。换句话说,客户端请求的资源限制永远不会覆盖服务器的限制,除非服务器支持不受任何资源限制或访问控制的根 DN 的概念,并且 LDAP 客户端使用它来验证其与服务器的连接根 DN。

另一种可能性是搜索请求参数过滤掉了您期望的一些条目。尝试使用已知良好的命令行工具,例如ldapsearch查询服务器并计算返回的条目数。

也可以看看

于 2013-03-19T09:40:27.623 回答