Recherche ldap avec pagination

Contenu du snippet

cette sources permet de faire une recherche dans un annuaire LDAP avec une résultat paginer. sa signifie que l'erreur
'javax.naming.SizeLimitExceededException: [LDAP: error code 4 - Sizelimit Exceeded]; remaining name ' ne s'affiche plus.

Source / Exemple :


PagedResultsControlJndiClient
/**
 *

  • PagedResultsControlJndiClient.java
  • Sample code to demostrate how Paged Results Control works.
*
  • /
import javax.naming.*; import javax.naming.directory.*; import javax.naming.ldap.*; import java.util.Hashtable; public class PagedResultsControlJndiClient { static final String PAGED_RESULT_CONTROL_OID = "1.2.840.113556.1.4.319"; public static void main(String[] args) { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory"); // Note: Active Directory Server supports Paged Results Control // SunOne does not supports Paged Results Control but it supports // Virtual List View Control instead. env.put(Context.PROVIDER_URL, "ldap://myAD.mydomain.com:389"); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, "administrator@mydomain.com"); env.put(Context.SECURITY_CREDENTIALS, "mypassword"); try{ /* Open an LDAP connection for the provided principal and credentials */ LdapContext ctx = new InitialLdapContext(env, null); System.out.println("Initial binding done!"); /* Query the server to see if the paged result control is supported */ if(!isPagedResultControlSupported(ctx)){ System.out.println("The server does not support Paged Results Control."); System.exit(1); } /* Activate paged results */ int pageSize = 5; byte[] cookie = null; int total; ctx.setRequestControls( new Control[]{new PagedResultsControl(pageSize, Control.CRITICAL)}); System.out.println("Paged control set!"); int count = 0; while(true){ count++; System.err.println("Search loop count = " + count); SearchControls ctls = new SearchControls(); ctls.setSearchScope(SearchControls.ONELEVEL_SCOPE); ctls.setCountLimit(0); // Perform the search NamingEnumeration results = ctx.search("dc=mydomain,dc=com", "(objectclass=*)", ctls); try{ // Iterate over a batch of search results while (results != null && results.hasMore()) { // Display an entry SearchResult entry = (SearchResult)results.next(); System.out.println("entryDN=" + entry.getName()); } }catch(Exception pe){ System.out.println(pe.toString()); // Patial Result Exception } // Examine the paged results control response Control[] controls = ctx.getResponseControls(); if(controls!=null){ for(int k = 0; k<controls.length; k++){ if(controls[k] instanceof PagedResultsResponseControl){ PagedResultsResponseControl prrc = (PagedResultsResponseControl)controls[k]; total = prrc.getResultSize(); cookie = prrc.getCookie(); }else{ // Handle other response controls (if any) } } } if(cookie==null) break; // Re-activate paged results ctx.setRequestControls(new Control[]{ new PagedResultsControl(pageSize, cookie, Control.CRITICAL)}); } // Close the LDAP association ctx.close(); }catch (Exception e){ e.printStackTrace(); } } /**
  • Is paged result control supported?
*
  • Query the rootDSE object to find out if the paged result control
  • is supported.
  • /
static boolean isPagedResultControlSupported(LdapContext ctx) throws NamingException { SearchControls ctl = new SearchControls(); ctl.setReturningAttributes(new String[]{"supportedControl"}); ctl.setSearchScope(SearchControls.OBJECT_SCOPE); /* search for the rootDSE object */ NamingEnumeration results = ctx.search("", "(objectClass=*)", ctl); while(results.hasMore()){ SearchResult entry = (SearchResult)results.next(); NamingEnumeration attrs = entry.getAttributes().getAll(); while (attrs.hasMore()){ Attribute attr = (Attribute)attrs.next(); NamingEnumeration vals = attr.getAll(); while (vals.hasMore()){ String value = (String) vals.next(); if(value.equals(PAGED_RESULT_CONTROL_OID)) return true; } } } return false; } }

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.