* @throws AuthenticationException
*/
private User authenticate(String login, String password) throws UserNotFoundException, AuthenticationException
{
String attrValue;
User user = null;
try
{
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, agent.getParamString(PARAM_CONTEXTFACTORY));
env.put(Context.PROVIDER_URL, "ldap://" + agent.getParamString(PARAM_HOSTURL) + ":" + agent.getParamString(PARAM_HOSTPORT));
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, getFormattedLogin(login));
env.put(Context.SECURITY_CREDENTIALS, password);
DirContext ctx = new InitialDirContext(env);
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
// Realiza la b�squeda del usuario en el directorio y se autentica
NamingEnumeration<?> results = ctx.search(agent.getParamString(PARAM_SEARCHBASE), "cn=" + login, constraints);
// Verifica si se ha encontrado la entrada en el directorio
if (results == null || !results.hasMore())
{
throw new UserNotFoundException();
}
else
{
user = new User();
user.setLogin(login);
user.setLogonCount(1);
user.setLastLogin(new Date());
SearchResult sr = (SearchResult) results.next();
Attributes attrs = sr.getAttributes();
for (NamingEnumeration<?> nenum = attrs.getAll(); nenum.hasMoreElements();)
{
Attribute attrib = (Attribute) nenum.next();
if (attrib.getID().trim().equalsIgnoreCase(agent.getParamString(PARAM_ATTR_MAIL)))
{
attrValue = "";
for (Enumeration<?> vals = attrib.getAll(); vals.hasMoreElements();)
{
attrValue += vals.nextElement();
}
user.setMail(attrValue);
}
else if (attrib.getID().trim().equalsIgnoreCase(agent.getParamString(PARAM_ATTR_NAME)))
{
attrValue = "";
for (Enumeration<?> vals = attrib.getAll(); vals.hasMoreElements();)
{
attrValue += vals.nextElement();
}
user.setName(attrValue);
}
else if (attrib.getID().trim().equalsIgnoreCase(agent.getParamString(PARAM_ATTR_SURNAME)))
{
attrValue = "";
for (Enumeration<?> vals = attrib.getAll(); vals.hasMoreElements();)
{
attrValue += vals.nextElement();
}
user.setName(user.getName() + " " + attrValue);
}
}
}
}
catch (NamingException ex)