<commit_before>/**
 *
 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either 
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 * 
 **/
package lucee.runtime.net.ldap;

import java.io.IOException;
import java.security.Provider;
import java.security.Security;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.ModificationItem;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;

import lucee.commons.lang.ClassException;
import lucee.commons.lang.ClassUtil;
import lucee.commons.lang.StringUtil;
import lucee.runtime.exp.PageException;
import lucee.runtime.op.Caster;
import lucee.runtime.type.Collection;
import lucee.runtime.type.KeyImpl;
import lucee.runtime.type.Query;
import lucee.runtime.type.QueryImpl;
import lucee.runtime.type.util.ListUtil;

/**
 * Ldap Client
 */
public final class LDAPClient {

	/**
	 * Field <code>SECURE_NONE</code>
	 */
	public static final short SECURE_NONE = 0;
	/**
	 * Field <code>SECURE_CFSSL_BASIC</code>
	 */
	public static final short SECURE_CFSSL_BASIC = 1;
	/**
	 * Field <code>SECURE_CFSSL_CLIENT_AUTH</code>
	 */
	public static final short SECURE_CFSSL_CLIENT_AUTH = 2;

	/**
	 * Field <code>SORT_TYPE_CASE</code>
	 */
	public static final int SORT_TYPE_CASE = 0;
	/**
	 * Field <code>SORT_TYPE_NOCASE</code>
	 */
	public static final int SORT_TYPE_NOCASE = 1;

	/**
	 * Field <code>SORT_DIRECTION_ASC</code>
	 */
	public static final int SORT_DIRECTION_ASC = 0;
	/**
	 * Field <code>SORT_DIRECTION_DESC</code>
	 */
	public static final int SORT_DIRECTION_DESC = 1;

	Hashtable env = new Hashtable();

	/**
	 * constructor of the class
	 * 
	 * @param server
	 * @param port
	 * @param binaryColumns
	 */
	public LDAPClient(String server, int port, int timeout, String[] binaryColumns) {

		env.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");
		env.put("java.naming.provider.url", "ldap://" + server + ":" + port);

		// rEAD AS bINARY
		for (int i = 0; i < binaryColumns.length; i++)
			env.put("java.naming.ldap.attributes.binary", binaryColumns[i]);

		// Referral
		env.put("java.naming.referral", "ignore");

		// timeout
		env.put("com.sun.jndi.ldap.read.timeout", timeout);		
	}

	/**
	 * sets username password for the connection
	 * 
	 * @param username
	 * @param password
	 */
	public void setCredential(String username, String password) {
		if (username != null) {
			env.put("java.naming.security.principal", username);
			env.put("java.naming.security.credentials", password);
		}
		else {
			env.remove("java.naming.security.principal");
			env.remove("java.naming.security.credentials");
		}
	}

	/**<commit_msg>LDEV-1860 fix ldap timeout<commit_after>/**
 *
 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either 
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 * 
 **/
package lucee.runtime.net.ldap;

import java.io.IOException;
import java.security.Provider;
import java.security.Security;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.ModificationItem;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;

import lucee.commons.lang.ClassException;
import lucee.commons.lang.ClassUtil;
import lucee.commons.lang.StringUtil;
import lucee.runtime.exp.PageException;
import lucee.runtime.op.Caster;
import lucee.runtime.type.Collection;
import lucee.runtime.type.KeyImpl;
import lucee.runtime.type.Query;
import lucee.runtime.type.QueryImpl;
import lucee.runtime.type.util.ListUtil;

/**
 * Ldap Client
 */
public final class LDAPClient {

	/**
	 * Field <code>SECURE_NONE</code>
	 */
	public static final short SECURE_NONE = 0;
	/**
	 * Field <code>SECURE_CFSSL_BASIC</code>
	 */
	public static final short SECURE_CFSSL_BASIC = 1;
	/**
	 * Field <code>SECURE_CFSSL_CLIENT_AUTH</code>
	 */
	public static final short SECURE_CFSSL_CLIENT_AUTH = 2;

	/**
	 * Field <code>SORT_TYPE_CASE</code>
	 */
	public static final int SORT_TYPE_CASE = 0;
	/**
	 * Field <code>SORT_TYPE_NOCASE</code>
	 */
	public static final int SORT_TYPE_NOCASE = 1;

	/**
	 * Field <code>SORT_DIRECTION_ASC</code>
	 */
	public static final int SORT_DIRECTION_ASC = 0;
	/**
	 * Field <code>SORT_DIRECTION_DESC</code>
	 */
	public static final int SORT_DIRECTION_DESC = 1;

	Hashtable env = new Hashtable();

	/**
	 * constructor of the class
	 * 
	 * @param server
	 * @param port
	 * @param binaryColumns
	 */
	public LDAPClient(String server, int port, int timeout, String[] binaryColumns) {

		env.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");
		env.put("java.naming.provider.url", "ldap://" + server + ":" + port);

		// rEAD AS bINARY
		for (int i = 0; i < binaryColumns.length; i++)
			env.put("java.naming.ldap.attributes.binary", binaryColumns[i]);

		// Referral
		env.put("java.naming.referral", "ignore");

		// timeout
		env.put("com.sun.jndi.ldap.read.timeout", String.valueOf(timeout));
	}

	/**
	 * sets username password for the connection
	 * 
	 * @param username
	 * @param password
	 */
	public void setCredential(String username, String password) {
		if (username != null) {
			env.put("java.naming.security.principal", username);
			env.put("java.naming.security.credentials", password);
		}
		else {
			env.remove("java.naming.security.principal");
			env.remove("java.naming.security.credentials");
		}
	}

	/**
{'fraction_non_alphanumeric': 0.11815368052751984, 'fraction_numerical': 0.004306284483918719, 'mean_word_length': 4.182705718270572, 'pattern_counts': {'":': 2, '<': 35, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7246695', 'n_tokens_mistral': 2497, 'n_tokens_neox': 2355, 'n_words': 611}