如何在内存LDAP服务器中为UnboundID添加/自定义控件

时间:2015-02-03 06:43:27

标签: java unit-testing ldap unboundid-ldap-sdk

我正在为我的代码编写单元测试,并且出于测试目的,我正在使用unboundsId的内存LDAP服务器。我创建并连接到内存服务器,之后我想执行同步请求,但服务器说" InMemory LDAP服务器不支持contentSyncRequestControl"。我查看了服务器API文档中有多少个控件。我尝试打印受支持的控件的OID,并且不存在contentSyncRequestControl的OID。所以我的问题是如何启用或添加控件到inMemory LDAP服务器?请参阅以下代码以获取参考。

public class InMemoryLDAPServer {
    private Logger logger = LoggerFactory.getLogger(InMemoryLDAPServer.class);

    private InMemoryDirectoryServer mServer;
    final String DEFAULT_INMEMORY_HOST = "localhost";
    final int DEFAULT_INMEMORY_PORT = 5389;
    final String LDAP_LISTENER_NAME = "LDAP_TEST_SERVER";
    final String INMEMORY_BASE = "dc=Contoso,dc=net";
    final String INMEMORY_DOMAIN = "Contoso.net";
    final String INMEMORY_USER = "uid=TestAdmin";
    final String INMEMORY_PASS = "password";


    public void start(int port) {
        try {

            InMemoryDirectoryServerConfig config =
                 new InMemoryDirectoryServerConfig(INMEMORY_BASE);
            config.setGenerateOperationalAttributes(true);
            config.addAdditionalBindCredentials(INMEMORY_USER, INMEMORY_PASS);
            config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig(LDAP_LISTENER_NAME, port));


            mServer = new InMemoryDirectoryServer(config);
            URI ldifFixture= InMemoryLDAPServer.class.getResource("/Contoso_rootdse_open.ldif").toURI();
            mServer.importFromLDIF(true, new LDIFReader(new File(ldifFixture)));


            mServer.startListening(LDAP_LISTENER_NAME); 
            // I tried here to check which controlls are supported
//The OID (1.3.6.1.4.1.4203.1.9.1.1) for the sync request control.
            LDAPConnection con = mServer.getConnection();
            RootDSE rootDSE = con.getRootDSE();
            String[] oids = rootDSE.getSupportedControlOIDs();

            for(int i=0; i<oids.length; i++){
                System.out.println(oids[i]);

            }
            con.close();

        } catch(Exception exception) {
            logger.error("Failed to start in memory ldap server", exception);
        }


    }

    public void start() {
        start(DEFAULT_INMEMORY_PORT);
    }

    public void stop() {
        try {
        mServer.shutDown(LDAP_LISTENER_NAME, true);
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}

结果

这是默认支持的控件OID。

1.2.840.113556.1.4.1413
1.2.840.113556.1.4.319
1.2.840.113556.1.4.473
1.2.840.113556.1.4.805
1.3.6.1.1.12
1.3.6.1.1.13.1
1.3.6.1.1.13.2
1.3.6.1.1.21.2
1.3.6.1.1.22
1.3.6.1.4.1.7628.5.101.1
2.16.840.1.113730.3.4.12
2.16.840.1.113730.3.4.16
2.16.840.1.113730.3.4.18
2.16.840.1.113730.3.4.2
2.16.840.1.113730.3.4.9

参考API文档:https://docs.ldap.com/ldap-sdk/docs/javadoc/index.html

请帮我正确配置。

1 个答案:

答案 0 :(得分:1)

这样做真的不是一个好方法。您可以添加对自定义扩展操作和SASL机制的支持,因为在这些情况下,自定义代码将执行所有处理。但是控件实际上改变了服务器处理操作的方式,因此它必须集成到服务器执行的核心处理中。

但是,如果您想要使用的控件足够简单,可以通过不更改操作的核心处理而只是更改请求或响应来支持它,那么您应该能够通过创建自定义InMemoryOperationInterceptor来实现这一点。 (虽然它不会显示在根DSE的受支持控件集中,除非您还截获了检索根DSE并在其中注入额外OID的请求。)

您对哪种控制感兴趣?它们是标准的还是专有的?内存中目录服务器旨在成为通用的,符合标准的服务器,并且它不适合包含专有元素。但是,如果它没有目前支持但可能有用的标准控件,那么我们可以考虑为其添加直接支持。

相关问题