Search in sources :

Example 11 with SearchHelper

use of com.novell.ldapchai.util.SearchHelper in project ldapchai by ldapchai.

the class JLDAPProviderImpl method searchImpl.

public Map<String, Map<String, List<String>>> searchImpl(final String baseDN, final SearchHelper searchHelper, final boolean onlyFirstValue) throws ChaiOperationException, ChaiUnavailableException, IllegalStateException {
    activityPreCheck();
    // make a copy so if it changes somewhere else we won't be affected.
    final SearchHelper effectiveSearchHelper = new SearchHelper(searchHelper);
    // replace a null dn with an empty string
    final String effectiveBaseDN = baseDN != null ? baseDN : "";
    final int ldapScope;
    switch(effectiveSearchHelper.getSearchScope()) {
        case ONE:
            ldapScope = LDAPConnection.SCOPE_ONE;
            break;
        case BASE:
            ldapScope = LDAPConnection.SCOPE_BASE;
            break;
        case SUBTREE:
            ldapScope = LDAPConnection.SCOPE_SUB;
            break;
        default:
            ldapScope = -1;
    }
    final Map<String, Map<String, List<String>>> returnMap = new LinkedHashMap<>();
    final LDAPSearchConstraints constraints = new LDAPSearchConstraints();
    constraints.setMaxResults(effectiveSearchHelper.getMaxResults());
    constraints.setTimeLimit(effectiveSearchHelper.getTimeLimit());
    final String[] returnAttributes = effectiveSearchHelper.getAttributes() == null ? null : effectiveSearchHelper.getAttributes().toArray(new String[effectiveSearchHelper.getAttributes().size()]);
    final LDAPSearchResults results;
    try {
        results = ldapConnection.search(effectiveBaseDN, ldapScope, effectiveSearchHelper.getFilter(), returnAttributes, false, constraints);
        while (results.hasMore()) {
            final LDAPEntry loopEntry = results.next();
            final String loopDN = loopEntry.getDN();
            final Map<String, List<String>> loopAttributes = new LinkedHashMap<>();
            final LDAPAttributeSet attrSet = loopEntry.getAttributeSet();
            for (final Object anAttrSet : attrSet) {
                final LDAPAttribute loopAttr = (LDAPAttribute) anAttrSet;
                if (onlyFirstValue) {
                    loopAttributes.put(loopAttr.getName(), Collections.singletonList(loopAttr.getStringValue()));
                } else {
                    loopAttributes.put(loopAttr.getName(), Arrays.asList(loopAttr.getStringValueArray()));
                }
            }
            returnMap.put(loopDN, loopAttributes);
        }
    } catch (LDAPException e) {
        if (!returnMap.isEmpty()) {
            return Collections.unmodifiableMap(returnMap);
        }
        throw ChaiOperationException.forErrorMessage(e.getLDAPErrorMessage());
    }
    return Collections.unmodifiableMap(returnMap);
}
Also used : LDAPAttribute(com.novell.ldap.LDAPAttribute) LDAPSearchConstraints(com.novell.ldap.LDAPSearchConstraints) LDAPAttributeSet(com.novell.ldap.LDAPAttributeSet) SearchHelper(com.novell.ldapchai.util.SearchHelper) LinkedHashMap(java.util.LinkedHashMap) LDAPEntry(com.novell.ldap.LDAPEntry) LDAPSearchResults(com.novell.ldap.LDAPSearchResults) LDAPException(com.novell.ldap.LDAPException) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 12 with SearchHelper

use of com.novell.ldapchai.util.SearchHelper in project ldapchai by ldapchai.

the class JLDAPProviderImpl method search.

@ChaiProvider.LdapOperation
public Map<String, Map<String, String>> search(final String baseDN, final String filter, final Set<String> attributes, final SearchScope searchScope) throws ChaiOperationException, ChaiUnavailableException, IllegalStateException {
    activityPreCheck();
    getInputValidator().search(baseDN, filter, attributes, searchScope);
    final SearchHelper searchHelper = new SearchHelper();
    searchHelper.setFilter(filter);
    searchHelper.setAttributes(attributes);
    searchHelper.setSearchScope(searchScope);
    return search(baseDN, searchHelper);
}
Also used : SearchHelper(com.novell.ldapchai.util.SearchHelper)

Example 13 with SearchHelper

use of com.novell.ldapchai.util.SearchHelper in project ldapchai by ldapchai.

the class JNDIProviderImpl method searchMultiValues.

public final Map<String, Map<String, List<String>>> searchMultiValues(final String baseDN, final String filter, final Set<String> attributes, final SearchScope searchScope) throws ChaiUnavailableException, ChaiOperationException {
    activityPreCheck();
    getInputValidator().searchMultiValues(baseDN, filter, attributes, searchScope);
    final SearchHelper searchHelper = new SearchHelper();
    searchHelper.setFilter(filter);
    searchHelper.setAttributes(attributes);
    searchHelper.setSearchScope(searchScope);
    final SearchEngine searchEngine = new SearchEngine(chaiConfig, baseDN, searchHelper, true);
    return searchEngine.getResults();
}
Also used : SearchHelper(com.novell.ldapchai.util.SearchHelper)

Example 14 with SearchHelper

use of com.novell.ldapchai.util.SearchHelper in project ldapchai by ldapchai.

the class ApacheLdapProviderImpl method searchMultiValues.

public Map<String, Map<String, List<String>>> searchMultiValues(final String baseDN, final String filter, final Set<String> attributes, final SearchScope searchScope) throws ChaiOperationException, ChaiUnavailableException, IllegalStateException {
    activityPreCheck();
    getInputValidator().searchMultiValues(baseDN, filter, attributes, searchScope);
    final SearchHelper searchHelper = new SearchHelper();
    searchHelper.setFilter(filter);
    searchHelper.setAttributes(attributes);
    searchHelper.setSearchScope(searchScope);
    return searchImpl(baseDN, searchHelper, true);
}
Also used : SearchHelper(com.novell.ldapchai.util.SearchHelper)

Example 15 with SearchHelper

use of com.novell.ldapchai.util.SearchHelper in project ldapchai by ldapchai.

the class SearchHelperTester method testAndFilter.

public void testAndFilter() throws Exception {
    final LinkedHashMap<String, String> props = new LinkedHashMap<String, String>();
    props.put("objectClass", "inetOrgPerson");
    props.put("cn", "joe");
    final SearchHelper sh = new SearchHelper();
    sh.setFilterAnd(props);
    final String expectedFilter = "(&(objectClass=inetOrgPerson)(cn=joe))";
    final String filterFromHelper = sh.getFilter();
    Assert.assertEquals(expectedFilter, filterFromHelper);
}
Also used : SearchHelper(com.novell.ldapchai.util.SearchHelper) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

SearchHelper (com.novell.ldapchai.util.SearchHelper)15 Map (java.util.Map)8 LinkedHashMap (java.util.LinkedHashMap)5 ChaiOperationException (com.novell.ldapchai.exception.ChaiOperationException)4 HashMap (java.util.HashMap)4 PwmUnrecoverableException (password.pwm.error.PwmUnrecoverableException)4 ChaiUnavailableException (com.novell.ldapchai.exception.ChaiUnavailableException)3 UserIdentity (password.pwm.bean.UserIdentity)3 ErrorInformation (password.pwm.error.ErrorInformation)3 PwmOperationalException (password.pwm.error.PwmOperationalException)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 TreeMap (java.util.TreeMap)2 SearchConfiguration (password.pwm.ldap.search.SearchConfiguration)2 UserSearchEngine (password.pwm.ldap.search.UserSearchEngine)2 LDAPAttribute (com.novell.ldap.LDAPAttribute)1 LDAPAttributeSet (com.novell.ldap.LDAPAttributeSet)1 LDAPEntry (com.novell.ldap.LDAPEntry)1 LDAPException (com.novell.ldap.LDAPException)1 LDAPSearchConstraints (com.novell.ldap.LDAPSearchConstraints)1