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

EWS: implement basic galFind search

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1330 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2010-08-07 18:59:49 +00:00
parent 889673e3e3
commit 9513f997dd
4 changed files with 114 additions and 5 deletions

View File

@ -27,6 +27,11 @@ public class ContainmentMode extends AttributeOption {
super("ContainmentMode", value); super("ContainmentMode", value);
} }
@Override
public String toString() {
return value;
}
/** /**
* Full String. * Full String.
*/ */

View File

@ -554,6 +554,7 @@ public abstract class EWSMethod extends PostMethod {
if (errorDetail != null) { if (errorDetail != null) {
if (!"ErrorAccessDenied".equals(errorDetail) if (!"ErrorAccessDenied".equals(errorDetail)
&& !"ErrorNameResolutionMultipleResults".equals(errorDetail) && !"ErrorNameResolutionMultipleResults".equals(errorDetail)
&& !"ErrorNameResolutionNoResults".equals(errorDetail)
&& !"ErrorMailRecipientNotFound".equals(errorDetail)) { && !"ErrorMailRecipientNotFound".equals(errorDetail)) {
try { try {
throw new EWSException(errorDetail + "\n request: " + new String(generateSoapEnvelope(), "UTF-8")); 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); logger.error("Error while parsing soap response: " + e, e);
} }
if (errorDetail != null) { if (errorDetail != null) {
logger.error(errorDetail); logger.debug(errorDetail);
} }
} }
} }

View File

@ -345,7 +345,7 @@ public class EwsExchangeSession extends ExchangeSession {
this.containmentComparison = containmentComparison; this.containmentComparison = containmentComparison;
} }
protected FieldURI getFieldURI(String attributeName) { protected FieldURI getFieldURI() {
FieldURI fieldURI = Field.get(attributeName); FieldURI fieldURI = Field.get(attributeName);
if (fieldURI == null) { if (fieldURI == null) {
throw new IllegalArgumentException("Unknown field: " + attributeName); throw new IllegalArgumentException("Unknown field: " + attributeName);
@ -353,6 +353,14 @@ public class EwsExchangeSession extends ExchangeSession {
return fieldURI; return fieldURI;
} }
protected String getValue() {
return value;
}
protected Operator getOperator() {
return operator;
}
public void appendTo(StringBuilder buffer) { public void appendTo(StringBuilder buffer) {
buffer.append("<t:").append(operator.toString()); buffer.append("<t:").append(operator.toString());
if (containmentMode != null) { if (containmentMode != null) {
@ -362,7 +370,7 @@ public class EwsExchangeSession extends ExchangeSession {
containmentComparison.appendTo(buffer); containmentComparison.appendTo(buffer);
} }
buffer.append('>'); buffer.append('>');
FieldURI fieldURI = getFieldURI(attributeName); FieldURI fieldURI = getFieldURI();
fieldURI.appendTo(buffer); fieldURI.appendTo(buffer);
buffer.append("<t:FieldURIOrConstant><t:Constant Value=\""); buffer.append("<t:FieldURIOrConstant><t:Constant Value=\"");
@ -376,6 +384,14 @@ public class EwsExchangeSession extends ExchangeSession {
buffer.append("</t:").append(operator.toString()).append('>'); buffer.append("</t:").append(operator.toString()).append('>');
} }
public String getAttributeName() {
return attributeName;
}
public ContainmentMode getContainmentMode() {
return containmentMode;
}
} }
protected static class HeaderCondition extends AttributeCondition { protected static class HeaderCondition extends AttributeCondition {
@ -385,7 +401,7 @@ public class EwsExchangeSession extends ExchangeSession {
} }
@Override @Override
protected FieldURI getFieldURI(String attributeName) { protected FieldURI getFieldURI() {
return new ExtendedFieldURI(ExtendedFieldURI.DistinguishedPropertySetType.InternetHeaders, attributeName); return new ExtendedFieldURI(ExtendedFieldURI.DistinguishedPropertySetType.InternetHeaders, attributeName);
} }
@ -685,6 +701,12 @@ public class EwsExchangeSession extends ExchangeSession {
super(folderPath, itemName, properties, etag, noneMatch); super(folderPath, itemName, properties, etag, noneMatch);
} }
/**
* Empty constructor for GalFind
*/
public Contact() {
}
protected Set<FieldUpdate> buildProperties() { protected Set<FieldUpdate> buildProperties() {
HashSet<FieldUpdate> list = new HashSet<FieldUpdate>(); HashSet<FieldUpdate> list = new HashSet<FieldUpdate>();
for (Map.Entry<String, String> entry : entrySet()) { for (Map.Entry<String, String> entry : entrySet()) {
@ -804,6 +826,10 @@ public class EwsExchangeSession extends ExchangeSession {
return itemResult; return itemResult;
} }
public void setName(String name) {
this.itemName = name;
}
} }
protected class Event extends ExchangeSession.Event { protected class Event extends ExchangeSession.Event {
@ -1209,6 +1235,64 @@ public class EwsExchangeSession extends ExchangeSession {
} }
} }
protected static final HashMap<String, String> GALFIND_ATTRIBUTE_MAP = new HashMap<String, String>();
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<ExchangeSession.Contact> galFind(Condition condition) throws IOException {
List<ExchangeSession.Contact> contacts = new ArrayList<ExchangeSession.Contact>();
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<EWSMethod.Item> 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<String, String> 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 { protected String convertDateFromExchange(String exchangeDateValue) throws DavMailException {
String zuluDateValue = null; String zuluDateValue = null;
if (exchangeDateValue != null) { if (exchangeDateValue != null) {

View File

@ -19,6 +19,7 @@
package davmail.exchange.ews; package davmail.exchange.ews;
import davmail.exchange.AbstractExchangeSessionTestCase; import davmail.exchange.AbstractExchangeSessionTestCase;
import davmail.exchange.ExchangeSession;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
@ -39,8 +40,26 @@ public class TestEwsExchangeSession extends AbstractExchangeSessionTestCase {
ResolveNamesMethod resolveNamesMethod = new ResolveNamesMethod("smtp:g"); ResolveNamesMethod resolveNamesMethod = new ResolveNamesMethod("smtp:g");
ewsSession.executeMethod(resolveNamesMethod); ewsSession.executeMethod(resolveNamesMethod);
List<EWSMethod.Item> items = resolveNamesMethod.getResponseItems(); List<EWSMethod.Item> items = resolveNamesMethod.getResponseItems();
for (EWSMethod.Item item:items) { for (EWSMethod.Item item : items) {
System.out.println(item); System.out.println(item);
} }
} }
public void testGalFind() throws IOException {
// find a set of contacts
List<ExchangeSession.Contact> 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());
}
}
} }