directory-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jfmel...@free.fr
Subject Re: [ApacheDS] search with french accents
Date Wed, 08 Apr 2009 13:35:57 GMT

Hi 

How to search entries with french accents in the filter ? 

I have two users : 

First user (InetOrgPerson): 
cn=Albert Einstein 
uid=aeinstein 
... 

Second user (InetOrgPerson): 
cn=rédacteur1 
uid=rédacteur1 
... 

and a group (GroupOfUniqueNames) with 2 members : 

uniqueMember=uid=aeinstein,ou=Users,dc=example,dc=com 
uniqueMember=uid=rédacteur1,ou=Users,dc=example,dc=com 


I have wrote a sample to find dn of users and the groups of users (by dn). 
I can find the two users but only the group of Albert 

the output of the test : 
------ Albert ------------- 
dn of User = uid=aeinstein,ou=Users,dc=example, dc=com 
search groups true 
Base=dc=example, dc=com 
Filter=(&(uniqueMember=uid=aeinstein,ou=Users,dc=example, dc=com)(ObjectClass=GroupOfUniqueNames))

cn=userAdmins,ou=Groups,dc=example, dc=com 
------ rédacteur1 : escape filter ------------- 
dn of User = uid=rédacteur1,ou=Users,dc=example, dc=com 
search groups true 
Base=dc=example, dc=com 
Filter=(&(uniqueMember=uid=r\c3\a9dacteur1,ou=Users,dc=example, dc=com)(ObjectClass=GroupOfUniqueNames))

not found 
------ rédacteur1 : not escape filter ------------- 
dn of User = uid=rédacteur1,ou=Users,dc=example, dc=com 
search groups false 
Base=dc=example, dc=com 
Filter=(&(uniqueMember=uid=rédacteur1,ou=Users,dc=example, dc=com)(ObjectClass=GroupOfUniqueNames))

not found 



here the code : 
package tests; 

import java.io.UnsupportedEncodingException; 
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.Iterator; 
import java.util.Properties; 
import java.util.Map.Entry; 

import javax.naming.Context; 
import javax.naming.NamingEnumeration; 
import javax.naming.NamingException; 
import javax.naming.NoInitialContextException; 
import javax.naming.directory.SearchControls; 
import javax.naming.directory.SearchResult; 
import javax.naming.ldap.InitialLdapContext; 

public class LdapSearch 
{ 
InitialLdapContext ldapContext = null; 
String dn="uid=admin,ou=system"; 
String pwd="secret"; 


String baseSearch="dc=example, dc=com"; 
String member="uniqueMember"; 
String groupClass="GroupOfUniqueNames"; 
String fistUser="Albert Einstein"; 
String secondUser="rédacteur1"; 



public static String charToUtf8Hex(char c) 
{ 
String ss = new String(); 
StringBuffer b = new StringBuffer(); 
ss += c; 
byte[] bs; 
try 
{ 
bs = ss.getBytes("UTF-8"); 
for (int k = 0; k < bs.length; k++) { 
b.append("\\"); 
b.append(UnicodeFormatter.byteToHex(bs[k])); 
} 
} catch (UnsupportedEncodingException e) 
{ 
} 
return b.toString(); 
} 

public static String native2ascii(String s){ 
StringBuffer sb = new StringBuffer(s.length()*2); 
char[] array=s.toCharArray(); 
for (int i = 0; i < array.length; i++) { 
switch(array[i]){ 
case '(': 
case ')': 
case '\\': 
case '*': 
sb.append( UnicodeFormatter.charToUtf8Hex(array[i]) ); 
break; 
default: 
if (array[i] <= 0x7f) { 
sb.append(array[i]); 
} else { 
sb.append( UnicodeFormatter.charToUtf8Hex(array[i]) ); 
} 
break; 
} 
} 
return sb.toString(); 
} 



private InitialLdapContext initializeLdapContext(final String dn, final Object credential)
throws NamingException { 
InitialLdapContext ldapContext = null; 

final Properties ldapEnvironment = new Properties(); 

ldapEnvironment.setProperty(Context.PROVIDER_URL, "ldap://localhost:10389"); 
ldapEnvironment.setProperty(Context.SECURITY_AUTHENTICATION, "simple"); 
ldapEnvironment.setProperty(Context.SECURITY_PRINCIPAL, dn); 
ldapEnvironment.put(Context.SECURITY_CREDENTIALS, credential); 
ldapEnvironment.put( "java.naming.ldap.version", "3" ); 
ldapEnvironment.put(Context.REFERRAL, "follow"); 
ldapEnvironment.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");


// traceLdapEnv(env); 
ldapContext = new InitialLdapContext(ldapEnvironment, null); 


return ldapContext; 
} 

private String getDNOfUser(final String userName, boolean escapeValue) throws NoInitialContextException,
NamingException { 
String dn = null; 
final StringBuffer filter = new StringBuffer(); 

filter.append("(&(cn="); 
if (escapeValue) 
filter.append(native2ascii(userName)); 
else 
filter.append(userName); 
filter.append(")(ObjectClass=inetOrgPerson))"); 

final String[] attrNames = new String[0]; 

final SearchControls constraints = new SearchControls(); 
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); 
constraints.setCountLimit(1); 
constraints.setReturningAttributes(attrNames); 

NamingEnumeration results = null; 
results = this.ldapContext.search(this.baseSearch, filter.toString(), null, constraints);


if (results.hasMore()) { 
final SearchResult sr = (SearchResult) results.next(); 
if (sr.isRelative() == true) { 
dn = sr.getName() + "," + this.baseSearch; 
} else { 
dn = sr.getName(); 
} 
} 
if (results != null) { 
results.close(); 
} 
return dn; 
} 


private Collection<String> getGroups(String sDN, boolean escapeValue) throws NamingException

{ 

final ArrayList<String> listDNs = new ArrayList<String>(); 

String sBaseSearch = null; 
final StringBuffer sFilter = new StringBuffer(); 

sBaseSearch = this.baseSearch; 

sFilter.append("(&("); 
sFilter.append(member); 
sFilter.append("="); 

if (escapeValue) 
sFilter.append(native2ascii(sDN)); 
else 
sFilter.append(sDN); 

sFilter.append(")(ObjectClass="); 
sFilter.append(groupClass); 
sFilter.append("))"); 

final String[] attrNames = new String[0]; 

final SearchControls constraints = new SearchControls(); 
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); 
constraints.setCountLimit(0); // no limit 
constraints.setReturningAttributes(attrNames); 

NamingEnumeration results = null; 
System.out.println("\t\t\tBase=" + sBaseSearch); 
System.out.println("\t\t\tFilter=" + sFilter.toString()); 
results = this.ldapContext.search(sBaseSearch, sFilter.toString(), null, constraints); 

while (results.hasMoreElements()) { 
final SearchResult sr = (SearchResult) results.nextElement(); 
final StringBuffer dn = new StringBuffer(); 

dn.append(sr.getName()); 
if (sr.isRelative() == true) { 
dn.append(","); 
dn.append(this.baseSearch); 
} 
listDNs.add(dn.toString()); 
} 
if (results != null) 
results.close(); 

return listDNs; 
} 



public static void main(String[] args) throws NamingException 
{ 

LdapSearch ldapSearch = new LdapSearch(); 

ldapSearch.ldapContext = ldapSearch.initializeLdapContext(ldapSearch.dn, ldapSearch.pwd.getBytes());



String userDn = ldapSearch.getDNOfUser(ldapSearch.fistUser, true); 
if (userDn != null) 
{ 
System.out.println("dn of User = " + userDn); 
System.out.println("search groups true"); 
Collection<String>groups=ldapSearch.getGroups(userDn,true); 
if (groups.size() > 0){ 
for (String s : groups) 
System.out.println(s); 
} 
else 
System.out.println("not found"); 
System.out.println("search groups false"); 
groups=ldapSearch.getGroups(userDn,false); 
if (groups.size() > 0){ 
for (String s : groups) 
System.out.println(s); 
} 
else 
System.out.println("not found"); 

} 
else 
System.out.println(ldapSearch.fistUser + " not found"); 

userDn = ldapSearch.getDNOfUser(ldapSearch.secondUser, true); 
if (userDn != null) 
{ 
System.out.println("dn of User = " + userDn); 
System.out.println("search groups true"); 
Collection<String>groups=ldapSearch.getGroups(userDn,true); 
if (groups.size() > 0){ 
for (String s : groups) 
System.out.println(s); 
} 
else 
System.out.println("not found"); 
System.out.println("search groups false"); 
groups=ldapSearch.getGroups(userDn,false); 
if (groups.size() > 0){ 
for (String s : groups) 
System.out.println(s); 
} 
else 
System.out.println("not found"); 

} 
else 
System.out.println(ldapSearch.secondUser + "(true) not found"); 

userDn = ldapSearch.getDNOfUser(ldapSearch.secondUser, false); 
if (userDn != null) 
{ 
System.out.println("dn of User = " + userDn); 
System.out.println("search groups true"); 
Collection<String>groups=ldapSearch.getGroups(userDn,true); 
if (groups.size() > 0){ 
for (String s : groups) 
System.out.println(s); 
} 
else 
System.out.println("not found"); 
System.out.println("search groups false"); 
groups=ldapSearch.getGroups(userDn,false); 
if (groups.size() > 0){ 
for (String s : groups) 
System.out.println(s); 
} 
else 
System.out.println("not found"); 

} 
else 
System.out.println(ldapSearch.secondUser + "(false) not found"); 

} 
} 


















Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message