diff --git a/src/java/davmail/ldap/LdapConnection.java b/src/java/davmail/ldap/LdapConnection.java index c81490a4..b4373110 100644 --- a/src/java/davmail/ldap/LdapConnection.java +++ b/src/java/davmail/ldap/LdapConnection.java @@ -94,25 +94,25 @@ public class LdapConnection extends AbstractConnection { } - static final HashMap CONTACT_ATTRIBUTE_MAP = new HashMap(); + static final HashMap CONTACT_TO_LDAP_ATTRIBUTE_MAP = new HashMap(); static { - CONTACT_ATTRIBUTE_MAP.put("imapUid", "uid"); - CONTACT_ATTRIBUTE_MAP.put("co", "countryname"); - CONTACT_ATTRIBUTE_MAP.put("extensionattribute1", "custom1"); - CONTACT_ATTRIBUTE_MAP.put("extensionattribute2", "custom2"); - CONTACT_ATTRIBUTE_MAP.put("extensionattribute3", "custom3"); - CONTACT_ATTRIBUTE_MAP.put("extensionattribute4", "custom4"); - CONTACT_ATTRIBUTE_MAP.put("email1", "mail"); - CONTACT_ATTRIBUTE_MAP.put("email2", "xmozillasecondemail"); - CONTACT_ATTRIBUTE_MAP.put("homeCountry", "mozillahomecountryname"); - CONTACT_ATTRIBUTE_MAP.put("homeCity", "mozillahomelocalityname"); - CONTACT_ATTRIBUTE_MAP.put("homePostalCode", "mozillahomepostalcode"); - CONTACT_ATTRIBUTE_MAP.put("homeState", "mozillahomestate"); - CONTACT_ATTRIBUTE_MAP.put("homeStreet", "mozillahomestreet"); - CONTACT_ATTRIBUTE_MAP.put("businesshomepage", "mozillaworkurl"); - CONTACT_ATTRIBUTE_MAP.put("description", "description"); - CONTACT_ATTRIBUTE_MAP.put("nickname", "mozillanickname"); + CONTACT_TO_LDAP_ATTRIBUTE_MAP.put("imapUid", "uid"); + CONTACT_TO_LDAP_ATTRIBUTE_MAP.put("co", "countryname"); + CONTACT_TO_LDAP_ATTRIBUTE_MAP.put("extensionattribute1", "custom1"); + CONTACT_TO_LDAP_ATTRIBUTE_MAP.put("extensionattribute2", "custom2"); + CONTACT_TO_LDAP_ATTRIBUTE_MAP.put("extensionattribute3", "custom3"); + CONTACT_TO_LDAP_ATTRIBUTE_MAP.put("extensionattribute4", "custom4"); + CONTACT_TO_LDAP_ATTRIBUTE_MAP.put("email1", "mail"); + CONTACT_TO_LDAP_ATTRIBUTE_MAP.put("email2", "xmozillasecondemail"); + CONTACT_TO_LDAP_ATTRIBUTE_MAP.put("homeCountry", "mozillahomecountryname"); + CONTACT_TO_LDAP_ATTRIBUTE_MAP.put("homeCity", "mozillahomelocalityname"); + CONTACT_TO_LDAP_ATTRIBUTE_MAP.put("homePostalCode", "mozillahomepostalcode"); + CONTACT_TO_LDAP_ATTRIBUTE_MAP.put("homeState", "mozillahomestate"); + CONTACT_TO_LDAP_ATTRIBUTE_MAP.put("homeStreet", "mozillahomestreet"); + CONTACT_TO_LDAP_ATTRIBUTE_MAP.put("businesshomepage", "mozillaworkurl"); + CONTACT_TO_LDAP_ATTRIBUTE_MAP.put("description", "description"); + CONTACT_TO_LDAP_ATTRIBUTE_MAP.put("nickname", "mozillanickname"); } static final HashMap STATIC_ATTRIBUTE_MAP = new HashMap(); @@ -219,24 +219,24 @@ public class LdapConnection extends AbstractConnection { CRITERIA_MAP.put("apple-group-realname", "DP"); } - static final HashMap CONTACT_MAP = new HashMap(); + static final HashMap LDAP_TO_CONTACT_ATTRIBUTE_MAP = new HashMap(); static { - CONTACT_MAP.put("uid", "imapUid"); - CONTACT_MAP.put("mail", "email1"); - CONTACT_MAP.put("displayname", "cn"); - CONTACT_MAP.put("cn", "cn"); - CONTACT_MAP.put("givenname", "givenName"); - CONTACT_MAP.put("sn", "sn"); - CONTACT_MAP.put("title", "title"); - CONTACT_MAP.put("company", "o"); - CONTACT_MAP.put("o", "o"); - CONTACT_MAP.put("l", "l"); - CONTACT_MAP.put("department", "department"); - CONTACT_MAP.put("apple-group-realname", "department"); - CONTACT_MAP.put("description", "description"); - CONTACT_MAP.put("mozillahomelocalityname", "homeCity"); - CONTACT_MAP.put("c", "c"); + LDAP_TO_CONTACT_ATTRIBUTE_MAP.put("uid", "imapUid"); + LDAP_TO_CONTACT_ATTRIBUTE_MAP.put("mail", "email1"); + LDAP_TO_CONTACT_ATTRIBUTE_MAP.put("displayname", "cn"); + LDAP_TO_CONTACT_ATTRIBUTE_MAP.put("cn", "cn"); + LDAP_TO_CONTACT_ATTRIBUTE_MAP.put("givenname", "givenName"); + LDAP_TO_CONTACT_ATTRIBUTE_MAP.put("sn", "sn"); + LDAP_TO_CONTACT_ATTRIBUTE_MAP.put("title", "title"); + LDAP_TO_CONTACT_ATTRIBUTE_MAP.put("company", "o"); + LDAP_TO_CONTACT_ATTRIBUTE_MAP.put("o", "o"); + LDAP_TO_CONTACT_ATTRIBUTE_MAP.put("l", "l"); + LDAP_TO_CONTACT_ATTRIBUTE_MAP.put("department", "department"); + LDAP_TO_CONTACT_ATTRIBUTE_MAP.put("apple-group-realname", "department"); + LDAP_TO_CONTACT_ATTRIBUTE_MAP.put("description", "description"); + LDAP_TO_CONTACT_ATTRIBUTE_MAP.put("mozillahomelocalityname", "homeCity"); + LDAP_TO_CONTACT_ATTRIBUTE_MAP.put("c", "c"); } /** @@ -974,7 +974,7 @@ public class LdapConnection extends AbstractConnection { } else if (IGNORE_MAP.contains(attributeName)) { // Ignore this specific attribute return true; - } else if (CRITERIA_MAP.get(attributeName) == null && CONTACT_MAP.get(attributeName) == null) { + } else if (CRITERIA_MAP.get(attributeName) == null && LDAP_TO_CONTACT_ATTRIBUTE_MAP.get(attributeName) == null) { DavGatewayTray.debug(new BundleMessage("LOG_LDAP_UNSUPPORTED_FILTER_ATTRIBUTE", attributeName, value)); @@ -1108,7 +1108,7 @@ public class LdapConnection extends AbstractConnection { } public String getContactAttributeName() { - return CONTACT_MAP.get(attributeName); + return LDAP_TO_CONTACT_ATTRIBUTE_MAP.get(attributeName); } } @@ -1296,10 +1296,14 @@ public class LdapConnection extends AbstractConnection { ldapReturningAttributes = new HashSet(); // always return uid ldapReturningAttributes.add("imapUid"); - ldapReturningAttributes.add("email1"); for (String attribute : returningAttributes) { if (!"objectclass".equals(attribute)) { - ldapReturningAttributes.add(attribute); + String mappedAttribute = LDAP_TO_CONTACT_ATTRIBUTE_MAP.get(attribute); + if (mappedAttribute == null) { + ldapReturningAttributes.add(attribute); + } else { + ldapReturningAttributes.add(mappedAttribute); + } } } } else { @@ -1404,7 +1408,7 @@ public class LdapConnection extends AbstractConnection { for (Map.Entry entry : person.entrySet()) { String contactAttribute = entry.getKey(); // get converted attribute name - String ldapAttribute = CONTACT_ATTRIBUTE_MAP.get(contactAttribute); + String ldapAttribute = CONTACT_TO_LDAP_ATTRIBUTE_MAP.get(contactAttribute); // no conversion, use exchange attribute name if (ldapAttribute == null) { ldapAttribute = contactAttribute; diff --git a/src/test/davmail/ldap/TestLdap.java b/src/test/davmail/ldap/TestLdap.java new file mode 100644 index 00000000..6d000c00 --- /dev/null +++ b/src/test/davmail/ldap/TestLdap.java @@ -0,0 +1,79 @@ +/* + * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway + * Copyright (C) 2010 Mickael Guessant + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package davmail.ldap; + +import davmail.AbstractDavMailTestCase; +import davmail.DavGateway; +import davmail.Settings; +import davmail.exchange.ExchangeSessionFactory; + +import javax.naming.NamingEnumeration; +import javax.naming.NamingException; +import javax.naming.directory.SearchControls; +import javax.naming.directory.SearchResult; +import javax.naming.ldap.InitialLdapContext; +import java.io.IOException; +import java.util.Hashtable; + +/** + * Test LDAP. + */ +public class TestLdap extends AbstractDavMailTestCase { + InitialLdapContext ldapContext; + + @Override + public void setUp() throws IOException { + super.setUp(); + if (ldapContext == null) { + // start gateway + DavGateway.start(); + Hashtable env = new Hashtable(); + env.put("java.naming.security.authentication", "simple"); + env.put("java.naming.security.principal", Settings.getProperty("davmail.username")); + env.put("java.naming.security.credentials", Settings.getProperty("davmail.password")); + + env.put("com.sun.jndi.ldap.connect.pool", "true"); + env.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory"); + env.put("java.naming.provider.url", "ldap://localhost:" + Settings.getIntProperty("davmail.ldapPort")); + env.put("java.naming.referral", "follow"); + + try { + ldapContext = new InitialLdapContext(env, null); + } catch (NamingException e) { + throw new IOException(e); + } + } + if (session == null) { + session = ExchangeSessionFactory.getInstance(Settings.getProperty("davmail.username"), Settings.getProperty("davmail.password")); + } + } + + public void testSearchOneLevel() throws NamingException { + SearchControls searchControls = new SearchControls(); + searchControls.setSearchScope(SearchControls.ONELEVEL_SCOPE); + NamingEnumeration searchResults = ldapContext.search("ou=people", "(objectclass=*)", searchControls); + } + + public void testSearchMail() throws NamingException { + SearchControls searchControls = new SearchControls(); + searchControls.setSearchScope(SearchControls.ONELEVEL_SCOPE); + searchControls.setReturningAttributes(new String[]{"mail"}); + NamingEnumeration searchResults = ldapContext.search("ou=people", "(objectclass=*)", searchControls); + } +}