Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright 2011 Tirasa. All rights reserved.
   *
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License. You can obtain
  * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
  * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  *
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
  * Sun designates this particular file as subject to the "Classpath" exception
  * as provided by Sun in the GPL Version 2 section of the License file that
  * accompanied this code.  If applicable, add the following below the License
  * Header, with the fields enclosed by brackets [] replaced by your own
  * identifying information: "Portions Copyrighted [year]
  * [name of copyright owner]"
  */
 package org.connid.ad.search;
 
 import  com.sun.jndi.ldap.ctl.VirtualListViewControl;
 import  com.sun.jndi.ldap.ctl.VirtualListViewResponseControl;
 import java.util.List;
 import static org.identityconnectors.common.StringUtil.isNotBlank;
 
 
 
     private static final Log LOG =
             Log.getLog(ADVlvIndexSearchStrategy.class);
 
     private final String vlvIndexAttr;
 
     private final int blockSize;
 
     private int index;
 
     private int lastListSize;
 
     private byte[] cookie;
 
     static String searchControlsToString(SearchControls controls) {
         StringBuilder builder = new StringBuilder();
         builder.append("SearchControls: {returningAttributes=");
         String[] attrs = controls.getReturningAttributes();
         builder.append(attrs != null ? Arrays.asList(attrs) : "null");
         builder.append(", scope=");
         switch (controls.getSearchScope()) {
             case .:
                 builder.append("OBJECT");
                 break;
             case .:
                 builder.append("ONELEVEL");
                 break;
             case .:
                 builder.append("SUBTREE");
                 break;
         }
         builder.append('}');
         return builder.toString();
     }
 
     public ADVlvIndexSearchStrategy(String vlvSortAttrint blockSize) {
         super(vlvSortAttrblockSize);
         this. = isNotBlank(vlvSortAttr) ? vlvSortAttr : "uid";
         this. = blockSize;
     }
 
     @Override
     public void doSearch(final LdapContext initCtx,
             final List<StringbaseDNs,
             final String query,
             final SearchControls searchControls,
             final SearchResultsHandler handler)
             throws IOExceptionNamingException {
 
         if (.isOk()) {
             .ok("Searching in {0} with filter {1} and {2}",
                     baseDNsquerysearchControlsToString(searchControls));
         }
        Iterator<StringbaseDNIter = baseDNs.iterator();
        boolean proceed = true;
        LdapContext ctx = initCtx.newInstance(null);
        try {
            while (baseDNIter.hasNext() && proceed) {
                proceed = searchBaseDN(
                        ctxbaseDNIter.next(), querysearchControlshandler);
            }
        } finally {
            ctx.close();
        }
    }
    private boolean searchBaseDN(
            final LdapContext ctx,
            final String baseDN,
            final String query,
            final SearchControls searchControls,
            final SearchResultsHandler handler)
            throws IOExceptionNamingException {
        if (.isOk()) {
            .ok("Searching in {0}"baseDN);
        }
         = 1;
         = 0;
         = null;
        String lastResultName = null;
        for (;;) {
            SortControl sortControl =
                    new SortControl(.);
            int afterCount =  - 1;
            VirtualListViewControl vlvControl = new VirtualListViewControl(
                    , 0, afterCount.);
            vlvControl.setContextID();
            if (.isOk()) {
                .ok("New search: target = {0}, afterCount = {1}",
                        afterCount);
            }
            ctx.setRequestControls(new Control[]{sortControlvlvControl});
            // Need to process the response controls, which are available after
            // all results have been processed, before sending anything to the caller
            // (because processing the response controls might throw exceptions that
            // invalidate anything we might have sent otherwise).
            // So storing the results before actually sending them to the handler.
            List<SearchResultresultList =
                    new ArrayList<SearchResult>();
            NamingEnumeration<SearchResultresults =
                    ctx.search(baseDNquerysearchControls);
            try {
                // hasMore call for referral resolution ... it fails with AD
                // while (results.hasMore()) {
                while (results.hasMoreElements()) {
                    SearchResult result = results.next();
                    boolean overlap = false;
                    if (lastResultName != null) {
                        if (lastResultName.equals(result.getName())) {
                            .warn(
                                    "Working around rounding error overlap at "
                                    + "index " + );
                            overlap = true;
                        }
                        lastResultName = null;
                    }
                    if (!overlap) {
                        resultList.add(result);
                    }
                }
            } finally {
                results.close();
            }
            processResponseControls(ctx.getResponseControls());
            SearchResult result = null;
            Iterator<SearchResultresultIter = resultList.iterator();
            while (resultIter.hasNext()) {
                result = resultIter.next();
                ++;
                if (!handler.handle(baseDNresult)) {
                    return false;
                }
            }
            if (result != null) {
                lastResultName = result.getName();
            }
            if ( > ) {
                break;
            }
            // DSEE seems to only have a single VLV index (although it claims to support more).
            // It returns at the server content count the sum of sizes of all indexes,
            // but it only returns the entries in the base context we are asking for.
            // So, in this case, index will never reach lastListSize. To avoid an infinite loop,
            // ending search if we received no results in the last iteration.
            if (resultList.isEmpty()) {
                .warn("Ending search because received no results");
                break;
            }
        }
        return true;
    }
    private void processResponseControls(final Control[] controls)
            throws NamingException {
        if (controls != null) {
            for (Control control : controls) {
                if (control instanceof SortResponseControl) {
                    SortResponseControl sortControl =
                            (SortResponseControlcontrol;
                    if (!sortControl.isSorted()
                            || (sortControl.getResultCode() != 0)) {
                        throw sortControl.getException();
                    }
                }
                if (control instanceof VirtualListViewResponseControl) {
                    VirtualListViewResponseControl vlvControl =
                            (VirtualListViewResponseControl) control;
                    if (vlvControl.getResultCode() == 0) {
                         = vlvControl.getListSize();
                         = vlvControl.getContextID();
                        if (.isOk()) {
                            .ok("Response control: lastListSize = {0}",
                                    );
                        }
                    } else {
                        throw vlvControl.getException();
                    }
                }
            }
        }
    }
New to GrepCode? Check out our FAQ X