Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
==================== DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. Copyright 2008-2009 Sun Microsystems, Inc. All rights reserved. Copyright 2011-2013 Tirasa. All rights reserved. The contents of this file are subject to the terms of the Common Development and Distribution License("CDDL") (the "License"). You may not use this file except in compliance with the License. You can obtain a copy of the License at https://oss.oracle.com/licenses/CDDL See the License for the specific language governing permissions and limitations under the License. When distributing the Covered Code, include this CDDL Header Notice in each file and include the License file at https://oss.oracle.com/licenses/CDDL. If applicable, add the following below this CDDL Header, with the fields enclosed by brackets [] replaced by your own identifying information: "Portions Copyrighted [year] [name of copyright owner]" ====================
 
 package org.connid.bundles.ad.search;
 
 import static java.util.Collections.singletonList;
 import static org.identityconnectors.common.StringUtil.isBlank;
 
 import java.util.List;
 import java.util.Set;
 
 public class ADSearch {
 
     private final LdapConnection conn;
 
     private final ObjectClass oclass;
 
     private final LdapFilter filter;
 
     private final OperationOptions options;
 
     private final String[] baseDNs;
 
     private final ADUtilities utils;
 
     private static final Log LOG = Log.getLog(ADSearch.class);
 
     public ADSearch(
             final LdapConnection conn,
             final ObjectClass oclass,
             final LdapFilter filter,
             final OperationOptions options,
             final String[] baseDNs) {
 
         this. = conn;
         this. = oclass;
         this. = filter;
         this. = options;
         this. = baseDNs;
 
         this. = new ADUtilities((ADConnectionthis.);
     }
 
     public ADSearch(
             final LdapConnection conn,
             final ObjectClass oclass,
             final LdapFilter filter,
             final OperationOptions options) {
 
         this(connoclassfilteroptionsoclass.is(.)
                 ? ((ADConfigurationconn.getConfiguration()).getUserBaseContexts()
                 : ((ADConfigurationconn.getConfiguration()).getGroupBaseContexts());
     }
 
     public final void executeADQuery(final ResultsHandler handler) {
         final String[] attrsToGetOption = .getAttributesToGet();
         final Set<StringattrsToGet = .getAttributesToGet(attrsToGetOption);
 
         final LdapInternalSearch search = getInternalSearch(attrsToGet);
        search.execute(new SearchResultsHandler() {
            @Override
            public boolean handle(String baseDNSearchResult result)
                    throws NamingException {
                return handler.handle(.createConnectorObject(
                        result.getNameInNamespace(),
                        result,
                        attrsToGet,
                        ));
            }
        });
    }
    private LdapInternalSearch getInternalSearch(Set<StringattrsToGet) {
        // This is a bit tricky. If the LdapFilter has an entry DN,
        // we only need to look at that entry and check whether it matches
        // the native filter. Moreover, when looking at the entry DN
        // we must not throw exceptions if the entry DN does not exist or is
        // not valid -- just as no exceptions are thrown when the native
        // filter doesn't return any values.
        //
        // In the simple case when the LdapFilter has no entryDN, we
        // will just search over our base DNs looking for entries
        // matching the native filter.
        LdapSearchStrategy strategy;
        List<Stringdns;
        int searchScope;
        final String filterEntryDN =  != null ? .getEntryDN() : null;
        if (filterEntryDN != null) {
            // Would be good to check that filterEntryDN is under the configured 
            // base contexts. However, the adapter is likely to pass entries
            // outside the base contexts, so not checking in order to be on the
            // safe side.
            strategy = new ADDefaultSearchStrategy(true);
            dns = singletonList(filterEntryDN);
            searchScope = .;
        } else {
            strategy = getSearchStrategy();
            dns = getBaseDNs();
            searchScope = getLdapSearchScope();
        }
        final SearchControls controls = LdapInternalSearch.createDefaultSearchControls();
        final Set<StringldapAttrsToGet = .getLdapAttributesToGet(attrsToGet);
        controls.setReturningAttributes(ldapAttrsToGet.toArray(new String[ldapAttrsToGet.size()]));
        controls.setSearchScope(searchScope);
        final String optionsFilter = LdapConstants.getSearchFilter();
        final String searchFilter = .equals(.)
                ? .getConfiguration().getAccountSearchFilter()
                : ((ADConfiguration.getConfiguration()).getGroupSearchFilter();
        final String nativeFilter =  != null ? .getNativeFilter() : null;
        final String membershipSearchFilter =
                .equals(.)
                        ? .getMembershipSearchFilter(((ADConfiguration.getConfiguration()))
                        : null;
        return new LdapInternalSearch(
                ,
                getSearchFilter(optionsFilternativeFiltersearchFiltermembershipSearchFilter),
                dns,
                strategy,
                controls);
    }
    private String getSearchFilter(String... optionalFilters) {
        final StringBuilder builder = new StringBuilder();
        final String ocFilter = getObjectClassFilter();
        int nonBlank = isBlank(ocFilter) ? 0 : 1;
        for (String optionalFilter : optionalFilters) {
            nonBlank += (isBlank(optionalFilter) ? 0 : 1);
        }
        if (nonBlank > 1) {
            builder.append("(&");
        }
        appendFilter(ocFilterbuilder);
        for (String optionalFilter : optionalFilters) {
            appendFilter(optionalFilterbuilder);
        }
        if (nonBlank > 1) {
            builder.append(')');
        }
        return builder.toString();
    }
        LdapSearchStrategy strategy;
        if (.is(.)) {
            // Only consider paged strategies for accounts,
            // just as the adapter does.
            boolean useBlocks = .getConfiguration().isUseBlocks();
            boolean usePagedResultsControl = .getConfiguration().isUsePagedResultControl();
            int pageSize = .getConfiguration().getBlockSize();
            if (useBlocks && !usePagedResultsControl && .supportsControl(.)) {
                String vlvSortAttr = .getConfiguration().getVlvSortAttribute();
                strategy = new ADVlvIndexSearchStrategy(vlvSortAttrpageSize);
            } else if (useBlocks && .supportsControl(.)) {
                strategy = new ADSimplePagedSearchStrategy(pageSize);
            } else {
                strategy = new ADDefaultSearchStrategy(false);
            }
        } else {
            strategy = new ADDefaultSearchStrategy(false);
        }
        return strategy;
    }
    private static void appendFilter(String filterStringBuilder toBuilder) {
        if (!isBlank(filter)) {
            String trimmedUserFilter = filter.trim();
            boolean enclose = filter.charAt(0) != '(';
            if (enclose) {
                toBuilder.append('(');
            }
            toBuilder.append(trimmedUserFilter);
            if (enclose) {
                toBuilder.append(')');
            }
        }
    }
    private List<StringgetBaseDNs() {
        List<Stringresult;
        QualifiedUid container = .getContainer();
        if (container != null) {
            result = singletonList(LdapSearches.findEntryDN(
                    container.getObjectClass(), container.getUid()));
        } else {
            result = Arrays.asList();
        }
        assert result != null;
        return result;
    }
    private String getObjectClassFilter() {
        StringBuilder builder = new StringBuilder();
        List<StringldapClasses =
                .getSchemaMapping().getLdapClasses();
        boolean and = ldapClasses.size() > 1;
        if (and) {
            builder.append("(&");
        }
        for (String ldapClass : ldapClasses) {
            builder.append("(objectClass=");
            builder.append(ldapClass);
            builder.append(')');
        }
        if (and) {
            builder.append(')');
        }
        return builder.toString();
    }
    private int getLdapSearchScope() {
        String scope = .getScope();
        if (scope == null) {
            if (.is(.)) {
                scope = ((ADConfiguration.getConfiguration()).getUserSearchScope();
            } else {
                scope = ((ADConfiguration.getConfiguration()).getGroupSearchScope();
            }
        }
        if (..equals(scope)) {
            return .;
        } else if (..equals(scope)) {
            return .;
        } else if (..equals(scope) || scope == null) {
            return .;
        } else {
            throw new IllegalArgumentException("Invalid search scope " + scope);
        }
    }
New to GrepCode? Check out our FAQ X