我想在javax.naming.ldap.LdapContext上设置放松控件(在https://tools.ietf.org/html/draft-zeilenga-ldap-relax-03中定义),但是我不知道如何正确构造控件:

LdapContext context = new InitialLdapContext(...);
Control[] controls = { new BasicControl(/* What to put here? */) };
context.setRequestControls(controls);

最佳答案

通常,您需要编写一个扩展BasicControl的类,并实现所有必需的ASN.1内容以进行编码和解码。鉴于官方JDK中缺少对ASN.1的支持,这并不是一件容易的事。

但是,由于此控件很简单:

import javax.naming.ldap.BasicControl;

/**
 * Relax Rules control
 * @author Esmond Pitt
 * @see <a href="https://tools.ietf.org/html/draft-zeilenga-ldap-relax-03">The Relax Rules Control</a>
 * @see <a href="http://stackoverflow.com/questions/30080294/how-to-set-relax-controls-on-a-ldap-context">Stack Overflow</a>
 */
public class RelaxRulesControl extends BasicControl
{
    /** The OID, see Tobias's answer for provenance. */
    public static final String  OID = "1.3.6.1.4.1.4203.666.5.12";

    /** Construct an instance with criticality = true */
    public RelaxRulesControl()
    {
        super(OID, true, null);
    }

    /** Construct an instance with critically as specified */
    public RelaxRulesControl(boolean critical)
    {
        super(OID, critical, null);
    }
}

08-16 17:58