Ver Mensaje Individual
  #8 (permalink)  
Antiguo 15/02/2011, 12:03
stack
 
Fecha de Ingreso: diciembre-2006
Mensajes: 14
Antigüedad: 17 años, 3 meses
Puntos: 0
Respuesta: JAVA y LDAP Como? conectar, consultar, modificar

/**
* Modificado 22/09/2009
* Este método permite obtener los datos de un usuario.
* @param uid, es el identificador del usuario.
* @param hmparametros, hashmap que contiene los atributos del usuario a consultar.
* @return HashMap con todos los valores de los atributos especificados por parámetro.
* @throws NamingException
*/
public HashMap consultarUsuario(String uid,HashMap hmparametros)
{
InitialDirContext iniDirContext=null;
try {
ctx = ContextFactory.getDirContext();
iniDirContext = (InitialDirContext) ctx;

//Create the search controls
SearchControls searchCtls = new SearchControls();

//Specify the search scope
searchCtls.setSearchScope(SearchControls.SUBTREE_S COPE);

//specify the LDAP search filter
String searchFilter = "(&(objectClass=usrportalfiscal)(uid="+uid+")) ";

//Specify the Base for the search
String searchBase = obtenerPropiedad(Constantes.BASEUSUARIOS)+","+obte nerPropiedad(Constantes.BASEDIRECTORIO);

//Specify the attributes to return

// recorriendo variables
String parametros="";

Iterator it = hmparametros.entrySet().iterator();
int i=0;
while (it.hasNext()) {
Map.Entry e = (Map.Entry)it.next();

if(i==0)
parametros+=""+e.getKey();
else
parametros+=","+e.getKey();

i++;
}
String returnedAtts[]=parametros.split(",");

searchCtls.setReturningAttributes(returnedAtts);
//Search for objects using the filter
NamingEnumeration answer = iniDirContext.search(searchBase, searchFilter, searchCtls);

//Loop through the search results
while (answer.hasMoreElements()) {
SearchResult sr = (SearchResult)answer.next();

Attributes attrs = sr.getAttributes();
if (attrs != null) {
//establecimiento de valores

it = hmparametros.entrySet().iterator();
while (it.hasNext()) {
Map.Entry e = (Map.Entry)it.next();

if (e.getKey().equals("jpegPhoto")){
hmparametros.put(e.getKey(),obtenerObjeto((String) e.getKey(),attrs));
}else{
//Antes
//hmparametros.put(e.getKey(),obtenerValor((String)e .getKey(),attrs));
//Inicio Ahora
if (attrs.get((String)e.getKey()) != null) {
if (attrs.get((String)e.getKey()).size() > 1)
{
ArrayList attrArray = new ArrayList();
for (NamingEnumeration enumera = attrs.get((String)e.getKey()).getAll(); enumera.hasMore();) {
attrArray.add((String)enumera.next());
}
hmparametros.put(e.getKey(),attrArray);
}
else
{
hmparametros.put(e.getKey(),attrs.get((String)e.ge tKey()).get().toString());
}
}
//Fin Ahora

}
}

}
}

}

catch (NamingException e) {
System.err.println("Problem searching directory: " + e);
}finally{

try{
if (iniDirContext!=null){
iniDirContext.close();
}
}catch(NamingException e){
System.err.println("Error al cerrar el iniDirContext" + e.getMessage());
}
}

return hmparametros;
}