/*
 * Copyright 2005-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.ldap.pool.validation;

import javax.naming.NamingEnumeration;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls;

import org.apache.commons.lang.Validate;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.ldap.pool.DirContextType;

/**
 * Default {@link DirContext} validator that executes {@link DirContext#search(String, String, SearchControls)}. The
 * name, filter and {@link SearchControls} are all configurable. There is no special handling for read only versus
 * read write {@link DirContext}s.
 * 
 * <br>
 * <br>
 * Configuration:
 * <table border="1">
 *     <tr>
 *         <th align="left">Property</th>
 *         <th align="left">Description</th>
 *         <th align="left">Required</th>
 *         <th align="left">Default</th>
 *     </tr>
 *     <tr>
 *         <td valign="top">base</td>
 *         <td valign="top">
 *             The name parameter to the search method.
 *         </td>
 *         <td valign="top">No</td>
 *         <td valign="top">""</td>
 *     </tr>
 *     <tr>
 *         <td valign="top">filter</td>
 *         <td valign="top">
 *             The filter parameter to the search method.
 *         </td>
 *         <td valign="top">No</td>
 *         <td valign="top">"objectclass=*"</td>
 *     </tr>
 *     <tr>
 *         <td valign="top">searchControls</td>
 *         <td valign="top">
 *             The {@link SearchControls} parameter to the search method.
 *         </td>
 *         <td valign="top">No</td>
 *         <td valign="top">
 *             {@link SearchControls#setCountLimit(long)} = 1<br/>
 *             {@link SearchControls#setReturningAttributes(String[])} = new String[] { "objectclass" }<br/>
 *             {@link SearchControls#setTimeLimit(int)} = 500
 *         </td>
 *     </tr>
 * </table>
 * 
 * @author Eric Dalquist
 */
public class DefaultDirContextValidator implements DirContextValidator {
    /**
     * Logger for this class and sub-classes
     */
    protected final Log logger = LogFactory.getLog(this.getClass());
    
    private String base;
    private String filter;
    private SearchControls searchControls;
    
    /**
     * Create the default validator, creates {@link SearchControls} with a countLimit of 1, returningAttributes of 
     * objectclass and timeLimit of 500. The default base is an empty string and the default filter is objectclass=*
     */
    public DefaultDirContextValidator() {
        this.searchControls = new SearchControls();
        this.searchControls.setCountLimit(1);
        this.searchControls.setReturningAttributes(new String[] { "objectclass" });
        this.searchControls.setTimeLimit(500);

        this.base = "";

        this.filter = "objectclass=*";
    }
    
    /**
     * @return the baseName
     */
    public String getBase() {
        return this.base;
    }
    /**
     * @param base the baseName to set
     */
    public void setBase(String base) {
        this.base = base;
    }
    /**
     * @return the filter
     */
    public String getFilter() {
        return this.filter;
    }
    /**
     * @param filter the filter to set
     */
    public void setFilter(String filter) {
        if (filter == null) {
            throw new IllegalArgumentException("filter may not be null");
        }
        
        this.filter = filter;
    }
    /**
     * @return the searchControls
     */
    public SearchControls getSearchControls() {
        return this.searchControls;
    }
    /**
     * @param searchControls the searchControls to set
     */
    public void setSearchControls(SearchControls searchControls) {
        if (searchControls == null) {
            throw new IllegalArgumentException("searchControls may not be null");
        }
        
        this.searchControls = searchControls;
    }


    /**
     * @see DirContextValidator#validateDirContext(DirContextType, javax.naming.directory.DirContext)
     */
    public boolean validateDirContext(DirContextType contextType, DirContext dirContext) {
        Validate.notNull(contextType, "contextType may not be null");
        Validate.notNull(dirContext, "dirContext may not be null");
        
        try {
            final NamingEnumeration searchResults = dirContext.search(this.base, this.filter, this.searchControls);

            if (searchResults.hasMore()) {
                if (this.logger.isDebugEnabled()) {
                    this.logger.debug("DirContext '" + dirContext + "' passed validation.");
                }

                return true;
            }
        }
        catch (Exception e) {
            this.logger.warn("DirContext '" + dirContext + "' failed validation with an exception.", e);
        }

        if (this.logger.isInfoEnabled()) {
            this.logger.info("DirContext '" + dirContext + "' failed validation.");
        }
        return false;
    }
}

@@ -2937,16 +2937,63 @@
 ls%7D with
+ search scope %3Ccode%3EOBJECT_SCOPE%3C/code%3E,%0A     *
  a count
@@ -3027,24 +3027,16 @@
 utes of 
-%0A     * 
 objectcl
@@ -3060,16 +3060,23 @@
  of 500.
+%0A     *
  The def
@@ -3190,24 +3190,393 @@
 lidator() %7B%0A
+        this(SearchControls.OBJECT_SCOPE);        %0A    %7D%0A%0A    /**%0A     * Create a validator with all the defaults of the default constructor, but with the search scope set to the%0A     * referred value.%0A     *%0A     * @param searchScope The searchScope to be set in the default %3Ccode%3ESearchControls%3C/code%3E%0A     */%0A    public DefaultDirContextValidator(int searchScope) %7B%0A
         this

Fix for LDAP-121: now using OBJECT_SCOPE as default search scope in DefaultDirContextValidator. Added alternate constructor to specify different search scope.