diff options
Diffstat (limited to 'src/net/java/sip/communicator/impl/ldap/LdapDirectoryImpl.java')
-rw-r--r-- | src/net/java/sip/communicator/impl/ldap/LdapDirectoryImpl.java | 272 |
1 files changed, 202 insertions, 70 deletions
diff --git a/src/net/java/sip/communicator/impl/ldap/LdapDirectoryImpl.java b/src/net/java/sip/communicator/impl/ldap/LdapDirectoryImpl.java index d0c89a7..974d8f0 100644 --- a/src/net/java/sip/communicator/impl/ldap/LdapDirectoryImpl.java +++ b/src/net/java/sip/communicator/impl/ldap/LdapDirectoryImpl.java @@ -7,6 +7,7 @@ package net.java.sip.communicator.impl.ldap; import java.util.*; +import java.util.regex.*; import javax.naming.*; import javax.naming.directory.*; @@ -54,7 +55,11 @@ public class LdapDirectoryImpl /** * Name of avatar attribute. */ - private static final String PHOTO_ATTRIBUTE = "jpegPhoto"; + private static final String[] PHOTO_ATTRIBUTES = new String[] + { + "jpegPhoto", + "thumbnailPhoto" + }; /** * data structure used to store the LDAP attributes that @@ -126,6 +131,11 @@ public class LdapDirectoryImpl new Hashtable<String, String>(); /** + * List of all phone number attributes. + */ + private final List<String> phoneNumberAttributes = new ArrayList<String>(); + + /** * The contructor for this class. * Since this element is immutable (otherwise it would be a real pain * to use with a Set), it takes all the settings we could need to store @@ -159,7 +169,8 @@ public class LdapDirectoryImpl if(this.settings.getPort() == 0) portText = ":" + this.settings.getEncryption().defaultPort(); - portText = ":" + this.settings.getPort(); + else + portText = ":" + this.settings.getPort(); /* fills environment for InitialDirContext */ this.env.put(Context.INITIAL_CONTEXT_FACTORY, @@ -198,16 +209,43 @@ public class LdapDirectoryImpl break; } + List<String> workPhoneOverrides = settings.getWorkPhoneSearchFields(); + List<String> mobilePhoneOverrides + = settings.getMobilePhoneSearchFields(); + List<String> homePhoneOverrides = settings.getHomePhoneSearchFields(); + + phoneNumberAttributes.addAll(workPhoneOverrides); + phoneNumberAttributes.addAll(mobilePhoneOverrides); + phoneNumberAttributes.addAll(homePhoneOverrides); + attributesMap.put("mail", settings.getMailSearchFields()); - attributesMap.put("workPhone", settings.getWorkPhoneSearchFields()); - attributesMap.put("mobilePhone", settings.getMobilePhoneSearchFields()); - attributesMap.put("homePhone", settings.getHomePhoneSearchFields()); + attributesMap.put("workPhone", workPhoneOverrides); + attributesMap.put("mobilePhone", mobilePhoneOverrides); + attributesMap.put("homePhone", homePhoneOverrides); this.searchableAttrs.addAll(searchableAttributes); for(String s : settings.getMailSearchFields()) { searchableAttrs.add(s); } + for(String s : workPhoneOverrides) + { + searchableAttrs.add(s); + } + for(String s : mobilePhoneOverrides) + { + searchableAttrs.add(s); + } + for(String s : homePhoneOverrides) + { + searchableAttrs.add(s); + } + + if (settings.isPhotoInline()) + { + retrievableAttributes.add("jpegPhoto"); + retrievableAttributes.add("thumbnailPhoto"); + } } /** @@ -350,10 +388,14 @@ public class LdapDirectoryImpl @Override public void run() { - logger.trace("starting search for " + realQueryString + + String filter = buildSearchFilter(realQueryString); + logger.trace("starting search for " + filter + " (initial query: \"" + query.toString() + "\") on directory \"" + LdapDirectoryImpl.this + "\""); + Pattern searchPattern = Pattern.compile(query.toString(), + Pattern.CASE_INSENSITIVE | Pattern.LITERAL); + SearchControls searchControls = buildSearchControls(searchSettings); @@ -373,7 +415,7 @@ public class LdapDirectoryImpl NamingEnumeration<?> results = dirContext.search( LdapDirectoryImpl.this.settings.getBaseDN(), - buildSearchFilter(realQueryString), + filter, searchControls ); @@ -385,8 +427,15 @@ public class LdapDirectoryImpl SearchResult searchResult = (SearchResult) results.next(); - Map<String, Set<String>> retrievedAttributes = + Map<String, Set<Object>> retrievedAttributes = retrieveAttributes(searchResult); + + if(!checkRetrievedAttributes( + query.toString(), + searchPattern, + retrievedAttributes)) + continue; + LdapPersonFound person = buildPerson( query, @@ -401,7 +450,7 @@ public class LdapDirectoryImpl } long time1 = System.currentTimeMillis(); - logger.trace("search for real query \"" + realQueryString + + logger.trace("search for real query \"" + filter + "\" (initial query: \"" + query.toString() + "\") on directory \"" + LdapDirectoryImpl.this + "\" took " + (time1-time0) + "ms"); @@ -411,10 +460,10 @@ public class LdapDirectoryImpl } catch(OperationNotSupportedException e) { - logger.trace( + logger.error( "use bind DN without password during search" + " for real query \"" + - realQueryString + "\" (initial query: \"" + + filter + "\" (initial query: \"" + query.toString() + "\") on directory \"" + LdapDirectoryImpl.this + "\": " + e); endEvent = new LdapEvent( @@ -425,10 +474,10 @@ public class LdapDirectoryImpl } catch(AuthenticationException e) { - logger.trace( + logger.error( "authentication failed during search" + " for real query \"" + - realQueryString + "\" (initial query: \"" + + filter + "\" (initial query: \"" + query.toString() + "\") on directory \"" + LdapDirectoryImpl.this + "\": " + e); endEvent = new LdapEvent( @@ -439,10 +488,10 @@ public class LdapDirectoryImpl } catch(NamingException e) { - logger.trace( + logger.error( "an external exception was thrown during search" + " for real query \"" + - realQueryString + "\" (initial query: \"" + + filter + "\" (initial query: \"" + query.toString() + "\") on directory \"" + LdapDirectoryImpl.this + "\": " + e); endEvent = new LdapEvent( @@ -453,7 +502,7 @@ public class LdapDirectoryImpl } catch(LdapQueryCancelledException e) { - logger.trace("search for real query \"" + realQueryString + + logger.trace("search for real query \"" + filter + "\" (initial query: \"" + query.toString() + "\") on " + LdapDirectoryImpl.this + " cancelled at state " + cancelState); @@ -469,6 +518,19 @@ public class LdapDirectoryImpl // whether sleep was interrupted // is not that important } + catch (Exception e) + { + logger.error("search for real query \"" + filter + + "\" (initial query: \"" + query.toString() + + "\") on " + LdapDirectoryImpl.this + + " cancelled at state " + cancelState, e); + + endEvent = new LdapEvent( + LdapDirectoryImpl.this, + LdapEvent.LdapEventCause.SEARCH_ERROR, + query + ); + } finally { fireLdapEvent(endEvent, caller); @@ -504,9 +566,50 @@ public class LdapDirectoryImpl searchThread.start(); } - private static String[] + /** + * Checks whether the found attributes match the current query. + * @param searchPattern the pattern we use for checking + * @param retrievedAttributes the attributes. + * @return whether the found attributes match the current query. + */ + private boolean checkRetrievedAttributes( + String queryString, + Pattern searchPattern, + Map<String, Set<Object>> retrievedAttributes) + { + for(Map.Entry<String, Set<Object>> en : retrievedAttributes.entrySet()) + { + if(!searchableAttrs.contains(en.getKey())) + { + continue; + } + + boolean isPhoneNumber = phoneNumberAttributes.contains(en.getKey()); + + for(Object o : en.getValue()) + { + if(!(o instanceof String)) + continue; + + if(searchPattern.matcher((String)o).find() + || (isPhoneNumber + && LdapActivator.getPhoneNumberI18nService() + .phoneNumbersMatch(queryString, (String)o))) + return true; + } + } + + return false; + } + + private String[] buildIntermediateQueryStrings(String initialQueryString) { + if (!this.settings.isMangleQuery()) + { + return new String[] { initialQueryString }; + } + // search for "doe john" as well "as john doe" String[] words = initialQueryString.split(" "); String[] intermediateQueryStrings; @@ -534,28 +637,27 @@ public class LdapDirectoryImpl * @param searchResult the results to browse for attributes * @return the attributes in a Map */ - private Map<String, Set<String>> + private Map<String, Set<Object>> retrieveAttributes(SearchResult searchResult) throws NamingException { Attributes attributes = searchResult.getAttributes(); - Map<String, Set<String>> retrievedAttributes = - new HashMap<String, Set<String>>(); + Map<String, Set<Object>> retrievedAttributes = + new HashMap<String, Set<Object>>(); NamingEnumeration<String> ids = attributes.getIDs(); while(ids.hasMore()) { String id = ids.next(); if(retrievableAttributes.contains(id) || containsAttribute(id)) { - Set<String> valuesSet = new HashSet<String>(); + Set<Object> valuesSet = new HashSet<Object>(); retrievedAttributes.put(id, valuesSet); Attribute attribute = attributes.get(id); NamingEnumeration<?> values = attribute.getAll(); while(values.hasMore()) { - String value = (String) values.next(); - valuesSet.add(value); + valuesSet.add(values.next()); } } } @@ -574,7 +676,7 @@ public class LdapDirectoryImpl LdapQuery query, String dn, Map<String, - Set<String>> retrievedAttributes + Set<Object>> retrievedAttributes ) { LdapPersonFound person = @@ -584,19 +686,22 @@ public class LdapDirectoryImpl if(retrievedAttributes.get("givenname") != null) { String firstName = - retrievedAttributes.get("givenname").iterator().next(); + (String) retrievedAttributes.get("givenname") + .iterator().next(); person.setFirstName(firstName); } else if(retrievedAttributes.get("givenName") != null) { String firstName = - retrievedAttributes.get("givenName").iterator().next(); + (String) retrievedAttributes.get("givenName") + .iterator().next(); person.setFirstName(firstName); } else if(retrievedAttributes.get("gn") != null) { String firstName = - retrievedAttributes.get("gn").iterator().next(); + (String) retrievedAttributes.get("gn") + .iterator().next(); person.setFirstName(firstName); } @@ -604,13 +709,15 @@ public class LdapDirectoryImpl if(retrievedAttributes.get("sn") != null) { String surname = - retrievedAttributes.get("sn").iterator().next(); + (String) retrievedAttributes.get("sn") + .iterator().next(); person.setSurname(surname); } else if(retrievedAttributes.get("surname") != null) { String surname = - retrievedAttributes.get("surname").iterator().next(); + (String) retrievedAttributes.get("surname") + .iterator().next(); person.setSurname(surname); } @@ -618,19 +725,22 @@ public class LdapDirectoryImpl if(retrievedAttributes.get("displayName") != null) { String displayName = - retrievedAttributes.get("displayName").iterator().next(); + (String) retrievedAttributes.get("displayName") + .iterator().next(); person.setDisplayName(displayName); } else if(retrievedAttributes.get("cn") != null) { String displayName = - retrievedAttributes.get("cn").iterator().next(); + (String) retrievedAttributes.get("cn") + .iterator().next(); person.setDisplayName(displayName); } else if(retrievedAttributes.get("commonname") != null) { String displayName = - retrievedAttributes.get("commonname").iterator().next(); + (String) retrievedAttributes.get("commonname") + .iterator().next(); person.setDisplayName(displayName); } if(person.getDisplayName() == null) @@ -648,20 +758,21 @@ public class LdapDirectoryImpl if(retrievedAttributes.get("o") != null) { String organization = - retrievedAttributes.get("o").iterator().next(); + (String) retrievedAttributes.get("o").iterator().next(); person.setOrganization(organization); } else if(retrievedAttributes.get("organizationName") != null) { String organization = - retrievedAttributes.get("organizationName").iterator() - .next(); + (String) retrievedAttributes.get("organizationName") + .iterator().next(); person.setOrganization(organization); } else if(retrievedAttributes.get("company") != null) { String organization = - retrievedAttributes.get("company").iterator().next(); + (String) retrievedAttributes.get("company") + .iterator().next(); person.setOrganization(organization); } @@ -669,42 +780,60 @@ public class LdapDirectoryImpl if(retrievedAttributes.get("company") != null) { String department = - retrievedAttributes.get("company").iterator().next(); + (String) retrievedAttributes.get("company") + .iterator().next(); person.setDepartment(department); } else if(retrievedAttributes.get("ou") != null) { String department = - retrievedAttributes.get("ou").iterator().next(); + (String) retrievedAttributes.get("ou").iterator().next(); person.setDepartment(department); } else if(retrievedAttributes.get("orgunit") != null) { String department = - retrievedAttributes.get("orgunit").iterator().next(); + (String) retrievedAttributes.get("orgunit") + .iterator().next(); person.setDepartment(department); } else if(retrievedAttributes.get("organizationalUnitName") != null) { String department = - retrievedAttributes.get("organizationalUnitName"). + (String) retrievedAttributes.get("organizationalUnitName"). iterator().next(); person.setDepartment(department); } else if(retrievedAttributes.get("department") != null) { String department = - retrievedAttributes.get("department").iterator().next(); + (String) retrievedAttributes.get("department") + .iterator().next(); person.setDepartment(department); } else if(retrievedAttributes.get("departmentNumber") != null) { String department = - retrievedAttributes.get("departmentNumber").iterator(). - next(); + (String) retrievedAttributes.get("departmentNumber") + .iterator().next(); person.setDepartment(department); } + if(retrievedAttributes.get("jpegPhoto") != null) + { + byte[] photo = + (byte[])retrievedAttributes.get("jpegPhoto") + .iterator().next(); + person.setPhoto(photo); + } + else if(retrievedAttributes.get("thumbnailPhoto") != null) + { + byte[] photo = + (byte[])retrievedAttributes.get("thumbnailPhoto") + .iterator().next(); + person.setPhoto(photo); + } + // mail List<String> attrs = attributesMap.get("mail"); @@ -712,8 +841,9 @@ public class LdapDirectoryImpl { if(retrievedAttributes.get(attr) != null) { - for(String mail : retrievedAttributes.get(attr)) + for(Object o : retrievedAttributes.get(attr)) { + String mail = o.toString(); if(!mail.contains("@")) { if(settings.getMailSuffix() != null) @@ -736,7 +866,8 @@ public class LdapDirectoryImpl if(retrievedAttributes.get(attr) != null) { String phone = - retrievedAttributes.get(attr).iterator().next(); + (String) retrievedAttributes.get(attr) + .iterator().next(); person.addWorkPhone(phone); } } @@ -748,9 +879,9 @@ public class LdapDirectoryImpl { if(retrievedAttributes.get(attr) != null) { - for(String phone : retrievedAttributes.get(attr)) + for(Object phone : retrievedAttributes.get(attr)) { - person.addMobilePhone(phone); + person.addMobilePhone(phone.toString()); } } } @@ -762,9 +893,9 @@ public class LdapDirectoryImpl { if(retrievedAttributes.get(attr) != null) { - for(String phone : retrievedAttributes.get(attr)) + for(Object phone : retrievedAttributes.get(attr)) { - person.addHomePhone(phone); + person.addHomePhone(phone.toString()); } } } @@ -836,27 +967,21 @@ public class LdapDirectoryImpl } /** - * Builds an LDAP search filter, base on the query string entered - * e.g. (&(|(mail=*)(telephoneNumber=*))(|(cn=*query*)(sn=*query*)(givenname=*query*))) - * + * Builds an LDAP search filter, based on the query string entered and the + * searchable fields defined in the static constructor. If a custom query is + * defined this is used instead. e.g. + * (|(|(mail=query)(telephoneNumber=query))) + * * @return an LDAP search filter */ private String buildSearchFilter(String query) { - StringBuffer searchFilter = new StringBuffer(); - - /* - searchFilter.append("(&(|"); - - for(String attribute : retrievableAttributes) + if ("custom".equals(settings.getQueryMode())) { - searchFilter.append("("); - searchFilter.append(attribute); - searchFilter.append("=*)"); + return settings.getCustomQuery().replace("<query>", query); } - searchFilter.append(")(|"); - */ + StringBuilder searchFilter = new StringBuilder(); searchFilter.append("(|"); /* cn=*query* OR sn=*query* OR ... */ @@ -966,6 +1091,11 @@ public class LdapDirectoryImpl */ byte[] fetchPhotoForPerson(String dn) { + if (this.settings.isPhotoInline()) + { + return null; + } + byte[] photo = null; InitialDirContext dirContext = null; @@ -973,8 +1103,7 @@ public class LdapDirectoryImpl final SearchControls searchCtl = new SearchControls(); searchCtl.setSearchScope(SearchControls.OBJECT_SCOPE); - String[] returningAttributes = { PHOTO_ATTRIBUTE }; - searchCtl.setReturningAttributes(returningAttributes); + searchCtl.setReturningAttributes(PHOTO_ATTRIBUTES); logger.trace("starting photo retrieval..."); try @@ -991,13 +1120,16 @@ public class LdapDirectoryImpl { SearchResult searchResult = (SearchResult) result.next(); Attributes attributes = searchResult.getAttributes(); - Attribute attribute = attributes.get(PHOTO_ATTRIBUTE); - if(attribute != null) + for (String a : PHOTO_ATTRIBUTES) { - NamingEnumeration<?> values = attribute.getAll(); - if(values.hasMore()) + Attribute attribute = attributes.get(a); + if(attribute != null) { - photo = (byte[]) values.next(); + NamingEnumeration<?> values = attribute.getAll(); + if(values.hasMore()) + { + photo = (byte[]) values.next(); + } } } } |