使用javax.naming查询LDAP服务器的信息?

时间:2013-07-29 10:34:08

标签: java ldap

我正在使用javax.naming。*库来处理LDAP服务器,只是想知道如何查询LDAP服务器以查看它是否支持特定的控件/扩展,特别是简单分页结果控件扩展?

http://www.ldapguru.info/ldap/the-root-dse.html提供了一些关于如何使用unboundid Java LDAP库进行操作的信息,但我找不到任何描述如何使用javax.naming。*库的文件?

非常感谢!

1 个答案:

答案 0 :(得分:0)

使用以下方法实现此目的:

private static boolean isSimplePagedResultsControlExtensionSupported(
          final LdapContext ctx, final String connectionUrl) throws NamingException {
        Attribute attribute = null;

    try {
      Attributes attributes = ctx.getAttributes("" /* rootDSE */,
          new String[]{"supportedcontrol"});
      attribute = attributes.get("supportedcontrol");
    } catch (NamingException namingException) {
      logger.warning("Error retrieving supportedControls from rootDSE in LDAP authority ["
          + connectionUrl
          + "] - unable to determine if the Simple Paged Results Control Extension (RFC2696) is supported: "
          + namingException.getMessage());
      return false;
    }

    if (attribute == null) {
      logger.warning("Error retrieving supportedControls from rootDSE in LDAP authority ["
          + connectionUrl
          + "] - unable to determine if the Simple Paged Results Control Extension (RFC2696) is supported");
      return false;
    }

    NamingEnumeration<?> namingEnumeration = attribute.getAll();
    while (namingEnumeration.hasMore()) {
      Object object = namingEnumeration.next();
      if (object instanceof String) {
        final String supportedControlOid = (String)object;
        if (supportedControlOid.equals(SIMPLE_PAGED_RESULTS_CONTROL_EXTENSION_OID)) {
          logger.info("LDAP authority [" 
              + connectionUrl 
              + "] reports that it supports the Simple Paged Results Control Extension (RFC2696)"
              + " - a page size of ["
              + DEFAULT_SIMPLE_PAGED_RESULTS_CONTROL_EXTENSION_PAGE_SIZE
              + "] will be used");

          namingEnumeration.close();
          return true;
        }
      }
    }

    logger.info("LDAP authority [" 
        + connectionUrl 
        + "] reports that it does not support the Simple Paged Results Control Extension (RFC2696)");

    namingEnumeration.close();
    return false;
  }