Search in sources :

Example 6 with Control

use of javax.naming.ldap.Control in project camel by apache.

the class LdapProducer method prepareNextPage.

private boolean prepareNextPage(LdapContext ldapContext) throws Exception {
    Control[] responseControls = ldapContext.getResponseControls();
    byte[] cookie = null;
    if (responseControls != null) {
        for (Control responseControl : responseControls) {
            if (responseControl instanceof PagedResultsResponseControl) {
                PagedResultsResponseControl prrc = (PagedResultsResponseControl) responseControl;
                cookie = prrc.getCookie();
            }
        }
    }
    if (cookie == null) {
        return false;
    } else {
        ldapContext.setRequestControls(new Control[] { new PagedResultsControl(pageSize, cookie, Control.CRITICAL) });
        return true;
    }
}
Also used : Control(javax.naming.ldap.Control) PagedResultsControl(javax.naming.ldap.PagedResultsControl) PagedResultsResponseControl(javax.naming.ldap.PagedResultsResponseControl) PagedResultsResponseControl(javax.naming.ldap.PagedResultsResponseControl) PagedResultsControl(javax.naming.ldap.PagedResultsControl)

Example 7 with Control

use of javax.naming.ldap.Control in project Openfire by igniterealtime.

the class LdapGroupTester method getGroups.

/**
     * Returns fist N groups found in LDAP. The returned groups are only able to return their name,
     * description and count of members. Count of members is considering all values that were found
     * in the member field.
     *
     * @param maxGroups max number of groups to return.
     * @return fist N groups found in the LDAP.
     */
public Collection<Group> getGroups(int maxGroups) {
    Collection<Group> groups = new ArrayList<>();
    LdapContext ctx = null;
    try {
        ctx = manager.getContext();
        // Sort on group name field.
        Control[] searchControl = new Control[] { new SortControl(new String[] { manager.getGroupNameField() }, Control.NONCRITICAL) };
        ctx.setRequestControls(searchControl);
        SearchControls searchControls = new SearchControls();
        // See if recursive searching is enabled. Otherwise, only search one level.
        if (manager.isSubTreeSearch()) {
            searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        } else {
            searchControls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
        }
        // Attributes to return for each group
        String[] standardAttributes = new String[3];
        standardAttributes[0] = manager.getGroupNameField();
        standardAttributes[1] = manager.getGroupDescriptionField();
        standardAttributes[2] = manager.getGroupMemberField();
        searchControls.setReturningAttributes(standardAttributes);
        // Limit results to those we'll need to process
        searchControls.setCountLimit(maxGroups);
        String filter = MessageFormat.format(manager.getGroupSearchFilter(), "*");
        NamingEnumeration answer = ctx.search("", filter, searchControls);
        while (answer.hasMoreElements()) {
            // Get the next group.
            Attributes attributes = ((SearchResult) answer.next()).getAttributes();
            String groupName = (String) attributes.get(manager.getGroupNameField()).get();
            String description = "";
            int elements = 0;
            try {
                description = ((String) attributes.get(manager.getGroupDescriptionField()).get());
            } catch (NullPointerException e) {
            // Do nothing since the group description field was not found
            } catch (Exception e) {
                Log.error("Error retrieving group description", e);
            }
            Attribute memberField = attributes.get(manager.getGroupMemberField());
            if (memberField != null) {
                NamingEnumeration ne = memberField.getAll();
                while (ne.hasMore()) {
                    ne.next();
                    elements = elements + 1;
                }
            }
            // Build Group with found information
            groups.add(new Group(groupName, description, elements));
        }
        // Close the enumeration.
        answer.close();
    } catch (Exception e) {
        Log.error(e.getMessage(), e);
    } finally {
        try {
            if (ctx != null) {
                ctx.setRequestControls(null);
                ctx.close();
            }
        } catch (Exception ignored) {
        // Ignore.
        }
    }
    return groups;
}
Also used : Attribute(javax.naming.directory.Attribute) ArrayList(java.util.ArrayList) Attributes(javax.naming.directory.Attributes) NamingEnumeration(javax.naming.NamingEnumeration) SearchResult(javax.naming.directory.SearchResult) SortControl(javax.naming.ldap.SortControl) Control(javax.naming.ldap.Control) SortControl(javax.naming.ldap.SortControl) SearchControls(javax.naming.directory.SearchControls) LdapContext(javax.naming.ldap.LdapContext)

Example 8 with Control

use of javax.naming.ldap.Control in project spring-security by spring-projects.

the class PasswordPolicyControlFactoryTests method returnsControlForCorrectOID.

@Test
public void returnsControlForCorrectOID() throws Exception {
    PasswordPolicyControlFactory ctrlFactory = new PasswordPolicyControlFactory();
    Control control = mock(Control.class);
    when(control.getID()).thenReturn(PasswordPolicyControl.OID);
    when(control.getEncodedValue()).thenReturn(PasswordPolicyResponseControlTests.OPENLDAP_LOCKED_CTRL);
    Control result = ctrlFactory.getControlInstance(control);
    assertThat(result).isNotNull();
    assertThat(PasswordPolicyResponseControlTests.OPENLDAP_LOCKED_CTRL).isEqualTo(result.getEncodedValue());
}
Also used : Control(javax.naming.ldap.Control)

Example 9 with Control

use of javax.naming.ldap.Control in project spring-security by spring-projects.

the class PasswordPolicyControlFactoryTests method returnsNullForUnrecognisedOID.

@Test
public void returnsNullForUnrecognisedOID() throws Exception {
    PasswordPolicyControlFactory ctrlFactory = new PasswordPolicyControlFactory();
    Control wrongCtrl = mock(Control.class);
    when(wrongCtrl.getID()).thenReturn("wrongId");
    assertThat(ctrlFactory.getControlInstance(wrongCtrl)).isNull();
}
Also used : Control(javax.naming.ldap.Control)

Example 10 with Control

use of javax.naming.ldap.Control in project cloudstack by apache.

the class OpenLdapUserManagerImpl method searchUsers.

@Override
public List<LdapUser> searchUsers(final String username, final LdapContext context) throws NamingException, IOException {
    final SearchControls searchControls = new SearchControls();
    searchControls.setSearchScope(_ldapConfiguration.getScope());
    searchControls.setReturningAttributes(_ldapConfiguration.getReturnAttributes());
    String basedn = _ldapConfiguration.getBaseDn();
    if (StringUtils.isBlank(basedn)) {
        throw new IllegalArgumentException("ldap basedn is not configured");
    }
    byte[] cookie = null;
    int pageSize = _ldapConfiguration.getLdapPageSize();
    context.setRequestControls(new Control[] { new PagedResultsControl(pageSize, Control.NONCRITICAL) });
    final List<LdapUser> users = new ArrayList<LdapUser>();
    NamingEnumeration<SearchResult> results;
    do {
        results = context.search(basedn, generateSearchFilter(username), searchControls);
        while (results.hasMoreElements()) {
            final SearchResult result = results.nextElement();
            if (!isUserDisabled(result)) {
                users.add(createUser(result));
            }
        }
        Control[] contextControls = context.getResponseControls();
        if (contextControls != null) {
            for (Control control : contextControls) {
                if (control instanceof PagedResultsResponseControl) {
                    PagedResultsResponseControl prrc = (PagedResultsResponseControl) control;
                    cookie = prrc.getCookie();
                }
            }
        } else {
            s_logger.info("No controls were sent from the ldap server");
        }
        context.setRequestControls(new Control[] { new PagedResultsControl(pageSize, cookie, Control.CRITICAL) });
    } while (cookie != null);
    return users;
}
Also used : PagedResultsResponseControl(javax.naming.ldap.PagedResultsResponseControl) ArrayList(java.util.ArrayList) SearchResult(javax.naming.directory.SearchResult) Control(javax.naming.ldap.Control) PagedResultsControl(javax.naming.ldap.PagedResultsControl) PagedResultsResponseControl(javax.naming.ldap.PagedResultsResponseControl) SearchControls(javax.naming.directory.SearchControls) PagedResultsControl(javax.naming.ldap.PagedResultsControl)

Aggregations

Control (javax.naming.ldap.Control)10 LdapContext (javax.naming.ldap.LdapContext)6 ArrayList (java.util.ArrayList)4 SearchControls (javax.naming.directory.SearchControls)4 SearchResult (javax.naming.directory.SearchResult)4 PagedResultsControl (javax.naming.ldap.PagedResultsControl)4 PagedResultsResponseControl (javax.naming.ldap.PagedResultsResponseControl)4 SortControl (javax.naming.ldap.SortControl)4 NamingException (javax.naming.NamingException)3 NamingEnumeration (javax.naming.NamingEnumeration)2 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)2 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)2 JiveInitialLdapContext (org.jivesoftware.util.JiveInitialLdapContext)2 IOException (java.io.IOException)1 Attribute (javax.naming.directory.Attribute)1 Attributes (javax.naming.directory.Attributes)1