Ámbito de Búsqueda
Los SearchControls
por defecto especifican que la búsqueda se realice en el contexto llamado (SearchControls.ONELEVEL_SCOPE).
Este valor por defecto se usa en los ejemplos de la sección de Filtros de Búsqueda.
Además de estos valores por defecto, podemos especificar que la búsqueda se
realice en un subárbol entero o sólo en el objeto nombrado.
Buscar en el Subárbol
Una búsqueda en el subárbol completo busca el objeto nombrado y todos su
descendientes. Para hacer que la búsqueda se comporte de esta forma, pasamos SearchControls.SUBTREE_SCOPE
a SearchControls.setSearchScope()
como sigue.
// Specify the ids of the attributes to return
String[] attrIDs = {"sn", "telephonenumber", "golfhandicap", "mail"};
SearchControls ctls = new SearchControls();
ctls.setReturningAttributes(attrIDs);
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
// Specify the search filter to match
// Ask for objects that have the attribute "sn" == "Geisel"
// and the "mail" attribute
String filter = "(&(sn=Geisel)(mail=*))";
// Search the subtree for objects by using the filter
NamingEnumeration answer = ctx.search("", filter, ctls);
Este ejemplo busca en el subárbol del
contexto ctx todas las entradas que cumplan el filtro de
búsqueda. Encuentra la entrada "cn= Ted Geisel, ou=People"
en este subárbol que cumple con el filtro.
# java SearchSubtree
>>>cn=Ted Geisel, ou=People
attribute: sn
value: Geisel
attribute: mail
value: Ted.Geisel@JNDITutorial.com
attribute: telephonenumber
value: +1 408 555 5252
Buscar el Objeto Nombrado
También podemos buscar el objeto nombrado. Esto es útil, por ejemplo, para
comprobar si el objeto nombrado cumple las condiciones de búsqueda. Para buscar
el objeto nombrado le pasamos SearchControls.OBJECT_SCOPE
a setSearchScope().
// Specify the ids of the attributes to return
String[] attrIDs = {"sn", "telephonenumber", "golfhandicap", "mail"};
SearchControls ctls = new SearchControls();
ctls.setReturningAttributes(attrIDs);
ctls.setSearchScope(SearchControls.OBJECT_SCOPE);
// Specify the search filter to match
// Ask for objects that have the attribute "sn" == "Geisel"
// and the "mail" attribute
String filter = "(&(sn=Geisel)(mail=*))";
// Search the subtree for objects by using the filter
NamingEnumeration answer =
ctx.search("cn=Ted Geisel, ou=People", filter, ctls);
Este ejemplo comprueba que el objeto "cn=Ted
Geisel, ou=People" cumple el filtro dado.
# java SearchObject
>>>
attribute: sn
value: Geisel
attribute: mail
value: Ted.Geisel@JNDITutorial.com
attribute: telephonenumber
value: +1 408 555 5252
El ejemplo econtró una respuesta y la imprimió. Observa que el nombre del
resultado es una cadena vacía. Esto es porque el nombre del objeto es siempre
relativo al contexto de búsqueda (en este caso "cn=Ted Geisel,
ou=People").