LDAP: use imap uid as ldap uid

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1135 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2010-07-06 09:49:26 +00:00
parent b988bd2f12
commit fb5f496960
1 changed files with 38 additions and 51 deletions

View File

@ -29,9 +29,6 @@ import davmail.exchange.ExchangeSession;
import davmail.exchange.ExchangeSessionFactory; import davmail.exchange.ExchangeSessionFactory;
import davmail.ui.tray.DavGatewayTray; import davmail.ui.tray.DavGatewayTray;
import davmail.util.StringUtil; import davmail.util.StringUtil;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
@ -39,8 +36,6 @@ import java.io.IOException;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.*; import java.net.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
/** /**
@ -103,6 +98,7 @@ public class LdapConnection extends AbstractConnection {
static final HashMap<String, String> CONTACT_ATTRIBUTE_MAP = new HashMap<String, String>(); static final HashMap<String, String> CONTACT_ATTRIBUTE_MAP = new HashMap<String, String>();
static { static {
CONTACT_ATTRIBUTE_MAP.put("imapUid", "uid");
CONTACT_ATTRIBUTE_MAP.put("co", "countryname"); CONTACT_ATTRIBUTE_MAP.put("co", "countryname");
CONTACT_ATTRIBUTE_MAP.put("extensionattribute1", "custom1"); CONTACT_ATTRIBUTE_MAP.put("extensionattribute1", "custom1");
CONTACT_ATTRIBUTE_MAP.put("extensionattribute2", "custom2"); CONTACT_ATTRIBUTE_MAP.put("extensionattribute2", "custom2");
@ -116,7 +112,7 @@ public class LdapConnection extends AbstractConnection {
CONTACT_ATTRIBUTE_MAP.put("homeState", "mozillahomestate"); CONTACT_ATTRIBUTE_MAP.put("homeState", "mozillahomestate");
CONTACT_ATTRIBUTE_MAP.put("homeStreet", "mozillahomestreet"); CONTACT_ATTRIBUTE_MAP.put("homeStreet", "mozillahomestreet");
CONTACT_ATTRIBUTE_MAP.put("businesshomepage", "mozillaworkurl"); CONTACT_ATTRIBUTE_MAP.put("businesshomepage", "mozillaworkurl");
CONTACT_ATTRIBUTE_MAP.put("textdescription", "description"); CONTACT_ATTRIBUTE_MAP.put("description", "description");
CONTACT_ATTRIBUTE_MAP.put("nickname", "mozillanickname"); CONTACT_ATTRIBUTE_MAP.put("nickname", "mozillanickname");
} }
@ -227,21 +223,21 @@ public class LdapConnection extends AbstractConnection {
static final HashMap<String, String> CONTACT_MAP = new HashMap<String, String>(); static final HashMap<String, String> CONTACT_MAP = new HashMap<String, String>();
static { static {
CONTACT_MAP.put("uid", "DAV:uid"); CONTACT_MAP.put("uid", "imapUid");
CONTACT_MAP.put("mail", "urn:schemas:contacts:email1"); CONTACT_MAP.put("mail", "email1");
CONTACT_MAP.put("displayname", "urn:schemas:contacts:cn"); CONTACT_MAP.put("displayname", "cn");
CONTACT_MAP.put("cn", "urn:schemas:contacts:cn"); CONTACT_MAP.put("cn", "cn");
CONTACT_MAP.put("givenname", "urn:schemas:contacts:givenName"); CONTACT_MAP.put("givenname", "givenName");
CONTACT_MAP.put("sn", "urn:schemas:contacts:sn"); CONTACT_MAP.put("sn", "sn");
CONTACT_MAP.put("title", "urn:schemas:contacts:title"); CONTACT_MAP.put("title", "title");
CONTACT_MAP.put("company", "urn:schemas:contacts:o"); CONTACT_MAP.put("company", "o");
CONTACT_MAP.put("o", "urn:schemas:contacts:o"); CONTACT_MAP.put("o", "o");
CONTACT_MAP.put("l", "urn:schemas:contacts:l"); CONTACT_MAP.put("l", "l");
CONTACT_MAP.put("department", "urn:schemas:contacts:department"); CONTACT_MAP.put("department", "department");
CONTACT_MAP.put("apple-group-realname", "urn:schemas:contacts:department"); CONTACT_MAP.put("apple-group-realname", "department");
CONTACT_MAP.put("description", "urn:schemas:httpmail:textdescription"); CONTACT_MAP.put("description", "description");
CONTACT_MAP.put("mozillahomelocalityname", "urn:schemas:contacts:homeCity"); CONTACT_MAP.put("mozillahomelocalityname", "homeCity");
CONTACT_MAP.put("c", "urn:schemas:contacts:co"); CONTACT_MAP.put("c", "c");
} }
/** /**
@ -1027,26 +1023,16 @@ public class LdapConnection extends AbstractConnection {
ExchangeSession.Condition condition; ExchangeSession.Condition condition;
// convert hex uid to base64
String actualValue = value;
if ("uid".equals(contactAttributeName)) {
try {
actualValue = StringUtil.hexToBase64(value);
} catch (DecoderException e) {
// ignore, this is not an hex uid
}
}
if (operator == LDAP_FILTER_EQUALITY) { if (operator == LDAP_FILTER_EQUALITY) {
condition = session.equals(contactAttributeName, actualValue); condition = session.equals(contactAttributeName, value);
} else if ("*".equals(actualValue)) { } else if ("*".equals(value)) {
condition = session.not(session.isNull(contactAttributeName)); condition = session.not(session.isNull(contactAttributeName));
} else { } else {
// endsWith not supported by exchange, convert to contains // endsWith not supported by exchange, convert to contains
if (mode == LDAP_SUBSTRING_FINAL || mode == LDAP_SUBSTRING_ANY) { if (mode == LDAP_SUBSTRING_FINAL || mode == LDAP_SUBSTRING_ANY) {
condition = session.contains(contactAttributeName, actualValue); condition = session.contains(contactAttributeName, value);
} else { } else {
condition = session.startsWith(contactAttributeName, actualValue); condition = session.startsWith(contactAttributeName, value);
} }
} }
return condition; return condition;
@ -1177,9 +1163,11 @@ public class LdapConnection extends AbstractConnection {
// first search in contact // first search in contact
try { try {
persons = contactFind(session.equals("uid", StringUtil.hexToBase64(uid)), returningAttributes); // check if this is a contact uid
} catch (DecoderException e) { Integer.parseInt(uid);
// ignore, this is not an hex uid persons = contactFind(session.equals("imapUid", uid), returningAttributes);
} catch (NumberFormatException e) {
// ignore, this is not a contact uid
} }
// then in GAL // then in GAL
@ -1213,7 +1201,7 @@ public class LdapConnection extends AbstractConnection {
if (ldapFilter.isFullSearch()) { if (ldapFilter.isFullSearch()) {
// append personal contacts first // append personal contacts first
for (Map<String, String> person : contactFind(null, returningAttributes).values()) { for (Map<String, String> person : contactFind(null, returningAttributes).values()) {
persons.put(person.get("uid"), person); persons.put(person.get("imapUid"), person);
if (persons.size() == sizeLimit) { if (persons.size() == sizeLimit) {
break; break;
} }
@ -1240,7 +1228,7 @@ public class LdapConnection extends AbstractConnection {
// ignored all attribute filters => return empty results // ignored all attribute filters => return empty results
if (ldapFilter.isFullSearch() || filter != null) { if (ldapFilter.isFullSearch() || filter != null) {
for (Map<String, String> person : contactFind(filter, returningAttributes).values()) { for (Map<String, String> person : contactFind(filter, returningAttributes).values()) {
persons.put(person.get("uid"), person); persons.put(person.get("imapUid"), person);
if (persons.size() == sizeLimit) { if (persons.size() == sizeLimit) {
break; break;
@ -1295,6 +1283,7 @@ public class LdapConnection extends AbstractConnection {
} }
/** /**
* Search users in contacts folder * Search users in contacts folder
* *
@ -1307,14 +1296,15 @@ public class LdapConnection extends AbstractConnection {
if (returningAttributes != null && !returningAttributes.isEmpty()) { if (returningAttributes != null && !returningAttributes.isEmpty()) {
ldapReturningAttributes = new HashSet<String>(); ldapReturningAttributes = new HashSet<String>();
// always return uid // always return uid
ldapReturningAttributes.add("uid"); ldapReturningAttributes.add("imapUid");
ldapReturningAttributes.add("email1");
for (String attribute : returningAttributes) { for (String attribute : returningAttributes) {
if (!"objectclass".equals(attribute)) { if (!"objectclass".equals(attribute)) {
ldapReturningAttributes.add(attribute); ldapReturningAttributes.add(attribute);
} }
} }
} else { } else {
ldapReturningAttributes = ExchangeSession.CONTACT_ATTRIBUTES; ldapReturningAttributes = ExchangeSession.CONTACT_ATTRIBUTES;
} }
Map<String, Map<String, String>> results = new HashMap<String, Map<String, String>>(); Map<String, Map<String, String>> results = new HashMap<String, Map<String, String>>();
@ -1336,15 +1326,12 @@ public class LdapConnection extends AbstractConnection {
} catch (ParseException e) { } catch (ParseException e) {
throw new IOException(e); throw new IOException(e);
} }
} else if ("textdescription".equals(propertyName) && " \n".equals(propertyValue)) { } else if ("description".equals(propertyName) && " \n".equals(propertyValue)) {
propertyValue = null; propertyValue = null;
} }
*/ */
if (contact.get("uid") != null) { if (contact.get("imapUid") != null) {
// convert uid to hex results.put(contact.get("imapUid"), contact);
String hexUid = StringUtil.base64ToHex(contact.get("uid"));
contact.put("uid", hexUid);
results.put(hexUid, contact);
} }
} }