Example usage for javax.naming.ldap LdapContext getRequestControls

List of usage examples for javax.naming.ldap LdapContext getRequestControls

Introduction

In this page you can find the example usage for javax.naming.ldap LdapContext getRequestControls.

Prototype

public Control[] getRequestControls() throws NamingException;

Source Link

Document

Retrieves the request controls in effect for this context.

Usage

From source file:org.springframework.ldap.control.AbstractRequestControlDirContextProcessor.java

/**
 * Get the existing RequestControls from the LdapContext, call
 * {@link #createRequestControl()} to get a new instance, build a new array
 * of Controls and set it on the LdapContext.
 * <p>// w  w w .j av  a 2  s  .com
 * The {@link Control} feature is specific for LDAP v3 and thus applies only
 * to {@link LdapContext}. However, the generic DirContextProcessor
 * mechanism used for calling <code>preProcess</code> and
 * <code>postProcess</code> uses DirContext, since it also works for LDAP
 * v2. This is the reason that DirContext has to be cast to a LdapContext.
 * 
 * @param ctx an LdapContext instance.
 * @throws NamingException
 * @throws IllegalArgumentException if the supplied DirContext is not an
 * LdapContext.
 */
public void preProcess(DirContext ctx) throws NamingException {
    LdapContext ldapContext;
    if (ctx instanceof LdapContext) {
        ldapContext = (LdapContext) ctx;
    } else {
        throw new IllegalArgumentException(
                "Request Control operations require LDAPv3 - " + "Context must be of type LdapContext");
    }

    Control[] requestControls = ldapContext.getRequestControls();
    if (requestControls == null) {
        requestControls = new Control[0];
    }
    Control newControl = createRequestControl();

    Control[] newControls = new Control[requestControls.length + 1];
    for (int i = 0; i < requestControls.length; i++) {
        if (replaceSameControlEnabled && requestControls[i].getClass() == newControl.getClass()) {
            log.debug("Replacing already existing control in context: " + newControl);
            requestControls[i] = newControl;
            ldapContext.setRequestControls(requestControls);
            return;
        }
        newControls[i] = requestControls[i];
    }

    // Add the new Control at the end of the array.
    newControls[newControls.length - 1] = newControl;

    ldapContext.setRequestControls(newControls);
}