From 9513f997dd78f36a18772f571bca6c41d351ec2e Mon Sep 17 00:00:00 2001 From: mguessan Date: Sat, 7 Aug 2010 18:59:49 +0000 Subject: [PATCH] EWS: implement basic galFind search git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1330 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- .../davmail/exchange/ews/ContainmentMode.java | 5 ++ src/java/davmail/exchange/ews/EWSMethod.java | 3 +- .../exchange/ews/EwsExchangeSession.java | 90 ++++++++++++++++++- .../exchange/ews/TestEwsExchangeSession.java | 21 ++++- 4 files changed, 114 insertions(+), 5 deletions(-) diff --git a/src/java/davmail/exchange/ews/ContainmentMode.java b/src/java/davmail/exchange/ews/ContainmentMode.java index 91eb73eb..ed7ec368 100644 --- a/src/java/davmail/exchange/ews/ContainmentMode.java +++ b/src/java/davmail/exchange/ews/ContainmentMode.java @@ -27,6 +27,11 @@ public class ContainmentMode extends AttributeOption { super("ContainmentMode", value); } + @Override + public String toString() { + return value; + } + /** * Full String. */ diff --git a/src/java/davmail/exchange/ews/EWSMethod.java b/src/java/davmail/exchange/ews/EWSMethod.java index 15d5a761..032795a3 100644 --- a/src/java/davmail/exchange/ews/EWSMethod.java +++ b/src/java/davmail/exchange/ews/EWSMethod.java @@ -554,6 +554,7 @@ public abstract class EWSMethod extends PostMethod { if (errorDetail != null) { if (!"ErrorAccessDenied".equals(errorDetail) && !"ErrorNameResolutionMultipleResults".equals(errorDetail) + && !"ErrorNameResolutionNoResults".equals(errorDetail) && !"ErrorMailRecipientNotFound".equals(errorDetail)) { try { throw new EWSException(errorDetail + "\n request: " + new String(generateSoapEnvelope(), "UTF-8")); @@ -818,7 +819,7 @@ public abstract class EWSMethod extends PostMethod { logger.error("Error while parsing soap response: " + e, e); } if (errorDetail != null) { - logger.error(errorDetail); + logger.debug(errorDetail); } } } diff --git a/src/java/davmail/exchange/ews/EwsExchangeSession.java b/src/java/davmail/exchange/ews/EwsExchangeSession.java index 08e3d49e..df3fc61a 100644 --- a/src/java/davmail/exchange/ews/EwsExchangeSession.java +++ b/src/java/davmail/exchange/ews/EwsExchangeSession.java @@ -345,7 +345,7 @@ public class EwsExchangeSession extends ExchangeSession { this.containmentComparison = containmentComparison; } - protected FieldURI getFieldURI(String attributeName) { + protected FieldURI getFieldURI() { FieldURI fieldURI = Field.get(attributeName); if (fieldURI == null) { throw new IllegalArgumentException("Unknown field: " + attributeName); @@ -353,6 +353,14 @@ public class EwsExchangeSession extends ExchangeSession { return fieldURI; } + protected String getValue() { + return value; + } + + protected Operator getOperator() { + return operator; + } + public void appendTo(StringBuilder buffer) { buffer.append("'); - FieldURI fieldURI = getFieldURI(attributeName); + FieldURI fieldURI = getFieldURI(); fieldURI.appendTo(buffer); buffer.append("'); } + + public String getAttributeName() { + return attributeName; + } + + public ContainmentMode getContainmentMode() { + return containmentMode; + } } protected static class HeaderCondition extends AttributeCondition { @@ -385,7 +401,7 @@ public class EwsExchangeSession extends ExchangeSession { } @Override - protected FieldURI getFieldURI(String attributeName) { + protected FieldURI getFieldURI() { return new ExtendedFieldURI(ExtendedFieldURI.DistinguishedPropertySetType.InternetHeaders, attributeName); } @@ -685,6 +701,12 @@ public class EwsExchangeSession extends ExchangeSession { super(folderPath, itemName, properties, etag, noneMatch); } + /** + * Empty constructor for GalFind + */ + public Contact() { + } + protected Set buildProperties() { HashSet list = new HashSet(); for (Map.Entry entry : entrySet()) { @@ -804,6 +826,10 @@ public class EwsExchangeSession extends ExchangeSession { return itemResult; } + + public void setName(String name) { + this.itemName = name; + } } protected class Event extends ExchangeSession.Event { @@ -1209,6 +1235,64 @@ public class EwsExchangeSession extends ExchangeSession { } } + protected static final HashMap GALFIND_ATTRIBUTE_MAP = new HashMap(); + + static { + GALFIND_ATTRIBUTE_MAP.put("cn", "DisplayName"); + GALFIND_ATTRIBUTE_MAP.put("givenName", "GivenName"); + GALFIND_ATTRIBUTE_MAP.put("sn", "Surname"); + GALFIND_ATTRIBUTE_MAP.put("email1", "EmailAddress1"); + GALFIND_ATTRIBUTE_MAP.put("email2", "EmailAddress1"); + GALFIND_ATTRIBUTE_MAP.put("email3", "EmailAddress1"); + } + + protected List galFind(Condition condition) throws IOException { + List contacts = new ArrayList(); + if (condition instanceof AttributeCondition) { + String mappedAttributeName = GALFIND_ATTRIBUTE_MAP.get(((AttributeCondition) condition).getAttributeName()); + if (mappedAttributeName != null) { + String value = ((AttributeCondition) condition).getValue().toLowerCase(); + Operator operator = ((AttributeCondition) condition).getOperator(); + ContainmentMode containmentMode = ((AttributeCondition) condition).getContainmentMode(); + String searchValue = value; + if (mappedAttributeName.startsWith("EmailAddress")) { + searchValue = "smtp:" + searchValue; + } + if (operator == Operator.IsEqualTo) { + searchValue = '=' + searchValue; + } + ResolveNamesMethod resolveNamesMethod = new ResolveNamesMethod(searchValue); + executeMethod(resolveNamesMethod); + List responses = resolveNamesMethod.getResponseItems(); + for (EWSMethod.Item response : responses) { + String actualValue = response.get(mappedAttributeName); + if (actualValue != null) { + actualValue = actualValue.toLowerCase(); + } + if (actualValue != null && ( + (operator == Operator.IsEqualTo && value.equals(actualValue)) || + (operator == Operator.Contains && containmentMode == ContainmentMode.Substring && actualValue.contains(value)) | + (operator == Operator.Contains && containmentMode == ContainmentMode.Prefixed && actualValue.startsWith(value)) + )) { + Contact contact = new Contact(); + contact.setName(response.get("DisplayName")); + for (Map.Entry entry : GALFIND_ATTRIBUTE_MAP.entrySet()) { + String attributeValue = response.get(entry.getValue()); + if (attributeValue != null) { + contact.put(entry.getKey(), attributeValue); + } + } + contacts.add(contact); + } else if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Actual value " + actualValue + " does not match " + + mappedAttributeName + ' ' + operator + ' ' + containmentMode + ' ' + value); + } + } + } + } + return contacts; + } + protected String convertDateFromExchange(String exchangeDateValue) throws DavMailException { String zuluDateValue = null; if (exchangeDateValue != null) { diff --git a/src/test/davmail/exchange/ews/TestEwsExchangeSession.java b/src/test/davmail/exchange/ews/TestEwsExchangeSession.java index 515291fa..5f5fce3e 100644 --- a/src/test/davmail/exchange/ews/TestEwsExchangeSession.java +++ b/src/test/davmail/exchange/ews/TestEwsExchangeSession.java @@ -19,6 +19,7 @@ package davmail.exchange.ews; import davmail.exchange.AbstractExchangeSessionTestCase; +import davmail.exchange.ExchangeSession; import java.io.IOException; import java.util.List; @@ -39,8 +40,26 @@ public class TestEwsExchangeSession extends AbstractExchangeSessionTestCase { ResolveNamesMethod resolveNamesMethod = new ResolveNamesMethod("smtp:g"); ewsSession.executeMethod(resolveNamesMethod); List items = resolveNamesMethod.getResponseItems(); - for (EWSMethod.Item item:items) { + for (EWSMethod.Item item : items) { System.out.println(item); } } + + public void testGalFind() throws IOException { + // find a set of contacts + List contacts = ewsSession.galFind(ewsSession.startsWith("cn", "a")); + for (ExchangeSession.Contact contact : contacts) { + System.out.println(contact); + } + if (contacts.size() > 0) { + ExchangeSession.Contact testContact = contacts.get(0); + contacts = ewsSession.galFind(ewsSession.isEqualTo("cn", testContact.get("cn"))); + assertEquals(1, contacts.size()); + contacts = ewsSession.galFind(ewsSession.isEqualTo("email1", testContact.get("email1"))); + assertEquals(1, contacts.size()); + contacts = ewsSession.galFind(ewsSession.startsWith("email1", testContact.get("email1"))); + assertEquals(1, contacts.size()); + } + } + }