1
0
mirror of https://github.com/moparisthebest/davmail synced 2024-12-13 11:12:22 -05:00

LDAP: improve Contact search filter support

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@700 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2009-09-04 22:30:03 +00:00
parent e7e103f103
commit 7bfc952437
2 changed files with 47 additions and 36 deletions

View File

@ -2643,7 +2643,7 @@ public class ExchangeSession {
"\"urn:schemas:contacts:title\"," + "\"urn:schemas:contacts:title\"," +
"\"urn:schemas:httpmail:textdescription\"") "\"urn:schemas:httpmail:textdescription\"")
.append(" FROM Scope('SHALLOW TRAVERSAL OF \"").append(contactsUrl).append("\"')\n"); .append(" FROM Scope('SHALLOW TRAVERSAL OF \"").append(contactsUrl).append("\"')\n");
if (searchFilter != null) { if (searchFilter != null && searchFilter.length() > 0) {
searchRequest.append(" WHERE ").append(searchFilter); searchRequest.append(" WHERE ").append(searchFilter);
} }
MultiStatusResponse[] responses = DavGatewayHttpClientFacade.executeSearchMethod( MultiStatusResponse[] responses = DavGatewayHttpClientFacade.executeSearchMethod(

View File

@ -114,8 +114,8 @@ 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", "notes"); CONTACT_ATTRIBUTE_MAP.put("textdescription", "description");
CONTACT_ATTRIBUTE_MAP.put("nickname", "xmozillanickname"); CONTACT_ATTRIBUTE_MAP.put("nickname", "mozillanickname");
} }
static final HashMap<String, String> STATIC_ATTRIBUTE_MAP = new HashMap<String, String>(); static final HashMap<String, String> STATIC_ATTRIBUTE_MAP = new HashMap<String, String>();
@ -232,11 +232,14 @@ public class LdapConnection extends AbstractConnection {
CONTACT_MAP.put("givenname", "urn:schemas:contacts:givenName"); CONTACT_MAP.put("givenname", "urn:schemas:contacts:givenName");
CONTACT_MAP.put("sn", "urn:schemas:contacts:sn"); CONTACT_MAP.put("sn", "urn:schemas:contacts:sn");
CONTACT_MAP.put("title", "urn:schemas:contacts:title"); CONTACT_MAP.put("title", "urn:schemas:contacts:title");
CONTACT_MAP.put("company", "urn:schemas:contacts:company"); CONTACT_MAP.put("company", "urn:schemas:contacts:o");
CONTACT_MAP.put("o", "urn:schemas:contacts:company"); CONTACT_MAP.put("o", "urn:schemas:contacts:o");
CONTACT_MAP.put("l", "urn:schemas:contacts:location"); CONTACT_MAP.put("l", "urn:schemas:contacts:l");
CONTACT_MAP.put("department", "urn:schemas:contacts:department"); CONTACT_MAP.put("department", "urn:schemas:contacts:department");
CONTACT_MAP.put("apple-group-realname", "urn:schemas:contacts:department"); CONTACT_MAP.put("apple-group-realname", "urn:schemas:contacts:department");
CONTACT_MAP.put("description", "urn:schemas:httpmail:textdescription");
CONTACT_MAP.put("mozillahomelocalityname", "urn:schemas:contacts:homeCity");
CONTACT_MAP.put("c", "urn:schemas:contacts:co");
} }
/** /**
@ -579,15 +582,18 @@ public class LdapConnection extends AbstractConnection {
} }
for (SimpleFilter simpleFilter : ldapFilter.getOrFilterSet()) { for (SimpleFilter simpleFilter : ldapFilter.getOrFilterSet()) {
if (persons.size() < sizeLimit) { if (persons.size() < sizeLimit) {
for (Map<String, String> person : session.galFind(simpleFilter.getGalFindAttributeName(), simpleFilter.value).values()) { String attributeName = simpleFilter.getGalFindAttributeName();
if ((simpleFilter.operator == LDAP_FILTER_SUBSTRINGS) if (attributeName != null) {
// exclude non exact match on exact search for (Map<String, String> person : session.galFind(attributeName, simpleFilter.value).values()) {
|| (simpleFilter.operator == LDAP_FILTER_EQUALITY && if ((simpleFilter.operator == LDAP_FILTER_SUBSTRINGS)
simpleFilter.value.equalsIgnoreCase(person.get(simpleFilter.getGalFindAttributeName())))) { // exclude non exact match on exact search
persons.put(person.get("AN"), person); || (simpleFilter.operator == LDAP_FILTER_EQUALITY &&
} simpleFilter.value.equalsIgnoreCase(person.get(attributeName)))) {
if (persons.size() == sizeLimit) { persons.put(person.get("AN"), person);
break; }
if (persons.size() == sizeLimit) {
break;
}
} }
} }
} }
@ -965,24 +971,23 @@ public class LdapConnection extends AbstractConnection {
filterString.append('(').append(simpleFilter.toString()).append(')'); filterString.append('(').append(simpleFilter.toString()).append(')');
String attributeName = simpleFilter.getAttributeName(); String attributeName = simpleFilter.getAttributeName();
// known search attribute // full search (objectclass=*) filter
if (CRITERIA_MAP.get(attributeName) != null) { if ("objectclass".equals(attributeName) && SimpleFilter.STAR.equals(simpleFilter.value)) {
isFullSearch = true;
// known search attribute
} else {
isFullSearch = false; isFullSearch = false;
if (ldapFilterType == 0 || ldapFilterType == LDAP_FILTER_OR) { if (ldapFilterType == 0 || ldapFilterType == LDAP_FILTER_OR) {
orCriteria.add(simpleFilter); orCriteria.add(simpleFilter);
} else if (ldapFilterType == LDAP_FILTER_AND) { } else if (ldapFilterType == LDAP_FILTER_AND) {
andCriteria.add(simpleFilter); andCriteria.add(simpleFilter);
} }
// full search (objectclass=*) filter
} else if ("objectclass".equals(attributeName) && SimpleFilter.STAR.equals(simpleFilter.value)) {
isFullSearch = true;
// ignore search attribute
} else if (IGNORE_MAP.contains(attributeName)) {
DavGatewayTray.debug(new BundleMessage("LOG_LDAP_IGNORE_FILTER_ATTRIBUTE", attributeName, simpleFilter.value));
// unknown search attribute: warn // unknown search attribute: warn
} else { if (!IGNORE_MAP.contains(attributeName) && CRITERIA_MAP.get(attributeName) == null && CONTACT_MAP.get(attributeName) == null) {
DavGatewayTray.warn(new BundleMessage("LOG_LDAP_UNSUPPORTED_FILTER_ATTRIBUTE", attributeName, simpleFilter.value)); DavGatewayTray.debug(new BundleMessage("LOG_LDAP_UNSUPPORTED_FILTER_ATTRIBUTE", attributeName, simpleFilter.value));
}
} }
} }
public String getContactSearchFilter() { public String getContactSearchFilter() {
@ -991,18 +996,24 @@ public class LdapConnection extends AbstractConnection {
} else { } else {
StringBuilder buffer = new StringBuilder(); StringBuilder buffer = new StringBuilder();
for (SimpleFilter simpleFilter : orCriteria) { for (SimpleFilter simpleFilter : orCriteria) {
if (buffer.length() > 0) { String contactAttributeName = simpleFilter.getContactAttributeName();
buffer.append(" OR "); if (contactAttributeName != null) {
if (buffer.length() > 0) {
buffer.append(" OR ");
}
buffer.append('"').append(contactAttributeName).append('"');
if (simpleFilter.operator == LDAP_FILTER_EQUALITY) {
buffer.append(" = '").append(simpleFilter.value).append('\'');
} else if ("mail".equals(simpleFilter.getAttributeName())) {
buffer.append(" LIKE '\"").append(simpleFilter.value).append("%'");
} else {
buffer.append(" LIKE '").append(simpleFilter.value).append("%'");
}
} }
buffer.append('"').append(simpleFilter.getContactAttributeName()).append('"'); }
if (simpleFilter.operator == LDAP_FILTER_EQUALITY) { // if criteria not empty but filter is, add a fake filter
buffer.append(" = '").append(simpleFilter.value).append('\''); if (!orCriteria.isEmpty() && buffer.length() == 0) {
} else if ("mail".equals(simpleFilter.getAttributeName())) { buffer.append("\"DAV:uid\"='#INVALID#'");
buffer.append(" LIKE '\"").append(simpleFilter.value).append("%'");
} else {
buffer.append(" LIKE '").append(simpleFilter.value).append("%'");
}
} }
return buffer.toString(); return buffer.toString();
} }