Example usage for javax.naming.ldap InitialLdapContext getResponseControls

List of usage examples for javax.naming.ldap InitialLdapContext getResponseControls

Introduction

In this page you can find the example usage for javax.naming.ldap InitialLdapContext getResponseControls.

Prototype

public Control[] getResponseControls() throws NamingException 

Source Link

Usage

From source file:com.adito.activedirectory.PagedResultTemplate.java

private void doPagedSearch(InitialLdapContext context, String filter, String[] attributes,
        PagedResultMapper mapper) throws NamingException {
    SearchControls constraints = new SearchControls();
    constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
    applyControls(context, pageSize);//w w  w .java2 s.co m

    for (String searchBase : ouSearchBase) {
        if (logger.isDebugEnabled()) {
            logger.debug("Looking for items starting at " + searchBase + " (filter = " + filter + ")");
        }

        try {
            int currentPage = 1;
            int startPosition = 0;
            int endPosition = pageSize - 1;
            byte[] cookie = null;

            do {
                String range = startPosition + "-" + endPosition;

                if (logger.isDebugEnabled()) {
                    logger.debug("Starting search on page " + currentPage + " " + range);
                }

                constraints.setReturningAttributes(attributes);
                NamingEnumeration<SearchResult> results = context.search(searchBase, filter, constraints);

                try {
                    mapResults(mapper, results);
                } catch (PartialResultException pre) {
                    // We're paging so we dont care and don't log anymore
                }

                // Examine the paged results control response
                Control[] controls = context.getResponseControls();
                if (controls != null) {
                    for (int index = 0; index < controls.length; index++) {
                        if (controls[index] instanceof PagedResultsResponseControl) {
                            PagedResultsResponseControl prrc = (PagedResultsResponseControl) controls[index];
                            cookie = prrc.getCookie();
                        }
                    }
                }

                applyControls(context, pageSize, cookie);
                startPosition = startPosition + pageSize;
                endPosition = endPosition + pageSize;
                currentPage++;
            } while ((cookie != null) && (cookie.length != 0));
        } catch (NamingException e) {
            mapper.processException(e);
            logger.error("Possible configuration error! Did you enter your OUs correctly? [" + searchBase + "]",
                    e);
        }
    }
}

From source file:org.atricore.idbus.idojos.ldapidentitystore.LDAPBindIdentityStore.java

/**
 * This store performs a bind to the configured LDAP server and closes the connection immediately.
 * If the connection fails, an exception is thrown, otherwise this method returns silentrly
 *
 * @return true if the bind is successful
 *///from   www  . j a  va2s.com
public boolean bind(String username, String password, BindContext bindCtx) throws SSOAuthenticationException {

    String dn = null;

    try {

        // first try to retrieve the user using an known user
        dn = selectUserDN(username);
        if (dn == null || "".equals(dn)) {
            if (logger.isDebugEnabled())
                logger.debug("No DN found for user : " + username);
            return false;
        }
        logger.debug("user dn = " + dn);

        // Create context without binding!
        InitialLdapContext ctx = this.createLdapInitialContext(null, null);
        Control[] ldapControls = null;

        try {

            ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, dn);
            ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, password);

            if (isPasswordPolicySupport()) {
                // Configure request control for password policy:
                ctx.reconnect(new Control[] { new BasicControl(PasswordPolicyResponseControl.CONTROL_OID) });
            } else {
                ctx.reconnect(new Control[] {});
            }

            // Get response controls from reconnect BEFORE dn search, or they're lost
            ldapControls = ctx.getResponseControls();

            // Bind to LDAP an check for authentication warning/errors reported in password policy control:
            if (validateBindWithSearch) {
                selectUserDN(ctx, username);

                // Perhaps controls are not send during reconnet, try to get them now
                if (ldapControls == null || ldapControls.length == 0)
                    ldapControls = ctx.getResponseControls();
            }

            if (logger.isTraceEnabled())
                logger.trace("LDAP Bind with user credentials succeeded");

        } catch (AuthenticationException e) {

            if (logger.isDebugEnabled())
                logger.debug("LDAP Bind Authentication error : " + e.getMessage(), e);

            return false;

        } finally {

            if (isPasswordPolicySupport()) {

                // If an exception occurred, controls are not retrieved yet
                if (ldapControls == null || ldapControls.length == 0)
                    ldapControls = ctx.getResponseControls();

                // Check password policy LDAP Control
                PasswordPolicyResponseControl ppolicyCtrl = decodePasswordPolicyControl(ldapControls);
                if (ppolicyCtrl != null)
                    addPasswordPolicyToBindCtx(ppolicyCtrl, bindCtx);

            }

            ctx.close();
        }

        return true;

    } catch (Exception e) {
        throw new SSOAuthenticationException(
                "Cannot bind as user : " + username + " [" + dn + "]" + e.getMessage(), e);
    }

}