From ab4dcb74c9bc3baaea20a15480596499cefe715e Mon Sep 17 00:00:00 2001 From: mguessan Date: Mon, 16 Aug 2010 12:24:16 +0000 Subject: [PATCH] LDAP: implement galFind MultiCondition over webdav and improve search by mail git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1342 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- .../davmail/exchange/ExchangeSession.java | 8 +++ .../exchange/dav/DavExchangeSession.java | 67 ++++++++++++++----- .../exchange/ews/EwsExchangeSession.java | 8 --- 3 files changed, 58 insertions(+), 25 deletions(-) diff --git a/src/java/davmail/exchange/ExchangeSession.java b/src/java/davmail/exchange/ExchangeSession.java index e47d709c..8ad3d71f 100644 --- a/src/java/davmail/exchange/ExchangeSession.java +++ b/src/java/davmail/exchange/ExchangeSession.java @@ -703,6 +703,14 @@ public abstract class ExchangeSession { } } + public List getConditions() { + return conditions; + } + + public Operator getOperator() { + return operator; + } + /** * Add a new condition. * diff --git a/src/java/davmail/exchange/dav/DavExchangeSession.java b/src/java/davmail/exchange/dav/DavExchangeSession.java index 6cfcbb1b..d500d3f9 100644 --- a/src/java/davmail/exchange/dav/DavExchangeSession.java +++ b/src/java/davmail/exchange/dav/DavExchangeSession.java @@ -183,9 +183,8 @@ public class DavExchangeSession extends ExchangeSession { static final HashMap GALFIND_CRITERIA_MAP = new HashMap(); static { - GALFIND_CRITERIA_MAP.put("uid", "AN"); - // assume mail starts with firstname - GALFIND_CRITERIA_MAP.put("smtpemail1", "FN"); + GALFIND_CRITERIA_MAP.put("imapUid", "AN"); + GALFIND_CRITERIA_MAP.put("smtpemail1", "EM"); GALFIND_CRITERIA_MAP.put("cn", "DN"); GALFIND_CRITERIA_MAP.put("givenName", "FN"); GALFIND_CRITERIA_MAP.put("sn", "LN"); @@ -240,21 +239,58 @@ public class DavExchangeSession extends ExchangeSession { public Map galFind(Condition condition, Set returningAttributes, int sizeLimit) throws IOException { Map contacts = new HashMap(); if (condition instanceof MultiCondition) { - // TODO + List conditions = ((ExchangeSession.MultiCondition) condition).getConditions(); + Operator operator = ((ExchangeSession.MultiCondition) condition).getOperator(); + if (operator == Operator.Or) { + for (Condition innerCondition : conditions) { + contacts.putAll(galFind(innerCondition, returningAttributes, sizeLimit)); + } + } else if (operator == Operator.And && !conditions.isEmpty()) { + Map innerContacts = galFind(conditions.get(0), returningAttributes, sizeLimit); + for (ExchangeSession.Contact contact : innerContacts.values()) { + if (condition.isMatch(contact)) { + contacts.put(contact.getName().toLowerCase(), contact); + } + } + } } else if (condition instanceof AttributeCondition) { String searchAttributeName = ((ExchangeSession.AttributeCondition) condition).getAttributeName(); String searchAttribute = GALFIND_CRITERIA_MAP.get(searchAttributeName); if (searchAttribute != null) { String searchValue = ((ExchangeSession.AttributeCondition) condition).getValue(); + StringBuilder query = new StringBuilder(); + query.append(getCmdBasePath()); + query.append("?Cmd=galfind"); + if ("EM".equals(searchAttribute)) { + // mail search, split + int atIndex = searchValue.indexOf('@'); + // remove suffix + if (atIndex >= 0) { + searchValue = searchValue.substring(0, atIndex); + } + // split firstname.lastname + int dotIndex = searchValue.indexOf('.'); + if (dotIndex >= 0) { + // assume mail starts with firstname + query.append("&FN=").append(searchValue.substring(0, dotIndex)); + query.append("&LN=").append(searchValue.substring(dotIndex + 1)); + } else { + query.append("&FN=").append(searchValue); + } + } else { + query.append('&').append(searchAttribute).append('=').append(searchValue); + } + GetMethod getMethod = new GetMethod(URIUtil.encodePathQuery(query.toString())); Map> results; - GetMethod getMethod = new GetMethod(URIUtil.encodePathQuery(getCmdBasePath() + "?Cmd=galfind&" + searchAttribute + '=' + searchValue)); try { DavGatewayHttpClientFacade.executeGetMethod(httpClient, getMethod, true); results = XMLStreamUtil.getElementContentsAsMap(getMethod.getResponseBodyAsStream(), "item", "AN"); } finally { getMethod.releaseConnection(); } - LOGGER.debug("galfind " + searchAttribute + '=' + searchValue + ": " + results.size() + " result(s)"); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(query.toString() + ": " + results.size() + " result(s)"); + } for (Map result : results.values()) { Contact contact = new Contact(); contact.setName(result.get("AN")); @@ -278,7 +314,7 @@ public class DavExchangeSession extends ExchangeSession { if (GALLOOKUP_ATTRIBUTES.contains(searchAttributeName)) { return true; } else if (returningAttributes == null) { - return false; + return true; // iCal search, do not call gallookup } else if (returningAttributes.contains("apple-serviceslocator")) { return false; @@ -305,7 +341,7 @@ public class DavExchangeSession extends ExchangeSession { */ public void galLookup(Contact contact) { if (!disableGalLookup) { - LOGGER.debug("galLookup("+contact.get("smtpemail1")+ ')'); + LOGGER.debug("galLookup(" + contact.get("smtpemail1") + ')'); GetMethod getMethod = null; try { getMethod = new GetMethod(URIUtil.encodePathQuery(getCmdBasePath() + "?Cmd=gallookup&ADDR=" + contact.get("smtpemail1"))); @@ -628,15 +664,9 @@ public class DavExchangeSession extends ExchangeSession { return false; } actualValue = actualValue.toLowerCase(); - if (operator == Operator.IsEqualTo) { - return actualValue.equals(lowerCaseValue); - } else if (operator == Operator.Like) { - return actualValue.contains(lowerCaseValue); - } else if (operator == Operator.StartsWith) { - return actualValue.startsWith(lowerCaseValue); - } else { - return false; - } + return (operator == Operator.IsEqualTo && actualValue.equals(lowerCaseValue)) || + (operator == Operator.Like && actualValue.contains(lowerCaseValue)) || + (operator == Operator.StartsWith && actualValue.startsWith(lowerCaseValue)); } } @@ -790,6 +820,9 @@ public class DavExchangeSession extends ExchangeSession { super(folderPath, itemName, properties, etag, noneMatch); } + /** + * Default constructor for galFind + */ public Contact() { } diff --git a/src/java/davmail/exchange/ews/EwsExchangeSession.java b/src/java/davmail/exchange/ews/EwsExchangeSession.java index efe01f35..7f6a8c8e 100644 --- a/src/java/davmail/exchange/ews/EwsExchangeSession.java +++ b/src/java/davmail/exchange/ews/EwsExchangeSession.java @@ -315,14 +315,6 @@ public class EwsExchangeSession extends ExchangeSession { } } } - - public List getConditions() { - return conditions; - } - - public Operator getOperator() { - return operator; - } } protected static class NotCondition extends ExchangeSession.NotCondition implements SearchExpression {