From 4e9e374ea19c1f160d107bc1ab429eb9ddbde49e Mon Sep 17 00:00:00 2001 From: mguessan Date: Thu, 3 Sep 2009 09:48:58 +0000 Subject: [PATCH] LDAP: refactor contact find, generic attribute mapping git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@695 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- .../davmail/exchange/ExchangeSession.java | 96 ++++--------------- src/java/davmail/ldap/LdapConnection.java | 53 ++++++---- 2 files changed, 52 insertions(+), 97 deletions(-) diff --git a/src/java/davmail/exchange/ExchangeSession.java b/src/java/davmail/exchange/ExchangeSession.java index 9d19cc5c..366544b5 100644 --- a/src/java/davmail/exchange/ExchangeSession.java +++ b/src/java/davmail/exchange/ExchangeSession.java @@ -356,8 +356,8 @@ public class ExchangeSession { if (a_sUrlEndIndex >= 0 && a_sLgnEndIndex >= 0) { URI initmethodURI = initmethod.getURI(); initmethodURI.setQuery( - scriptValue.substring(a_sLgnIndex + 1, a_sLgnEndIndex) + - scriptValue.substring(a_sUrlIndex, a_sUrlEndIndex)); + scriptValue.substring(a_sLgnIndex + 1, a_sLgnEndIndex) + + scriptValue.substring(a_sUrlIndex, a_sUrlEndIndex)); String src = initmethodURI.getURI(); LOGGER.debug("Detected script based logon, redirect to form at " + src); HttpMethod newInitMethod = DavGatewayHttpClientFacade.executeFollowRedirects(httpClient, src); @@ -2610,82 +2610,26 @@ public class ExchangeSession { Map> results = new HashMap>(); Map item; for (MultiStatusResponse response : responses) { - DavPropertySet properties = response.getProperties(HttpStatus.SC_OK); - // TODO: improve mapping - String uid = getPropertyIfExists(properties, "uid", Namespace.getNamespace("DAV:")); - String email1 = getPropertyIfExists(properties, "email1", Namespace.getNamespace("urn:schemas:contacts:")); - // patch email - if (email1 != null && email1.startsWith("\"")) { - int endIndex = email1.indexOf('\"', 1); - if (endIndex > 0) { - email1 = email1.substring(1, endIndex); - } - } - String cn = getPropertyIfExists(properties, "cn", Namespace.getNamespace("urn:schemas:contacts:")); - String givenName = getPropertyIfExists(properties, "givenName", Namespace.getNamespace("urn:schemas:contacts:")); - String sn = getPropertyIfExists(properties, "sn", Namespace.getNamespace("urn:schemas:contacts:")); - String title = getPropertyIfExists(properties, "title", Namespace.getNamespace("urn:schemas:contacts:")); - String company = getPropertyIfExists(properties, "o", Namespace.getNamespace("urn:schemas:contacts:")); - String location = getPropertyIfExists(properties, "location", Namespace.getNamespace("urn:schemas:contacts:")); - String department = getPropertyIfExists(properties, "department", Namespace.getNamespace("urn:schemas:contacts:")); - - String telephoneNumber = getPropertyIfExists(properties, "telephoneNumber", Namespace.getNamespace("urn:schemas:contacts:")); - String initials = getPropertyIfExists(properties, "initials", Namespace.getNamespace("urn:schemas:contacts:")); - String street = getPropertyIfExists(properties, "street", Namespace.getNamespace("urn:schemas:contacts:")); - String st = getPropertyIfExists(properties, "st", Namespace.getNamespace("urn:schemas:contacts:")); - String postalcode = getPropertyIfExists(properties, "postalcode", Namespace.getNamespace("urn:schemas:contacts:")); - String c = getPropertyIfExists(properties, "c", Namespace.getNamespace("urn:schemas:contacts:")); - String mobile = getPropertyIfExists(properties, "mobile", Namespace.getNamespace("urn:schemas:contacts:")); item = new HashMap(); - if (uid != null) { - item.put("AN", uid); + + DavPropertySet properties = response.getProperties(HttpStatus.SC_OK); + DavPropertyIterator propertiesIterator = properties.iterator(); + while (propertiesIterator.hasNext()) { + DavProperty property = propertiesIterator.nextProperty(); + String propertyName = property.getName().getName(); + String propertyValue = (String) property.getValue(); + if ("email1".equals(propertyName)) { + propertyName = "mail"; + if (propertyValue != null && propertyValue.startsWith("\"")) { + int endIndex = propertyValue.indexOf('\"', 1); + if (endIndex > 0) { + propertyValue = propertyValue.substring(1, endIndex); + } + } + } + item.put(propertyName, propertyValue); } - if (email1 != null) { - item.put("EM", email1); - } - if (cn != null) { - item.put("DN", cn); - } - if (givenName != null) { - item.put("first", givenName); - } - if (sn != null) { - item.put("last", sn); - } - if (title != null) { - item.put("TL", title); - } - if (company != null) { - item.put("CP", company); - } - if (location != null) { - item.put("OFFICE", location); - } - if (department != null) { - item.put("department", department); - } - if (telephoneNumber != null) { - item.put("PH", telephoneNumber); - } - if (initials != null) { - item.put("initials", initials); - } - if (street != null) { - item.put("street", street); - } - if (st != null) { - item.put("state", st); - } - if (postalcode != null) { - item.put("zip", postalcode); - } - if (c != null) { - item.put("country", c); - } - if (mobile != null) { - item.put("mobile", mobile); - } - results.put(uid, item); + results.put(item.get("uid"), item); } LOGGER.debug("contactFind " + searchFilter + ": " + results.size() + " result(s)"); diff --git a/src/java/davmail/ldap/LdapConnection.java b/src/java/davmail/ldap/LdapConnection.java index 45a12391..832ac94d 100644 --- a/src/java/davmail/ldap/LdapConnection.java +++ b/src/java/davmail/ldap/LdapConnection.java @@ -496,13 +496,19 @@ public class LdapConnection extends AbstractConnection { if (session != null) { // single user request String uid = dn.substring("uid=".length(), dn.indexOf(',')); - Map> persons = session.galFind("AN", uid); - Map person = persons.get(uid.toLowerCase()); - // filter out non exact results - if (persons.size() > 1 || person == null) { - persons = new HashMap>(); - if (person != null) { - persons.put(uid.toLowerCase(), person); + // first search in contact + Map> persons = session.contactFind("\"DAV:uid\"='" + uid + '\''); + // then in GAL + if (persons.isEmpty()) { + persons = session.galFind("AN", uid); + + Map person = persons.get(uid.toLowerCase()); + // filter out non exact results + if (persons.size() > 1 || person == null) { + persons = new HashMap>(); + if (person != null) { + persons.put(uid.toLowerCase(), person); + } } } size = persons.size(); @@ -713,23 +719,24 @@ public class LdapConnection extends AbstractConnection { boolean needObjectClasses = returningAttributes.contains("objectclass") || returningAttributes.isEmpty(); boolean iCalSearch = returningAttributes.contains("apple-serviceslocator"); boolean returnAllAttributes = returningAttributes.isEmpty(); - for (Map person : persons.values()) { - boolean needDetails = returnAllAttributes; - if (!needDetails) { - for (String attributeName : EXTENDED_ATTRIBUTES) { - if (returningAttributes.contains(attributeName)) { - needDetails = true; - break; - } + boolean needDetails = returnAllAttributes; + if (!needDetails) { + for (String attributeName : EXTENDED_ATTRIBUTES) { + if (returningAttributes.contains(attributeName)) { + needDetails = true; + break; } } - // iCal search, do not lookup details - if (iCalSearch) { - needDetails = false; - } + } + // iCal search, do not lookup details + if (iCalSearch) { + needDetails = false; + } - // add detailed information - if (needDetails) { + for (Map person : persons.values()) { + + // add detailed information, only for GAL entries + if (needDetails && person.get("AN") != null) { session.galLookup(person); } @@ -743,6 +750,10 @@ public class LdapConnection extends AbstractConnection { String ldapAttribute = entry.getKey(); String exchangeAttribute = entry.getValue(); String value = person.get(exchangeAttribute); + // contactFind return ldap attributes directly + if (value == null) { + value = person.get(ldapAttribute); + } if (value != null && (returnAllAttributes || returningAttributes.contains(ldapAttribute.toLowerCase()))) { ldapPerson.put(ldapAttribute, value);