From 1d4c095dc1d3fd8ea1b73399fecc855e03f65545 Mon Sep 17 00:00:00 2001 From: mguessan Date: Tue, 3 Aug 2010 22:22:13 +0000 Subject: [PATCH] EWS: implement resolvenames response parsing git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1325 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- src/java/davmail/exchange/ews/EWSMethod.java | 16 ++++-- .../exchange/ews/ResolveNamesMethod.java | 49 ++++++++++++++++++- .../exchange/ews/TestEwsExchangeSession.java | 10 +++- 3 files changed, 69 insertions(+), 6 deletions(-) diff --git a/src/java/davmail/exchange/ews/EWSMethod.java b/src/java/davmail/exchange/ews/EWSMethod.java index 05b1501a..15d5a761 100644 --- a/src/java/davmail/exchange/ews/EWSMethod.java +++ b/src/java/davmail/exchange/ews/EWSMethod.java @@ -82,10 +82,21 @@ public abstract class EWSMethod extends PostMethod { * @param methodName method name */ public EWSMethod(String itemType, String methodName) { + this(itemType, methodName, itemType + 's'); + } + + /** + * Build EWS method + * + * @param itemType item type + * @param methodName method name + * @param responseCollectionName item response collection name + */ + public EWSMethod(String itemType, String methodName, String responseCollectionName) { super("/ews/exchange.asmx"); this.itemType = itemType; this.methodName = methodName; - responseCollectionName = itemType + 's'; + this.responseCollectionName = responseCollectionName; setRequestEntity(new RequestEntity() { byte[] content; @@ -649,7 +660,6 @@ public abstract class EWSMethod extends PostMethod { if (event == XMLStreamConstants.START_ELEMENT) { String tagLocalName = reader.getLocalName(); String value = null; - // detect version if ("ExtendedProperty".equals(tagLocalName)) { addExtendedPropertyValue(reader, responseItem); } else if (tagLocalName.endsWith("MimeContent")) { @@ -751,7 +761,7 @@ public abstract class EWSMethod extends PostMethod { } } - private String getTagContent(XMLStreamReader reader) throws XMLStreamException { + protected String getTagContent(XMLStreamReader reader) throws XMLStreamException { String tagLocalName = reader.getLocalName(); while (reader.hasNext() && !(reader.getEventType() == XMLStreamConstants.END_ELEMENT)) { reader.next(); diff --git a/src/java/davmail/exchange/ews/ResolveNamesMethod.java b/src/java/davmail/exchange/ews/ResolveNamesMethod.java index fb9ff693..db0c2f55 100644 --- a/src/java/davmail/exchange/ews/ResolveNamesMethod.java +++ b/src/java/davmail/exchange/ews/ResolveNamesMethod.java @@ -18,20 +18,65 @@ */ package davmail.exchange.ews; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + /** * Resolve Names method. */ public class ResolveNamesMethod extends EWSMethod { protected static final AttributeOption RETURN_FULL_CONTACT_DATA = new AttributeOption("ReturnFullContactData", "true"); + /** * Build Resolve Names method * - * @param value search value + * @param value search value */ public ResolveNamesMethod(String value) { - super("Contact", "ResolveNames"); + super("Contact", "ResolveNames", "ResolutionSet"); addMethodOption(SearchScope.ActiveDirectory); addMethodOption(RETURN_FULL_CONTACT_DATA); unresolvedEntry = new ElementOption("m:UnresolvedEntry", value); } + + @Override + protected Item handleItem(XMLStreamReader reader) throws XMLStreamException { + Item responseItem = new Item(); + responseItem.type = "Contact"; + // skip to Contact + while (reader.hasNext() && !isStartTag(reader, "Contact")) { + reader.next(); + } + while (reader.hasNext() && !isEndTag(reader, "Contact")) { + int event = reader.next(); + if (event == XMLStreamConstants.START_ELEMENT) { + String tagLocalName = reader.getLocalName(); + if ("EmailAddresses".equals(tagLocalName)) { + handleAddresses(reader, responseItem); + } else { + responseItem.put(tagLocalName, reader.getElementText()); + } + } + } + return responseItem; + } + + protected void handleAddresses(XMLStreamReader reader, Item responseItem) throws XMLStreamException { + while (reader.hasNext() && !isEndTag(reader, "EmailAddresses")) { + int event = reader.next(); + if (event == XMLStreamConstants.START_ELEMENT) { + String tagLocalName = reader.getLocalName(); + if ("Entry".equals(tagLocalName)) { + String key = getAttributeValue(reader, "Key"); + String value = reader.getElementText(); + if (value.startsWith("smtp:") || value.startsWith("SMTP:")) { + value = value.substring(5); + } + responseItem.put(key, value); + } + } + } + } + } diff --git a/src/test/davmail/exchange/ews/TestEwsExchangeSession.java b/src/test/davmail/exchange/ews/TestEwsExchangeSession.java index 3abb363d..515291fa 100644 --- a/src/test/davmail/exchange/ews/TestEwsExchangeSession.java +++ b/src/test/davmail/exchange/ews/TestEwsExchangeSession.java @@ -21,6 +21,7 @@ package davmail.exchange.ews; import davmail.exchange.AbstractExchangeSessionTestCase; import java.io.IOException; +import java.util.List; /** * Webdav specific unit tests @@ -34,5 +35,12 @@ public class TestEwsExchangeSession extends AbstractExchangeSessionTestCase { ewsSession = ((EwsExchangeSession) session); } - + public void testResolveNames() throws IOException { + ResolveNamesMethod resolveNamesMethod = new ResolveNamesMethod("smtp:g"); + ewsSession.executeMethod(resolveNamesMethod); + List items = resolveNamesMethod.getResponseItems(); + for (EWSMethod.Item item:items) { + System.out.println(item); + } + } }